/************************************************************************************
*		ToolBox Object: collection of methods and properties for referring
*		and manipulation of non-browser-specific properties in NS respective
*		IE browsers. (note clip is not implemented on IE4 Mac)
*
*		author: Marco Romeny
*		
*		no resposibility is taken for damages on software, hardware or anything
*		one ever can figure out in this universe.
*		use it for free. you may not resale this mumsich code.
*
*
*	constructor:	ToolBox()
*	initializer:	flatten(), is to be called after document has loaded.
*					reexecute after adding new layers.			
*	//properties:	
*					images, links, forms, applets, layers in a flat array-
*					structure. NS is true for netscape, MAC is true for 
*					MAcintosh-platforms. cookies (hash-array with cookies)
*	//methods:		
*					flatten() as described above,
*					writelayer('layername','string') - is to be replaced by 
*												[layerref].write('string')
*					showlayer('layername') - is to be replaced by 
*												[layerref].show()
*					hidelayer('layername') - is to be replaced by 
*												[layerref].hide()
*					tbLayer('newlayername',width) - as 'new Layer(width)'. buggy.
*					setCookie(name,val)
*	[special]
*		layers:
*	//properties:
*					[layerref].clip.top,[layerref].clip.left,[layerref].clip.bottom
*					[layerref].clip.top.bottom (read-only properties, use setClip
*					to manipulate values), [layerref].docTxt (only after it have
*					been written to by one of the native methods of this object)
*	//methods:
*					[layerref].show(),[layerref].hide(),[layerref].write('string')
*					[layerref].moveTo(y,x),[layerref].setClip(x0,y0,x1,y1),
*					[layerref].getDocHeight(),[layerref].getDocWidth(),
*					optional: { [layerref].tbFloat(),[layerref].slideTo(x,y,timeinsec),
*					[layerref].makeDraggable(), [layerref].toLayString(),
*					[layerref].restorePos('name=value,asfrom=toLayString'), }
*					note! when makeDraggable has been invoked, after a dragoperation
*					the layers 'onDragStop' event is called (if it exists) 
*					with the event as argument.
*					I intend to implement [layerref].src(url) one day....
*	
************************************************************************************/
function ToolBox(){
	//properties
	this.NS=(navigator.appName == 'Netscape')?true:false;
	this.MAC=(navigator.platform.indexOf('Mac')!=-1)?true:false;
	this.DOM=(document.getElementById)?true:false;
	if(this.DOM)
	{
		this.NS=false;
	}
	this.images = new Array();
	this.forms = new Array();
	this.applets = new Array();
	this.links = new Array();
	this.layers = new Object();
	this.layers.length=0;
	this.layers.topZ=0;
	this.cookies = new cookieObj();
	//methods
	this.flatten=FlattenDOM;
	this.writelayer=WriteLayer;
	this.showlayer=showlayer;
	this.hidelayer=hidelayer;
	this.tbLayer=tbLayer;
	this.setCookie=SetCookie;
	this.restorePos=restorePos;
	window.__ToolBox=this;
}
function showlayer(lay){
	if(this.layers[lay]){
		if(this.NS)
			this.layers[lay].visibility='show';
		else
			this.layers[lay].style.visibility='visible';
		if(this.DOM)
			this.layers[lay].style.display='block';
	}
}
function hidelayer(lay){
	if(this.layers[lay]){
		if(this.NS)
			this.layers[lay].visibility='hidden';
		else
			this.layers[lay].style.visibility='hidden';
		if(this.DOM)
			this.layers[lay].style.display='none';
	}
}
function hideNS(){this.visibility='hidden';}
function hideIE(){this.style.visibility='hidden';}
function hideDOM(){
			this.style.visibility='hidden';
			this.style.display='none';

		  }

function showNS(){this.visibility='show';}
function showIE(){this.style.visibility='visible';}
function showDOM(){
			this.style.visibility='visible';
			this.style.display='block';
		}

function floatNS(){this.zIndex=(this.parentObj.layers.topZ++)+1;}
function floatIE(){this.style.zIndex=(this.parentObj.layers.topZ++)+1;}
function floatDOM(){this.style.zIndex=(this.parentObj.layers.topZ++)+1;}
function FlattenDOM(){
	var doc=(arguments[0])?arguments[0]:window.document;
	for(var a in doc.images)
		this.images[a]=doc.images[a];
	//for(var a in doc.links)
	//	this.links[a]=doc.links[a];
	for(var a in doc.forms)
		this.forms[a]=doc.forms[a];
	if(this.NS && doc.layers.length!=0){
		for(var a=0;a<doc.layers.length;a++) {
			this.layers[this.layers.length++]=doc.layers[a];
			this.layers[doc.layers[a].name]=this.layers[a];
			this.layers[a].setClip=setClipNS;
			this.layers[a].show=showNS;
			this.layers[a].hide=hideNS;
			this.layers[a].write=writeNS;
			this.layers[a].tbFloat=floatNS;
			this.layers[a].getDocHeight=docHeightNS;
			this.layers[a].getDocWidth=docWidthNS;
			this.layers[a].toExtString=layerToString;
			this.layers[a].makeDraggable=makeDraggableNS;
			this.layers[a].dragStart=dragStartNS;
			this.layers[a].dragStop=dragStopNS;
			this.layers[a].dragNow=dragNowNS;
			this.layers[a].slideTo=slideTo;
			//this.layers[a].toLayString=layerToString;
			this.layers[a].parentObj=this;
			this.layers.topZ=(this.layers.topZ<this.layers[a].zIndex)?this.layers[a].zIndex:this.layers.topZ
			this.flatten(doc.layers[a].document);//recurse it
		}
	} 
	else if(doc.all){
		//for(var a in doc.all){
		for(var a = 0; a < doc.all.length; a++){
			if(doc.all[a].style && doc.all[a].tagName=='DIV'){				
				this.layers[a]=doc.all[a];
				this.layers[doc.all[a].id]=doc.all[a];
				this.layers[a].top=this.layers[a].style.pixelTop;
				this.layers[a].left=this.layers[a].style.pixelLeft;
				this.layers[a].setClip=setClipIE;
				this.layers[a].moveTo=moveToIE;
				this.layers[a].show=showIE;
				this.layers[a].hide=hideIE;
				this.layers[a].write=writeIE;
				this.layers[a].tbFloat=floatIE;
				this.layers[a].getDocHeight=docHeightIE;
				this.layers[a].getDocWidth=docWidthIE;
				this.layers[a].toExtString=layerToString;
				this.layers[a].makeDraggable=makeDraggableIE;
				this.layers[a].dragStart=dragStartIE;
				this.layers[a].dragStop=dragStopIE;
				this.layers[a].dragNow=dragNowIE;
				this.layers[a].clip= new mClip(this.layers[a]);
				this.layers[a].slideTo=slideTo;
				//this.layers[a].toLayString=layerToString;
				this.layers[a].parentObj=this;
				this.layers[this.layers.length++]=this.layers[a];
				this.layers.topZ=(this.layers.topZ<this.layers[a].style.zIndex)?this.layers[a].style.zIndex:this.layers.topZ
			}
		}
		
			
	}
	else if(this.DOM){
				var nodelist=document.getElementsByTagName('DIV');
				for(var a=0;a<nodelist.length;a++)
				{
					this.layers[a]=nodelist.item(a);
					this.layers[nodelist.item(a).getAttribute('ID')]=nodelist.item(a);
					this.layers[a].top=this.layers[a].style.top;
					this.layers[a].left=this.layers[a].style.left;
					
					this.layers[a].setClip=setClipDOM;
					this.layers[a].moveTo=moveToDOM;
					this.layers[a].show=showDOM;
					this.layers[a].hide=hideDOM;
					this.layers[a].write=writeDOM;
					this.layers[a].tbFloat=floatDOM;
					this.layers[a].getDocHeight=docHeightDOM;
					this.layers[a].getDocWidth=docWidthDOM;
					this.layers[a].toExtString=layerToString;
					this.layers[a].makeDraggable=makeDraggableDOM;
					this.layers[a].dragStart=dragStartDOM;
					this.layers[a].dragStop=dragStopDOM;
					this.layers[a].dragNow=dragNowDOM;
					this.layers[a].slideTo=slideTo;
					this.layers[a].clip= new mClip(this.layers[a]);
					
					
					this.layers[a].parentObj=this;
					this.layers[this.layers.length++]=this.layers[a];
				this.layers.topZ=(this.layers.topZ<this.layers[a].style.zIndex)?this.layers[a].style.zIndex:this.layers.topZ
				}
				
		}
}

function docHeightNS(){return this.document.height;}
function docHeightDOM(){return this.height;}
function docHeightIE(){return this.clientHeight;}


function docWidthNS(){return this.document.width;}
function docWidthDOM(){return this.width;}
function docWidthIE(){return this.clientWidth;}	

function mClip(IElay){
	if(!IElay.style.clip){
		this.left = 0;		
		this.top = 0;
		var re = new RegExp('px');
		this.right = parseInt(IElay.style.width.replace(re,''));
		this.bottom = parseInt(IElay.style.height.replace(re,''));
	} 
}
function setClipNS(x0,y0,x1,y1){
	this.clip.left = x0;		
	this.clip.top = y0;		
	this.clip.right = x1;		
	this.clip.bottom = y1;
}
function setClipDOM(x0,y0,x1,y1){

	if(arguments.length==0 && this.clip.bottom){
		x0=this.left;
		y0=this.top;
		x1=x0+this.getDocWidth();
		y1=y0+this.getDocHeight();
	}
	//if(this.MAC)
	//	this.clip = new Object();
	//else
		this.style.clip='rect('+y0+'px '+x1+'px '+y1+'px '+x0+'px)';		
	this.clip.left=x0;		
	this.clip.top=y0;		
	this.clip.right=x1;		
	this.clip.bottom=y1;	

}	
function setClipIE(x0,y0,x1,y1){
	if(arguments.length==0 && this.clip.bottom){
		x0=this.left;
		y0=this.top;
		x1=x0+this.getDocWidth();
		y1=y0+this.getDocHeight();
	}
	//if(this.MAC)
	//	this.clip = new Object();
	//else
		this.style.clip='rect('+y0+'px '+x1+'px '+y1+'px '+x0+'px)';		
	this.clip.left=x0;		
	this.clip.top=y0;		
	this.clip.right=x1;		
	this.clip.bottom=y1;
}		
function moveToIE(y,x){
	this.style.posTop=x;
	this.style.posLeft=y;
	this.top=x;
	this.left=y
}
function moveToDOM(y,x){
	this.style.top=x;
	this.style.left=y;
	this.top=x;
	this.left=y
	
}
function writeNS(str){	
	this.docTxt=str; 
	this.document.writeln('<html><body>'+str+'</html></body>'); 
	this.document.close();
}
function writeIE(str){
	this.docTxt=str;
	this.innerHTML=str;
}
function writeDOM(str){
	this.docTxt=str;
	this.innerHTML=str;
}

function WriteLayer(lay,str){
	if(this.layers[lay]){
		this.layers[lay].docTxt=str;
		if(this.NS){
		   this.layers[lay].document.writeln('<html><body>'+str+'</html></body>');	
		   this.layers[lay].document.close();
		} 
		if(!this.NS) {
		   this.layers[lay].innerHTML=str;
		}
	}
}

function SetCookie (name, value) { 
               var argv = SetCookie.arguments; 
               var argc = SetCookie.arguments.length; 
               var expires = (argc > 2) ? argv[2] : null; 
               var path = (argc > 3) ? argv[3] : null; 
               var domain = (argc > 4) ? argv[4] : null; 
               var secure = (argc > 5) ? argv[5] : false; 
               document.cookie = name + "=" + escape (value) + 
               ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
               ((path == null) ? "" : ("; path=" + path)) + 
               ((domain == null) ? "" : ("; domain=" + domain)) + 
               ((secure == true) ? "; secure" : "");
}

function cookieObj(){
	var obj = new Array()
	var c = document.cookie.split("; ");
	for (var i=0;i < c.length;i++)
        obj[c[i].substring(0,c[i].indexOf("="))] = c[i].substring(c[i].indexOf("=")+1,c[i].length);
	return obj;
}

function layerToString(){
	var laystr = "";
	nm = (this.NS)?'name':'id';
	laystr += "name="+this[nm]+"\b";
	laystr += "top="+this.top+"\b";
	laystr += "left="+this.left+"\b";
	laystr += "ct="+this.clip.top+"\b";
	laystr += "cl="+this.clip.left+"\b";
	laystr += "cr="+this.clip.right+"\b";
	laystr += "cb="+this.clip.bottom+"\b";
	return laystr;
}

function restorePos(str){
	var valpairs = unescape(str).split('\b');
	var props = new Object();
	for(var a in valpairs){
		var b = valpairs[a].split('=');
		props[b[0]]=b[1];
	}
	if(props.name && this.layers[[props.name]]){
		this.layers[[props.name]].moveTo(Math.abs(props.left),Math.abs(props.top));
		if(!isNaN(props.cl) && !isNaN(props.ct) && !isNaN(props.cr) &&!isNaN(props.cb))		
			this.layers[[props.name]].setClip(props.cl,props.ct,props.cr,props.cb);
	}
}

function tbLayer(name,width){
	if(this.NS){
		this.layers[name] = new Layer(width);
		this.layers[this.layers.length++]=this.layers[name];
		this.layers[name].setClip=setClipNS;
		this.layers[name].show=showNS;
		this.layers[name].hide=hideNS;
		this.layers[name].write=writeNS;
		this.layers[name].tbFloat=floatNS;
		this.layers[name].getDocHeight=docHeightNS;
		this.layers[name].getDocWidth=docWidthNS;
		this.layers[name].parentObj=this;
		this.layers[name].toExtString=layerToString;
		this.layers.topZ=(this.layers.topZ<this.layers[name].zIndex)?this.layers[name].zIndex:this.layers.topZ
	}
	if(doc.all)
	{
	
		document.body.innerHTML+="<div id='"+name+"' style=\"width:"+width+"; visibility:hidden; top:0; left;0\"></div>";
		this.layers[name]=document.all[name];
		this.layers[this.layers.length++]=this.layers[name];
		this.layers[name].clip= new mClip(this.layers[name]);
		this.layers[name].setClip=setClipIE;
		this.layers[name].moveTo=moveToIE;
		this.layers[name].show=showIE;
		this.layers[name].hide=hideIE;
		this.layers[name].write=writeIE;
		this.layers[name].tbFloat=floatIE;
		this.layers[name].getDocHeight=docHeightIE;
		this.layers[name].getDocWidth=docWidthIE;
		this.layers[name].parentObj=this;
		this.layers[name].toExtString=layerToString;
		this.layers.topZ=(this.layers.topZ<this.layers[name].style.zIndex)?this.layers[name].style.zIndex:this.layers.topZ
	}
	if(this.DOM)
	{
		
			document.body.innerHTML+="<div id='"+name+"' style=\"width:"+width+"; visibility:hidden; display:none; top:0; left;0\"></div>";
			this.layers[name]=document.all[name];
			this.layers[this.layers.length++]=this.layers[name];
			this.layers[name].clip= new mClip(this.layers[name]);
			this.layers[name].setClip=setClipDOM;
			this.layers[name].moveTo=moveToDOM;
			this.layers[name].show=showDOM;
			this.layers[name].hide=hideDOM;
			this.layers[name].write=writeDOM;
			this.layers[name].tbFloat=floatDOM;
			this.layers[name].getDocHeight=docHeightDOM;
			this.layers[name].getDocWidth=docWidthDOM;
			this.layers[name].parentObj=this;
			this.layers[name].toExtString=layerToString;
			this.layers.topZ=(this.layers.topZ<this.layers[name].style.zIndex)?this.layers[name].style.zIndex:this.layers.topZ
	}
}


function makeDraggableNS(){
	this.captureEvents(Event.MOUSEUP|Event.MOUSEDOWN);
	this.onmousedown=dragStartNS;
	this.onmouseup=dragStopNS;
	this.isDRAG = false;
}

function dragStartNS(e) {
        if (e.which == 1) {
			this.tbFloat();
            window.captureEvents(Event.MOUSEMOVE);
			window.onmousemove=dragNowNS;
            this.DRAGx=e.pageX;
            this.DRAGy=e.pageY;
            this.isDRAG=true;
			window.DRAGobj = this;
            return routeEvent(e);//fall-through!
        } else {
            return true;
        }
}

	
	
function dragStopNS(e) {
        if (e.which == 1) {
            window.releaseEvents(Event.MOUSEMOVE);
            if(window.DRAGobj.onDragStop)
				window.DRAGobj.onDragStop(e);
		    window.onmousemove=null
			window.DRAGobj = null;
            this.isDRAG=false;
            return routeEvent(e);;//fall-through!
        } else {
          return true;
        }
}
function dragNowNS(e) {
        if (window.DRAGobj && window.DRAGobj.isDRAG) {
                window.DRAGobj.moveTo(parseInt(window.DRAGobj.left + e.pageX-window.DRAGobj.DRAGx), parseInt(window.DRAGobj.top + e.pageY - window.DRAGobj.DRAGy));
                window.DRAGobj.DRAGx = e.pageX;
                window.DRAGobj.DRAGy = e.pageY;
                return false;
        } else {
	       return true;
	    }
}


function makeDraggableIE(){
	this.onmousedown=dragStartIE;
	this.onmouseup=dragStopIE;
	this.isDRAG = false;
}

function dragStartIE(e) {
        if (event.button == 1) {
			this.tbFloat();
			document.onmousemove=dragNowIE;
            this.DRAGx=event.clientX;
            this.DRAGy=event.clientY;
            this.isDRAG=true;
			window.DRAGobj = this;
            event.returnValue=false;
        } else {
            event.returnValue=true;
        }
}

function dragStopIE(e) {
        if (event.button == 1) {
		    document.onmousemove=null
            if(window.DRAGobj.onDragStop)
				window.DRAGobj.onDragStop();
			window.DRAGobj = null;
            this.isDRAG=false;
            event.returnValue=false;
        } else {
          event.returnValue=true;
        }
}
function dragNowIE(e) {
        if (window.DRAGobj && window.DRAGobj.isDRAG) {
                window.DRAGobj.moveTo(parseInt(window.DRAGobj.left + event.clientX-window.DRAGobj.DRAGx), parseInt(window.DRAGobj.top + event.clientY - window.DRAGobj.DRAGy));
                window.DRAGobj.DRAGx = event.clientX;
                window.DRAGobj.DRAGy = event.clientY;
                event.returnValue=false;
                event.cancelBubble=true;
        } else {
	       event.returnValue=true;
	    }
}


function makeDraggableDOM(){
	this.onmousedown=dragStartDOM;
	this.onmouseup=dragStopDOM;
	this.isDRAG = false;
}

function dragStartDOM(e) {
        if (event.button == 1) {
			this.tbFloat();
			document.onmousemove=dragNowDOM;
            this.DRAGx=event.clientX;
            this.DRAGy=event.clientY;
            this.isDRAG=true;
			window.DRAGobj = this;
            event.returnValue=false;
        } else {
            event.returnValue=true;
        }
}

function dragStopDOM(e) {
        if (event.button == 1) {
		    document.onmousemove=null
            if(window.DRAGobj.onDragStop)
				window.DRAGobj.onDragStop();
			window.DRAGobj = null;
            this.isDRAG=false;
            event.returnValue=false;
        } else {
          event.returnValue=true;
        }
}
function dragNowDOM(e) {
        if (window.DRAGobj && window.DRAGobj.isDRAG) {
                window.DRAGobj.moveTo(parseInt(window.DRAGobj.left + event.clientX-window.DRAGobj.DRAGx), parseInt(window.DRAGobj.top + event.clientY - window.DRAGobj.DRAGy));
                window.DRAGobj.DRAGx = event.clientX;
                window.DRAGobj.DRAGy = event.clientY;
                event.returnValue=false;
                event.cancelBubble=true;
        } else {
	       event.returnValue=true;
	    }
}


function slideTo(x,y,time){
	if(time <= 0)
		this.moveTo(x,y);
	var xDelta = x-this.left;
	var yDelta = y-this.top;
	var times = 20*time;
	var xStep = xDelta/time;
	var yStep = yDelta/time;
	var curX = this.left;
	var curY = this.top;
	curX += xStep;curY+=yStep
	this.moveTo(curX,curY);
	var t = 50;
	while(time-- > 1){
		setTimeout('window.__ToolBox.layers.'+this.id+'.moveTo('+parseInt(curX+=xStep)+','+parseInt(curY+=yStep)+');',t+=50);
	}
	setTimeout('window.__ToolBox.layers.'+this.id+'.moveTo('+x+','+y+')',t+50);

}
