/* Begin Main story module */
var currPic=0;
var timer1, timer2;
var paused = false;
var opacity = 100;
var msDivs = new Array(nodeLen);
var msButtons = new Array(nodeLen);
var msWrap;
var button;
var position;
var currPosition;

function initPageComponents() {
	/*  Used to load all components on the page */
	for (var i=0; i<nodeLen; i++) {
		msDivs[i] = document.getElementById('feature'+(i+1));
		msButtons[i] = document.getElementById('a'+(i+1)); 
	}
	msWrap = document.getElementById('featureContain');
	initPausePlayEvents();
	paused = false; 
	timer1 = setTimeout('timedFeature()', timeoutVal);
}

function initPausePlayEvents() {
	/* add Event Handlers for the Photo Module */
	if (!document.getElementById || !document.getElementsByTagName) {
		return true;
	}
	/* checks for Javascript operability  */ 
	
	/*  get all the links in the photo module  */
	var featureTxt = document.getElementById('featureTxt');
	var links = featureTxt.getElementsByTagName('a');
	
	for (i=0;i < links.length; i++) {
		if (links.item(i).id.substring(0,1) == 'a'){  
			//filter the links for those that have a class name beginnig with 'a'
			//add the doNumber event handler for the number links
			links.item(i).href='javascript:{}';
			addEventHandler (links [i], 'click', function (event) {
				doNumber (event);
			}, false);
		}
	}

}

/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
function fader(opac) {
	if (msWrap.style.MozOpacity!=null) {  
		/* Mozilla's pre-CSS3 proprietary rule */ 
		msWrap.style.MozOpacity = (opac/100) - .001;
	} else if (msWrap.style.opac!=null) {
		/* CSS3 compatible */
		msWrap.style.opacity = (opac/100) - .001;
	} else if (msWrap.style.filter!=null) {
		/* IE's proprietary filter */ 
		if (opac==100){
			msWrap.style.filter = "none;";
		} else {
			msWrap.style.filter = "alpha(opacity="+opac+");";
		}
	}
}

function change(num, step) {
	/*fadeOut*/
	if (step == 1) {
		opacity -= 10;
		if (opacity > 0) {
			fader(opacity);
			timer2=setTimeout('change(' + num + ', 1)',50);
		} else { 
			change(num, 2);
		}
	}
	/*change picture*/
	else if (step == 2) {
		currPic = num;
		for (var i=0; i<nodeLen; i++) {
			msDivs[i].style.display = (num == i ? "block" : "none");
			msButtons[i].className = (num == i ? "on" : "off");
		}
		change(num, 3);
	}
	/*fadeIn*/
	else if (step == 3) { 
		opacity += 10;
		if (opacity <= 100) {
			fader(opacity);
			timer2=setTimeout('change(' + num + ', 3)',50);
		}
	}
}

/* change picture, wait 5 seconds, repeat */
function timedFeature() {
	if (currPic<(nodeLen-1)){
		currPic++;
		change(currPic, 1);
		timer1=setTimeout('timedFeature()', timeoutVal);
	}else{
		currPic=0;
		clearTimeout(timer1);
		change(currPic,1);
		paused = false;
		timer1=setTimeout('timedFeature()', timeoutVal);
	}
}

/*executed when a number link is selected */
function doNumber (event) {
	var eventSource = typeof event.target != 'undefined' ? event.target : window.event.srcElement;
	/*  get the number portion of the class name of the event source */ 
	currPic = eventSource.id.substring(1,2) - 1;
	paused = false;
	clearTimeout(timer1);
	clearTimeout(timer2);
	change(currPic, 1);
	//timer1=setTimeout('timedFeature()', 10000);
	timer1=setTimeout('timedFeature()', timeoutVal);
}
