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 = (1000*(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) {
		/*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.changeSlideCounter();

		var startPause = document.getElementById("playPause");
		startPause.title="Play";
		startPause.className = "paused";

		sl.stopSlideShow();*/
		/*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;
			}
		}
		alert("story = "+story.id+"\n\r"+GLOBAL_SLIDESHOW_ON+"\n\r"+next_index);
		*/
		var move_to = parseInt(story.id.replace(/[^0-9]/g,''));
		next_index = move_to-2;
		//alert(" move_to = "+move_to+"\n\r next_index = "+next_index);
		GLOBAL_SLIDESHOW_ON=true;
		sl.slide(next_index);
		GLOBAL_SLIDESHOW_ON=false;
		sl.changeSlideCounter();

	};
	
	this.changeSlideCounter = function() {
		var sliderCounters = getElementsByClassName(document.getElementById("slider_counter"),"li","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"),"slider_arrow_dark.png");
				document.getElementById("startPause").title="Play";
			}
			return false;
		}
	}
}



var setupSlideshow = function() {
	try {
		mf=document.getElementById('main_feature_extend');
		lis=mf.getElementsByTagName('li').length;
		mf.style.width = (1000*lis)+"px";
		for(var i=0;i<lis;i++) {
			var html='<li class="slider_num" id="slider_num'+(i+1)+'" onclick="sl.focusOnStory(this);"></li>';
			document.getElementById('slider_counter').innerHTML+=html;
		}
		document.getElementById('slider_counter').innerHTML+="<li id='playPause' class='playing'></li>";
		var startPause = document.getElementById("playPause");
		startPause.onclick = function() {
			if (!GLOBAL_SLIDESHOW_ON) { // turn slide show back on.
				GLOBAL_SLIDESHOW_ON = true;
				this.title="Pause";
				this.className = "playing";
			} else { // turn off slide show
				sl.stopSlideShow();
				this.title="Play";
				this.className = "paused";
			}
		}
	
	
		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('portfolio-label').innerHTML = h3[i].innerHTML;
			}
		}
	} catch(e) {
		//setTimeout("setupSlideshow()",500);
	}
}
$(document).ready(function(){
	runSlideShow();
	setupSlideshow();
});

