var rotateSpeed = 10000; 
var currentTab = 0; 
var numTabs; 
var autoRotate;

function ajaxLoad(sObj) {
	var urlLoad = $(sObj).attr("href");
	
	var urlLoadclean = urlLoad.replace("/agegroup/article/","/site/agegroup_tab/");

	$(".tabs li a").removeClass("active");
	sObj.addClass("active");	
	
	$('#load').remove();
	$('#tabbed-content-wrapper').append('<span id="load"><img src="/loading.gif" /></span>');
	$('#load').fadeIn('fast');		
	$('.tabbed_content').hide();

	$.ajax({
		type: "GET",
		url: urlLoadclean,
		success: function(msg){
			$('#load').fadeOut('fast');					
			$('.tabbed_content').html(msg);
			$('.tabbed_content').fadeIn('fast');
		}
	});
	
	currentTab = currentTab+1;
	if (currentTab==4) {currentTab=-1}
}

function rotateTabs() {
	var nextTab = (currentTab == (numTabs - 1)) ? 0 : currentTab + 1;

	var ss = $(".tabbed_box li a:eq("+nextTab+")");

	ajaxLoad(ss);
}


$(document).ready(function(){ 
    	
		$("ul.sf-menu").supersubs().superfish({ 
         //pathClass:  'current',
		 delay:200,
		 dropShadows: false
        });  // call supersubs first, then superfish, so that subs are 
                         // not display:none when measuring. Call before initialising 
                         // containing tabs for same reason. 

		//Loads videos in player
		$(".video_playlist a").click(function(){ 
    	// capture the url to load from the href
		$(".video_playlist li a").removeClass("active");
		$(this).addClass("active");
    	var urlLoad = $(this).attr("href");
		$("#video_player").load(urlLoad);
    	// just make sure the page does not reload 
		if(urlLoad.indexOf("multimedia") == -1) {
    	return false;
		}
  		}); 
		
		//Tab Panel
		$(".article_video_playlist a").mouseover(function(){ 
    	// capture the url to load from the href 
    	var urlLoad = $(this).attr("href");
		$("#article_video").load(urlLoad);
    	// just make sure the page does not reload 
    	return false; 
  		}); 

		$(".tabbed_box li a").hover(function(){		
			clearInterval(autoRotate);
			currentTab = -2;
			
	    	var urlLoad = $(this).attr("href");
			var urlLoadclean = urlLoad.replace("/agegroup/article/","/site/agegroup_tab/");
			
			ajaxLoad($(this));		
		 
			if(urlLoadclean.indexOf("news") == -1) {
				return false;
			}
		},
		function() {
		}
			);
	
		//Load the tabs
		$(".tabbed_content").load("/site/agegroup_tab/");
		$('#load').fadeOut('fast');
		
		$(".tabbed_content").mouseover(function(){clearInterval(autoRotate)});
		
		//$(".tabbed_content").mouseout();
		autoRotate = setInterval("rotateTabs()", rotateSpeed);
});
