// handles the rotation of the 'specials' boxes

function SpecialsRotator(baseNode) {
	
	this.init = function() {
		me.getSpecials(me.drawSpecial);		
	};
	
	this.getSpecials = function(callback) {
		me.getSpecialsCallback = callback;
		var xml = new XMLTransferHelper();
		var doc = xml.createDocument();
		var root = doc.createElement("root");
		doc.appendChild(root);
		
		xml.sendDocument(SpecialsRotator.XML_TARGET, doc, me.getSpecialsResponse);
	};
	
	this.getSpecialsResponse = function(doc) {
		var specials = doc.getElementsByTagName("special");
		var response = new Object();
		var specs = new Array();
		for(var i=0; i<specials.length; i++) {
			specs[i] = me.fromNode(specials[i]);
		}
		response.specials = specs;
		me.getSpecialsCallback(response);
	};
	
	this.fromNode = function(specialNode) {
		var special = new Special();
		for(var i=0; i<specialNode.childNodes.length; i++) {
			
			var cNode = specialNode.childNodes[i];
			if(!cNode.firstChild || !cNode.firstChild.nodeValue) continue;
			
			if(cNode.nodeName == "id") {
				special.id = parseInt(cNode.firstChild.nodeValue, 10);
			} else if(cNode.nodeName == "title") {
				special.title = cNode.firstChild.nodeValue;
			} else if(cNode.nodeName == "content") {
				special.content = cNode.firstChild.nodeValue;
			} else if(cNode.nodeName == "image") {
				special.image = cNode.firstChild.nodeValue;
			} else alert(cNode.nodeName);
		}
		
		return special;
	}

	// sets the variables for the type 1 variety
	this.drawSpecial = function(response) {
		if(!response) alert("no response");
		cleanNode(me.baseNode);
		
		var specialsHeader = document.createElement("h1");
		specialsHeader.appendChild(document.createTextNode(SpecialsRotator.Header));
		me.baseNode.appendChild(specialsHeader);
		
		if(response && response.specials && response.specials.length) {
			var rand_num = rand(response.specials.length)-1;
			var random_special = response.specials[rand_num];
			
			var specialContentImg = document.createElement("img");
			specialContentImg.src = "/lib/php/fnc/image.html?url="+random_special.image+"&w=100";
			specialContentImg.style.cssFloat = "left";
			specialContentImg.style.styleFloat = "left";
			specialContentImg.style.marginRight = "20px";
			me.baseNode.appendChild(specialContentImg);
			
			var specialName = document.createElement("p");
			specialName.style.fontWeight = "bold";
			specialName.appendChild(document.createTextNode(random_special.title ? random_special.title : "N/A"));
			me.baseNode.appendChild(specialName);
			
			var specialContentDiv = document.createElement("div");
			me.baseNode.appendChild(specialContentDiv);
			
			specialContentDiv.innerHTML = random_special.content;
		}
		$(me.baseNode).show('slide');
		
		if(me.timer) {
			window.clearTimeout(me.timer);
			me.timer = null;
		}
		
		me.timer = window.setTimeout(function() {
			$(me.baseNode).hide('drop');
			me.getSpecials(me.drawSpecial);
		}, SpecialsRotator.TIMEOUT);
		
	};
	
	var me = this;
	this.baseNode = document.getElementById(baseNode);
	this.timer = null;
	
	this.init();
}

SpecialsRotator.Header = "Specials";
SpecialsRotator.TIMEOUT = 30000;

SpecialsRotator.XML_TARGET = "/lib/php/ajax/specials.xml.php";

function Special() {
	this.id = 0;
	this.title = "";
	this.content = "";
	this.image = "";
}