/* MEGA HOVER */

$(document).ready(function() {
	//On Hover Over
		function megaHoverOver(){
		    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
		    (function($) {
		        //Function to calculate total width of all ul's
		        jQuery.fn.calcSubWidth = function() {
		            rowWidth = 0;
		            //Calculate row
		            $(this).find("ul").each(function() { //for each ul...
		                rowWidth += $(this).width(); //Add each ul's width together
		            });
		        };
		    })(jQuery); 
		
		    if ( $(this).find(".row").length > 0 ) { //If row exists...
		
		        var biggestRow = 0;	
		
		        $(this).find(".row").each(function() {	//for each row...
		            $(this).calcSubWidth(); //Call function to calculate width of all ul's
		            //Find biggest row
		            if(rowWidth > biggestRow) {
		                biggestRow = rowWidth;
		            }
		        });
		
		        $(this).find(".sub").css({'width' :biggestRow}); //Set width
		        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
		
		    } else { //If row does not exist...
		
		        $(this).calcSubWidth();  //Call function to calculate width of all ul's
		        $(this).find(".sub").css({'width' : rowWidth}); //Set Width
		
		    }
		}
		//On Hover Out
		function megaHoverOut(){
		  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		      $(this).hide();  //after fading, hide it
		  });
		}
		
		
		//Set custom configurations
		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#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
		$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
		});


/* SLIDESHOW */

$(function() {
	if($('body').hasClass('our-history'))
	{	
		intSlideshow();
	}
});
	
function intSlideshow() {	
		var currentPosition = 0;
		var slideWidth = 549;
		var slides = $('.slide');
		var numberOfSlides = slides.length;
		
		// Show slide 
		$('.slide').show();
		// Remove scrollbar in JS
		$('#slidesContainer').css('overflow', 'hidden');
		
		// Wrap all .slides with #slideInner div
		slides
		.wrapAll('<div id="slideInner"></div>')
		// Float left to display horizontally, readjust .slides width
		.css({
		  'float' : 'left',
		  'width' : slideWidth
		});
		
		// Set #slideInner width equal to total width of all slides
		$('#slideInner').css('width', slideWidth * numberOfSlides);
		
		// Insert controls in the DOM
		$('#slideshow')
		.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
		.append('<span class="control" id="rightControl">Clicking moves right</span>');
		
		// Hide left arrow control on first load
		manageControls(currentPosition);
		
		// Create event listeners for .controls clicks
		$('.control')
		.bind('click', function(){
		// Determine new position
		currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
		
		// Hide / show controls
		manageControls(currentPosition);
		// Move slideInner using margin-left
		$('#slideInner').animate({
		  'marginLeft' : slideWidth*(-currentPosition)
		});
		});
		
		// manageControls: Hides and Shows controls depending on currentPosition
		function manageControls(position){
		// Hide left arrow if position is first slide
		if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
		// Hide right arrow if position is last slide
		if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
		}	
  
}




