//
// 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);


function generateCalendarPopout(divID,calID,objDay,objMonth,objYear,pageID){
		calendarPopout = "";
		$('#'+divID).ajaxError(function(event, request, settings){
		   switch (request.status){
				case 404:
					$(this).html("<p><strong>Calendar popup error</strong></p><p>/_includes/code/calendarpopout.ajax.asp not found!</p>");
				break;
				case 500:
					$(this).html("<p><strong>Calendar popup error</strong></p><p>Check that calendarData.asp is <strong>NOT</strong> included on common.asp.<br />Including this file in common.asp will prevent the calendar popup from working and displays this message.</p>");
				break;
				default:
					$(this).html("<p><strong>Calendar popup error</strong></p><p>Something went wrong, but I'm not totally sure what :(</p>");
				break;
		   }
		});
		calendarPopout = $.ajax({
		  type: "GET",
		  url: "/_includes/code/calendarpopout.ajax.asp?viewtype=calblock&calid="+calID+"&divID="+divID+"&day="+objDay+"&month="+objMonth+"&year="+objYear+"&pid="+pageID,
		  dataType: "html",
		  success: function (data){
			$('#'+divID).html(data);
		  }
		});
	}
	
function getQueryVariable(variable) {
	  var query = window.location.search.substring(1);
	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
		  return pair[1];
		}
	  } 
}