var rotatorInterval;
var restart = 0;
var ContentHeight = 172;
var TimeToSlide = 250.0;
var openAccordion = '';
var totalNum = 5;
var cur = 0;
		var hasClicked = 0;

function theRotator() {
	//$('div.rotator ul li').css({opacity: 0.0}); 	//Set the opacity of all images to 0
	$('div.rotator ul li').css('display', 'none');
	$('div.rotator ul li').eq(0).css('display', 'list-item'); 	//Get the first image and display it (gets set to full opacity)	
	rotatorInterval = setInterval(rotateNext,6000); //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
}





function carousel(){
	//disable left scroll buttons
	$('#PCarouselScrollLeft').addClass('disabled');
	
	//This is just for the home page best sellers section.
	$('#PCarouselScrollLeft').click(function () {
		var scrollIncrement = $(this).next().children().children('.productListItem').eq(0).attr('clientWidth')*5;
		var scrollLeftValue = $(this).next().attr('scrollLeft');
 	if (scrollLeftValue > 0 && hasClicked == 0) {
			hasClicked = 1;
			$('#PCarouselScrollRight').removeClass('disabled');
			if (scrollLeftValue - scrollIncrement <= 0) {
				$(this).next().animate({scrollLeft: 0}, 1000, 'easeInOutQuart', function () {
					hasClicked = 0;
					$('#PCarouselScrollLeft').addClass('disabled');
				});
			}
			else {
				$(this).next().animate({scrollLeft: scrollLeftValue - scrollIncrement}, 1000, 'easeInOutQuart', function () {hasClicked = 0;});
			}
		}
	});

	$('#PCarouselScrollRight').click(function () {
		var scrollIncrement = $(this).prev().children().children('.productListItem').eq(0).attr('clientWidth')*5;
		var scrollLeftValue = $(this).prev().attr('scrollLeft');
		var maxScroll = ($(this).prev().children().children('.productListItem').length-5) * (scrollIncrement/5);

		if (scrollLeftValue + scrollIncrement < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#PCarouselScrollLeft').removeClass('disabled');
			$(this).prev().animate({scrollLeft: scrollLeftValue+ scrollIncrement}, 1000, 'easeInOutQuart', function () {hasClicked = 0;});
		}
		else if (scrollLeftValue < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#PCarouselScrollLeft').removeClass('disabled');
			$(this).prev().animate({scrollLeft: maxScroll}, 1000, 'easeInOutQuart', function () {
				hasClicked = 0;
				$('#PCarouselScrollRight').addClass('disabled');
			});
		}
	});
}

function rotate(next) {	
	//cur = (cur+1) % (totalNum);
	var old = cur;
	cur = next;
	
	$('#P'+cur).css("background-color", "#fff").css("color", "#246ae1");
	$('#P'+old).css("background-color", "").css("color", "#fff");
	

	var oldLi = $('div.rotator ul li').eq(old); //Get the first image	
	//Get next image, when it reaches the end, rotate it back to the first image
	
	
	var curLi = $('div.rotator ul li').eq(cur)
	//Set the fade in effect for the next image, the show class has higher z-index
	
	curLi.fadeIn(500);

	//Hide the current image
	oldLi.fadeOut(500);
};
function rotateNext() {
	rotate((cur+1)%totalNum);
}



/////////ACCORDION///////////////

function animate(lastTick, timeLeft, closingId, openingId, openingTitle)
{  
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;

	var opening = (openingId == '') ? null : document.getElementById(openingId);
	var closing = (closingId == '') ? null : document.getElementById(closingId);
	for(i=1; i< 8; i++)
	{	
		
		if(("#Accordion" + i + "Title") == openingTitle)
			$("#Accordion" + i + "Title").css("background-image", "url('" + resourcesRoot + "AcSelected.png')").css("color", "#fff");
		else
			$("#Accordion" + i + "Title").css("background-image", "url('" + resourcesRoot + "AcNotSelected.png')").css("color", "#919191");
		
	}
	
	
	if(timeLeft <= elapsedTicks)
	{
		if(opening != null)
		  opening.style.height = ContentHeight + 'px';

		if(closing != null)
		{
		  closing.style.display = 'none';
		  closing.style.height = '0px';
		}
		return;
	}

	timeLeft -= elapsedTicks;
	var newClosedHeight = Math.round((timeLeft/TimeToSlide) * ContentHeight);

	if(opening != null)
	{
		if(opening.style.display != 'block')
		  opening.style.display = 'block';
		opening.style.height = (ContentHeight - newClosedHeight) + 'px';
		//opening.style.height = '169px';
		//=============================this.style.backgroundImage
	}

	if(closing != null)
	closing.style.height = newClosedHeight + 'px';

	setTimeout("animate(" + curTick + "," + timeLeft + ",'"
	  + closingId + "','" + openingId + "','" + openingTitle + "')", 33);
}
function runAccordion(index)
{

	var nID = "Accordion" + index + "Content";
	var nTID = "#Accordion" + index + "Title";
	if(openAccordion == nID)
		return;
		//nID = '';

	setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'"
	  + openAccordion + "','" + nID + "','" + nTID + "')", 33);
	
	openAccordion = nID;
}

$(document).ready(function() {
	$(".prev").css({opacity: 0.3}).css({cursor: "default"});
	/////Carousel
	$(".carousel").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev"
		,circular: false
	});


//// Expander Stuff ////
	$('.scrollOver').hover(function(){
		popupNumber = this.id.charAt(2);
		if($("#PU" + popupNumber).css('display') == 'none'){
			$("#PU" + popupNumber).fadeIn('slow', function(){});
	}},function(){
		popupNumber = this.id.charAt(2);
		if($("#PU" + popupNumber).css('display') == 'block'){
			$("#PU" + popupNumber).fadeOut('slow', function(){});
	}});
	
//// ON AIR STUFF ////
	$( function() 
	{
		$('#P0').css("background-color", "#fff").css("color", "#246ae1");
		$('#InfoTabs li').eq(0).addClass('selectedTab');
		$('#VisibleTab div').eq(0).addClass('selectedTab');
		carousel();
		$('#InfoTabs > ul > li').click(function () 
		{
			var thingIndex = $('#InfoTabs li').index(this);
			$('#InfoTabs li').removeClass('selectedTab');
			$('#VisibleTab div').removeClass('selectedTab');
			$(this).addClass('selectedTab');
			/* USE THIS LINE FOR OBJECT EMBEDS */
			/* $('#VisibleTab div').eq(thingIndex).addClass('selectedTab'); */
			/* USE THIS LINE FOR FLOWPLAYER */
			$('#VisibleTab div.tabInfo').eq(thingIndex).addClass('selectedTab');
			if(thingIndex == 1)
				$('#SelectedTabArrow').css("left", "200px");
			else
				$('#SelectedTabArrow').css("left", "53px");
		}

	
		);
	});
	
	$(".paging .page").click(function(){
		if($(this).index() != cur) {
			clearInterval(rotatorInterval);
			var num = parseInt($(this).attr('id').substr(1));
			rotate(num);

			rotatorInterval = setInterval(rotateNext, 3000);
		} else {
			clearInterval(rotatorInterval);
			rotatorInterval = setInterval(rotateNext, 3000);
		}
		
	});
	theRotator();
	$('div.rotator').fadeIn(1000);
	runAccordion(1);
});
