//This first line is to ensure the ready function fires if the back button is used to reach the page
history.navigationMode = 'compatible';
$(document).ready(function(){
	loadHomeTicker();
	//scrollTicker('ticker');
	$('#nextticker').click(function(){
		clearTimer(0);
		showNextEntry(0);
		setTimer(0);
	});
});
function scrollTicker(TickerID) {
	$("#"+TickerID).simplyScroll({
		className: 'vert',
		horizontal: false,
		frameRate: 25,
		speed: 3
	});
}

function switchHomeAction(action,state) {
	if ( !document.getElementById ) {return false;}
	if ( !document.getElementsByTagName ) {return false;}
	if ( !document.getElementById('action-' + action) ) {return false;}
	if ( !document.getElementById('action-' + action).getElementsByTagName('img') ) {return false;}
	
	var mylink = document.getElementById('action-' + action);
	var myimg = mylink.getElementsByTagName('img')[0];
	
	if ( state == 'on' ) {
		myimg.src = '/i/d/1/home/' + action + '_on.gif';
	} else {
		myimg.src = '/i/d/1/home/' + action + '.gif';
	}
}
function off_profile() {switchHomeAction('profile','off')}
function on_profile() {switchHomeAction('profile','on')}
function off_learnmore() {switchHomeAction('learnmore','off')}
function on_learnmore() {switchHomeAction('learnmore','on')}
function off_join() {switchHomeAction('join','off')}
function on_join() {switchHomeAction('join','on')}
function loadHomeActions() {
	if ( !document.getElementById ) {return false;}
	
	var actionlist = 'profile,learnmore,join';
	var actions = actionlist.split(',');
	var i = 0;
	var action = '';
	var myid = '';
	
	for ( i=0; i < actions.length; i++ ) {
		action = actions[i];
		myid = 'action-' + action;
		if ( document.getElementById(myid) ) {
			addEventToId(myid,'mouseover',eval('on_'+action));
			addEventToId(myid,'mouseout',eval('off_'+action));
		}
	}
}
addEvent(window,'load',loadHomeActions);

//showNextEntry, setTimer and clearTimer are used to control the ticker on the homepage and wherever else it occurs

function loadHomeTicker() {
	arrIdx = 0;
	showNextEntry(arrIdx);
	setTimer(arrIdx);
}
function loadTrainingTicker() {
	arrIdx = 1;
	showNextEntry(arrIdx);
	setTimer(arrIdx);
}
		
function showNextEntry(arrIdx) {

	//initialize locals (see /layouts/Home.cfc for other ticker initializations)
	NextContent[arrIdx] = "";
	NextTitle[arrIdx] = "";
	EntryHTML[arrIdx] = "";
	EntryTitle[arrIdx] = "";
	var VertDiv = $('#'+TickerElement[arrIdx]).parent().parent('.vert');
	//load them up and display the ticker if there is anything in the ticker array
	if (arrTickerContent[arrIdx].length > 0) {
		NextContent[arrIdx] = arrTickerContent[arrIdx][NextEntry[arrIdx]];
		NextTitle[arrIdx] = arrTickerTitle[arrIdx][NextEntry[arrIdx]];
	
		if (NextContent[arrIdx].length > 0) {
			EntryHTML[arrIdx] = NextContent[arrIdx];
			EntryTitle[arrIdx] = NextTitle[arrIdx];
		}
		
		if (arrTickerContent[arrIdx].length > 1) { //no need to loop if only one entry
			//move to the next entry or start from the top if we're at the end of the array
			if (NextEntry[arrIdx] < arrTickerContent[arrIdx].length - 1)
				NextEntry[arrIdx] = NextEntry[arrIdx] + 1;
			else
				NextEntry[arrIdx] = 0;
		} else {
			clearTimer(arrIdx);
		}
	} else { //clear the timer if the ticker is empty
		clearTimer(arrIdx);
	}
	
	//display the ticker
	$("h1#"+TickerTitle[arrIdx]).html(EntryTitle[arrIdx]);

	if (VertDiv.length) {
		VertDiv.replaceWith('<div id="'+TickerElement[arrIdx]+'">'+EntryHTML[arrIdx]+'</div>');
	} else {
		$('#'+TickerElement[arrIdx]).html(EntryHTML[arrIdx]);
	}
	scrollTicker('ticker');
}

function showNextHome() {
	showNextEntry(0);
}
function showNextTraining() {
	showNextEntry(1);
}
function setTimer(arrIdx) {
//Don't really like this solution, but setInterval will not let you pass an argument with the function called.
	if (arrIdx == 0)
		TimerID[arrIdx] = setInterval( "showNextHome()", 10000 );
	if (arrIdx == 1)
		TimerID[arrIdx] = setInterval( "showNextTraining()", 10000 );
}
function clearTimer(arrIdx) {
	clearInterval ( TimerID[arrIdx] );
}
