/******************************************************************************
	START photo-finish
******************************************************************************/
var rotPF = {
	current : 0,
	total : 5,
	interval : 10000,
	rotate : true
};
jQuery(document).ready(function(){
	if(jQuery("#photo-finish .album").length>1) {
		rotPF.total=jQuery("#photo-finish .album").length;
		setInterval("photoFinishRotate()",rotPF.interval);
		jQuery("#photo-finish .left-button").click(function() {
			photoFinishPreviousButton();
		});
		jQuery("#photo-finish .right-button").click(function() {
			photoFinishNextButton();
		});
	}
});

function photoFinishSelect(num) {
	rotPF.rotate = false;
	jQuery("#photo-finish .album").fadeOut(250);
	jQuery("#photo-finish .dot").removeClass("active");
	rotPF.current = num;
	jQuery("#photo-finish .dot:eq("+rotPF.current+")").addClass("active");
	setTimeout("photoFinishShow()",250);
	setTimeout("photoFinishSetActive()",550);
}

function photoFinishRotate() {
	if(rotPF.rotate)
		photoFinishNext();	
}

function photoFinishPreviousButton() {
	rotPF.rotate = false;
	photoFinishPrevious();
}

function photoFinishNextButton() {
	rotPF.rotate = false;
	photoFinishNext();
}

function photoFinishPrevious() {
	for(x=0;x<jQuery("#photo-finish .album").length;x++) {
		if(jQuery("#photo-finish .album:eq("+x+")").hasClass("active")) {
			rotPF.current=x;
			//jQuery("#photo-finish .album").removeClass("active");
			jQuery("#photo-finish .album").fadeOut(250);
			jQuery("#photo-finish .dot").removeClass("active");
			if(rotPF.current==0) 
				rotPF.current = rotPF.total - 1;
			else 
				rotPF.current --;
			//jQuery("#photo-finish .album:eq("+rotPF.current+")").addClass("active");
			//jQuery("#photo-finish .album:eq("+rotPF.current+")").fadeIn(500);
			jQuery("#photo-finish .dot:eq("+rotPF.current+")").addClass("active");
			setTimeout("photoFinishShow()",250);
			setTimeout("photoFinishSetActive()",550);
			break;	
		}	
	}
}

function photoFinishNext() {
	for(x=0;x<jQuery("#photo-finish .album").length;x++) {
		if(jQuery("#photo-finish .album:eq("+x+")").hasClass("active")) {
			rotPF.current=x;
			//jQuery("#photo-finish .album").removeClass("active");
			jQuery("#photo-finish .album").fadeOut(250);
			jQuery("#photo-finish .dot").removeClass("active");
			if(rotPF.current<(rotPF.total - 1)) 
				rotPF.current ++;
			else 
				rotPF.current = 0;
			//jQuery("#photo-finish .album:eq("+rotPF.current+")").addClass("active");
			//jQuery("#photo-finish .album:eq("+rotPF.current+")").fadeIn(500);
			jQuery("#photo-finish .dot:eq("+rotPF.current+")").addClass("active");
			setTimeout("photoFinishShow()",250);
			setTimeout("photoFinishSetActive()",550);
			break;	
		}	
	}
}

function photoFinishShow() {
	jQuery("#photo-finish .album:eq("+rotPF.current+")").fadeIn(250);
}

function photoFinishSetActive() {
	jQuery("#photo-finish .album").removeClass("active");
	jQuery("#photo-finish .album:eq("+rotPF.current+")").addClass("active");
}
/******************************************************************************
	END photo-finish
******************************************************************************/

var index;
var r_timer;
var maxElements;
var first=1;
var fadeType="dualFade";
var deltaAnimation=500;


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

function initRotator(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);
      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)
{
	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
		}
	}
}

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");
	}
}

