var index;
var r_timer;
var maxElements;
var first;
var fadeType="dualFade";
var deltaAnimation=500;
var rotatorObject;
var rotatorPaused=true;
var deltaRotatorChange=8000;

function rotate(obj){
	loadArticle(obj, index++);
}
function initRotator(obj)
{
	rotatorPaused=false;
	first=1;
	rotatorObject=obj;
//	alert(fadeType);
	index=1;
	maxElements=jQuery(obj+" .rotatorSubstories .substory").length;
	jQuery(obj+" .mainImage").children(".mainStory").css("position", "absolute");
//  Maybe this will cause issues? change to setTimeout?
	r_timer=setInterval(
		function(e){
	    index=parseInt(index)+1;
//	    alert(index);
			index=cleanIndex(index);
//**********************************************************************
//****************** Begin slider logic ********************************
//**********************************************************************
	var sliderClicks=jQuery("#rotator .rotatorSubstories .controls").attr("sldrclks");
	switch(index)
	{
		case 1:
			while(sliderClicks>0)
			{
				jQuery("#rotator .rotatorSubstories .controls .prev").click();
				sliderClicks--;
			}
		break;
		case 8:
			while(sliderClicks<4)
			{
				jQuery("#rotator .rotatorSubstories .controls .next").click();
				sliderClicks++;
			}
		break;
		case 7:
			while(sliderClicks<3)
			{
				jQuery("#rotator .rotatorSubstories .controls .next").click();
				sliderClicks++;
			}
		break;
		case 6:
			while(sliderClicks<2)
			{
				jQuery("#rotator .rotatorSubstories .controls .next").click();
				sliderClicks++;
			}
		break;
		case 5:
			while(sliderClicks<1)
			{
				jQuery("#rotator .rotatorSubstories .controls .next").click();
				sliderClicks++;
			}

		break;
		case 4:
			while(sliderClicks>3)
			{
				jQuery("#rotator .rotatorSubstories .controls .prev").click();
				sliderClicks--;
			}
		break;
		case 3:
			while(sliderClicks>2)
			{
				jQuery("#rotator .rotatorSubstories .controls .prev").click();
				sliderClicks--;
			}
		break;
		case 2:
			while(sliderClicks>1)
			{
				jQuery("#rotator .rotatorSubstories .controls .prev").click();
				sliderClicks--;
			}
		break;
	}
//**********************************************************************
//***************** END slider logic ***********************************
//**********************************************************************


      rotatorTrigger(obj+" .rotatorSubstories .substory_"+index, obj);
		},deltaRotatorChange);
	jQuery(obj+" .rotatorSubstories .substory").bind("click", function(e){
//			alert("Rotator Triggered::"+first);
		if(first!=1)
		{
//			alert("Click Triggered for index::" +this);
			clearInterval(r_timer);
		}
		else
		{
			first=0;
		}
		rotatorTrigger(this, obj);
//		alert(jQuery(obj+" .rotatorSubstories .substory").attr("class"));
	});
  jQuery(obj+" .rotatorSubstories .substory_1").trigger("click");
}
function rotatorTrigger(obj, name)
{
	//alert('rotatorTrigger');
	if(rotatorPaused==false)
	{
		jQuery(obj).parent().children().removeClass("substoryOn");
		jQuery(obj).addClass("substoryOn");
		var rotatorClasses=jQuery(obj).attr("class").split(" ");
		for(var i=0; i<rotatorClasses.length; i++)
		{
			if(rotatorClasses[i].substr(0,9)=="substory_")
			{
				index=rotatorClasses[i].split("_").slice(-1);
				switch(fadeType)                                                  //  Switch for different fade animations
				{
					case "hoverFade":
						loadArticleHoverFade(obj, index, name);
					break;
					case "dualFade":
						loadArticleDualFade(obj, index, name);
					break;
					default:
						loadArticle(obj, index, name);
					break;
				}                                                                 //  End switch for different fade animations
			}
		}
	}
	else{
		hideNeuLionVid();
		jQuery('#rotator .mainStoryOn').hide();
		rotatorTrigger(obj, name);
	}
}

function cleanIndex(index)                  //  Makes sure that the index supplied is within the current range of the cardinality of the  rotator stories
{
	if(index>maxElements)
	{
		index=1;
	}
	if(index<=0)
	{
		index=1;
	}
	return index
}

function isCurrent(obj, index)              //  Checks to see if the selected substory is currently the active substory
{
	if(jQuery(name+" .mainImage .mainStoryOn.mainStory_"+index).length>0)
	{
		return true;
	}
	return false;
}

function loadArticle(obj, index, name)
{
	index=cleanIndex(index);                                                                            //  Makes sure the index is in range of the provided stories
	jQuery(name+" .mainImage .mainStoryOn").hide();																											//  Hide Story
	jQuery(name+" .mainImage .mainStoryOn").removeClass("mainStoryOn").addClass("mainStoryOff");
	jQuery(name+" .mainImage .mainStory_"+index).show();                                                //  Show Story
	jQuery(name+" .mainImage .mainStory_"+index).addClass("mainStoryOn").removeClass("mainStoryOff");
//	jQuery(name+" .mainImage .mainStory_"+index+" .teaser").animate({top:"0px"}, 0);                  //  Move the teaser to it's original position
//	jQuery(name+" .mainImage .mainStory_"+index+" .teaser").animate({top:"-50px"}, deltaAnimation);   //  Move the teaser up to a viewable position
}

function loadArticleDualFade(obj, index, name)
{
	index=cleanIndex(index);                                                                            	//  Makes sure the index is in range of the provided stories
	if(!isCurrent(name, index))
	{
		if(jQuery.browser.msie && jQuery.browser.version == 6.0)																						//  Degrade for smoothness in IE6
		{
			jQuery(name+" .mainImage .mainStoryOn").hide(); 							    	                              //  Fade out Animation of main story
		}
		else
		{
			jQuery(name+" .mainImage .mainStoryOn").fadeOut(deltaAnimation); 	                                //  Fade out Animation of main story
		}
		jQuery(name+" .mainImage .mainStoryOn").removeClass("mainStoryOn").addClass("mainStoryOff");
		if(jQuery.browser.msie && jQuery.browser.version == 6.0)                                            //  Degrade for smoothness in IE6
		{
			jQuery(name+" .mainImage .mainStory_"+index).show();
		}
		else
		{
			jQuery(name+" .mainImage .mainStory_"+index).fadeIn(deltaAnimation);
		}
		jQuery(name+" .mainImage .mainStory_"+index).addClass("mainStoryOn").removeClass("mainStoryOff");
	}
}

function loadArticleHoverFade(obj, index, name)
{
	index=cleanIndex(index);                                                                           	 	//  Makes sure the index is in range of the provided stories
	if(!isCurrent(name, index))
	{
//		jQuery(name+" .mainImage .mainStoryOn").fadeOut(deltaAnimation);                                  //  Fade out Animation of main story
		jQuery(name+" .mainImage .mainStoryOn").css("z-index", "20");
//		jQuery(name+" .mainImage .mainStory").hide();                                                     //  Hiding animation of main story

		jQuery(name+" .mainImage .mainStory_"+index).css("z-index", "10");
		if(jQuery.browser.msie && jQuery.browser.version == 6.0)                                            //  Degrade for smoothness in IE6
		{
			jQuery(name+" .mainImage .mainStory_"+index).show();
		}
		else
		{
			jQuery(name+" .mainImage .mainStory_"+index).fadeIn(deltaAnimation);
		}
		jQuery(name+" .mainImage .mainStory_"+index).addClass("mainStoryOn").removeClass("mainStoryOff");
		jQuery(name+" .mainImage .mainStoryOn").removeClass("mainStoryOn").addClass("mainStoryOff");
		jQuery(name+" .mainImage .mainStoryoff").css("z-index", "0");
	}
}

function rotatorPause(){
//	alert("Pausing::"+rotatorPaused);
//	alert("Pause Rotator");
	rotatorPaused=true;
	rotatorObject="#rotator";
	var obj=jQuery(rotatorObject+" .substoryOn");
	var i=obj.attr('counter');
	clearInterval(r_timer);
//	alert(rotatorPaused);
}

function rotatorPlay()
{
	rotatorPaused=false;
//	alert("Play Rotator");
	r_timer=setInterval(
		function(e){
			index=parseInt(index)+1;
//	    alert(index);
			index=cleanIndex(index);
			rotatorTrigger(rotatorObject+" .rotatorSubstories .substory_"+index, rotatorObject);
		},5000);
}
