var Spotlight = {

	current: 1,
	num_items: 0,
	titles: false,
	messages: false,
	link_titles: false,
	link_urls: false,
	timer: 0,
	stopped: false,

	next: function(stop){
		Spotlight.current += 2;
		Spotlight.load(Spotlight.current, stop);
	},

	prev: function(stop){
		Spotlight.load(Spotlight.current, stop);
	},

	load: function(num, stop){

		var container = document.getElementById('questions').childNodes[0];
		if(container.nodeName == '#text'){
			container = document.getElementById('questions').childNodes[1];
		}
		if(container && container.hasChildNodes && container.removeChild){
			while(container.hasChildNodes()){
				container.removeChild(container.firstChild);
			}
		}

		Spotlight.current = (num - 1);
		if(Spotlight.current >= Spotlight.num_items){
			Spotlight.current = 0;
		}else if(Spotlight.current < 0){
			Spotlight.current = (Spotlight.num_items - 1);
		}

		var title_text = Spotlight.titles[Spotlight.current];
		var message_text = Spotlight.messages[Spotlight.current];
		var link_text = Spotlight.link_titles[Spotlight.current];
		var link_url = Spotlight.link_urls[Spotlight.current];

		var title = document.createElement('h3');
		title.innerHTML = title_text;

		var message = document.createElement('p');
		message.innerHTML = message_text;

		var link = document.createElement('p');
		var ahref = document.createElement('a');
		ahref.href = link_url;
		ahref.innerHTML = link_text;
		link.appendChild(ahref);

		try {			
			container.appendChild(title);
			container.appendChild(message);
			container.appendChild(link);
		} catch(err) {
			Spotlight.stopTimer();
		}

		if(stop){
			Spotlight.stopTimer();
		}else{
			Spotlight.startTimer();
		}

	},

	stopTimer: function(){
		Spotlight.stopped = true;
		clearTimeout(Spotlight.timer);
	},

	startTimer: function(force){
		if(!Spotlight.stopped || force){
			Spotlight.stopped = false;
			clearTimeout(Spotlight.timer);
			Spotlight.timer = setTimeout(Spotlight.next, 5000, false);
		}
	},

	resetTimer: function(){
		Spotlight.stopTimer();
		Spotlight.startTimer();
	}

}

function loadSpotlight(xml, func){
	var xdoc;

	if(window.ActiveXObject){
		xdoc = new ActiveXObject("Microsoft.XMLDOM");
		xdoc.async = false;
		xdoc.load(xml);
		func(xdoc.documentElement);

		return true;
	}else if(document.implementation && document.implementation.createDocument){
		$.ajax({type: 'GET', url: xml, dataType: 'xml', success: func});
		return true;
	}else{
		return false;
	}
}

function parseSpotlight(doc){

	if(doc.documentElement){
		doc = doc.documentElement;
	}

	//var doc = xdoc.documentElement;
	var num_items = doc.childNodes.length;
	var titles = new Array();
	var messages = new Array();
	var link_titles = new Array();
	var link_urls = new Array();

	for(var i=0; i < num_items; i++){
		var item = doc.childNodes[i];
		if(item.nodeName == 'item'){
			var num_nodes = item.childNodes.length;
			for(var j=0; j < num_nodes; j++){
				var node = item.childNodes[j];
				switch(node.nodeName){
					case 'title':
						var title = node.childNodes[0].nodeValue;
						if(title) titles.push(title);
						break;
					case 'message':
						var message = node.childNodes[0].nodeValue;
						if(message) messages.push(message);
						break;
					case 'link':
						if(node.childNodes.length == 2){
							var link_title = node.childNodes[0].childNodes[0].nodeValue;
							var link_url = node.childNodes[1].childNodes[0].nodeValue;
						}else{
							var link_title = node.childNodes[1].childNodes[0].nodeValue;
							var link_url = node.childNodes[3].childNodes[0].nodeValue;
						}
						if(link_title){
							link_titles.push(link_title);
							link_urls.push(link_url);
						}
						break;
				}
			}
		}
	}

	Spotlight.num_items = titles.length;
	Spotlight.titles = titles;
	Spotlight.messages = messages;
	Spotlight.link_titles = link_titles;
	Spotlight.link_urls = link_urls;
	Spotlight.load(1);
	Spotlight.startTimer();

	if(obj = document.getElementById('question_prev')){
		obj.href = 'javascript: void(0);';
		obj.onclick = function(){ Spotlight.prev(true); }
	}
	if(obj = document.getElementById('question_1')){
		obj.href = 'javascript: void(0);';
		obj.onclick = function(){ Spotlight.load(1, true); }
	}
	if(obj = document.getElementById('question_2')){
		obj.href = 'javascript: void(0);';
		obj.onclick = function(){ Spotlight.load(2, true); }
	}
	if(obj = document.getElementById('question_3')){
		obj.href = 'javascript: void(0);';
		obj.onclick = function(){ Spotlight.load(3, true); }
	}
	if(obj = document.getElementById('question_4')){
		obj.href = 'javascript: void(0);';
		obj.onclick = function(){ Spotlight.load(4, true); }
	}
	if(obj = document.getElementById('question_next')){
		obj.href = 'javascript: void(0);';
		obj.onclick = function(){ Spotlight.next(true); }
	}

}