// JavaScript Document

/**
 * Globals
 */
var amsFlashLoaded = false;          
var amsFlashTimerSent = false;

/*################################################################################################*/
 
/**
 * Helper functions
 */
 
function create(aElementName) {
	return document.createElement(aElementName);
}

function get(aElementName) {
	return document.getElementById(aElementName);
}

/*################################################################################################*/

/**
 * Events
 */
 
/**
 * Called by the flash player when loaded.
 */
function playerReady(obj) {
  flashPlayer = get(obj['id']);
  flashPlayer.addModelListener("BUFFER","onLoadBuffer");
}

/**
 * Event called when the buffer of the Player changes
 */
function onLoadBuffer(obj) {  
  if(obj.percentage > 0) {
    amsFlashLoaded = true;
    //alert('Loaded more than 0%');
  }
  if(!amsFlashTimerSent) {    
    amsFlashTimerSent = true;
    //alert('Timer set!');
    //Errormesssage is triggert after 5 seconds
    setTimeout("showLoadingError('feedBox', 'errorMessageBox')", 5000);
  }
}

/**
 * Event called by a timer in "onloadBuffer".
 * Writes an Error to the overhanded element id.
 * @param aErrorMesssageBoxId ID if the element
 *        the errormessage will be written in.
 */
function showLoadingError(aReplaceId, aErrorMesssageBoxId) {
  if(!amsFlashLoaded){
    //Delete the possible existing items
    var vObject = get(aReplaceId);
    var vNewObject = vObject.cloneNode(false);
    vObject.parentNode.insertBefore(vNewObject,vObject);
    vObject.parentNode.removeChild(vObject);
  
    //Write Error Message when not loading
    vSpan = create('span');
    vSpan.setAttribute('class', aErrorMesssageBoxId);
    vSpan.setAttribute('id', aErrorMesssageBoxId);
    vSpan.appendChild(document.createTextNode(gErrorMessage))
    vNewObject.appendChild(vSpan);
  }
}

/*################################################################################################*/

/**
 * Page controller functions
 */
 
/**
 * Inserts text inside an element.
 * @param aElementId The Id of the element to clear.
 * @param aText The text to be insert.
 */
function switchText(aElementId, aText){
	var vObject = get(aElementId);
	var vNewObject = vObject.cloneNode(false);
	vObject.parentNode.insertBefore(vNewObject,vObject);
	vObject.parentNode.removeChild(vObject);
	//Write text inside the element box
	vNewObject.appendChild(document.createTextNode(aText));
}

/**
 * Inserts a reference inside an element.
 * @param aElementId The Id of the element to clear.
 * @param aReference A Reference to link at
 * @param aText The text to be insert.
 */
function switchLink(aElementId, aReference, aText){
	var vObject = get(aElementId);
	var vNewObject = vObject.cloneNode(false);
	vObject.parentNode.insertBefore(vNewObject,vObject);
	vObject.parentNode.removeChild(vObject);

	var vNewRef = create('a');
	vNewRef.setAttribute('href', aReference);
	vNewRef.setAttribute('target', '_blank');
	vNewRef.appendChild(document.createTextNode(aText));
	vNewObject.appendChild(vNewRef);
}

/**
 * Inserts the upcoming program items
 * @param aElementId The Id of the element to clear.
 * @param aReference A Reference to link at
 * @param aText The text to be insert.
 */
function switchProgrammItems(aElementId, aProgramData, aIconPath){
	var vObject = get(aElementId);
	var vNewObject = vObject.cloneNode(false);
	vObject.parentNode.insertBefore(vNewObject,vObject);
	vObject.parentNode.removeChild(vObject);	
	
	if(aProgramData)
	{		
		var vTable = create('table');
		for(i=0; i < aProgramData.length; i++){
			var vTr = create("tr");
			
			//Time
			var vTd = create("td");
			vTd.setAttribute("class", "time");
			vTd.appendChild(document.createTextNode(aProgramData[i].time));
			vTr.appendChild(vTd);
			
			//Info
			var vTd = create("td");
			vTd.setAttribute("class", "info");
			
			
			var vA = create("a");
			vA.setAttribute("href", aProgramData[i].ref);
			vA.setAttribute("target", "_blank");			
			var vImg = create("img");
			vImg.setAttribute("src", aIconPath);
			vImg.setAttribute('alt', 'Info');
			vA.appendChild(vImg);			
			vTd.appendChild(vA);
			vTr.appendChild(vTd);
			
			//Title
			var vTd = create("td");
			vTd.setAttribute("class", "title");
			vTd.appendChild(document.createTextNode(aProgramData[i].title));
			vTr.appendChild(vTd);
			
			vTable.appendChild(vTr);
		}
				
		vNewObject.appendChild(vTable);
	}	
}


/**
 * Emties a block element by copying the original
 * without children and deleting it afterwards.
 * @param aElementId The Id of the element to clear.
 */
function switchChannel(aChannelName, aStreamsHash, aFeedHash, aTitleElementId, aDescriptionElementId, aPlayerElementId, aProgramElementId, aLinkElementId, aSourceFolder, aPlayerHeight, aAutoplay, aImage){
	//Changing the stream and if needed the player
	var vStream = aStreamsHash[aChannelName];
	var imageUrl = 'images/' + aSourceFolder + vStream.image_url;
  
  //Resetting global variables for loader and timer
  amsFlashLoaded = false;
  amsFlashTimerSent = false;
	
	if(vStream.stream_type == 'flash') {
		var so = new SWFObject('flash/' + aSourceFolder + 'mediaplayer45.swf', 'ply', '100%', aPlayerHeight, '9', '#ffffff');
		so.addParam('allowfullscreen','true');
		so.addParam('allowscriptaccess','always');
		so.addParam('wmode','opaque');
		so.addVariable('image', imageUrl);
		so.addVariable('file', vStream.stream_file);
		so.addVariable('streamer', vStream.stream_url);
		so.addVariable('type', 'video');		
		so.addVariable('autostart', aAutoplay);
		so.write(aPlayerElementId);
	}
	else { //if stream_type == 'wmm'
		var cnt = get(aPlayerElementId);
		var src = 'flash/' + aSourceFolder + 'wmvplayer.xaml';
		var cfg = { file:vStream.stream_url,
					width:'100%', height: aPlayerHeight,
					showstop:'true',
					windowless:'true',
					image:imageUrl,
					autostart:aAutoplay};
		var ply = new jeroenwijering.Player(cnt, src, cfg);	
	}
	
	// Changing the ChannelName, vStream.lang includes the channel name in the language,
	// specificly chosen for the site.
	switchText(aTitleElementId, vStream.channel_name);
	
	// Changing the description
	switchText(aDescriptionElementId, vStream.description);
	
	// Changing the link to the specific Hope Channel site
	switchLink(aLinkElementId, vStream.link_url, vStream.channel_name);
	
	// Changing the upcoming program items	
	switchProgrammItems(aProgramElementId, aFeedHash[aChannelName], 'images/' + aSourceFolder + aImage);
}