// Narrowstep Player 1.2/2.0 javascript functions

/*//---- Player Launch Control ---
// 050630	jb	all removed and put serverside

if (typeof gPlayerSessionID == "undefined") var gPlayerSessionID = 0;
if (typeof refCky == "undefined") var refCky = getCookie(GetSessionCookieName());

if (refCky) {	// all pages run this - PSC ok, if not go defualt
	var ob = TabDelimStringToObject(refCky);

	if (gPlayerSessionID != ob.psid) {
		gPlayerSessionID = ob.psid;
	}
} else if (GetSessionCookieName().indexOf("list") == -1) {
	var pth = "";
	if (window.location.href.indexOf("/skins/") > -1) pth = "../../";
	window.location.replace(pth + "default.aspx" + window.location.search);
}
*/

// nsp-cookies.js shared cookie funcs
//----------------------
// mmm, more cookies!


function checkCookie() {
	setCookie("CookieTest", "OK", 1);
	var v = getCookie("CookieTest");
	setCookie("CookieTest", "");
	return (v == "OK");
}	// checkCookie() end

function getCookie(inName) {
	var search = inName + "="
	if (document.cookie.length > 0) {
		var offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			var end = document.cookie.indexOf(";", offset);
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
}	// getCookie(inName) end

function setCookie(inName, inVal, inTime, inPeriodInSecond) {
	if (!inPeriodInSecond) inPeriodInSecond = 86400;
	if (!inTime && (document.location.href.indexOf("localhost") > -1 ||
			document.location.href.indexOf("127.0.0.1") > -1)) {
		inTime = 6;			// on IIS localhost non expiring cookies don't seem to work?
		inPeriodInSecond = 3600;	// so lets have 6 hours instead
	}
	var expire = "";
	if (inTime) {
		var today = new Date();
		today.setTime(today.getTime() + (inTime * inPeriodInSecond * 1000)); 
		expire = ";expires=" + today.toGMTString();
	}
	document.cookie = inName + "=" + escape(inVal) + expire + ";path=/;";
}	// setCookie(inName, inVal, inDays) end

function GetSessionCookieName() {
	var oStr = "";
	if (window.location.search.match(/player=([^&]*)/i)) {
		oStr = RegExp.$1;
	} else if (window.location.search.indexOf("list=on") > -1 || debugMode) {
		oStr = "list";
	}
	return "nsp2_PSC_" + oStr;
}

function TabDelimStringToObject(inString) {
	var ob = new Object();
	var ar = inString.split("\t");
	
	for (var key in ar) {
		var ar2 = ar[key].split("=");
		var offs = ar[key].indexOf("=");
		ob[ar[key].substr(0, offs)] = ar[key].substr(offs+1);
	}
	return ob;
}

//------------------------------

var LineEnd = "\n\r";

function findMenuIndex( inMenu, inString ) {
	for (i = 0; i < inMenu.options.length; i++) {
		if (inMenu.options[ i ].value == inString ) return i;
	}
	return -1;
}

function Trim(inString) {
	var retVal = "";
	var start = 0;
	while ((start < inString.length) && (inString.charAt(start) == ' ')) {
		++start;
	}
	var end = inString.length;
	while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
		--end;
	}
	retVal = inString.substring(start, end);
	return retVal;
}	// Trim(inString) end

function hex(n) {
	var h=parseInt(n).toString(16);
	return h.toUpperCase();
//	return (h.length%2)?"0"+h:h
}

function Truncate(inString, inMaxLen, inPad) {
	if (!inPad) inPad = "...";
	var oString = "";
	if (inString.length > inMaxLen)
		oString = inString.substr(0, inMaxLen - inPad.length) + inPad;
	else
		oString = inString;
	
	return oString;
}


// ---- dhtml funcs

function setTextID(inID, inText) {
	var el ;
	//= findElement(inID);
	el=document.getElementById(inID); 

	try {
		if (el) {
			el.innerHTML = inText;
			return true;
		}
		//else if (debugMode) alert("(debug) No ID = " + inID + LineEnd + el + LineEnd + window.location);
	} catch(e) {
		if (debugMode) alert("(debug exception?) No ID = " + inID + LineEnd + el + LineEnd + window.location);
	}
	return false;
}	// setTextID(inID, inText) end

/* old version (bad!)
	if (document.all) {
		if (document.all(inID)) {
			document.all(inID).innerHTML = inText;
		} else {
			//alert("Debug message: cannot find "+inID+" to display "+inText);
		}
	} else if (document.getElementById) {
		var el = findElement(inID);
		try {
			if (el) el.innerHTML = inText;
			//else if (debugMode) alert("(debug) No ID = " + inID + LineEnd + el + LineEnd + window.location);
		} catch(e) {
			//if (debugMode) alert("(debug) No ID = " + inID + LineEnd + el + LineEnd + window.location);
		}
	} else {
		document.writeln(msg);
	}
*/

function newImage(arg) {
	if (document.images) {
		var rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
var userAgent = window.navigator.userAgent;
var browserVers= 6;//= parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);
var mustInitImg = true;
function initImgID() {di = document.images; if (mustInitImg && di) { for (var i=0; i<di.length; i++) { if (!di[i].id) di[i].id=di[i].name; } mustInitImg = false;}}

function findElement(n,ly) {
	var d = document;
	if (browserVers < 4)		return d[n];
	if ((browserVers >= 5) && (d.getElementById)) {initImgID; return(d.getElementById(n))}; 
	var cd = ly ? ly.document : d;
	var elem = cd[n];
	if (!elem && cd.all) elem=cd.all[n];	// added by JB - odd!
	if (!elem && cd.layers) {
		for (var i=0;i<cd.layers.length;i++) {
			elem = findElement(n,cd.layers[i]);
			if (elem) return elem;
		}
	}
	return elem;
}

function changeImages() {
	var d = document;
	var argv = changeImages.arguments;
	
	if (d.images) {
		var img;
		for (var i=0; i<argv.length; i+=2) {
			img = null;
			if (d.layers) {img = findElement(argv[i],0);}
			else {img = d.images[argv[i]];}
			if (img) {img.src = argv[i+1];}
		}
	}
}
function CSClickReturn () {
	var bAgent = window.navigator.userAgent; 
	var bAppName = window.navigator.appName;
	if ((bAppName.indexOf("Explorer") >= 0) && (bAgent.indexOf("Mozilla/3") >= 0) && (bAgent.indexOf("Mac") >= 0))
		return true; // dont follow link
	else return false; // dont follow link
}
var CSAg = window.navigator.userAgent;
var CSBVers = parseInt(CSAg.charAt(CSAg.indexOf("/")+1),10);
var CSIsW3CDOM = ((typeof document.getElementById != "undefined" && document.getElementById) && !(IsIE()&&CSBVers<6)) ? true : false;
function IsIE() { return CSAg.indexOf("MSIE") > 0;}
function CSIEStyl(s) { return document.all.tags("div")[s].style; }
function CSNSStyl(s) { if (CSIsW3CDOM) return document.getElementById(s).style; else return CSFindElement(s,0);  }
var CSIImg=false;
function CSInitImgID() {if (!CSIImg && document.images) { for (var i=0; i<document.images.length; i++) { if (!document.images[i].id) document.images[i].id=document.images[i].name; } CSIImg = true;}}
function CSFindElement(n,ly) { if (CSBVers<4) return document[n];
	if (CSIsW3CDOM) {CSInitImgID();return(document.getElementById(n));}
	var curDoc = ly?ly.document:document; var elem = curDoc[n];
	if (!elem) {for (var i=0;i<curDoc.layers.length;i++) {elem=CSFindElement(n,curDoc.layers[i]); if (elem) return elem; }}
	return elem;
}
function CSGetImage(n) {if(document.images) {return ((!IsIE()&&CSBVers<5)?CSFindElement(n,0):document.images[n]);} else {return null;}}
var CSDInit=false;
function CSIDOM() { if (CSDInit)return; CSDInit=true; if(document.getElementsByTagName) {var n = document.getElementsByTagName('DIV'); for (var i=0;i<n.length;i++) {CSICSS2Prop(n[i].id);}}}
function CSICSS2Prop(id) { var n = document.getElementsByTagName('STYLE');for (var i=0;i<n.length;i++) { var cn = n[i].childNodes; for (var j=0;j<cn.length;j++) { CSSetCSS2Props(CSFetchStyle(cn[j].data, id),id); }}}
function CSFetchStyle(sc, id) {
	var s=sc; while(s.indexOf("#")!=-1) { s=s.substring(s.indexOf("#")+1,sc.length); if (s.substring(0,s.indexOf("{")).toUpperCase().indexOf(id.toUpperCase())!=-1) return(s.substring(s.indexOf("{")+1,s.indexOf("}")));}
	return "";
}
function CSGetStyleAttrValue (si, id) {
	var s=si.toUpperCase();
	var myID=id.toUpperCase()+":";
	var id1=s.indexOf(myID);
	if (id1==-1) return "";
	s=s.substring(id1+myID.length+1,si.length);
	var id2=s.indexOf(";");
	return ((id2==-1)?s:s.substring(0,id2));
}
function CSSetCSS2Props(si, id) {
	var el=document.getElementById(id);
	if (el==null) return;
	var style=document.getElementById(id).style;
	if (style) {
		if (style.left=="") style.left=CSGetStyleAttrValue(si,"left");
		if (style.top=="") style.top=CSGetStyleAttrValue(si,"top");
		if (style.width=="") style.width=CSGetStyleAttrValue(si,"width");
		if (style.height=="") style.height=CSGetStyleAttrValue(si,"height");
		if (style.visibility=="") style.visibility=CSGetStyleAttrValue(si,"visibility");
		if (style.zIndex=="") style.zIndex=CSGetStyleAttrValue(si,"z-index");
	}
}
function setStyleVis(s,v) {
	if (CSIsW3CDOM){CSIDOM();document.getElementById(s).style.visibility=(v==0)?"hidden":"visible";}
	else if(IsIE())CSIEStyl(s).visibility=(v==0)?"hidden":"visible";
	else CSNSStyl(s).visibility=(v==0)?'hide':'show';
}
function getStyleVis(s) {
	if (CSIsW3CDOM) {CSIDOM();return(document.getElementById(s).style.visibility=="hidden")?0:1;}
	else if(IsIE())return(CSIEStyl(s).visibility=="hidden")?0:1;
	else return(CSNSStyl(s).visibility=='hide')?0:1;
}

// window resizing func. Thanks to: Mihai Bazon (http://dynarch.com/mishoo/) for the start! JB

function setClientSize(inWidth, inHeight) {

	if (this.self != this.top || (typeof debugMode != "undefined" && debugMode)) return;	// not if i'm in a frame

    if (inWidth < 0 || inHeight < 0) {	// fullscreen
		return;
		//inWidth = window.screen.width;
		//inHeight = window.screen.height;
	}
	var dw = 0;	// delta width & height
	var dh = 0;
	var current_width = 0;
	var current_height = 0;
	var offX = 0;
	var offY = 0;
    
	if (!IsIE()) {
//		window.innerWidth = inWidth;
//		window.innerHeight = inHeight;

//		if (window.innerWidth != inWidth || window.innerHeight != inHeight) { // didn't work?
//			window.resizeBy(inWidth-window.innerWidth, inHeight-window.innerHeight);
//			// other way dont seem to work in Safari! This does!
//		}

		dw = inWidth-window.innerWidth;
		dh = inHeight-window.innerHeight;

	} else {	// is IE and a right pain!
    
//alert(inWidth +"x"+ inHeight);

		// move the window onto the screen for the first (wrong) resize
		if (inWidth + window.screenLeft > window.screen.availWidth) {
			offX = window.screen.availWidth - (inWidth + window.screenLeft) - 20;
			if (window.screenLeft + offX < 0)
				offX = -window.screenLeft;
		}
		if (inHeight + window.screenTop > window.screen.availHeight) {
			offY = window.screen.availHeight - (inHeight + window.screenTop) - 30;
			if (window.screenTop + offY < 0)
				offY = -window.screenTop + 20;
		}
		// it is important to resize the window to the
		// wanted values first, even if we won't get them.
		window.resizeTo(inWidth, inHeight);
		window.scrollTo(0,0);
		// do the move and reset for next time
		if (offX || offY) {
			window.moveBy(offX, offY);
			offX = 0; offY = 0;
		}

		// create the checkpoint element
		var cp = document.createElement("div");
		cp.style.position = "absolute";
		cp.style.width = "0px";
		cp.style.height = "0px";
		cp.style.right = "0px";
		cp.style.bottom = "0px";
		//cp.style.backgroundColor = "000";
		//cp.style.fontSize = "0px";
		
		// we can only read it's position after we
		// insert it into the document
		document.body.appendChild(cp);
		
		// here we get the actual client size
		current_width = cp.offsetLeft;
		current_height = cp.offsetTop;
		
		if (findElement("divPlayerMain")) {	// can't do this to all cos all the old players have work-arounds for this
			// however IE decides to make this at least 2px tall to fit the non-existant text in so...
			current_height = current_height + cp.offsetHeight;
			// and while we're there...
			current_width = current_width + cp.offsetWidth;
		}
			
		// here we find out how much more we need
		// in order to get to the needed inWidth x inHeight size
		// (or in other words, we compute the size of
		// window decorations: border, scroll bars, title)
		dw = inWidth - current_width;
		dh = inHeight - current_height;
				
		// we can safely delete the checkpoint now
		document.body.removeChild(cp);
		
		// for the curious, return to see how much
		// did IE miss it
//		return; nah! no returns { missed_width: dw, missed_height: dh };
	} // if IsIE
	
	// check for repositioning on screen

	// move it back on screen if over sides
	if (IsIE()) {
	// for IE try using the size it should be afterwards...?
		//if (current_width + window.screenLeft + dw > window.screen.availWidth) {
		if (inWidth + window.screenLeft > window.screen.availWidth) {
			offX = window.screen.availWidth - (inWidth + window.screenLeft);
			if (window.screenLeft + offX < 0)
				offX = -window.screenLeft;
		}
		if (inHeight + window.screenTop > window.screen.availHeight) {
			offY = window.screen.availHeight - (inHeight + window.screenTop);
			if (window.screenTop + offY < 0)
				offY = -window.screenTop + 20;
		}
	} else {
		if (window.outerWidth + window.screenX + dw > window.screen.availWidth) {
			offX = window.screen.availWidth - (window.outerWidth + window.screenX + dw);
			if (window.screenX + offX < 0)
				offX = -window.screenX;
		}
		if (window.outerHeight + window.screenY + dh > window.screen.availHeight) {
			offY = window.screen.availHeight - (window.outerHeight + window.screenY + dh);
			if (window.screenY + offY < 0)
				offY = -window.screenY + 20;
		}
	}
	if (offX || offY) {
		window.moveBy(offX, offY);
	}

	window.resizeBy(dw, dh);
	
}

/*
// Correctly handle PNG transparency in Win IE 5.5 or higher.
// http://homepage.ntlworld.com/bobosola. Updated 02-March-2004

function correctPNG() 
{
	for(var i=0; i < document.images.length; i++) {
		var img = document.images[i];
		//var imgName = "";
		//try {
		//	imgName = img.src.toUpperCase();
		//} catch (e) {}
		if (img.src && /\.png$/.test(img.src.toLowerCase()) ) {
		
			/ * imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;		
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src='" + img.src + "', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			* /
			var realSrc = img.src;
			
			img.src = gPlayerObject.gPathToSiteRoot + "images/spacer.gif";
			img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + realSrc + "', sizingMethod='scale')";
			i = i-1;
		}
	}
}
//if (window.navigator.userAgent.indexOf("MSIE") > 0) {
//	window.attachEvent("onload", correctPNG);
//}
//debugger
*/

//This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
function sprintf() {
	if (!arguments || arguments.length < 1 || !RegExp) {
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
					if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
				else if (pPad) pad = pPad;
			var justifyRight = true;
					if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
					if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
					if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
					if (pType == 'b') subst = parseInt(param).toString(2);
				else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
				else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
				else if (pType == 'u') subst = Math.abs(param);
				else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
				else if (pType == 'o') subst = parseInt(param).toString(8);
				else if (pType == 's') subst = param;
				else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
				else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}
