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=5000; // 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 IS_SLIDING = false;


var slideShow = function() {
	this.slide = function(slide_to) {
		if (GLOBAL_SLIDESHOW_ON && !IS_SLIDING) {
			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 = (300*(next_index+1));
			}
			IS_SLIDING = true;
			$('#main_feature_extend').animate({
				marginLeft: "-"+marLeft
			},800,function() {
				IS_SLIDING = false;
				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);};
//window.onload = function() {runSlideShow();}


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"),"/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,"/images/slider_pause_dark.png");
			this.title="Pause";
		} else { // turn off slide show
			sl.stopSlideShow();
			swapImg(this,"/images/slider_arrow_dark.png");
			this.title="Play";
		}
	}
}

function changeSlider(newId) {
	var holder	= document.getElementById('main_feature_extend');
	var totalLi = holder.getElementsByTagName('li').length;
	var len		= 300;
		newId	= Math.abs(newId);
	var togoto	= parseInt(newId/len)+1;
	for(var i=0;i<totalLi;i++){
		if(document.getElementById('slider_num'+(i+1))) {
			document.getElementById('slider_num'+(i+1)).className='slider_num';
		}
	}
	var h3 = document.getElementById('main_feature'+togoto).childNodes;
	var h3Val = '';
	for(var i=0;i<h3.length;i++){
		if(h3[i].tagName && h3[i].tagName.toLowerCase() == 'div' && h3[i].className=="description") {
			h3Val = h3[i].innerHTML;
		}
	}
	var feaText1 = document.getElementById("featureText1");
	if (feaText1.style.marginTop && parseInt(feaText1.style.marginTop) < 0) { // slide down
		document.getElementById("featureText1").innerHTML = h3Val;
		//IS_SLIDING = true;
		$("#featureText1").animate({
			"marginTop":(0)
		},800,function(){/*IS_SLIDING=false;*/});
	} else { // slide up
		document.getElementById("featureText2").innerHTML = h3Val;
		//IS_SLIDING=true;
		$("#featureText1").animate({
			"marginTop":(-200)
		},800,function(){
			//IS_SLIDING=false;
			document.getElementById("featureText1").innerHTML = h3Val;
			document.getElementById("featureText1").style.marginTop = 0;
		});
	}
	//document.getElementById('slider_num'+togoto).className='slider_num slider_num_selected';
}

function moveRight() {
	var holder	= document.getElementById('main_feature_extend');
	var totalLi = holder.getElementsByTagName('li').length;
	var len		= 300;
	var newLen	= (holder.style.marginLeft) ? parseInt(holder.style.marginLeft)-300 : 300;
		newLen	= Math.abs(newLen);
	if (newLen/totalLi == 300) {
		newLen = 0;
		if (!IS_SLIDING) {
			IS_SLIDING=true;
			$('#main_feature_extend').animate({
				marginLeft: newLen
			},800,function() {IS_SLIDING=false;});
			changeSlider(-newLen);
		}
	} else {
		if (!IS_SLIDING) {
			IS_SLIDING=true;
			$('#main_feature_extend').animate({
				marginLeft: -newLen
			},800,function() {IS_SLIDING=false;});
			changeSlider(-newLen);
		}
	}
}

function moveLeft() {
	var holder	= document.getElementById('main_feature_extend');
	var totalLi = holder.getElementsByTagName('li').length;
	var len		= 300;
	var newLen	= (holder.style.marginLeft) ? parseInt(holder.style.marginLeft)+len : len;
		newLen	= Math.abs(newLen);
		//alert(newLen);
	if (parseInt(holder.style.marginLeft) == 0 || !holder.style.marginLeft) {
		var newLen = newLen*(totalLi-1);
		if (!IS_SLIDING) {
			IS_SLIDING=true;
			$('#main_feature_extend').animate({
				marginLeft: -newLen
			},800,function() {IS_SLIDING=false;});
			changeSlider(-newLen);
		}
	} else {
		holder.style.marginLeft - len;
		if (!IS_SLIDING) {
			IS_SLIDING=true;
			$('#main_feature_extend').animate({
				marginLeft: -newLen
			},800,function() {IS_SLIDING=false;});
			changeSlider(-newLen);
		}
	}
}

window.onload=function() {
	mf=document.getElementById('main_feature_extend');
	lis=mf.getElementsByTagName('li').length;
	mf.style.width = (300*lis)+"px";
	for(var i=0;i<lis;i++) {
		var html='<li class="slider_num" id="slider_num'+(i+1)+'"></li>';
		document.getElementById('slider_counter').innerHTML+=html;
	}
	if (document.getElementById('slider_num1')) {
		document.getElementById('slider_num1').className='slider_num slider_num_selected';
	}
	mfe=document.getElementById('main_feature_extend');
	for(var i=0;i<mfe.getElementsByTagName('li').length;i++){
		mfe.getElementsByTagName('li')[i].id = 'main_feature'+(i+1);
	}
	marLef = Math.round(lis*7.5);
	document.getElementById('slider_counter').style.marginLeft = parseInt(document.getElementById('slider_counter').style.marginLeft)-marLef+"px";

	var h3 = document.getElementById('main_feature1').childNodes;
	for(var i=0;i<h3.length;i++){
		if(h3[i].tagName && h3[i].tagName.toLowerCase() == 'h3') {
			document.getElementById('featureText').innerHTML = h3[i].innerHTML;
		}
	}
}
