var GLOBAL_FEATURE_SHOWN = ""; // holds the "id" of the div that is currently displayed on main feature
var GLOBAL_FEATURE_INDEX = 0; // holds the number association in the array of main features
var GLOBAL_SLIDESHOW_ON	 = true; // determines whether or not to show the next slide
var GLOBAL_SLIDESHOW_FPS = 30; // frames per second for slide transitions
var GLOBAL_SLIDESHOW_DELAY=12000; // time spent between slides in milliseconds
var GLOBAL_SLIDESHOW_RUN_ONCE = false; // just so slideshow is initiated once
var next_feature = null; // next item in main feature array (<div> id)
var next_index = 0; // holds the number association in the array of main feature for the NEXT item
var recurrance = parseInt(1000/GLOBAL_SLIDESHOW_FPS); // how many milliseconds between each frame transition


var slideShow = function() {
	this.slide = function(slide_to) {
		if (GLOBAL_SLIDESHOW_ON) {
			var fea_length = getElementsByClassName(document,"li","main_feature_content").length;
			if (slide_to) {
				next_index = slide_to;
			}
			if (fea_length==next_index+1){
				marLeft = 0;
			} else {
				marLeft = (960*(next_index+1));
			}
			$('#main_feature_extend').animate({
				marginLeft: "-"+marLeft
			},800,function() {
				var fea_length = getElementsByClassName(document,"li","main_feature_content").length;
				if (fea_length==next_index+1) {
					next_index=0;
				} else {
					next_index++;
				}
				GLOBAL_FEATURE_INDEX = next_index;
				GLOBAL_FEATURE_SHOWN = "main_feature"+(GLOBAL_FEATURE_SHOWN+1);
				sl.changeSlideCounter();
			});
			if (!GLOBAL_SLIDESHOW_RUN_ONCE) {
				//var tempNextIndex = (GLOBAL_FEATURE_INDEX==next_index+1) ? 0 : GLOBAL_FEATURE_INDEX+1;
				GLOBAL_SLIDESHOW_RUN_ONCE = setInterval("sl.slide()",GLOBAL_SLIDESHOW_DELAY);
			}
		}
	};
	
	this.focusOnStory = function(story) {
		//alert(GLOBAL_FEATURE_SHOWN +" "+ story.id);
		//if (GLOBAL_FEATURE_SHOWN != story.id) { // don't need to fade if they are already on this story
			var features = getElementsByClassName(document,"li","main_feature_content");
			next_feature = story.id;
			if (GLOBAL_FEATURE_SHOWN==""){GLOBAL_FEATURE_SHOWN="main_feature1";}
			for(var i=0;i<features.length;i++) {
				if(features[i].id == next_feature) {
					next_index = i-1;
					break;
				}
			}
			GLOBAL_SLIDESHOW_ON=true;
			sl.slide(next_index);
			GLOBAL_SLIDESHOW_ON=false;
			//sl.animateSlide(GLOBAL_FEATURE_SHOWN,next_feature,false,true);
		//} else {
		//	sl.stopSlideShow();
		//}
		sl.changeSlideCounter();
	};
	
	this.changeSlideCounter = function() {
		var sliderCounters = getElementsByClassName(document.getElementById("slider_counter"),"div","slider_num");
		for(var i=0;i<sliderCounters.length;i++) {
			if (sliderCounters[i].className.substr(" slider_num_selected") != -1) {
				sliderCounters[i].className = sliderCounters[i].className.replace(" slider_num_selected","");
			}
			if (i == GLOBAL_FEATURE_INDEX) {
				sliderCounters[i].className = sliderCounters[i].className + " slider_num_selected";
			}
		}
	};
	
	this.stopSlideShow = function() {
		GLOBAL_SLIDESHOW_ON=false;
	}
}

var sl = new slideShow;
var runSlideShow = function() {setTimeout("sl.slide()",GLOBAL_SLIDESHOW_DELAY);};


var sliderLinks = function() {
	slideLinks = getElementsByClassName(document.getElementById("slider_counter"),"a","slide_num_link");
	for(var i=0;i<slideLinks.length;i++) {
		slideLinks[i].onclick = function() {
			sl.stopSlideShow();
			GLOBAL_FEATURE_INDEX = parseInt(this.id.replace(/[^0-9]/g,""))-1;
			sl.focusOnStory(document.getElementById("main_feature"+this.id.replace(/[^0-9]/g,"")));
			if (document.getElementById("startPause").title == "Pause") {
				swapImg(document.getElementById("startPause"),"http://home.worldvision.org/images/slider_arrow_dark.png");
				document.getElementById("startPause").title="Play";
			}
			return false;
		}
	}
	var startPause = document.getElementById("startPause");
	startPause.onclick = function() {
		if (!GLOBAL_SLIDESHOW_ON) { // turn slide show back on.
			GLOBAL_SLIDESHOW_ON = true;
			swapImg(this,"http://home.worldvision.org/images/slider_pause_dark.png");
			this.title="Pause";
		} else { // turn off slide show
			sl.stopSlideShow();
			swapImg(this,"http://home.worldvision.org/images/slider_arrow_dark.png");
			this.title="Play";
		}
	}
}
