// javascript functions 

function check(clock) {
	
	//alert("check this 1");
	alert(document.getElementById(clock).value);
	
}


	function loadEpisodesBack(prog_id,channel_id,load) {

	
		//alert("prog id :: "+prog_id+" channel id :: "+channel_id);
		
		//alert("prog id :: "+prog_id);
		//alert('category is '+category+' ID is '+ID);
		http_request = '';
	
				if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	
				    http_request = new XMLHttpRequest();
	
				} else if (window.ActiveXObject) { // IE
	
				    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	
				}
		// callback
	
		http_request.onreadystatechange = function() {
	
		if (http_request.readyState == 4) {
	
	            if (http_request.status == 200){
	
			 	var arr = xml2array(http_request.responseXML);

			 	if (arr) {  		
	    	        	if(typeof(arr['xmlresponse']) == 'undefined')  // the array key 'xmlresponse' is for some unknown reason not defined in IE
	        	    		arr = arr['result'];
	            		else
	            			arr = arr['xmlresponse']['result'];
	            		
	            		
	            		document.getElementById(load).options.length = 0;
	            		
	            		if (arr[0]) {
							for(var item in arr) {
								//retur += "<option id='" + arr[item]['EPISODE_ID'] + "'>"+arr[item]['EPISODE_NAME']+"</option>";
								var optionn = new Option(arr[item]['title'],arr[item]['clipid']);
								document.getElementById(load).options.add(optionn);
								document.getElementById(load).style.visibility = 'visible';
							}
	            		} else {
	            			var optionn = new Option(arr['title'],arr['clipid']);
							document.getElementById(load).options.add(optionn);
							document.getElementById(load).style.visibility = 'visible';
	            		}
	            	
	            		
			 	} else {
			 		document.getElementById(load).options.length = 0;
			 	}
					
		
	
				}
	
			}
	
		}
	
		http_request.open('POST', '../admin/listEpisodes.php', true);
	
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//alert(document.getElementById('catt').value);
		
		http_request.send('prog_id='+prog_id+'&channel_id='+channel_id+'&column='+load);
	
	
		return false; 

	}
	
	
	
	
	function checkProgramFields() {
		
		msg = '';
		
		if(document.getElementById("program_1").value == 'select'){
			
			msg += "Please select a Program from listing 1\n";
			
		}
			
		if(document.getElementById("program_2").value == 'select'){
			
			msg+="Please select a Program from listing 2";
			
		}
		
		if(msg.length > 0) {
			
			alert(msg);
			return false;
		}
		 
		else 
			return true;	
		
		
		
	}
	
	function playClip(clipID,frontDomain,channelID) {
		
		//alert("playclip: "+clipID+", "+frontDomain+", "+channelID);
		// fetch the clip information based on the clip id and roll it out!
		
	if(clipID == "_") 
			return;
		
	http_request = '';

			if (window.XMLHttpRequest) { // Mozilla, Safari, ...

			    http_request = new XMLHttpRequest();

			} else if (window.ActiveXObject) { // IE

			    http_request = new ActiveXObject("Microsoft.XMLHTTP");

			}
// callback

	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
            if (http_request.status == 200){
				
            	var arr = xml2array(http_request.responseXML);
            	
            	if(typeof(arr['xmlresponse']) == 'undefined')  // the array key 'xmlresponse' is for some unknown reason not defined in IE
            		arr = arr['result'];
            	else
            		arr = arr['xmlresponse']['result'];

				document.getElementById('title').innerHTML = arr['title'];

				document.getElementById('image_top_left').src = arr['leftimage'];
					
				document.getElementById('VIDEO_IFRAME').src = "portal_player.php?clip="+arr['clipid']+"&program="+arr['program']+"&channel="+channelID+"&url=" + escape(arr['clipurl']);
				document.getElementById('heading').innerHTML = arr['heading'];
				document.getElementById('statictext').innerHTML = arr['text'];
				document.getElementById('description').innerHTML = arr['description'];
				document.getElementById('channel_link').href = arr['url'];
				
				document.getElementById("learnmore_button").src = "images/nutv_readmore_"+arr['channelid']+".jpg";
				document.getElementById("topleft_play").src = "images/nutv_play_"+arr['channelid']+".jpg";
				document.getElementById("td_topleft").style.backgroundImage = "url(images/nutv_topleft_back_"+arr['channelid']+".jpg)";
				
				/* et lille hack for at overføre parametre til changePlayer2 */
				XclipID = arr['clipid'];
				XchannelID = channelID;
				Xurl = arr['clipurl'];
				document.getElementById("topleft_play").onclick = changePlayer2; // <- reference til funktion
				
				switch(arr['channelid']) {
					
					case '1':document.getElementById("topleft_hr").style.borderTopColor = "#44283C"; break;
					case '2':document.getElementById("topleft_hr").style.borderTopColor = "#421010"; break;
					case '4':document.getElementById("topleft_hr").style.borderTopColor = "#194711"; break;
				}
				
				
				window.scroll(0,0); 
			}
		}
	}

	http_request.open('POST', 'fetchClipInfo.php', true);

	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//alert(document.getElementById('catt').value);
	
	http_request.send('clipID='+clipID+'&channelID='+channelID);
	
	return false; 
		

		
		
		
	///////////////////// function for dynamic onClick change
		
	/*document.getElementById("playbutton_1_1").onclick=function(){ 
		
			
		
		
	}*/

	}
	
	function changePlayer(clipID,url,channelID) {
			
				document.getElementById('VIDEO_IFRAME').src = "portal_player.php?clip="+clipID+"&channel="+channelID+"&url=" + escape(url);
	}
	/* same as above, but with global variables */
	function changePlayer2() {
				document.getElementById('VIDEO_IFRAME').src = "portal_player.php?clip="+XclipID+"&channel="+XchannelID+"&url=" + escape(Xurl);
	}
	
	function fullScreen() {
		
		mediaplayer = document.getElementById('VIDEO');
		
		if(mediaplayer.playState == 3)
			mediaplayer.fullScreen = true;
		/*if(mediaplayer.playState != 1 && mediaplayer.playState != 6)
			document.getElementById('VIDEO').fullScreen = true;*/
		
		
	}
	
	



	// this function fetches the episodes when the program select box changes 
	
	function loadEpisodes(channelID,program,load) {
		
		//alert("channelID is :: "+channelID);
		//alert("program id :: "+program);
		if(program == "_") 
			return;
		
		
		http_request = '';
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			http_request = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
		    http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		http_request.onreadystatechange = function() {
			if (http_request.readyState == 4) {
				if (http_request.status == 200){
					
	            	
					parent_count = new Object;
					var arra = xml2array(http_request.responseXML);
	            	
            		var alertStr = "";
    	        	if(typeof(arra['xmlresponse']) == 'undefined') { // the array key 'xmlresponse' is for some unknown reason not defined in IE
        	    		arra = arra['result'];
        	    		
    	        	} else {
            			arra = arra['xmlresponse']['result'];
            			
    	        	}
	            	
            		
            		var c=0;
            		document.getElementById(load).options.length = 0;
            		var optionn = new Option("- Vælg episode","_");
					document.getElementById(load).options.add(optionn);
					document.getElementById(load).style.visibility = 'visible';
						
					padarray = false;
					for(var item in arra) {
						if(item == 'EPISODE_NAME' || item == 'EPISODE_ID') { padarray = true; }
					}
					
					if (padarray == true) {
						padarra = new Array();
						padarra[0] = arra;
						arra = padarra;
					}
								
					for(var item in arra) {
						alertStr += c+"; item="+item+"; ";
						//retur += "<option id='" + arr[item]['EPISODE_ID'] + "'>"+arr[item]['EPISODE_NAME']+"</option>";
						try {
							alertStr += "try; ";
							if(item != 'EPISODE_NAME' && item != 'EPISODE_ID') {
								alertStr += "not_empty name="+arra[item]['EPISODE_NAME']+" id="+arra[item]['EPISODE_ID']+"; ";

								var optionn = new Option(arra[item]['EPISODE_NAME'],arra[item]['EPISODE_ID']);
								document.getElementById(load).options.add(optionn);
								document.getElementById(load).style.visibility = 'visible';
							} else {
								alertStr += "empty; ";
							}	
						} catch (e) {
							alertStr += "catch;";
						}
						alertStr += "\n";
						c++;
					}
					//alert(alertStr);
					//document.getElementById('episodebox_'+channelID).innerHTML = retur;
					//document.getElementById('episodebox_'+channelID).style.visibility = 'visible';
						
					/*
					response = http_request.responseXML;
					document.getElementById('episodebox_'+channelID).options.length = 0;
					if(response.getElementsByTagName('result').length == 0) {
						document.getElementById('episodebox_'+channelID).style.visibility = 'hidden';
					}

					
					for (i = 0; i < response.getElementsByTagName('result').length; i++) {
						
						data = response.getElementsByTagName('result')[i];
						var optionn = new Option(data.childNodes[1].firstChild.data,data.childNodes[0].firstChild.data);
						document.getElementById('episodebox_'+channelID).options.add(optionn);
						document.getElementById('episodebox_'+channelID).style.visibility = 'visible';
					}
					if(load == 'all') { // if we need to load episodes for the rest 2 as well
						loadEpisodes(2,'page');
					} else if(load == 'page') {
						loadEpisodes(4,'end');
					}
					*/
				}
			}
		}
		
		http_request.open('POST', 'fetchEpisodes.php', true);
		//alert('channelID='+channelID+'&programID='+program)
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send('channelID='+channelID+'&programID='+program);
		return false;
		
	}
	
	function loadSrc() {
		
		parent.player_frame.location.href = 'http://www.yahoo.com';
	
	}
	
