/*
Author/Autor: 
	Carlos Andres Caballero Pino, Panama   2007
	version 2.0 2007
*/

var jsLib={
	jsroot:'',
	__loader:null,
	files:new Array(),
	_class_listener:new Array(),
	browserInfo:{
		browser:navigator.userAgent,
		isBrowser:function(str){
			return this.browser.indexOf(str)!=-1?true:false;
		}
	},
	classListener:function(patern,action){
		this._class_listener[patern]=action;
	},
	init:function(){
		var objlist = document.getElementsByTagName('script');
		var tmparr=new Array();
		var src;
		for(var k=0,kl=objlist.length;k<kl;k++){
			src=objlist[k].src;
			if(jsLib.isString(src) && src!='undefined' && src!=''){				
				if(src.indexOf('jslib.js')>=0){
					this.jsroot=src;					
				}
				tmparr.push(src);
			}
		}		
		this.jsroot=this.jsroot.replace('jslib.js','');		
		for(var i in tmparr){		
			if(jsLib.isString(tmparr[i])){
				jsLib.files.push(tmparr[i].replace(jsLib.jsroot,''));
			}				
		}
		document.scrollPositionX=function(){
			var res=window.pageXOffset ? window.pageXOffset : 0;
			if(!res) res=document.documentElement ? document.documentElement.scrollLeft : 0;
			if(!res) res=document.body ? document.body.scrollLeft : 0;
			return res;
		}
		document.scrollPositionY=function(){
			var res=window.pageYOffset ? window.pageYOffset : 0;
			if(!res) res=document.documentElement ? document.documentElement.scrollTop : 0;
			if(!res) res=document.body ? document.body.scrollTop : 0;
			return res;				
		}
		delete tmparr;
		this.processListeners();
	},
	processListeners:function(){
		var elems=document.getElementsByTagName('*');
		var celem;
		var parts;
		for(var i=0;i<elems.length;i++){
			celem=elems[i];
			if(celem.className){
				parts=celem.className.split(' ');
				for(k=0;k<parts.length;k++){					
					if(this._class_listener[parts[k]]){
						this._class_listener[parts[k]](_(celem));						
					}
				}
			}
		}
	},
	include:function(file,is_absolute){
		if(!is_absolute){
			file=this.jsroot+file;
		}
		if(!this.files.in_array(file)){
			this.files.push(file);
			var tdoc = document.getElementsByTagName('head').item(0);
			var js = document.createElement('script');
			js.setAttribute('language', 'javascript');
			js.setAttribute('type', 'text/javascript');
			js.setAttribute('src', file);			
			tdoc.appendChild(js);
		}
	},
	getValue:function(k,src,def){
		var r=def;
		if(jsLib.isArray(src)){
			r=src[k]?src[k]:def;
		}
		return r;
	},
	isObject:function(obj){	return typeof obj=='object' && !jsLib.isFunction(obj);	},
	isArray:function(obj){	return jsLib.isObject(obj) && obj.constructor==Array;	},
	isFunction:function (obj){	return typeof obj=='function';	},
	isString:function (obj){	return typeof obj=='string';	},
	addOnload:function(obj,func){
		if(!obj._load_funcs){
			obj._load_funcs=new Array();
			obj.onload=function(){
				while(this._load_funcs.length>0){
					var func=this._load_funcs.shift();
					func();
				}
			}
		}
		obj._load_funcs.push(func);
	},
	onDomReady:function(func){
		if(!document._ready_calls){
			document._ready_calls=new Array();
			document._dom_ready=function(){
				while(this._ready_calls.length>0){
					var func=this._ready_calls.shift();
					func();
				}
			}			
			if(this.browserInfo.isBrowser('MSIE')){
				document.onreadystatechange = function(){
					document._dom_ready();
				}			
			}else{
				if(this.browserInfo.isBrowser('Safari')){
					window._safari_timer=window.setInterval(
						function(){
								 if(document.readyState=='complete');
									window.clearInterval(window._safari_timer);
									document._dom_ready();
								},100);
				}else{
					document.addEventListener("DOMContentLoaded", function(){
						document._dom_ready();
					},false);
				}
			}
		}
		document._ready_calls.push(func);
	},
	getCoordinates:function(obj){
		var sx=0;
		var sy=0;
		obj=jsObject(obj);
		if(obj){
			sx=obj.offsetLeft?obj.offsetLeft:0;
			sy=obj.offsetTop?obj.offsetTop:0;
			if(obj.tagName=="DIV"){
				sx-=obj.scrollLeft;
				sy-=obj.scrollTop;
			}
			oparent=obj.offsetParent;
			if(oparent){
				if(oparent.tagName!='BODY'){
					tmp=jsLib.getCoordinates(oparent);
					sx+=tmp[0];
					sy+=tmp[1];
				}
			}
		}	
		return [sx,sy];
	},
	initErrorHandler:function(){
		window.onerror=function(msg,url,l){
			txt="There was an error on this page.\n\n"
			txt+="Error: " + msg + "\n"
			txt+="URL: " + url + "\n"
			txt+="Line: " + l + "\n\n";			
			alert(txt);
			return true
		}
	},
	window:{
		resolution:{
			x:function(){return jsLib.browserInfo.isBrowser('MSIE')?document.documentElement.clientWidth:window.innerWidth; },
			y:function(){return jsLib.browserInfo.isBrowser('MSIE')?document.documentElement.clientHeight:window.innerHeight; }	
			}
	},
	colorOps:{
		getRGB:function(col){
			var str=col;
			var res={r:0,g:0,b:0};
			if(str.indexOf('#')==0 && str.length==7){
				res.r=parseInt(str.substr(1,2),16);
				res.g=parseInt(str.substr(3,2),16);
				res.b=parseInt(str.substr(5,2),16);
			}
			return res;
		},
		getHEX:function(r,g,b){
			return '#'+Math.converToHex(r)+Math.converToHex(g)+Math.converToHex(b)
		}
	},
	objectError:function(obj,msg){
		alert(obj+' Error:\n'+msg);
	},
	parseXMLString:function(str){
		var parser=null; 
		try{
			if(window.ActiveXObject){ //ms crap
				parser=new ActiveXObject("Microsoft.XMLDOM");
				parser.async='false';				
				parser.loadXML(str);
			}else{//FF & others
				parser=new DOMParser();
				parser=parser.parseFromString(str,"text/xml");
			}
			return parser;
		}catch(e){
			alert(e.message); 
		}
	}	
}
jsLib.onDomReady(function(){jsLib.init()});

function jsObject(id){
	var instance=jsLib.isObject(id)?id:document.getElementById(id);
	if(instance){
		if(instance.setStyle) return instance;
		instance.extend=function(parent,overlap){ //beta
			if(jsLib.isObject(parent)){
				for(m in parent){
					if(this[m] && parent[m] && overlap){
						continue;
					}else{
						this[m]=parent[m];
					}
				}
			}else{
				alert('jsObject::extend::Error->Invalid parent');
			}
		}		
		instance.addClass=function(cls){
			this.className+=' '+cls;
		}
		instance.removeClass=function(cls){
			this.className=this.className.replace(cls,' ');			
		}
		instance.addEvent=function(evname,func,ret){
			ret=ret?true:false;
			if(this.addEventListener){
				this.addEventListener(evname, func, ret);
			}else if(this.attachEvent){
				this.attachEvent("on" + evname, func);
			}
			return this;
		}		
		instance.removeEvent=function(ev,func,ret){
			ret=ret?true:false;
			if(this.removeEventListener){
				this.removeEventListener(ev,func,ret);
			}else if(this.detachEvent){
				this.detachEvent(ev,func);
			}
		}
		instance.getParent=function(){
			return _(this.parentElement?this.parentElement:this.parentNode);
		}
		instance.getStyle=function(stl,as_obj){
			if(jsLib.isArray(stl)){
				if(as_obj){
					var res={};
					for(var k=0;k<stl.length;k++){
						res[stl[k]]=this.style[stl[k]];
					}
				}else{
					var res=new Array();
					for(var k=0;k<stl.length;k++){
						res[k]=this.style[stl[k]];
					}
				}
				return res;
			}else{
				return this.style[stl];	
			}
		}		
		instance.setStyle=function(stl,val){
			try{
				if(jsLib.isArray(stl)){
					for(var i=0;i<stl.length;i++){
						this.style[stl[i]]=val[i];
					}
				}else if(jsLib.isString(stl)){
					this.style[stl]=val;
				}else if(jsLib.isObject(stl)){
					for(var k in stl){
						if(stl[k]) this.style[k]=stl[k]
							
					}
				}
			}catch(e){}
			return this;
		}
		instance.setOpacity=function(alpha){
			if(jsLib.browserInfo.isBrowser('MSIE')){
				this.style['filter']='alpha(opacity='+alpha+')';
			}else{
				this.style['opacity']=alpha/100.00;
			}
			return this;
		}
		instance.copySize=function(obj,no_css){
			if(jsLib.isObject(obj)){
				if(no_css){
					this.setStyle({
								  'width':obj.offsetWidth+'px',
								  'height':obj.offsetHeight+'px'
								  });
				}else{
					this.setStyle(obj.getStyle(['width','height'],true));
				}
			}
			return this;
		}
		instance.copyPosition=function(obj,no_css){
			if(jsLib.isObject(obj)){				
				if(no_css){
					var pos=obj.getCoordinates();
					this.setStyle({
								  left:pos[0]+'px',
								  top:pos[1]+'px'
								  });
				}else{
					this.setStyle(obj.getStyle(['top','left'],true));
				}
			}
			return this;
		}
		instance.show=function(mode){
			this.setStyle('display',mode?mode:'');
			return this;
		}
		instance.hide=function(){
			this.setStyle('display','none')
			return this;
		}
		instance.switchVisibility=function(){
			if(this.getStyle('display')==''){
				this.hide();
			}else{
				this.show();
			}
		}
		instance.getCoordinates=function(){
			var sx=0;
			var sy=0;			
			var tmp;
			sx=this.offsetLeft?this.offsetLeft:0;
			sy=this.offsetTop?this.offsetTop:0;
			if(this.tagName=="DIV"){
				sx-=this.scrollLeft;
				sy-=this.scrollTop;
			}
			oparent=this.offsetParent;
			if(oparent){
				if(oparent.tagName!='BODY'){
					tmp=jsObject(oparent).getCoordinates();
					sx+=tmp[0];
					sy+=tmp[1];
				}
			}
			return [sx,sy];
		}	
	}
	return instance;
}
//alias
function _(o){return jsObject(o);}

// Array object additions/overwrite
Array.prototype.iterate=function(call){
	if(jsLib.isFunction(call)){
		for(k in this){
			if(k!='iterate' && k!='in_array' && k!='array_key_exists' && k!='clear'){
				if(call(this[k],k)==false) break;
			}
		}
	}
}

Array.prototype.in_array=function(val){
	for(var k=0,len=this.length;k<len;k++){
		if(val==this[k]){
			return true;
		}
	}
	return false;
}
Array.prototype.array_key_exists=function(k){
	return this[k]?true:false;
}
Array.prototype.clear=function(){	
	for(var k,len=this.length;k<len;k++){
		delete this[k];
	}
}
Array.prototype.toString=function(){
	var str='';
	for(var k=0,len=this.length;k<len;k++){
		if(isNaN(k)){
			str+="\n"+k+"=>";
		}
		str+=this[k]+",";
	}
	if(str.length>0){
		str=str.substr(0,str.length-1);
	}
	return str;
}
Math.converToHex=function(num){
	num=parseInt(num)
	var helper=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
	var n1=Math.floor(num/16);
	return helper[n1]+helper[num-n1*16];
}