var Protection = {

	current: 1,
	num_items: 0,
	link_titles: false,
	link_urls: false,
	timer: 0,
	stopped: false,

	next: function(stop){
		Protection.current += 2;
		Protection.load(Protection.current, stop);
	},

	prev: function(stop){
		Protection.load(Protection.current, stop);
	},

	load: function(num, stop){

		var container = document.getElementById('questions-link-noflash');

		if(container){

			if(container.hasChildNodes && container.removeChild){
				while(container.hasChildNodes()){
					container.removeChild(container.firstChild);
				}
			}

			Protection.current = (num - 1);
			if(Protection.current >= Protection.num_items){
				Protection.current = 0;
			}else if(Protection.current < 0){
				Protection.current = (Protection.num_items - 1);
			}

			var link_text = Protection.link_titles[Protection.current];
			var link_url = Protection.link_urls[Protection.current];

			var ahref = document.createElement('a');
			ahref.href = link_url;
			ahref.innerHTML = link_text;

			container.appendChild(ahref);

			if(stop){
				Protection.stopTimer();
			}else{
				Protection.startTimer();
			}
		}

	},

	stopTimer: function(){
		Protection.stopped = true;
		clearTimeout(Protection.timer);
	},

	startTimer: function(force){
		if(!Protection.stopped || force){
			Protection.stopped = false;
			clearTimeout(Protection.timer);
			Protection.timer = setTimeout(Protection.next, 5000, false);
		}
	},

	resetTimer: function(){
		Protection.stopTimer();
		Protection.startTimer();
	}

}


function loadProtection(xml, func){
	var xdoc;

	var container = document.getElementById('questions-link-noflash');

	if(container){

		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 parseProtection(doc){
	
	if(doc.documentElement){
		doc = doc.documentElement;
	}

	//var doc = xdoc.documentElement;
	var num_items = doc.childNodes.length;
	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 == 'QUESTION'){
			link_titles.push(item.getAttribute('COPY'));
			link_urls.push(item.getAttribute('LINK'));
		}
	}

	Protection.num_items = link_titles.length;
	Protection.link_titles = link_titles;
	Protection.link_urls = link_urls;
	Protection.load(1);
	Protection.startTimer();

}