//
// common.js -- common javascipt functions used throughout the website
// Written by Jon Burney (e4education)
// Copyright 2007 Bluestone New Media Ltd (e4education)
//


//  function:  addLoadEvent
//  arguments:  func - the name of the function to attach
//  return: none
// desc: Attaches a function to the onload event while preserving existing onload functions
//  Credit to Simon Willison (see http://simonwillison.net/2004/May/26/addLoadEvent/ for more details)
function addLoadEvent(func) {

	var oldonload = window.onload;
	
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		
		window.onload = function() {
		
			if (oldonload) {
				oldonload();
			}
		
			func();
		}
	}
}


function playPodcast(strPodcastID, strSkinName) {
	
	var elmMediaContainer = document.getElementById("mediaplayer");
	if (elmMediaContainer.childNodes.length >= 1) {	
		while (elmMediaContainer.firstChild) {
			elmMediaContainer.removeChild(elmMediaContainer.firstChild);
		}
	}
	
	intSkinHeight 	= strSkinName.substr(strSkinName.lastIndexOf("_")+1, strSkinName.length-strSkinName.lastIndexOf("_"));
	intSkinWidth 	= strSkinName.substr(strSkinName.indexOf("_")+1, strSkinName.length-strSkinName.lastIndexOf("_")-1);
	intPodcastID 	= strPodcastID.replace("podcast_", "");
	
	if (deconcept) {
		var swfObjectVer = new deconcept.SWFObjectUtil.getPlayerVersion();
		
		if (swfObjectVer) {
			if (swfObjectVer.major > 8) {
				strSRC = "http://podcasts.e4education.co.uk/player3.swf?siteurl=" + escape(strSiteURL)  + "&podcastid=" + intPodcastID + "&theme=" + strSkinName;
				var swfObjectFlash = new SWFObject(strSRC, "podcastSWFPlayer", intSkinWidth, intSkinHeight, "9", "#FFFFFF");
				swfObjectFlash.write(elmMediaContainer.id);
			} else if (swfObjectVer.major == 8) {
				strSRC = "http://podcasts.e4education.co.uk/player2.swf?siteurl=" + escape(strSiteURL) + "&podcastid=" + intPodcastID + "&theme=" + strSkinName;
				var swfObjectFlash = new SWFObject(strSRC, "podcastSWFPlayer", intSkinWidth, intSkinHeight, "8", "#FFFFFF");
				swfObjectFlash.write(elmMediaContainer.id);
			} else {
				
				var xmlReq = new XMLRequester();
	
				xmlReq.connectorPath = "/feeds/podcasts/podcast_detail.asp?";
				xmlReq.registerHandler("podcastdetail", showFmp3Player);	
				xmlReq.sendRequest("podcastdetail", "podcastid=" + intPodcastID + "&cms=true");	
			}
			
			
		}
	}
}

function showFmp3Player(xmlResponse) {
	var elmMediaContainer = document.getElementById("mediaplayer");
	var xml = xmlResponse.DOMDocument;
	var aryErrors = xml.getElementsByTagName("error");
	
	if (aryErrors.length == 0) {
		
		var aryPodcastFile = xml.getElementsByTagName("filename")
		strFileName = 	aryPodcastFile[0].firstChild.nodeValue;
		
		var aryPodcastTitle = xml.getElementsByTagName("title")
		strFileTitle = 	aryPodcastTitle[0].firstChild.nodeValue;
		
		strSRC = "/_includes/swf/audio/fmp3.swf?mp3="+strFileName+"&action=play&title="+strFileTitle+"&color=E6E9FB&vol=60&loop=no&lma=yes&textcolor=033066";
		var swfObjectFlash = new SWFObject(strSRC, "podcastSWFPlayer", "250", "60", "7", "#FFFFFF");
		swfObjectFlash.write(elmMediaContainer.id);
	}
}

//	Function:	setUpExternalLinks()
//	Scope:		Public
//	Arguments:	N/A
//	Returns:	N/A
//	Description:	Finds all of the links on a page and checks if the rel attribute is set to 'external' if it is then it sets the DOM target to be '_blank' so 
//			the link opens in a new window
function setUpExternalLinks() {

	var elmLinks = document.getElementsByTagName("a");
	
	for (var i=0; i < elmLinks.length; i++) {
		if (elmLinks[i].rel == "external") {
			elmLinks[i].target = "_blank";
		}
	}
}


function getElementStyle(elemID, IEStyleProp, CSSStyleProp) {
    var elem = document.getElementById(elemID);
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}


function htmldecode( strTextData ) {
  var tmpDiv = document.createElement("DIV");
  tmpDiv.innerHTML=strTextData;
  return ((tmpDiv.innerText) ? tmpDiv.innerText : tmpDiv.textContent);
}

addLoadEvent(setUpExternalLinks);