$(function() {

	// quick access panel
	$(".quickLink").click(function(){
		$("#quickPanel").slideToggle("slow");
		$(this).toggleClass("quickLinkActive"); return false;
	});

	// main navigation
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}

	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#nav li .sub").css({'opacity':'0'});
	$("ul#nav li").hoverIntent(config);

	// fancybox - single
	$("a.fancybox").fancybox({
		'overlayColor'	: '#000',
		'overlayOpacity': 0.5,
		'titlePosition'	: 'over'
	});

	// fancybox - slideshow
	$("a.fancybox_group").fancybox({
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.5,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}

	});

	$("a.fancybox_iframe").fancybox({
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.5,
		'width'			: 650,
		'height'		: 500,
		'autoScale'		: false,
		'titleShow'		: false,
		'type'			: 'iframe'
	});

	// featured slideshow
	$(".paging").show();
	$(".paging a:first").addClass("active");
		
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".image_reel").animate({ 
			left: -image_reelPosition
		}, 500 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			//$active = $('.paging a.active').next();
			$active = $('.paging li a.active').parent().next().find('a');
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (3 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		//return false; //Prevent browser jump to link anchor
	});	

	//////////////////////////////////////
	//start - Testing On HoverIntent PAGING
	/////////////////////////////////////
	// when hovering over paging element, execute this functions
	function paging_hoverIntent_over(){ 
		$active = $(this); 		//Activate the clicked paging
		//Reset Timer
		clearInterval(play); 	//Stop the rotation
		rotate(); 				//Trigger rotation immediately
		rotateSwitch(); 		// Resume rotation
		}
	//Function paging_hoverIntent_out is required by hoverIntent plugin, So its set to nothing
	function paging_hoverIntent_out(){ 
		
		}
	
	// Settings for the paging_hoverIntent call from the hoverIntent plugin
	var paging_hoverIntent_config = {    
			 sensitivity: 2, 						// number 	= sensitivity threshold (must be 1 or higher)    
			 interval:    100, 						// number 	= milliseconds for onMouseOver polling interval    
			 over:        paging_hoverIntent_over,  // function = onMouseOver callback (REQUIRED)    
			 timeout:     500, 						// number 	= milliseconds delay before onMouseOut    
			 out:         paging_hoverIntent_out 	// function = onMouseOut callback (REQUIRED)    
		};
	
	
	$(".paging a").hoverIntent(paging_hoverIntent_config);
	
	////////////////////////////
	//end - Testing On HoverIntent PAGING
	///////////////////////////


	// input box hint
	$('input[title!=""]').hint();
	$('textarea[title!=""]').hint();

	// submit button hover
	$('.submitBtn').hover(
		function(){ $(this).addClass('submitBtnHover'); },
		function(){ $(this).removeClass('submitBtnHover'); }
	);	

	// link type icon
	$(".newWindow").attr({ target: "_blank" });
	$(".pdf").attr({ target: "_blank" });
	$("a[href$=doc]").addClass("word");
	$("a[href$=docx]").addClass("word");
	$("a[href^=mailto:]").addClass("email");

	// popup window
	$('.newWindow1').popupWindow({ 
		centerBrowser:1 
	});
	
	//hover effect for health topics drop down list	
	$('div[class=column3 last]').hover(
				function() { $('#topicUL', this).css('display', 'block'); },
				function() { $('#topicUL', this).css('display', 'none'); }
	); 
	
	//hover effect for sub navigation
	$('.childdropdown').hover(
				function() { $('.child_topic', this).css('display', 'block'); },
				function() { $('.child_topic', this).css('display', 'none'); }
	); 
	
	//click event for expanding health topics drop down list	
	$('#expand_topics').click(
			function() { 
				$("div[class=column3 last]").unbind('mouseenter').unbind('mouseleave');				
				$("div[class=column3 last] > ul").css("display", "block");	
				$("#expand_topics").css("display", "none");
				$("#collapse_topics").css("display", "block");
			}
	); 

	//click event for collapsing health topics drop down list	
	$('#collapse_topics').click(
			function() { 						
				$("div[class=column3 last] > ul").css("display", "none");				
				$("#collapse_topics").css("display", "none");
				$("#expand_topics").css("display", "block");
				//re-introducing the hover event for topic list after health topics drop down list has collapsed
				$('div[class=column3 last]').hover(
					function() { $('#topicUL', this).css('display', 'block'); },
					function() { $('#topicUL', this).css('display', 'none'); }
				); 
			}
	); 	
	
		//click event for expanding refine search	
	$('.refineSearchText').click(
			function() { 
				$("div[class=refineSearchContainer]").unbind('mouseenter').unbind('mouseleave');				
				$("div[class=refineSearchContainer]").css("display", "block");
				$(".closeRefineSearch").css("display", "block");				
			}
	); 
	
	//click event for expanding refine search	
	$('.refineSearchText2').click(
			function() { 
				$("div[class=refineSearchContainer2]").unbind('mouseenter').unbind('mouseleave');				
				$("div[class=refineSearchContainer2]").css("display", "block");
				$(".closeRefineSearch2").css("display", "block");				
			}
	); 
	
	//click event for closing refine search	
	$('.closeRefineSearch').click(
			function() {		  		
				$("div[class=refineSearchContainer]").css("display", "none");			
				$(".closeRefineSearch").css("display", "none");		
			}
	); 
	
	//click event for closing refine search	
	$('.closeRefineSearch2').click(
			function() {		  		
				$("div[class=refineSearchContainer2]").css("display", "none");			
				$(".closeRefineSearch2").css("display", "none");		
			}
	); 
	
});

//Terms and Conditions script for Overview table powerpoint slides
function TOC() 
{		
	var answer = confirm("Do you agree to the following Terms and Conditions: \n \n 1. something \n 2. something else \n 3. something more")
	if (answer)
	{
		alert("You have agreed to the Terms and Conditions!");
		window.location = "/health-facts/overviews";
	}
	else
	{
		alert("Download cannot begin unless you agree to the Terms and Conditions!");
		return false;
	}
}

