/*
	hottopics.js - Topic cycler v1.0
	2007-11-02 Chris Langtiw
	
	This takes a series of topics classed by hottopic with IDs of hottopic1
	and will display them in timed fashion

*/

var hotTopicCycler = new function() {
	this.topicDivs = [];
	this.divCount = 0;
	this.currentTopic = 0;
	this.timer = null;
	
	this.init = function() {
		divList = document.getElementsByTagName("DIV");
		count = divList.length;
		for (c=0; c < count; c++) {
			if (divList[c].className == 'hottopiclist') {
				this.topicDivs[this.divCount] = divList[c];
				this.divCount++;
				// add a click event to cancel any settimeout events if clicked
				divList[c].onclick = function() {
					clearTimeout(hotTopicCycler.timer);
				}
				
//				alert('Added topic number ' + this.divCount);
			}
		}
		
		if (this.divCount > 1) {
			// create nav link div dynamically when needed
			temp = document.getElementById("hottopic");
			parent1 = document.createElement("DIV");
			parent1.id = 'hottopicnav';
			temp.appendChild(parent1);

			// create prev link
			temp = document.createElement("A");
			temp.innerHTML = '&lt;&lt;Prev';
			temp.href = '#';
//			temp.id = 'hottopicprev';
			temp.onclick = function() {
				clearTimeout(hotTopicCycler.timer);
				hotTopicCycler.moveTopic('prev');
				return false;
			};
			
			parent1.appendChild(temp);
			
			parent1.appendChild(document.createTextNode(' '));
	   
			// create next link
			temp = document.createElement("A");
			temp.innerHTML = 'Next&gt;&gt;';
			temp.href = '#';
//			temp.id = 'hottopicnext';
			temp.onclick = function() {
				clearTimeout(hotTopicCycler.timer);
				hotTopicCycler.moveTopic('next');
				return false;
			};
			parent1.appendChild(temp);
			
		}

	
/*		
try {
}
catch (err) {
	errmsg = '';
	for (var i in err) {
		errmsg = errmsg + i + ': ' + err[i] + "\n";
	}
	alert(errmsg);
}

		document.getElementById("hottopicprev").onclick = function() {
			clearTimeout(hotTopicCycler.timer);
			hotTopicCycler.moveTopic('prev');
			return false;
		};
		
		document.getElementById("hottopicnext").onclick = function() {
			clearTimeout(hotTopicCycler.timer);
			hotTopicCycler.moveTopic('next');
			return false;
		};
*/		
	};
	
	this.cycleTopic = function() {
		hotTopicCycler.topicDivs[hotTopicCycler.currentTopic].style.display = 'none';
		hotTopicCycler.currentTopic++;
		if (hotTopicCycler.currentTopic >= hotTopicCycler.divCount) {
			hotTopicCycler.currentTopic = 0;
		}
		hotTopicCycler.topicDivs[hotTopicCycler.currentTopic].style.display = 'block';
//		alert('Displaying topic ' + hotTopicCycler.currentTopic);
		hotTopicCycler.timer = setTimeout(hotTopicCycler.cycleTopic, 5500);
	}
	
	this.moveTopic = function(path) {
		clearTimeout(hotTopicCycler.timer);
		if (path != 'next') path = 'prev';
		hotTopicCycler.topicDivs[hotTopicCycler.currentTopic].style.display = 'none';
		this.currentTopic = (path == 'next') ? this.currentTopic + 1 : this.currentTopic - 1;
		if (this.currentTopic < 0) this.currentTopic = this.divCount - 1;
		if (this.currentTopic >= this.divCount) this.currentTopic = 0;
		this.topicDivs[this.currentTopic].style.display = 'block';
//		alert(path + ', ' + this.currentTopic);
		hotTopicCycler.timer = setTimeout(hotTopicCycler.cycleTopic, 6500);
	};
	
	this.start = function() {
		this.init();
		this.topicDivs[this.currentTopic].style.display = 'block';
		if (this.divCount > 1) hotTopicCycler.timer = setTimeout(hotTopicCycler.cycleTopic, 5500);
	};
	
};

/* Because we're not including this code in the <head> section, will need to start it manually at runtime */
hotTopicCycler.start();

/*
window.onload = function() {
	hotTopicCycler.start();	
}

window.onunload = function() {

}
*/

/*
if(window.addEventListener){
    window.addEventListener("load",hotTopicCycler.start,false);
}
else if(window.attachEvent){
    window.attachEvent("onload",hotTopicCycler.start);
}
else{
	window.onload = hotTopicCycler.start;
}
*/	
