// retrieve content from pages with xmlHttpRequest
// file = the file to parse
var content= '';
function getPage(file){
	// clear the fetching variable
	var xmlhttp=false; 
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); // try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // if we were able to get a working active x object, start an XMLHttpRequest
	}
	// open the file through GET
	xmlhttp.open('GET', file, true); 
	xmlhttp.onreadystatechange=function() {
		// check if it is ready to receive data
		if (xmlhttp.readyState==4) { 
			// get the data retrieve
			content = xmlhttp.responseXML; 
			if(content){ // check if there is something in the content variable
				handleResponse(content); // handle the response
			}
		}
	}
	// reset the XMLHttpRequest
    xmlhttp.send(null) 
	return;
	
}

// common var
var d = document;

// create element
function dce(obj) {
	return d.createElement(obj);
}

//create text node
function dct(str) {
	return d.createTextNode(str);
}

// clear content of the html elements with cloneNode method
function clearobj(e) {
	f = e.cloneNode(false);
	e.parentNode.insertBefore(f,e);
	e.parentNode.removeChild(e);
}

// get objects
function getobj(e){
	return d.getElementById(e);
}

// parse the results
function handleResponse(r) {
	
	// clear content
	clearobj(getobj('show_type'));
	clearobj(getobj('show_info'));
	clearobj(getobj('show_desc'));
	
	// get reference to div elements 
	var show_type = getobj('show_type');
	var show_info = getobj('show_info');
	var show_desc = getobj('show_desc');
	
	// parse the xml 
	var live = r.getElementsByTagName('live');
	// could skip the loop since we only have one element
	for (var i=0;i<live.length;i++){
		// append the type of the show to show_type div
		show_type.appendChild(dct(getNodeValue(live[i],'type')));
		// append the info of the show to show_info div
		var y = dce('strong'); // set the date in bold
		y.appendChild(dct(getNodeValue(live[i],'date'))); // append date
		show_info.appendChild(y);
		show_info.appendChild(dce('br'));
		show_info.appendChild(dct(getNodeValue(live[i],'time'))); // append time
		show_info.appendChild(dce('br'));
		show_info.appendChild(dct(getNodeValue(live[i],'shortdesc'))); // append short desc
		// append the description of the show to show_desc div
		show_desc.appendChild(dct(getNodeValue(live[i],'desc')));
	}
}

function getNodeValue(obj,tag){
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

// change the hover state of the current element
// e = current clicked element
function changeHoverState(e){
	var links, i;
	// get all <a> tag in the list
	links=getobj('show_schedule').getElementsByTagName('a');
	// set default class to all links
	for(i in links)
	{
		links[i].className='row';
	}
	// set the class to the current one
	e.className='row-current';
}
