var containerId = 'article',
	tabsId = '.ajax',
	selected = 'current',
	leftSpan = '#left span',
	infoHeight = $('article').height(),
	theList = $('article ul');

$(window).load(function() {
	

	$('.slider').nivoSlider({
        effect: 'boxRandom', // Specify sets like: 'fold,fade,sliceDown'
        slices: 10, // For slice animations
        animSpeed: 400, // Slide transition speed
        pauseTime: 5000, // How long each slide will show
        startSlide: 0, // Set starting Slide (0 index)
        directionNav: false, // Next & Prev navigation
        directionNavHide: false, // Only show on hover
        controlNav: true, // 1,2,3... navigation
        controlNavThumbs: false, // Use thumbnails for Control Nav
        keyboardNav: true, // Use left & right arrows
        pauseOnHover: true // Stop animation while hovering
    });

	$('.slider').width(930);	
	
    //Navigation Functions
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo(0, 1);			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width() + 20; 
				});	
			};
		})(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 + 100});
			$(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().show(function() {
		  $(this).hide(); 
	  });
	}

	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 0, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("nav ul li").hoverIntent(config);

//AJAX LOADING BUTTONS STARTS HERE.
	if($(tabsId).length > 0){
			loadTab($(tabsId));
		}
		$(tabsId).click(function(){
			var $el = $(this);

			if($el.hasClass(selected)){ return false; }

			$(tabsId).removeClass(selected);
			$el.addClass(selected);	

			loadTab($el);

			theList.parent().height($('article').outerHeight() + $('h2.long').outerHeight(true));
			$(window).scrollTop('2000')
			return false;
		});	
	
	
	function loadTab(tabObj){
    	if(!tabObj || !tabObj.length){ return; }
    	$(containerId).load(tabObj.attr('href'), function(){
			 var config2 = {    
			     over: goingDown,  
			     timeout: 75, // number = milliseconds delay before fadeMenu is called
			     out: goingUp 
			 };
			 // list Hovers
			 function goingDown(){
			   var truHeight = $('span', this).height();
			     $(this).delay(200).stop().animate({
			     	height: truHeight + 'px'
			     }, 350).css({
			     	'color' : '#fff',
			     	'border-color': '#fff'
			     }) }
			 function goingUp(){
			   $(this).stop().animate({
			     	height: '16px' 
			 	}, 250).css({
			     	'color' : '#ddd',
			     	'border-color': '#ddd'
			 	});
			 }; 
			 $('article ul li').wrapInner('<span />').hoverIntent(config2);
    	});
	}

	$('.spotlight').each(function() {
		
		$(this).hover(function(e) {
			$('.captionWrap', this).hoverFlow(e.type, { opacity: 1 }, 300).show(); 
		}, function(e) {
			$('.captionWrap', this).hoverFlow(e.type, { opacity: 0 }, 300).hide();		
		})

	})
	
	$('ul.top li a + .sub').hover(function() {
	    $(this).prev().addClass('hover');
	}, function() {
	    $(this).prev().removeClass('hover');
	});

	$('.ageGroup').each(function() {
	var $buttons = $(this).find('.ajax');
		
		$(this).find('.current').width(150);
		
		$buttons.click(function(e) {
	
			$buttons.stop().animate({
				width: 70}, {
				duration: 'slow',
				easing: 'easeInOutBack'
              	});
			$(this).stop().animate({
				width: 150}, {
                   duration: 'slow',
                   easing: 'easeInOutBack'
              });

		});
	});
	
	$('body').append('<a href="#"><div class="backTop"></div></a>');
	$(window).scroll(function() {		
		if ( $(this).scrollTop() >= 300) {	
			$('.backTop').stop().fadeTo('200', 1);
		} else {
			$('.backTop').stop().fadeTo('200', 0);
		}
	});

});
