/**
 * BlackLight - A server side webpage framework
 *
 * @package BlackLight
 * @author Icewind <icewind (at) derideal (dot) com>
 * @copyright 2009
 * @license http://www.gnu.org/licenses/gpl.html GNU Public License
 * @url http://blacklight.metalwarp.com
 * @version 0.1
 */

//Some of the objects/functions are based from the X Library (www.cross-browser.com).

if(typeof Object.create!=='function'){
	Object.create=function(obj){
		function F(){}
		F.prototype=obj;
		return new F();
	};
}

if(typeof Object.prototype.clone!=='function'){
	Object.prototype.clone=function(obj){
		return Object.create(this);
	}
}
if(typeof String.prototype.trim!=='function'){
	String.prototype.trim=function(){  
		return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1");   
	};
}
//The callBack object provides an easy way to pass a member of an object as callback parameter and makes sure that the 'this' is always set correctly when called.
callBack=function(func,obj){
	this.func=func;
	this.obj=obj;
}

callBack.prototype=function(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
	this.call(false,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10);
}
callBack.prototype.func=false;
callBack.prototype.obj=false;
callBack.prototype.call=function(dummy,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
	//the dummy is just to provide compatibility with the normal call function and isn't used
	this.func.call(this.obj,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10);
}
callBack.prototype.apply=function(dummy,arguments){
	//the dummy is just to provide compatibility with the normal call function and isn't used
	this.apply(this.obj,arguments);
}


function addSlashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripSlashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

function urlEncode(text){
	if(text.replace){
		if(text.replace.call){
			text=text.replace(/&/g,'\\\\amp\\\\');
		}
	}
	return escape(text);
}

function dechex(dec){return dec.toString(16);}
function hexdec(hex){return parseInt(hex,16);} 

/**
 * removes unallowed characters
 * @param string text
 * @return string
 */
function cleanText(text){
	if(text.replace){
		if(text.replace.call){
			text=addSlashes(text);
			text=text.replace(/\n/g,'\\\\n');
		}
	}
	if(text){
// 		alert(text);
	}
	return text;
}

point=function(x,y){
	this.x=x;
	this.y=y;
}

function parseUri(uri){
	meta=document.getElementsByTagName('link');
	root="";
	for (var i=0;i<meta.length;i++){
		if (meta[i].getAttribute('rel')=="documentRoot"){
			root=meta[i].getAttribute('href');
		}
		if (meta[i].getAttribute('rel')=="scriptRoot"){
			scriptRoot=meta[i].getAttribute('href');
		}
		if (meta[i].getAttribute('rel')=="modFolder"){
			modFolder=meta[i].getAttribute('href');
		}
	}
	var SLRoot=root+'/modules/starlight';
	uri=uri.replace('%root%',root);
	uri=uri.replace('%slroot%',SLRoot);
	uri=uri.replace('%scriptroot%',scriptRoot);
	uri=uri.replace('%modFolder%',modFolder);
	return uri;
}

function encodeText(text){
	return BLCharEncode(text);
}

function decodeText(text){
	return BLCharDecode(text);
}

String.prototype.parseUri=function(){
	return parseUri(this);
}

loadStack=function(){
	this.functions=new Array();
	this.objects=new Array();
}
loadStack.prototype={
	append:function(func,obj){
		this.functions[this.functions.length]=func;
		this.objects[this.objects.length]=obj;
	},
	run:function(func){
		for (var i=0;i<this.functions.length;i++){
			if (this.objects[i]){
				this.functions[i].call(this.objects[i]);
			}else{
				this.functions[i].call();
			}
		}
	}
}
var mainLoadStack=new loadStack();
document.loadStack=mainLoadStack;

getChildNodesByTagName=function(element,tag){
	var ret =new Array();
	var childs=element.childNodes;
	for (var i=0;i<childs.length;i++){
		if (childs[i].tagName.toLowerCase()==tag.toLowerCase()){
			ret[ret.length]=childs[i];
		}
	}
	return ret;
}

function xCamelize(cssPropStr){
	var i, c, a = cssPropStr.split('-');
	var s = a[0];
	for (i=1; i<a.length; ++i) {
		c = a[i].charAt(0);
		s += a[i].replace(c, c.toUpperCase());
	}
	return s;
}

getStyle=function (node,strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(node, "").getPropertyValue(strCssRule);
	}
	else if(node.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = node.currentStyle[strCssRule];
	}
	return strValue;
}

getComputedElementStyle=function(element,property, integer){
	var style, value = 'undefined', dv = document.defaultView;
	if(dv && dv.getComputedStyle){
		style = dv.getComputedStyle(element,'');
		if (style) value = style.getPropertyValue(property);
	}
	else if(element.currentStyle) {
	value = element.currentStyle[xCamelize(property)];
	}
	else return null;
	return integer ? (parseInt(value) || 0) : value;
}

getProperty=function(node,property){
	return (node.style.getPropertyValue(property)!=undefined)
}

getPosition=function(element){
	var left,top;
	var css=element.style;
	if (css && element.style.left) {
		left=parseInt(e.style.left);
		if(isNaN(left)) left=GetComputedElementStyle(element,'left',1);
		if(isNaN(left)) left=0;
	}
	else if(css && element.style.pixelLeft) {
		left=element.style.pixelLeft;
	}else{
		if(isNaN(left)) left=GetComputedElementStyle(element,'left',1);
	}
	if(css && element.style.top) {
		top=parseInt(e.style.top);
		if(isNaN(top)) top=xGetComputedElementStyle(element,'top',1);
		if(isNaN(top)) top=0;
	}
	else if(css && element.style.pixelTop) {
		top=e.style.pixelTop;
	}
	return new point(left,top);
}

getTextContent=function(node,recursive){
	recursive=(recursive)?true:false;
	var text="";
	for(var i=0;i<node.childNodes.length;i++){
		child=node.childNodes.item(i);
		if (child.nodeType==3){
			text=text+child.data;
		}else if (recursive){
			text=text+child.getTextContent(true)
		}
	}
	return text;
}

// Object.prototype.clone=function(){
// 	newObject=new Object();
// 	if(typeof this=='function'){
// 		return this;
// 	}
// 	for(var name in this){
// 		if((this[name].clone||false) && (name!="clone")){
// 			newObject[name]=this[name].clone();
// 		}else{
// 			newObject[name]=this[name];
// 		}
// 	}
// 	return newObject;
// }


/* randomUUID - Version 1.0
*
* Copyright 2008, Robert Kieffer
*
* This software is made available under the terms of the Open Software License
* v3.0 (available here: http://www.opensource.org/licenses/osl-3.0.php )
*
* The latest version of this file can be found at:
* http://www.broofa.com/Tools/randomUUID.js
*
* For more information, or to comment on this, please go to:
* http://www.broofa.com/blog/?p=151
*/
 
/**
* Create and return a "version 4" RFC-4122 UUID string.
*/
function randomUUID() {
  var s = [], itoh = '0123456789ABCDEF';
 
  // Make array of random hex digits. The UUID only has 32 digits in it, but we
  // allocate an extra items to make room for the '-'s we'll be inserting.
  for (var i = 0; i <36; i++) s[i] = Math.floor(Math.random()*0x10);
 
  // Conform to RFC-4122, section 4.4
  s[14] = 4;  // Set 4 high bits of time_high field to version
  s[19] = (s[19] & 0x3) | 0x8;  // Specify 2 high bits of clock sequence
 
  // Convert to hex chars
  for (var i = 0; i <36; i++) s[i] = itoh[s[i]];
 
  // Insert '-'s
  s[8] = s[13] = s[18] = s[23] = '-';
 
  return s.join('');
}