if(!base){
	var base={};
}
base.Profiler=function(){}
base.Profiler.prototype.watches=new Array()
base.Profiler.prototype.getWatch=function(id,start){
	if(typeof this.watches[id]=="undefined"){
		this.watches[id]=new base.Watch();
	}
	if(start){
		this.watches[id].start()
	}
	return this.watches[id];
}
base.Profiler.prototype.stop=function(id){
	if(this.watches[id]){
		this.watches[id].stop()
	}
}
base.Profiler.prototype.generateReport=function(){
	var div=document.getElementById(div);
	var str=""
	for(var v in this.watches){
		str+="Id: "+v
		for(var x in this.watches[v].events){
			str+="<div>duration: "+this.watches[v].events[x].duration+"</div>"
		}
	}
	return str
}
base.Watch=function(){
	this.events=new Array()
}
base.Watch.prototype.start=function(){
	this.current=new base.TimeEvent()
	this.events.push(this.current);
}
base.Watch.prototype.stop=function(){
	if(this.current){
		this.current.stop()
		this.current=null;
	}
}
base.TimeEvent=function(){
	this.start=new Date().getTime()
}
base.TimeEvent.prototype.stop=function(){
	this.duration=new Date().getTime()-this.start
}
base.Debug=function(){
	this.errcon=document.getElementById("base.errcon");
	if(!this.errcon){
		this.errcon=document.createElement("div");
		this.errcon.setAttribute("id","base.errcon")
		var body=document.getElementsByTagName("body")[0]
		body.appendChild(this.errcon)
	}
	this.errcon.style["border"]="1px solid red"
	this.errcon.style["height"]="120px"
	this.errcon.style["overflow"]="auto"
}
base.Debug.prototype.writeln=function(msg){
	var ch=document.createElement("div");
	var te=document.createTextNode(msg);
	ch.appendChild(te);
	this.errcon.appendChild(ch);
}
base.innerWidth=function(){
  var myWidth = 0
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
  } else if(document.documentElement && document.documentElement.clientWidth) {
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth) {
    myWidth = document.body.clientWidth;
  }
  return myWidth
}
base.innerHeight=function(){
  var myHeight = 0;
  if( typeof(window.innerHeight) == 'number' ) {
    myHeight = window.innerHeight;
  } else if(document.documentElement && document.documentElement.clientHeight ) {
    myHeight = document.documentElement.clientHeight;
  } else if(document.body && document.body.clientHeight) {
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

/*Event connector*/
base.EventConnector=function(source,eventType){
    this.source=source;
	if(!source.eventConnector){
		source.eventConnector={};
	}
    source.eventConnector[eventType]=this;
    source[eventType]=base.EventConnector.eventHandler;
	this.listeners=new Array();
}
base.EventConnector.prototype.addListener=function(listener){
    if(typeof listener=="function"){
        for(var i=0;i<this.listeners.length;i++){
            if(this.listeners[i]==listener){
                this.listeners[i]=listener;
                return;
            }
        }
        this.listeners[this.listeners.length]=listener;
    }
}
base.EventConnector.prototype.removeListener=function(listener){
    if(typeof listener=="function"){
        for(var i=0;i<this.listeners.length;i++){
            if(this.listeners[i]==listener){
                this.listeners.splice(i)
				return;
            }
        }
    }
}
base.EventConnector.eventHandler=function(event){
	var e=base.createUniversalEvent(event)
	var conector=this.eventConnector["on"+e["type"]];
    conector.notify(e);
}
base.EventConnector.prototype.notify=function(event){
    for(var i=0;i<this.listeners.length;i++){
            this.listeners[i].call(this.source,event);
    }
}
base.createUniversalEvent=function(event){
	var e=event||window.event
	if(!e.target){
		e.target=e.srcElement
	}
	return e;
}

base.JSONstring={
	compactOutput:false, 		
	includeProtos:false, 	
	includeFunctions: false,
	detectCirculars:true,
	restoreCirculars:true,
	make:function(arg,restore) {
		this.restore=restore;
		this.mem=[];this.pathMem=[];
		return this.toJsonStringArray(arg).join('');
	},
	toObject:function(x){
		eval("this.myObj="+x);
		if(!this.restoreCirculars || !alert){return this.myObj};
		this.restoreCode=[];
		this.make(this.myObj,true);
		var r=this.restoreCode.join(";")+";";
		eval('r=r.replace(/\\W([0-9]{1,})(\\W)/g,"[$1]$2").replace(/\\.\\;/g,";")');
		eval(r);
		return this.myObj
	},
	toJsonStringArray:function(arg, out) {
		if(!out){this.path=[]};
		out = out || [];
		var u; // undefined
		switch (typeof arg) {
		case 'object':
			this.lastObj=arg;
			if(this.detectCirculars){
				var m=this.mem; var n=this.pathMem;
				for(var i=0;i<m.length;i++){
					if(arg===m[i]){
						out.push('"JSONcircRef:'+n[i]+'"');return out
					}
				};
				m.push(arg); n.push(this.path.join("."));
			};
			if (arg) {
				if (arg.constructor == Array) {
					out.push('[');
					for (var i = 0; i < arg.length; ++i) {
						this.path.push(i);
						if (i > 0)
							out.push(',\n');
						this.toJsonStringArray(arg[i], out);
						this.path.pop();
					}
					out.push(']');
					return out;
				} else if (typeof arg.toString != 'undefined') {
					out.push('{');
					var first = true;
					for (var i in arg) {
						if(!this.includeProtos && arg[i]===arg.constructor.prototype[i]){continue};
						this.path.push(i);
						var curr = out.length; 
						if (!first)
							out.push(this.compactOutput?',':',\n');
						this.toJsonStringArray(i, out);
						out.push(':');                    
						this.toJsonStringArray(arg[i], out);
						if (out[out.length - 1] == u)
							out.splice(curr, out.length - curr);
						else
							first = false;
						this.path.pop();
					}
					out.push('}');
					return out;
				}
				return out;
			}
			out.push('null');
			return out;
		case 'unknown':
		case 'undefined':
		case 'function':
			out.push(this.includeFunctions?arg:u);
			return out;
		case 'string':
			if(this.restore && arg.indexOf("JSONcircRef:")==0){
				this.restoreCode.push('this.myObj.'+this.path.join(".")+"="+arg.split("JSONcircRef:").join("this.myObj."));
			};
			out.push('"');
			var a=['\n','\\n','\r','\\r','"','\\"'];
			arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])};
			out.push(arg);
			out.push('"');
			return out;
		default:
			out.push(String(arg));
			return out;
		}
	}
}



;(function(){
	document.clickhandler=new base.EventConnector(document,"onclick");
    window.getScrollX=function(){
        var scrollX
        if(typeof window.scrollX!="undefined"){
            scrollX=window.scrollX
        }else{
            scrollX=document.body.scrollLeft
        }
        return scrollX;
    }
    window.getScrollY=function(){
        var scrollY
        if(typeof window.scrollY!="undefined"){
            scrollY=window.scrollY
        }else{
            scrollY=document.body.scrollTop
        }
        return scrollY;
    }
	document.mousemovehandler=new base.EventConnector(document,"onmousemove");
	document.mouseuphandler=new base.EventConnector(document,"onmouseup");
	document.mouseuphandler.addListener(function(evt){
		window.activeWindow=null;
	})
	document.mousemovehandler.addListener(function(evt){
		if(window.activeWindow){
			window.activeWindow.style.top = evt.clientY-window.activeWindowY;
			window.activeWindow.style.left = evt.clientX-window.activeWindowX;
		}
	})
	window.activeWindowZIndex=0;
})()

base.makeDragable=function(el,handle){
	if(!handle){
		handle=el
	}
	if(!handle.mousedownhandler){
		handle.mousedownhandler=new base.EventConnector(handle,"onmousedown");
	}
	handle.mousedownhandler.addListener(function(evt){
		window.activeWindow=el;
		window.activeWindowY = evt.clientY-parseInt(window.activeWindow.style.top);
		window.activeWindowX = evt.clientX-parseInt(window.activeWindow.style.left);
		window.activeWindow.style["zIndex"]=window.activeWindowZIndex++
	})
}
base.WaitMessage=function(context){
	this.context=context
	this.start()
}
base.WaitMessage.prototype.start=function(){
	var top=parseFloat((this.context.offsetTop)?this.context.offsetTop:0)
	var left=parseFloat((this.context.offsetLeft)?this.context.offsetLeft:0)
	var width=parseFloat(this.context.clientWidth)
	var height=parseFloat(this.context.clientHeight)
	
	var sheild=document.createElement("div");
	var style={position: "absolute"}
	for(var v in style){sheild.style[v]=style[v];}
	sheild.style["top"]=top;
	sheild.style["left"]=left;
	sheild.style["width"]=width;
	sheild.style["height"]=height;

	var div=document.createElement("div");
	style={position: "absolute", border: "2px outset silver",
	fontWeight: "bold",fontFamily: "arial",fontSize: "12px",overflow: "hidden",padding: "2px"}
	for(var v in style){div.style[v]=style[v]}
	
	//div.innerHTML="loading..."
	var waitImg=document.createElement("img")
	waitImg.src="images/wait.gif"
	div.appendChild(waitImg)
	div.style["width"]=35
	div.style["height"]=35
	div.style["top"]=(height-35)/2;
	div.style["left"]=(width-35)/2;
	
	sheild.appendChild(div)
	this.el=sheild
	this.context.offsetParent.appendChild(sheild)
}
base.WaitMessage.prototype.end=function(){
	this.el.parentNode.removeChild(this.el)
	this.el=null;
}
base.getElementTop=function(el){
	if(typeof el=="undefined")return null
	var top=el,x=0
	while(top){
		x+=top.offsetTop;
		top=top.offsetParent;
	}
	return x;
}
base.getElementLeft=function(el){
	if(typeof el=="undefined")return null
	var top=el,x=0
	while(top){
		x+=top.offsetLeft;
		top=top.offsetParent;
	}
	return x;
}
