// jQuery Functions Below

	/* Variable Display Box
	
		$('#error p').fadeOut(500, function() { $(this).remove(); });
		$('<p>Variable: '+ variableName +'</p>').appendTo('#error');
		$('#error').fadeIn();
		
	*/


// FUNCTION ANCHOR ANIMATE: animates to a local anchor

jQuery.fn.anchorAnimate = function(speed) {
	
	if (!speed) speed = 1200;
	var halfSpeed = speed/2;
	
	return this.each(function(){
	
		var caller = this

		$(caller).click(function(event) {
		
			event.preventDefault()
			
			var locationHref = window.location.href
			var elementClick = $(caller).attr('href')
			var destination = $(elementClick).offset().top;
			
			$("html:not(:animated),body:not(:animated)")
				.animate({ scrollTop: destination}, speed, function() {
			
				window.location.hash = elementClick
				
				$('#nav li.active, .page .page_head ul .active').removeClass('active');
				$(caller).parent().addClass('active');
												
				$('.subpage_defualt').fadeIn(500);
				$('.subpage_inner').fadeOut(500);
				
			});

			return false;
		})
	})
};


// FUNCTION ACCCORDION NAVIGATION: drops down sub nav menu

jQuery.fn.accorNav = function(speed) {
	
	if (!speed) speed = 400;
		
	return this.each(function() {
	
		var caller = this
		
		$(caller).click(function() {
						
			if ($(caller).next().is(':visible')) {
				
				$(caller).next().slideUp(speed).parent().removeClass('expanded');
				
			} else if ($('#nav ul ul:visible')) {
			
				$('#nav ul ul:visible').slideUp(speed).parent().removeClass('expanded');
				$(caller).next().slideDown(speed).parent().addClass('expanded');
				
			} else {
			
				$(caller).next().slideDown(speed).parent().addClass('expanded');
				
			}
			
			return false;
		});
	
	})

};


// FUNCTION SUBPAGE: Fetches content from a subpage through AJAX

$.ajaxSetup({ 
	cache: false 
});

jQuery.fn.subpage = function(resource) {

	if(!resource) resource = 'ajax_request.php';
	
	return this.each(function() {
	
		var caller = this;
		var callerHref = $(caller).attr('href');
						
		$(caller).click(function(event) {
			
			//var local = window.location.hash;
			var local = $(this).parent().parent().prevAll('a.nav_link:first').attr('href');
			var subLocal = local + ' .subpage';
						
			$('#nav ul .active, #nav ul ul .active, .page .page_head ul .active').removeClass('active');
			$('li:has(a[href$='+callerHref+'])').addClass('active');
			
			$.ajax({
				url: resource,
				type: 'GET',
				data: { page: this.hash },
				dataType: 'html',
				beforeSend: function(xhr) {
					$('#speakers .subpage_defualt').fadeOut(500);
					$('.subpage_inner').fadeOut(500, function() { $(this).remove(); });
					$('<div class="loading"><span>loading...</span></div>').appendTo(subLocal).hide().fadeIn();
				},
				success: function(data) {
					$('.loading').fadeOut(500, function() { $(this).remove(); });
					$('<div></div>').html(data).addClass('subpage_inner').appendTo(subLocal).hide().fadeIn(500);
					
					$('.largeImage img').hide(); $('.largeImage img:first').show();
					$('td').bindHover();
					$("a[href*='http://']:not([href*='"+location.hostname+"'])").addClass('external');
					$("a[href*='pdf']").addClass('pdf_file');
				}
			});
						
			return false;
		
		});
		
	});

};

jQuery.fn.externalNavSubpage = function(resource) {

	if(!resource) resource = 'ajax_request.php';
	
	return this.each(function() {
	
		var caller = this;
		var callerHref = $(caller).attr('href');
						
		$(caller).click(function(event) {
			
			//var local = window.location.hash;
			var local = '#speakers';
			var subLocal = local + ' .subpage';
			
			$('#nav ul .active, #nav ul ul .active, .page .page_head ul .active').removeClass('active');
			$('li:has(a[href$='+callerHref+'])').addClass('active');
			
			$.ajax({
				url: resource,
				type: 'GET',
				data: { page: this.hash },
				dataType: 'html',
				beforeSend: function(xhr) {
					$('#speakers .subpage_defualt').fadeOut(500);
					$('.subpage_inner').fadeOut(500, function() { $(this).remove(); });
					$('<div class="loading"><span>loading...</span></div>').appendTo(subLocal).hide().fadeIn();
				},
				success: function(data) {
					$('.loading').fadeOut(500, function() { $(this).remove(); });
					$('<div></div>').html(data).addClass('subpage_inner').appendTo(subLocal).hide().fadeIn(500);
					
					$('.largeImage img').hide(); $('.largeImage img:first').show();
					
					$('td').bindHover();
					$("a[href*='http://']:not([href*='"+location.hostname+"'])").addClass('external');
					$("a[href*='pdf']").addClass('pdf_file');
				}
			});
			return false;
		});
	});
};

jQuery.fn.subAnchor = function(speed) {

	if (!speed) speed = 800;
	var halfSpeed = speed/2;
	
	return this.each(function() {
	
		var caller = this;
		var callerHref = $(caller).attr('href');
		
		$(caller).click(function(event) {
			
			event.preventDefault()
			
			var locationHref = window.location.href
			var elementClick = $(caller).parent().parent().prevAll('.nav_link').attr('href')
			var destination = $(elementClick).offset().top;
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 800);
			
			window.location.hash = elementClick
			
			return false;
		
		});
		
	});

};


// FUNCTION WELCOMEPAGE: Fade in welcome page to first time visitors

// delay function
(function($){
	jQuery.fn.delay = function(millis,callBack){
		var object = $(this);
		$.extend(object,{callBack:callBack});
		return window.setTimeout(function() {
			object.callBack();
			return object;
		}, millis);
	}
})(jQuery);

// close popup function
(function($){
	jQuery.fn.closeWelcome = function(){
		if(popupStatus==1) {
			$('#innerWelcome').slideUp(500).delay(500, function() {
				$('#welcome').fadeOut(500, function() { $(this).remove(); });
				popupStatus = 0;
			});
		}
	}
})(jQuery);

// The popup display function
jQuery.fn.welcomePage = function() {
	
	// initial animation
	$(this).delay(1000, function() { 
		$(this).fadeIn(500).children().hide().delay(500, function() {
			$(this).slideDown(500);
			popupStatus = 1;
		});
	});
	
	// close on button
	$('.closeBtn').click(function() {
		$().closeWelcome();
		return false;
	});
	
	// close on escape
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			$().closeWelcome();
		} 
	});
	
};

// Enter Site Button Display

jQuery.fn.fadeInTimer = function() {
	
	// initial animation
	$(this).delay(6000, function() { 
		$(this).fadeIn(500);
	});
	
};	

// BIND HOVER FUNCTION 

jQuery.fn.bindHover = function() {
	$(this).hover(
  		function () { $(this).addClass("hover").parent().addClass("hover"); },
  		function () { $(this).removeClass("hover").parent().removeClass("hover"); }
	);
};