
//Timetable scripts:

var IE = false;
var favView = false;


var req;

function loadXMLDoc(url) {
	//window.alert(url);
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
   	 mozilla = true;
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
    	IE = true;
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}


function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
            //alert(req.responseXML.getElementsByTagName('act'));
            xmlDoc = req.responseXML;
            createSchedule();
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}


//setup xml for both IE and Firefox
function importXML(currentDay) {
	alert(currentDay);
	if (timeschedule == true){
		
		if (document.implementation && document.implementation.createDocument) {
		alert("fa");
			xmlDoc = document.implementation.createDocument("", "", null);
			xmlDoc.onload = createSchedule;
		} else if (window.ActiveXObject) {
		alert("fa");
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function () {
				if (xmlDoc.readyState == 4) createSchedule();
				IE = true; //proves this is IE (nescesary for parsing xml in a different way as Firefox)
			}
	 	} else {
			alert('Your browser can\'t handle this script');
			return;
		}
		xmlDoc.load("http://www.strp.nl/strp/strp/day/"+currentDay);		
	}
}


//setup an array with id's of act-objects (for access favorites styles etc.)
var actArrayObjectIds = new Array();


//function to switch colors of all elements according to whether they are favorited or not and if favView = true or not
function toggleFavView(){
	
	if (favView == true) {
		favView = false;
	} else {
		favView = true;
	}
	
	for (i=0; i<actArrayObjectIds.length; i++){
		var actElement = document.getElementById(actArrayObjectIds[i]);
		var typeId = actElement.getAttribute("typeId");
		var favorited = actElement.getAttribute("favorited");
		var printImgId = actElement.getAttribute("printImgId");
		var printImgUrl = "http://www.strp.nl/strp/img/bg_"+typeId+".gif";
		var printImgHover = "http://www.strp.nl/strp/img/bg_"+typeId+"_h.gif";
		var printImgNoFav = "http://www.strp.nl/strp/img/bg_nofavorite.gif";
		var printImgNoFavHover = "http://www.strp.nl/strp/img/bg_nofavorite_h.gif";
		var printImg = document.getElementById(printImgId);
		
		if (typeId!="empty"){
			if (favView == false){
				printImg.src=printImgUrl;
				actElement.setAttribute("onmouseover","document.getElementById('"+printImgId+"').src='"+printImgHover+"'");
				actElement.setAttribute("onmouseout","document.getElementById('"+printImgId+"').src='"+printImgUrl+"'");
			} else if (favView == true) {
				if (favorited == "true") {
					printImg.src=printImgUrl;
					actElement.setAttribute("onmouseover","document.getElementById('"+printImgId+"').src='"+printImgHover+"'");
					actElement.setAttribute("onmouseout","document.getElementById('"+printImgId+"').src='"+printImgUrl+"'");				
				}
				if (favorited == "false") {
					printImg.src=printImgNoFav;
					actElement.setAttribute("onmouseover","document.getElementById('"+printImgId+"').src='"+printImgNoFavHover+"'");
					actElement.setAttribute("onmouseout","document.getElementById('"+printImgId+"').src='"+printImgNoFav+"'");
				}
			}
		}
	}
}

//toggle each individual element tot favorited or not
//this will also change colors when favview is true
function toggleFavorite(id) {
	
	
	var actElement = document.getElementById(id);
	var typeId = actElement.getAttribute("typeId");
	var favorited = actElement.getAttribute("favorited");
	var favIcon = document.getElementById("imgId"+id);
	var printImgId = actElement.getAttribute("printImgId");
	var printImgUrl = "http://www.strp.nl/strp/img/bg_"+typeId+".gif";
	var printImgHover = "http://www.strp.nl/strp/img/bg_"+typeId+"_h.gif";
	var printImgNoFav = "http://www.strp.nl/strp/img/bg_nofavorite.gif";
	var printImgNoFavHover = "http://www.strp.nl/strp/img/bg_nofavorite_h.gif";
	var printImg = document.getElementById(printImgId);
	
	if(favorited == "false"){ //make it favorite if it is not
		if(favView == true){ //only change img when favView is true
			printImg.src=printImgUrl;
			actElement.setAttribute("onmouseover","document.getElementById('"+printImgId+"').src='"+printImgHover+"'");
			actElement.setAttribute("onmouseout","document.getElementById('"+printImgId+"').src='"+printImgUrl+"'");
		}
		favIcon.setAttribute("src","http://www.strp.nl/strp/img/favorite_active.png");
		favIcon.setAttribute("onmouseout","javascript:this.setAttribute('src','http://www.strp.nl/strp/img/favorite_active.png')");
		actElement.setAttribute("favorited","true"); //save favorited status in element
	} else if(favorited == "true"){ // make it not favorite when it is
		if(favView == true){		
			printImg.src=printImgNoFav;
			actElement.setAttribute("onmouseover","document.getElementById('"+printImgId+"').src='"+printImgNoFavHover+"'");
			actElement.setAttribute("onmouseout","document.getElementById('"+printImgId+"').src='"+printImgNoFav+"'");
		}
		favIcon.setAttribute("src","http://www.strp.nl/strp/img/favorite.png");
		favIcon.setAttribute("onmouseout","javascript:this.setAttribute('src','images/favorite.png')");
		actElement.setAttribute("favorited","false");
	}	
}

//construct the timeschedule
function createSchedule() {
	
	//make an array with all the xml-elements for the acts
	var actArray = xmlDoc.getElementsByTagName('act');
	
	
	//loop through this array and construct a div for each element in it
	//for (i=0;i<2;i++){     //DEBUGGING LOOP
	for (i=0;i<actArray.length;i++){
			
			//parse xml
			if (IE == true) {
				var namevalue = actArray[i].childNodes[0].firstChild.nodeValue;
				var name = document.createTextNode(namevalue);
				var id = actArray[i].childNodes[1].firstChild.nodeValue; //not in use							
				var begintime = actArray[i].childNodes[2].firstChild.nodeValue;
				var endtime = actArray[i].childNodes[3].firstChild.nodeValue;
				var typeId = actArray[i].childNodes[4].firstChild.nodeValue;
				var location = actArray[i].childNodes[5].firstChild.nodeValue;
			} else {
				var namevalue = actArray[i].childNodes[1].firstChild.nodeValue;
				var name = document.createTextNode(namevalue);			
				var id = actArray[i].childNodes[3].firstChild.nodeValue; //not in use						
				var begintime = actArray[i].childNodes[5].firstChild.nodeValue;
				var endtime = actArray[i].childNodes[7].firstChild.nodeValue;
				var typeId = actArray[i].childNodes[9].firstChild.nodeValue;
				var location = actArray[i].childNodes[11].firstChild.nodeValue;
			}
			
			//create elements for an act		
			var act = document.createElement("div");
			var title = document.createElement("div");
			title.className = "act_title";
			title.appendChild(name);	
				var plainid = id;				
			var id = location+"_"+(i+1);
			act.setAttribute("id", id);
			act.className = "timetable_act";
			act.setAttribute("typeId", typeId);
			act.setAttribute("favorited", "false"); //this initial 'true' will actually switch all elements to false
			var id2 = document.createTextNode(id); // only for test-display in browser appendchild this to act to see wat ID's the elements have
		
		
		
		
		
		
			//create image for printing and clickable area for the link to the act
			var printImgAtag = document.createElement('a');
			// Link to the act page, except for expo which will switch to expo overview
			if (plainid == 9999) {
				printImgAtag.setAttribute("href","http://www.strp.nl/strp/program/expo");
			} else if (plainid == 8888) {
				printImgAtag.setAttribute("href","http://www.strp.nl/strp/program/60,174");
			} else if (plainid == 0000) {
				printImgAtag.setAttribute("href","http://www.strp.nl/strp/program/189,148");
			} else if(plainid == 7777) {
				printImgAtag.setAttribute("href","http://www.strp.nl/strp/program/workshops");
			} else {
				printImgAtag.setAttribute("href","http://www.strp.nl/strp/artist/"+plainid);
			} //LINK TO ACT PAGE
			printImgAtag.setAttribute("title",typeId+": "+namevalue); //STANDARD BROWSER TOOLTIP
			var printImg = document.createElement('img');
			var printImgId = id+"_printimg";
			


			
			//set src for images (which contain the grid) relative to typeId (empty types included)
			printImgUrl = "http://www.strp.nl/strp/img/bg_"+typeId+".gif";
			printImg.setAttribute("src",printImgUrl);
			printImgHover = "http://www.strp.nl/strp/img/bg_"+typeId+"_h.gif";
			printImg.setAttribute("id",printImgId);
			printImg.setAttribute("typeId",typeId);
			printImg.className="print_img";	
			printImg.name=printImgId;
			printImgAtag.appendChild(title);
			printImgAtag.appendChild(printImg);
			act.setAttribute("printImgId",printImgId); //store ID of img in act, for easy access when setting src later
			
			//add act elements to an array so they can easily be accessed
			//when switching favorites view on or off
			actArrayObjectIds[i] = id;
		
			if (typeId != "empty"){ //attributes for all act except when they're empty
			act.setAttribute("onmouseover","document.getElementById('"+printImgId+"').src='"+printImgHover+"'");
			act.setAttribute("onmouseout","document.getElementById('"+printImgId+"').src='"+printImgUrl+"'");
			}

			//create favorites icon
			var favoritesIcon = document.createElement('div');
			var favoritesAtag = document.createElement('a');
			favoritesAtag.setAttribute("href","javascript:toggleFavorite('"+id+"');");
			favoritesAtag.setAttribute("title","Add / remove as favorite"); //browser tooltip
			favoritesAtag.setAttribute("onFocus", "blur()"); //remove dirty line around clicked links
			
			favoritesImg = document.createElement('img');
			favoritesImg.setAttribute("src", 'http://www.strp.nl/strp/img/favorite.png');
			favoritesImg.setAttribute("alt", "Add to favorites");
			favoritesImg.setAttribute("id", "imgId"+id);
			favoritesImg.setAttribute("onmouseover","javascript:this.setAttribute('src','http://www.strp.nl/strp/img/favorite_active_hover.png')");
			favoritesImg.setAttribute("onmouseout","javascript:this.setAttribute('src','http://www.strp.nl/strp/img/favorite.png')");
			
			favoritesAtag.appendChild(favoritesImg);
			favoritesIcon.appendChild(favoritesAtag);
			favoritesIcon.className = "favorites";
			

			
			
			//define basic values for calculating height, width and positioning
			var quarterHeight = 15; //can't be adjusted without manually adjusting the grid!
			var hourHeight = quarterHeight * 4;
			var scheduleBegin = 9; //schedule begins at 9:00
			var locationWidth = 92;
			var y_pos = (begintime - scheduleBegin) * hourHeight;
			var x_pos = (location - 1) * locationWidth;
			var duration = (endtime - begintime) * 4; //in quarters
			var height = (duration * quarterHeight);
			
			//check last to digits in begintime for determining what quarter it is
			//the position of the grid (in printImg) needs to adjusted to this
			//XML must contain only times in quarters! after 24:00, 01:00 has to be 25:00!
			var backgroundShift=0;
			var quarter = begintime.charAt(begintime.length-2)+begintime.charAt(begintime.length-1);
			if (quarter == "15") {
				backgroundShift = 15;
			}
			if (quarter == ".5") {
				backgroundShift = 30;
			}
			if (quarter == "75") {
				backgroundShift = 45;
			}
			printImg.style.top = (-1*backgroundShift)+"px";

			//positioning and sizing of an act					
			act.style.height = height-2 +"px";
			act.style.width = locationWidth-2 +"px";
			act.style.top = y_pos +"px";
			act.style.left = x_pos + "px";
			
			//construct content of an act
			if(typeId!="empty"){
				//title.appendChild(id2); //check id names in browser
				act.appendChild(printImgAtag);
				act.appendChild(favoritesIcon);
			} else {
				act.appendChild(printImg);
			}
			
			//add acts to timeschedule
			document.getElementById("schedule").appendChild(act);
	}
}





