/*
	Blackout List Library
	
	This library contains the code for handling list events
	
	Copyright (C) Jeremy Bell
	jeremy@blackout.biz
	Blackout Entertainment Limited
*/

/*
	Stuff to do with widths and heights and positions of page elements
	
	Copyright (C) Jeremy Bell
	jeremy@blackout.biz
	Blackout Entertainment Limited
*/

var originalBodyHeight = null;
function updateBodyHeight(){
	// Refresh body height
	document.getElementById("body").style.height = null;
	// Get window height
	windowHeight = getWindowHeight();
	// Get height of non-content elements
	headerHeight = document.getElementById("header") ? getElementGrossHeight(document.getElementById("header")): 0;
	menuHeight = document.getElementById("mainMenu") ? getElementGrossHeight(document.getElementById("mainMenu")): 0;
	footerHeight = document.getElementById("footer") ? getElementGrossHeight(document.getElementById("footer")): 0;
	bodyHeight = document.getElementById("body") ? getElementHeight(document.getElementById("body")): 0;
	bodyGrossHeight = document.getElementById("body") ? getElementGrossHeight(document.getElementById("body")): 0;
	bodyMargin = bodyGrossHeight-bodyHeight;
	//if(originalBodyHeight==null) originalBodyHeight = bodyGrossHeight;
	originalBodyHeight = bodyGrossHeight;
	
	//alert("Header = "+headerHeight+"\nMenu = "+menuHeight+"\nBody "+bodyGrossHeight+"\nFooter = "+footerHeight+"\n");
	
	// Get the minimum body height
	minBodyHeight = windowHeight - (headerHeight+menuHeight+footerHeight);
	
	// Resize body if smaller than minimum height
	if(originalBodyHeight<minBodyHeight||1){
		document.getElementById("body").style.minHeight = (minBodyHeight-bodyMargin-1)+'px';
	}
	document.getElementById("footer").style.visibility = 'visible';
}

function getWindowHeight(){
	return window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
}

function getMousePosition(e){
	if (!e) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	// show the position values in the form named Show
	// in the text fields named MouseX and MouseY
	return {x:tempX,y:tempY};
}

function getElementPosition(obj){
	var x=0,y=0;
	if(obj){
		var objp = obj;
		while(1){
			if(!objp.parentNode){
				break;
			}
			objp = objp.parentNode;
			x -= objp.scrollLeft?objp.scrollLeft:0;
			y -= objp.scrollTop?objp.scrollTop:0;
		}
		if(obj.offsetParent){
			while(1){
				x += obj.offsetLeft;
				y += obj.offsetTop;
				if(!obj.offsetParent){
					break;
				}
				obj = obj.offsetParent;
			}
		} else {
			if(obj.x) x += obj.x;
			if(obj.y) y += obj.y;
		}
	}
	return {x:x,y:y};
}

function getElementWidth(obj){
	return obj.offsetWidth;
}

function getElementHeight(obj){
	return obj.offsetHeight;
}

function getElementGrossHeight(obj){
	return obj.offsetHeight+(obj.style.marginTop&&!isNaN(parseInt(obj.style.marginTop))?parseInt(obj.style.marginTop):0)+(obj.style.marginBottom&&!isNaN(parseInt(obj.style.marginBottom))?parseInt(obj.style.marginBottom):0);
}

function getElementGrossWidth(obj){
	return obj.offsetWidth+(obj.style.marginLeft&&!isNaN(parseInt(obj.style.marginLeft))?parseInt(obj.style.marginLeft):0)+(obj.style.marginRight&&!isNaN(parseInt(obj.style.marginRight))?parseInt(obj.style.marginRight):0);
}

function getElementNetWidth(obj){
	return obj.offsetWidth-(obj.style.borderLeftWidth&&!isNaN(parseInt(obj.style.borderLeftWidth))?parseInt(obj.style.borderLeftWidth):0)-(obj.style.borderRightWidth&&!isNaN(parseInt(obj.style.borderRightWidth))?parseInt(obj.style.borderRightWidth):0)-(obj.style.paddingLeft&&!isNaN(parseInt(obj.style.paddingLeft))?parseInt(obj.style.paddingLeft):0)-(obj.style.paddingRight&&!isNaN(parseInt(obj.style.paddingRight))?parseInt(obj.style.paddingRight):0);
}

function getElementNetHeight(obj){
	return obj.offsetHeight-(obj.style.borderTopWidth&&!isNaN(parseInt(obj.style.borderTopWidth))?parseInt(obj.style.borderTopWidth):0)-(obj.style.borderBottomWidth&&!isNaN(parseInt(obj.style.borderBottomWidth))?parseInt(obj.style.borderBottomWidth):0)-(obj.style.paddingTop&&!isNaN(parseInt(obj.style.paddingTop))?parseInt(obj.style.paddingTop):0)-(obj.style.paddingBottom&&!isNaN(parseInt(obj.style.paddingBottom))?parseInt(obj.style.paddingBottom):0);
}

function nothing(){}

/*
	Stuff to do with displaying and hiding text or other page elements
	
	Copyright (C) Jeremy Bell
	jeremy@blackout.biz
	Blackout Entertainment Limited
*/

function expand(box){
	$('#'+box).slideToggle('fast');
}

/*
	Stuff to do with AJAX
	
	Copyright (C) Jeremy Bell
	jeremy@blackout.biz
	Blackout Entertainment Limited
*/

function parseForKeyValuePairs(str){
	if(str&&str.length>0){
		var pairsObj = new Object();
		var pairs = new Array();
		var keyvalue = new Array();
		if(str.substr(0,1)=='&') str = str.substr(1);
		pairs = str.split('&');
		for(i=0;i<pairs.length;i++){
			keyvalue = pairs[i].split('=');
			if(keyvalue.length!=2) {
				alert("Key or Value missing.\n\nInitial string => "+str);
				return false;
			} else {
				eval("pairsObj."+keyvalue[0]+" = '"+escapeBlackout(keyvalue[1])+"';");
				eval("pairsObj."+keyvalue[0]+" = unescape(pairsObj."+keyvalue[0]+");");
			}
		}
		return pairsObj;
	}
	return false;
}

function executeEmbeddedScripts(node){
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

  if (!node) return;
	
  /* IE wants it uppercase */
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
	var scripts = new Array();
	
	for(var i=0;i<st.length;i++){
		if (bSaf) {
      strExec = st[i].innerHTML;
      st[i].innerHTML = "";
    } else if (bOpera) {
      strExec = st[i].text;
      st[i].text = "";
    } else if (bMoz) {
      strExec = st[i].textContent;
      st[i].textContent = "";
    } else {
      strExec = st[i].text;
      st[i].text = "";
    }
		
		if(strExec!=null&&strExec!="") scripts.push(strExec);
	}
	
	for(var i=0;i<scripts.length;i++){
		strExec = scripts[i];
		
		// Remove html comment syntax
		strExec = strExec.replace("<!--","");
		strExec = strExec.replace("//-->","");
		strExec = strExec.replace("// -->","");
		
		// Create attribute to hold document write output
		document.getElementsByTagName('body')[0].setAttribute("docWriteOutput","");
		
		// Fix document.write statements
		if(strExec) strExec = fixDocWrite(strExec);
		
    try {
      var x = document.createElement("script");
      x.type = "text/javascript";
			
			if(strExec){
				/* In IE we must use .text! */
				if ((bSaf) || (bOpera) || (bMoz))
					x.innerHTML = strExec;
				else x.text = strExec;
			} else {
				x.src = strSrc;
			}
			
      document.getElementsByTagName("head")[0].appendChild(x);
			
			// Get position just after script
			var html = node.innerHTML;
			var pos = html.indexOf('</script>',html.indexOf(strExec))>=0
				? html.indexOf('</script>',html.indexOf(strExec))
				: html.indexOf('</SCRIPT>',html.indexOf(strExec));
			pos += String('</script>').length;
			
			// Insert document.write output
			if(document.getElementsByTagName('body')[0].getAttribute("docWriteOutput")!=null){
				html = html.substr(0,pos) + document.getElementsByTagName('body')[0].getAttribute("docWriteOutput") + html.substr(pos);
				node.innerHTML = html;
			}
    } catch(e) {
      alert("Script execution error: "+e);
    }
  }
}

function fixDocWrite(str){
	var replacee = 'document.write(';
	var replacer = 'document.getElementsByTagName("body")[0].setAttribute("docWriteOutput",document.getElementsByTagName("body")[0].getAttribute("docWriteOutput")+';
	while(str.indexOf(replacee)>=0){
		// Get left side
		var left = str.substr(0,str.indexOf(replacee));
		// Get position of closing bracket
		var pos = getNextScriptChar(str,(str.indexOf(replacee)+replacee.length),')');
		// Get length of middle
		var middleLength = pos - (str.indexOf(replacee)+replacee.length);
		// Get middle
		var middle = str.substr(str.indexOf(replacee)+replacee.length,middleLength);
		// Get right
		var right = str.substr(pos);
		// Rebuild string
		str = left+replacer+middle+right;
	}
	return str;
}

function getNextScriptChar(str,startingpos,char){
	var insideDoubleQuote = false;
	var insideSingleQuote = false;
	var escaped = false;
	for(j=startingpos;j<str.length;j++){
		
		if(str.charAt(j)==char&&!insideDoubleQuote&&!insideSingleQuote){ // Look for the char
			return j;
		}
		
		if(str.charAt(j)=="'"&&!escaped&&insideSingleQuote){ // Look for closing single quote
			insideSingleQuote = false;
		} else if(str.charAt(j)=="'"&&!escaped&&!insideDoubleQuote){ // Look for opening single quotes
			insideSingleQuote = true;
		}
		
		if(str.charAt(j)=='"'&&!escaped&&insideDoubleQuote){ // Look for closing double quote
			insideDoubleQuote = false;
		} else if(str.charAt(j)=='"'&&!escaped&&!insideSingleQuote){ // Look for opening double quotes
			insideDoubleQuote = true;
		}
		
		if(str.charAt(j)=='\\'&&!escaped){ // Look for escape
			escaped = true;
		} else {
			escaped = false;
		}
		
	}
	return -1;
}

/*
	Stuff which can be used almost anywhere
	
	Copyright (C) Jeremy Bell
	jeremy@blackout.biz
	Blackout Entertainment Limited
*/

function isMSIE(){
	return navigator.appName == 'Microsoft Internet Explorer';
}

function getElementByAttributeValue(att,val){
	var allElements = document.getElementsByTagName('*');
	for(a=0;a<allElements.length;a++){
		if(allElements[a].getAttribute(att)&&allElements[a].getAttribute(att)==val) return allElements[a];
	}
	return false;
}

function belongsToClass(obj,targetcls,alerts){
	while(1){
		cls = obj.className?obj.className:(obj.getAttribute?obj.getAttribute('className'):"");
		if(cls){
			if(targetcls.substr(0,1)=='%'){
				tmpcls = targetcls.substr(1);
				if(cls.substr(cls.length-tmpcls.length)==tmpcls||cls.indexOf(tmpcls+" ")>=0){
					return obj;
				}
			} else if(targetcls.substr(targetcls.length-1)=='%'&&targetcls.substr(targetcls.length-2,1)!='\\'){
				tmpcls = targetcls.substr(0,targetcls.length-1);
				if(cls.substr(0,tmpcls.length)==tmpcls||cls.indexOf(" "+tmpcls)>=0){
					return obj;
				}
			} else {
				if(cls==targetcls||cls.substr(0,5)==targetcls+" "||cls.substr(cls.length-5)==" "+targetcls||cls.indexOf(" "+targetcls+" ")>=0){
					return obj;
				}
			}
		}
		if(!obj.parentNode){
			break;
		}
		obj = obj.parentNode;
	}
	return false;
}

// By Richard Rutter
// http://clagnut.com/sandbox/imagefades/
function setOpacity(obj,opacity) {
  //opacity = (opacity == 100)?99.999:opacity;
  obj.style.filter = "alpha(opacity:"+opacity+")";
  obj.style.KHTMLOpacity = opacity/100;
  obj.style.MozOpacity = opacity/100;
  obj.style.opacity = opacity/100;
}

function removeOpacity(obj){
	obj.style.filter = "";
  obj.style.KHTMLOpacity = "";
  obj.style.MozOpacity = "";
  obj.style.opacity = "";
}

function escapeBlackout(str){
	str = encodeURIComponent(str);
	return str;
}

function updateLoginBox(box,holder,textboxid){
	var temp=holder.innerHTML;
	holder.innerHTML=box.innerHTML;
	box.innerHTML=temp;
	if(textboxid) window.setTimeout("document.getElementById('"+textboxid+"').focus();",1);
}

function in_array (needle, haystack, argStrict) {
	var key = '', strict = !!argStrict;

	if (strict) {
			for (key in haystack) {
					if (haystack[key] === needle) {
							return true;
					}
			}
	} else {
			for (key in haystack) {
					if (haystack[key] == needle) {
							return true;
					}
			}
	}

	return false;
}

function getLoader(txt){
	return '<div class="clr" style="height:24px;"><div style="float:left; width:26px; padding-top:5px;"><img src="images/loader.gif" width="21" height="19" border="0" alt="" /></div><div style="float:left; width:auto; padding-top:7px;">'+txt+'</div></div>';
}

function time(){
	var mydate = new Date();
	return Math.round(mydate.getTime()/1000);
}

