var jQuerylastHoveredItem;
var jQueryrichMediaContentItems;
var global_automatedRotationInterval;
var popped;

jQuery(document).ready(function(){
   var currIndex = 0;
   
   //get all of the RMC items
   jQueryrichMediaContentItems = jQuery(".RMC");
   
   //start the auto-rotate
   //global_automatedRotationInterval = window.setInterval("rotateSlider()", 3500);
   
   //on initialization, we'll need to move around the background
   //image based on the li's index
    jQuery("#sliderNav ul li.slider").each(
      function()
      {
          if(jQuery("#sliderNav ul li.slider").index(this) != 0)
          {
            var vertOffset = -currIndex * 51;
            jQuery(this).css("backgroundPosition", "0px " + vertOffset + "px");
          }
          
          jQuery(this).attr("name", currIndex);
            currIndex++;
      }
    );
    
	 //pop the first button on initialization
   Pop(0);
   
	//pop the li iff it's not popped yet 
	//and slide the RMC slider to relevant content
	jQuery("#sliderNav ul li.slider").click(
	function(){
	  var currIndex = jQuery(this).attr("name");
	  
	  //this is for auto-rotation
	  last_highLighted = currIndex;
	  //window.clearInterval(global_automatedRotationInterval);
	  //global_automatedRotationInterval = window.setInterval("rotateSlider()", 3500);
	  //'pop' the button
		Pop(currIndex);
	});
	
	//push iff the user is currently hovering over
	//unpush when they mouse off iff they haven't popped
	jQuery("#sliderNav ul li.slider").hover(
		function()
		{
			var currIndex = jQuery(this).attr("name");
			
			if(!isPopped(currIndex))
        jQuery(this).css("backgroundPosition", "0px " + (-255 - currIndex * 51) + "px");		
		},
		function()
		{
			var currIndex = jQuery(this).attr("name");
			
			if(!isPopped(currIndex))
        jQuery(this).css("backgroundPosition", "0px " + (-currIndex * 51) + "px");
		}
		);
		
		jQuery("#sliderContent .RMC").hover(
		function()
		{
		  jQuery(this).css("display","block");
      jQuery(this).find(".teaser").stop().animate({"height":"150px", "width":"300px", "opacity":"1"}, 500);
		}
		, 
		function()
		{
		  jQuery(this).find(".teaser").stop().animate({"height":"0px", "width":"0px", "opacity":"0"}, 500);
		  this.style.display = "";
		}
		);
});

//pop the given slider button at index
function Pop(index)
{			
			if(!isPopped(index))
		  {
        jQuery(jQuery("#sliderNav ul li.slider")[index]).css("backgroundPosition","0px " + (-510 - index * 51) + "px");
        
        slideTo(index);
        
        UnPopSelected();
        
        //and set this to the new pop
        popped = index;
		  }		  
}

//unpop the currently selected button
function UnPopSelected()
{
    if(popped != null)
    {
       jQuery(jQuery("#sliderNav ul li.slider")[popped]).css("backgroundPosition","0px " + (- popped * 51) + "px");
    }
}

//accessor to 'private' state array
function isPopped(index)
{
  if(popped != null)
    return popped == index;
  else
    return false;
}

//iff we aren't at the last slider image
//slide forward one RMC
function slideForward()
{
  var jQuerysliderContent = jQuery("#sliderContent");
  
  if(jQuerysliderContent.css("top") != "-1020px")
    jQuerysliderContent.animate({"top":"-=255px"}, 200);
}

//iff we aren't at the last slider image
//slide backward one RMC
function slideBackward()
{
  var jQuerysliderContent = jQuery("#sliderContent");
  
  if(jQuerysliderContent.css("top") != "0px")
    jQuerysliderContent.animate({"top":"+=255px"}, 200);
}

//slide to a specific index from another index
function slideTo(index)
{
  var numOfSlides = 0;

  //if popped isn't defined, we're initializing
  //and the # of slides is zero
  if(popped != null)
    numOfSlides = index - popped;

    
  if(numOfSlides > 0)
    slideForwardMany(numOfSlides);
  else
    slideBackwardMany(numOfSlides);
}
function slideForwardMany(slides)
{
    for(var i =0; i < slides; i++)
        slideForward();
}
function slideBackwardMany(slides)
{
    for(var i  =0; i > slides; i--)
        slideBackward();
}

//this is for auto-rotation
var last_highLighted;
function rotateClick(sender)
{
  var currIndex = jQuery(sender).attr("name");  
	  //'pop' the button
		Pop(currIndex);
}
function rotateSlider()
{
 if(last_highLighted == null)
 {
    rotateClick(jQuery("#sliderNav ul li.slider")[0]);
    last_highLighted = 0;
 }
 else
 {
    last_highLighted++;
    rotateClick(jQuery("#sliderNav ul li.slider")[last_highLighted % 5]);
 }
}