var index;
var r_timer;
var maxElements;
var first=1;

function rotate(obj){
	loadArticle(obj, index++);
}

function initRotator(obj)
{
	index=1;
	maxElements=jQuery(obj+" .rotatorSubstories .substory").length;

//  Maybe this will cause issues? change to setTimeout?
	r_timer=setInterval(
		function(e){
	    index=parseInt(index)+1;
//	    alert(index);
			if(index>maxElements)
			{
				index=1;
			}
			if(index<=0)
			{
				index=1;
			}
      rotatorTrigger(obj+" .rotatorSubstories .substory_"+index, obj);
		},5000);
	jQuery(obj+" .rotatorSubstories .substory").bind("click", function(e){
		if(first!=1)
		{
			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("hello world");
	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);
			loadArticle(obj, index, name);
		}
	}
}
function loadArticle(obj, index, name)
{
//	alert(index+"::"+maxElements);
	if(index>maxElements)
	{
		index=1;
	}
	if(index<=0)
	{
		index=1;
	}
	jQuery(name+" .mainImage .mainStory").hide();
	jQuery(name+" .mainImage .mainStory_"+index).show();
	jQuery(name+" .mainImage .mainStory_"+index+" .teaser").animate({top:"0px"}, 0);
	jQuery(name+" .mainImage .mainStory_"+index+" .teaser").animate({top:"-50px"}, 500);

}