// JavaScript Document
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 = 7; // frames per second for slide transitions
var GLOBAL_SLIDESHOW_DELAY=6000; // 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(300/GLOBAL_SLIDESHOW_FPS);   
// how many milliseconds between each frame transition


var slideShow = function() {
	this.findStory = function() {
		// find which article is currently being shown.
		var features = getElementsByClassName(document,"div","main_feature_content");
		if (GLOBAL_FEATURE_SHOWN == "") {
			GLOBAL_FEATURE_SHOWN = features[0].id;
			GLOBAL_FEATURE_INDEX = 0;
		} else {
			for(var i=0;i<features.length;i++) {
				if(features[i].style.visibility == "visible") {
					GLOBAL_FEATURE_INDEX = i;
					GLOBAL_FEATURE_SHOWN = features[i].id;
					break;
				}
			}
		}
		if(!GLOBAL_SLIDESHOW_ON) {
			GLOBAL_SLIDESHOW_ON = true;
		}
	};
	
	this.fade = function(obj,intVal) {
		if (BrowserDetect.browser == "Explorer") {
			try {
				var childNodes = obj.childNodes;
				for(var i=0;i<childNodes.length;i++) {
					if (childNodes[i].className != "main_feature_short_text" && childNodes[i].className.indexOf("feature_buttons") == -1) {
						try{childNodes[i].style.filter = 'alpha(opacity=' + intVal*10 + ')';}
						catch(e) {}
					}
				}
				obj.style.filter = 'alpha(opacity=' + intVal*10 + ')';
			} catch(e) {}
		}
		obj.style.opacity = intVal/10;
	};
	
	this.animateSlide = function(objFadeFrom,objFadeTo,continueSlide,slideOverride) {
		if (GLOBAL_SLIDESHOW_ON || slideOverride) {
			var fadeInStart = 10*recurrance;
			var obj1 = document.getElementById(objFadeFrom);
			for(var i=10;i>0;i--) {
				var timer = parseInt(recurrance*(Math.abs(i-10)+1));
				setTimeout("sl.fade(document.getElementById(GLOBAL_FEATURE_SHOWN),"+i+")",timer);
			}
			setTimeout("sl.featureSwitch(document.getElementById(next_feature),next_index)",fadeInStart);
			for (var i=10;i>=0;i--) {
				setTimeout("sl.fade(document.getElementById(GLOBAL_FEATURE_SHOWN),"+i+")",((recurrance*i)+fadeInStart));
			}
			if (continueSlide) {
				GLOBAL_SLIDESHOW_ON=true;
			} else {
				sl.stopSlideShow();
			}
		}
	};
	
	this.featureSwitch = function() {
		document.getElementById(GLOBAL_FEATURE_SHOWN).style.visibility = "hidden";
		document.getElementById(GLOBAL_FEATURE_SHOWN).style.position = "absolute";
		document.getElementById(next_feature).style.visibility = "visible";
		document.getElementById(next_feature).style.position = "relative";
		document.getElementById(next_feature).style.opacity = 1;
		document.getElementById(next_feature).style.filter = "alpha(opacity=100)";
		GLOBAL_FEATURE_SHOWN = next_feature;
		GLOBAL_FEATURE_INDEX = next_index;
		sl.changeSlideCounter();
	};
	
	this.slide = function() {
		if (GLOBAL_SLIDESHOW_ON) {
			var features = getElementsByClassName(document,"div","main_feature_content");
			this.findStory();
			for(var i=0;i<features.length;i++) {
				if (features[i].id == GLOBAL_FEATURE_SHOWN) {
					GLOBAL_FEATURE_SHOWN = features[i].id;
					GLOBAL_FEATURE_INDEX = i;
					if ((GLOBAL_FEATURE_INDEX + 1) == features.length) { // last story reached, go to first
						next_index = 0;
						next_feature = features[0].id
					} else {
						next_index = i+1;
						next_feature = features[i+1].id
					}
					break;
				}
			}
			if (!GLOBAL_SLIDESHOW_RUN_ONCE) {
				GLOBAL_SLIDESHOW_RUN_ONCE = setInterval("sl.slide()",GLOBAL_SLIDESHOW_DELAY);
			}
			sl.animateSlide(GLOBAL_FEATURE_SHOWN,next_feature,true)
		}
	};
	
	this.focusOnStory = function(story) {
		if (GLOBAL_FEATURE_SHOWN != story.id) { // don't need to fade if they are already on this story
			var features = getElementsByClassName(document,"div","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;
					break;
				}
			}
			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://mumbles.amorhq.net/amorhome/images/index/slider_play.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://mumbles.amorhq.net/amorhome/images/index/slider_pause.png");
			this.title="Pause";
		} else { // turn off slide show
			sl.stopSlideShow();
			swapImg(this,"http://mumbles.amorhq.net/amorhome/images/index/slider_play.png");
			this.title="Play";
		}
	}
}
