//-------------------------------------------------------------------------------------------
// marquee.js
//-------------------------------------------------------------------------------------------

var stopTimer = false;
var marqueeRotateTimer = null;
var selectedMarquee = 0;
var showingMarquee = 0;

$(function(){  

  if(startMarquee) initMarquee();

  //-------------------------------------------------------------------------------------------
  // Setup clicks for changing marquee
  //-------------------------------------------------------------------------------------------


  $('.marqueeNext > a').click(function(){
	rotateMarquee();
  });

  $('.marqueePrev > a').click(function(){
	previousMarquee();
  });

  $('.marqueePause > a').click(function(){
	pauseMarquee();
       $(this).children("img").attr("src","/images/marquee-pause1.gif");
       $(this).parent().parent().children(".marqueePlay").children("a").children("img").attr("src","/images/marquee-play.gif");
  });

  $('.marqueePlay > a').click(function(){
	stopTimer=false;
	marqueeRotateTimer = setTimeout('rotateMarquee();',marqueeTime/4);
	$(this).children("img").attr("src","/images/marquee-play1.gif");
	$(this).parent().parent().children(".marqueePause").children("a").children("img").attr("src","/images/marquee-pause.gif");
  });

});

function initMarquee()
{

   $("#marqueeControls").show();


   var marqueeHeight = $("#marqueeContainer").height();

   $('.marquee').each(function(){
	if($(this).attr("id")!="marquee1")
	{
		$(this).children(".right-box").children(".featuredTbl").hide();
	}
   });

   marqueeRotateTimer = setTimeout('rotateMarquee();',marqueeTime);
}

function selectMarquee(index,fromClick)
{
	if(fromClick)
	{
		clearTimeout(marqueeRotateTimer);
		stopTimer = true; //stop automatic rotation for a little while when user clicks
	}

	var data = marqueeData[index];
	//alert("index="+index+" showingMarquee="+showingMarquee);
	if (data != null && index!=showingMarquee) 
	{

		//alert("move marquee from selected: "+selectedMarquee+ " to index: "+index + " showing: "+showingMarquee);

		selectedMarquee = index;
		
		$('#'+marqueeData[showingMarquee]).children(".right-box").children(".featuredTbl").fadeOut('slow', function() {

		    $('#'+marqueeData[showingMarquee]).hide();
		    $('#'+marqueeData[selectedMarquee]).show();
		    //alert("in here");
		    $('#'+marqueeData[selectedMarquee]).children(".right-box").children(".featuredTbl").fadeIn('slow');
		});

		showingMarquee = index;
	}

	if(fromClick)
	{
		marqueeRotateTimer = setTimeout('restartTimer();',marqueeTime); //restart again if no clicks for 8 secs
	}
}

function resetMarqueeRotation()
{
	if (marqueeRotateTimer != null || stopTimer == true)
	{
		clearTimeout(marqueeRotateTimer);
	}

	if(!stopTimer)
		marqueeRotateTimer = setTimeout('rotateMarquee();',marqueeTime);

	else
		marqueeRotateTimer = setTimeout('restartTimer();',marqueeTime); //loop until stopTimer is reset
}

function rotateMarquee()
{
	var id = selectedMarquee + 1;
	if (id >= marqueeData.length) id = 0;
	selectMarquee(id,false);
	if(!stopTimer) resetMarqueeRotation();
}

function previousMarquee()
{
	var id = selectedMarquee - 1;
	if (id < 0) id = marqueeData.length-1;
	selectMarquee(id,false);
	if(!stopTimer) resetMarqueeRotation();
}

function pauseMarquee()
{
	stopTimer=true;
 	clearTimeout(marqueeRotateTimer);
}

function restartTimer()
{
	stopTimer=false;
	resetMarqueeRotation();
}

function setMarqueeHeight(newHeight)
{
  $("#marqueeContainer").height(newHeight);
}


