var tii_isopera = typeof window.opera != 'undefined';
var tii_isie = typeof document.all != 'undefined' && !tii_isopera && navigator.vendor != 'KDE';
var tii_issafari = navigator.vendor == 'Apple Computer, Inc.';


function callFunctionOnWindowLoad (functionToCall) {
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener ('load', functionToCall, false);
	} else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener ('load', functionToCall, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent ('onload', functionToCall);
	} else {
		var oldFunctionToCall = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = functionToCall;
		} else {
			window.onload = function () {
				oldFunctionToCall ();
				functionToCall ();
			};
		}
	}
}


function callFunctionOnElementLoad (targetId, functionToCall) {
	var myArguments = arguments;
	callFunctionOnWindowLoad (function () {
			window.loaded = true;
		});
	var targetElement = document.getElementById (targetId);
	if (targetElement == null && !window.loaded) {
		var pollingInterval = setInterval (function () {
			if (window.loaded) {
				clearInterval (pollingInterval);
			}
			targetElement = document.getElementById (targetId);
			if (targetElement != null) {
				clearInterval (pollingInterval);
				var argumentsTemp = new Array ();
				var argumentsTempLength = myArguments.length - 2;
				for (var i = 0; i < argumentsTempLength; i++) {
					argumentsTemp [i] = myArguments [i + 2];
				}
				functionToCall.apply (this, argumentsTemp);
			}
		}, 10);
	}
}

function tii_addEventHandlerOnElementLoad (targetId, eventType, functionToCall, bubbleEventUpDOMTree) {
	callFunctionOnWindowLoad (function () {
			window.loaded = true;
		});
	var targetElement = document.getElementById (targetId);
	if (targetElement == null && !window.loaded) {
		var pollingInterval = setInterval (function () {
			if (window.loaded)
			{
				clearInterval (pollingInterval);
			}
			targetElement = document.getElementById (targetId);
			if (targetElement != null)
			{
				clearInterval (pollingInterval);
				tii_addEventHandler (targetElement, eventType, functionToCall, bubbleEventUpDOMTree);
			}
		}, 10);
	}
}

function addEventHandler (targetElement, eventType, functionToCall, bubbleEventUpDOMTree) {
	if (!targetElement) {
		window.status = 'Warning: Tried to attach event to null object';
		return false;
	}
	if (typeof targetElement.addEventListener != 'undefined') {
		targetElement.addEventListener (eventType, functionToCall, bubbleEventUpDOMTree);
	} else if (typeof targetElement.attachEvent != 'undefined') {
		targetElement.attachEvent ('on' + eventType, functionToCall);
	} else {
		eventType = 'on' + eventType;
		if (typeof targetElement [eventType] == 'function') {
			var oldListener = targetElement [eventType];
			targetElement [eventType] = function () {
				oldListener ();
				return functionToCall ();
			}
		} else {
			targetElement [eventType] = functionToCall;
		}
	}
	
	return true;
}


/* 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);
}


var keyevent = tii_issafari || tii_isie ? 'keydown' : 'keypress';
tii_addEventHandlerOnElementLoad ( 'query', keyevent, 
function (event){
	var qBox = typeof event.target != 'undefined' ? event.target : window.event.srcElement;
	qBox.style.color = '#000';
}, false);

callFunctionOnElementLoad('player', initPageComponents);

