// Switch through some specials
// Other browsers (including IE 7.x-8.x) ignore this
if (typeof(XMLHttpRequest) === "undefined") {
 XMLHttpRequest = function() {
 try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
 catch(e) {}
 try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
 catch(e) {}
 try { return new ActiveXObject("Msxml2.XMLHTTP"); }
 catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); }
	 catch(e) {}
 throw new Error("This browser does not support XMLHttpRequest.");
 }
}

window.download_specials = function() {
	var r = new XMLHttpRequest();
	r.open('GET', 'http://www.dinesmart.co.nz/dinesmart.rss?specials=1', true);
	r.onreadystatechange = function()
	{
		if (r.readyState != 4) { return; }
		if (r.status != 200) { return; }
		var doc = r.responseXML.documentElement;
		var items = doc.getElementsByTagName("item");
		window.dinesmart_specials = [];
		try {
			for(i=0; i<items.length; i++)
			{
				var title = items[i].getElementsByTagName("title")[0].firstChild.nodeValue.toString();
				try {
									var from = items[i].getElementsByTagName("creator")[0].firstChild.nodeValue.toString();
				} catch (e1) {
									var from = items[i].getElementsByTagName("dc:creator")[0].firstChild.nodeValue.toString();
				}
				var desc = items[i].getElementsByTagName("description")[0].firstChild.nodeValue.toString();
				var link = items[i].getElementsByTagName("link")[0].firstChild.nodeValue.toString();
				var special = { "title":title, "body":desc, "link":link, "from":from };
				window.dinesmart_specials.push(special);
			}
		} catch (e) {}
	}
	r.send('');
	return window.dinesmart_specials;
}

var current_special = 0;

window.next_special = function()
{
	jump_to_special(current_special + 1);
}

window.prev_special = function()
{
	jump_to_special(current_special - 1);
}

window.jump_to_special = function(specialnum)
{
	if (!window.dinesmart_specials.length)
	{
		return download_specials(specialnum);
	}
	
	while(specialnum < 0)
		specialnum += window.dinesmart_specials.length;
	specialnum = specialnum % window.dinesmart_specials.length;
	
	var special = window.dinesmart_specials[specialnum];
	
	document.getElementById('specialslider_title').innerHTML = special.title + " from " + special.from;
	document.getElementById('specialslider_body').innerHTML = special.body + "...";
	document.getElementById('specialslider_link').href = special.link;
	
	current_special = specialnum;
}

setTimeout(window.download_specials, 100);
