var slidePanelTimer;
var slidePanelsWidth = 0;
var panelCount = 0;
var currentPanel=1;
$(document).ready(function(){
	$('#top-menu > li').each(function(){
		if($(this).find("ul")){
			$(this).hover(function(){
				$("ul:first", this).show();
			},function(){
				$("ul:first", this).hide();
			});
		}
	});
	$('#top-menu > li > ul.sub-nav > li:has(ul)').each(function(){ $(this).addClass("accordian"); });
	$('.accordian').each(function(){
		var t=$(this);
		t.mouseenter(function(){
			t.find('ul').show();	
		}).mouseleave(function(){
			t.find('ul').hide();	
		});	
	});
	$('a[rel=blank]').attr("target","_blank");
	$('#top-menu li:first').css('padding-left","0px');
	if($('.panelContainer').size()>0){
		startInterval($('.panelContainer'));
		$('.panelContainer').each(function(i){
			$(this).mouseover(function(){ stopInterval() });
			$(this).mouseout(function(){ startInterval($(this)) });
		});
		$('.panelContainer .panel').each(function(i){
			slidePanelsWidth += $(this).width();
			panelCount++;
		});
		$('.panelContainer').width(slidePanelsWidth+"px");
	}
});

if(typeof Cufon!=undefined){
	Cufon.replace('h2').replace('#top-menu li').replace('h1').replace('#left-col-div-home a').replace('#right-col-div a').replace('#side_column_video_li a');
}

function startInterval(obj){
	if(obj.length>0){
	  slidePanelTimer = setInterval(
		  function(){
			  panelWidth=slidePanelsWidth/panelCount;
			  if(currentPanel>=panelCount){
				  currentPanel=1;
			  }
			  else{
				  currentPanel++;
			  }
			  slideToPanel(currentPanel);
		  },
		  6000
		);
	}
}

function stopInterval(){
	clearInterval(slidePanelTimer);
}

function slideToPanel(num){
	currentPanel=num;
	stopInterval();
	panelWidth = slidePanelsWidth/panelCount;
	$('.panelContainer').animate({left:(panelWidth * (currentPanel-1) * -1)+6 + 'px'},'slow');
	activateLink(currentPanel);
	startInterval($('.panelContainer'));
}

function activateLink(num){
	num = Math.round(num);
	$('.panelLinks .panelLink').removeClass('active');
	$('.panelLinks .panelLink:nth-child('+num+')').addClass('active');
}
