$(document).ready(function(){	

   /* Set the sub menu "on" state */
   if (($('.menuCol ul').length) && ($('#menuIndex').length)) {
     var menuIndex = $('#menuIndex').val();
     if(menuIndex) {
       $('.menuCol li').eq(menuIndex).addClass('on');
     }
   }

// if the rotator object exists on the page
if($('#rotator').length) {
	
	  // rotate the slides every 12 seconds if user does nothing
	  //var slideIndex = 1;
	  var slideLength = $('#rotator div.slide').length;
	  
	  if(Get_Cookie('homeRotateCookie')=='True') {
	    var slideIndex = GetRandomNumber(slideLength-1);
	  } else {
		var slideIndex = 0;
		Set_Cookie('homeRotateCookie','True', 365, '', '', '');
	  }
	  
	  $('#rotator div.slide').eq(slideIndex).addClass('on');
	  $('#rotator div.thumbBox a').eq(slideIndex).addClass('on');
	  
	  
	  var rotateFunction = setInterval(function() {
		$('#rotator .thumbBox a').removeClass('on').eq(slideIndex).addClass('on');
		//$('#rotator div.slide').hide().eq(slideIndex).show()
		//slideIndex=slideIndex+1;
	    if(slideIndex==slideLength) slideIndex=0;
	  }, 6000);
	

   // Turn the thumb nav on if the user has js
   $('#rotator .thumbBox').css('visibility','visible');
	
   // change the slide on thumb hover 
   $('#rotator .thumbBox a').hover(function(){
	 clearInterval(rotateFunction);
     var index = $('#rotator .thumbBox a').index(this);
	 $('#rotator div.slide').hide().eq(index).show();
	 $('#rotator .thumbBox a').removeClass('on');
	 $(this).addClass('on');
	 //alert("dsadsa");
   });
   
   $('#rotator #numbers a').click(function(){
	 clearInterval(rotateFunction);
     var index = $('#rotator #numbers a').index(this);
	 $('#rotator div.slide').hide().eq(index).show();
	 $('#rotator .thumbBox a').removeClass('on');
	 //alert("dsadsa");
   });

  // do nothing on thumb click
  $('#rotator .thumbBox a').click(function(){return false;});
}


// Fade Cycles the ad promos on the homepage
if($('.adHomeRotate').length) {
  var adList = $('.adHomeRotate a');
  adList.hide().eq(0).show();
  var adIndex = 1;
  var adLength = adList.length;
  var adRotateFunction = setInterval(function() {
	  adList.css('z-index','0').eq(adIndex).css('z-index','9999').fadeIn(400,function() {
		adList.hide().eq(adIndex).show().css('z-index','0');
		adIndex=adIndex+1;
	    if(adIndex==adLength) adIndex=0;
	});
}, 1000);
}


}); /* end onload */


function Set_Cookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {expires = expires * 1000 * 60 * 60 * 24;}
	
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie(name) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
	  return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

//Returns a random number <= to the number of slides.
function GetRandomNumber(upperBound){	
try {
	var seed = new Date().getTime();
	seed = (seed*9301+49297) % 233280;
	seed = seed/(233280.0);
	return Math.ceil(seed*upperBound);
}
catch(oError) {
	//alert("GetRandomNumber error: " + oError);
}
}



$(function() {
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div");
	$("div.tabs-slider").tabs("div.panes-slider > div");
	$("ul.tabs-Inner").tabs("div.panes-inner > div");
});



