//The Show Selector
//2007-11-12, Luke McKenzie theclockspot.com

//Generates show selector control and populates it
//will be placed in dom @ <div id='showselectordiv'>
//"parent" .js should handle onChange with onShowSelect()
//Also holds and populates an array of show image urls.

//requires ajax.js for population
//requires enclosing file to specify pathtohome in javascript


//persistent variables---
var showimgs=new Array(); //this will hold the image urls
	//showimgs[v]=m, where v=showid and m=showimage

//initialization---
//Why do these weird "init" functions exist?
//The creation of the show selector is registered for the onload event of the enclosing page.
//Ordinarily we'd have it call placeShowSelector() with appropriate "mode" parameter,
//but the registration method prevents specifying parameters, which totally sucks.
//So these functions are a stopgap, allowing placeShowSelector() to be called with "mode" parameter
//(so it knows how to tailor the show selector for the page requesting its presence).

function testFunction() {
	alert("YESS!");
}

function initShowSelectorForPlaylist() {
	//have "all shows" option as default, val=-1 (means everything)
	initShowSelector('playlist');
}

function initShowSelectorForDJConsole() {
	//have "select a show" option as default, val=-2 (does nothing)
	initShowSelector('dj');
}

function initShowSelectorForPrograms() {
	//have "select a show" option as default, val=-1 (shows pictures)
	initShowSelector('programs');
}
function initShowSelectorForMgmtConsole() {
	//have "select a show" option as default, val=-1 (shows pictures)
	initShowSelector('mgmt');
}

function initShowSelector(mode) { //not intended to be public
	//builds code for show selector, places it in dom @ <div id='showselectordiv'>
	//the onchange onShowSelector() to be handled in parent document
	
	somecode=''; //construction playground
	somecode+='<select id=\'showselector\' name=\'showselector\' disabled=\'true\' onChange="onShowSelect();">\n';
	//first & default option
	if(mode=='dj') somecode+='<option value=\'-2\'>Please select a show here.</option>\n';
	if(mode=='programs') somecode+='<option value=\'-1\'>Please select a program here.</option>\n';
	if(mode=='playlist') somecode+='<option value=\'-1\'>All shows</option>\n';
	if(mode=='mgmt') somecode+='<option value=\'-2\'>Which show is giving away the tickets?</option>\n';
	//space
	somecode+='<option disabled=\'true\'>&nbsp;</option>\n';
	//dj console "empty show" and another space
	if(mode=='dj') somecode+='<option value=\'-1\'>My show isn\'t any of these!</option>\n<option disabled=\'true\'>&nbsp;</option>\n';
	//close select
	somecode+='</select>\n';
	
	//place that sucka on the page
	document.getElementById("showselectordiv").innerHTML = somecode;
	
	//populate that sucka with the shows
	populateShowSelector();
}

function populateShowSelector() { //not intended to be public function
	//run to populate the show selector
	//get a request object - requires ajax.js
	request1 = getAjaxRequestObj();
	//establish url .. main file must init pathtohome variable in js code!
	var url = pathtohome+'data/lookupShows.php?dummy=' + new Date().getTime(); //to prevent browser caching
	//no other properties are needed
	request1.open("GET", url, true);
	request1.onreadystatechange = populateShowSelectorUpdate;
	request1.send(null); //there it gooooes
}

function populateShowSelectorUpdate() {
	//alert(request1.readyState+' '+request1.status); for debugging
	if(request1.readyState == 4) { //when it's done
		if(request1.status == 200) {
			//var xmadoc = request1.responseText; //Don't delete these two lines!
			//alert(xmadoc); //They are good for debugging PHP errors in lookupSuggestions.php
			var xmlDoc = request1.responseXML; //as opposed to responseText, this is XML, which turns into a DOM tree
			var weekdayCounter = -1;
			var xmlShowid = xmlDoc.getElementsByTagName("showid");
			var xmlWeekdays = xmlDoc.getElementsByTagName('weekday');
				//I sure hope there's an entry for each and every one of them,
				//even if the entry is 'null'.
			//var xmlTime = xmlDoc.getElementsByTagName('time');
			var xmlName = xmlDoc.getElementsByTagName('name');
			var xmlDj = xmlDoc.getElementsByTagName('dj');
			var xmlImage = xmlDoc.getElementsByTagName('image');
			for(i=0; i<xmlWeekdays.length; i++) {
				//alert(i); //for testing purposes
				if(xmlWeekdays[i].firstChild.nodeValue!=weekdayCounter) {
					weekdayCounter = xmlWeekdays[i].firstChild.nodeValue;
					addWeekday(weekdayCounter);
				}
				f=xmlShowid[i].firstChild.nodeValue;
				g=unescape((xmlName[i].firstChild ? xmlName[i].firstChild.nodeValue : ''));
				h=unescape((xmlDj[i].firstChild ? ' with '+xmlDj[i].firstChild.nodeValue : ''));
				img=unescape((xmlImage[i].firstChild ? xmlImage[i].firstChild.nodeValue : 'placeholder.jpg'));
				//alert(f+' '+g+h+' '+img);
				addShow(f, g+h, img);
			}
			
			//make things not be disabled!
			document.getElementById("showselector").disabled=false;
			onSelectLoaded(); //tells main file to update stuff
			
		} else { //if the status isn't 200, this will alert us to problems //FIX EVERYWHERE
			var msgs = 'Error! request1 came back with status '+request1.status+'.';
			var bitsy = request1.getResponseHeader("Status");
			if(bitsy) msgs += '\n\n'+bitsy;
			alert(msgs);
	    } //close if request1.status = 200
	} //close if request1.readyState = 4
}
//Show-adding functions
var weekday=new Array(7);
weekday[0]='Sunday';weekday[1]='Monday';weekday[2]='Tuesday';weekday[3]='Wednesday';
weekday[4]='Thursday';weekday[5]='Friday';weekday[6]='Saturday';
function addWeekday(n) {
	//adds option i to selectbox with no value, text = name of weekday n
	var optn = document.createElement("OPTION");
	optn.text=' ';
	optn.disabled=true; //a blank row
	document.getElementById("showselector").options.add(optn);
	var optn2 = document.createElement('option');
	optn2.text=weekday[n]; //the weekday
	optn2.disabled=true;
	document.getElementById("showselector").options.add(optn2);
}
function addShow(v,n,im) {
	//adds option to selectbox with value v (showid), text = n (includes time, name, dj)
	showimgs[v]=im; //store the image url in this showimgs array
	var optn = document.createElement("option");
	optn.text=n; optn.value=v;
	a = document.getElementById("showselector");
	a.options.add(optn);
}

function getShowSelectorValue() {
	return document.getElementById("showselector").value;
}

function getImgurl() {
	return showimgs[document.getElementById("showselector").value];
}
