/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	Written by Bugimus 
	Copyright © 1998-2001 Bugimus, all rights reserved.
	You may use this code for your own *personal* use provided you leave this comment block intact.  
	A link back to Bugimus' page would be much appreciated.  
	http://bugimus.com/
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */

// ==============================================================================================================
// BEGIN Layer object code --------------------------------------------------------------------------------------
// 
o = new Array()
var g_numObjects=0      // global number of custom objects on page.
var g_stackBias=500     // biases zindex reordering above all none draggable objects.
var is_mac=(navigator.userAgent.toLowerCase().indexOf("mac")!=-1) // detect macintosh
var g_overlap=0

var Xm=0	// global mouse x coordinate
var Ym=0	// global mouse y coordinate

// allows drag events to identify movingObject information
function findObject(objId) {
	for(var i=0; i<g_numObjects; i++ ) {
		if(o[i].divname==objId && o[i].drag) return o[i].objname 
	}
}
// Reorders vertical stacking of all movingobjects.  The last one clicked upon
// takes top order and the rest maintain their relative stacking.
function reorderZindex(objNum) {
	for(var i=0; i<g_numObjects; i++ ) {
		if(o[i].getzindex() > o[objNum-1].getzindex()) o[i].setzindex(o[i].getzindex()-1)
		o[i].zztop=false
	}
	o[objNum-1].setzindex(g_numObjects+g_stackBias)
	o[objNum-1].zztop=true

	// this loop is quite kludgy.  it checks for linked divs and sets 
	// the *very* next div to the same zindex.
	// this is the way I've coded bugimus.com/dhtml but it may not
	// be very versatile for later.  revisit this for robustness.
	for(var i=0; i<g_numObjects; i++ ) {
		if(o[i].divlink) {
			var t=o[i].getzindex()
			o[i+1].setzindex(t)
			o[i].setzindex(t+1)
			o[i].setzindex(t)
		}
	}

/*
	document.stats.x0.value=objNum
	document.stats.x1.value=o[5].getzindex()
	document.stats.x2.value=o[6].getzindex()
	document.stats.x3.value=o[12].getzindex()
	document.stats.x4.value=o[13].getzindex()
	document.stats.x5.value=o[19].getzindex()
	document.stats.x6.value=o[20].getzindex()
*/
}
function movingObject(objectName,divString,xinit,yinit,speed,dragOn,divLink,dragimgObjectname,dragimgPlacement) {
	g_numObjects++

	this.IE5=this.NN4=this.NN6=false
	if(document.all)this.IE5=true
	else if(document.layers)this.NN4=true
	else if(document.getElementById)this.NN6=true
	
	var netscapestring=""
	var tmp
	var bgn, end
	var i
	var divstring=divString

	i=0
	bgn=0
	end=0
	do { 
		i++
		tmp = ""
		netscapestring += "document."
		end = divstring.indexOf(",")
		if( end > -1 ){
			tmp = divstring.substring(bgn,end)
			divstring = divstring.substring(end+1,divstring.length)
		} else {
			end = divstring.length
			tmp = divstring.substring(bgn,end)
			divstring = null
		}
		netscapestring += tmp
		if(divstring)netscapestring += "."
	} while( divstring )

	if(this.NN4)this.obj=eval(netscapestring)
	if(this.IE5)this.obj=eval("document.all."+tmp+".style")
	if(this.NN6)this.obj=eval("document.getElementById(\""+tmp+"\")"+".style")

	this.hx=xinit
	this.hy=yinit
	this.constrainX=false
	this.constrainY=false
	this.dragarea=false
	this.drag = dragOn
	this.dragimgobjectname = dragimgObjectname
	this.dragimgplacement = dragimgPlacement
	this.nested = (i>1)?true:false
	this.divname = tmp
	this.objname = objectName
	this.objnum = g_numObjects
	this.timer=null
	this.speed=speed
	this.active=false
	this.hoverflag=false
	this.hoverstep=generate(1,360)  // random num to keep each object's movement unique.
	this.minimized=true
	this.onscreen=false
	this.zztop=false // top of the draggable zindex stacking order
	this.decay = 5   // basically the speed of the chasing object, the higher the slower

	this.divlink = divLink  // this allows drag logic to link another div to this one.
	if(this.NN4 && divLink) this.divlinkobj=eval("document."+divLink)
	if(this.IE5 && divLink) this.divlinkobj=eval("document.all."+divLink+".style")		
	if(this.NN6 && divLink) this.divlinkobj=eval("document.getElementById(\""+divLink+"\")"+".style")

	this.chase = chaseTo
	this.show = showDiv
	this.hide = hideDiv
	this.clip = clipDiv
	this.setdragarea = setDragarea
	this.setzindex = setZindex
	this.getzindex = getZindex
	this.getx = getX
	this.gety = getY
	this.setx = setX
	this.sety = setY
	this.getwidth = getWidth
	this.getheight = getHeight
	this.warp = warpTo
	this.hover = Hover
	this.clearall = clearAll
	this.showtext = showText
	
	this.warp(this.hx,this.hy)
	this.setzindex(g_numObjects)
}
function clearAll() {
	clearTimeout(this.timer)
}
function clipDiv(top,right,bottom,left){
	if(this.IE5) {
		this.obj.clip="rect("+top+","+right+","+bottom+","+left+")"
	} else {
		this.obj.clip.top=top
		this.obj.clip.right=right
		this.obj.clip.bottom=bottom
		this.obj.clip.left=left
	}
}
function setDragarea(t,r,b,l) {
	this.t=t
	this.r=r
	this.b=b
	this.l=l
	this.dragarea=true
}
function getX() {
	if(this.IE5)return parseInt(this.obj.pixelLeft)
	else if(this.NN4)return parseInt(this.obj.left)
	else if(this.NN6)return parseInt(this.obj.left)
}
function getY() {
	if(this.IE5)return parseInt(this.obj.pixelTop)
	else if(this.NN4)return parseInt(this.obj.top)
	else if(this.NN6)return parseInt(this.obj.top)
}
function setX(x) {
	if(!this.constrainX) {
		if(this.IE5)this.obj.pixelLeft=x
		else if(this.NN4)this.obj.left=x
		else if(this.NN6)this.obj.left=x + "px"
	}
}
function setY(y) {
	if(!this.constrainY) {
		if(this.IE5)this.obj.pixelTop=y
		else if(this.NN4)this.obj.top=y
		else if(this.NN6)this.obj.top=y + "px"
	}
}
function getWidth() {
	if(this.IE5)return eval("document.all."+this.divname+".clientWidth")
	else if(this.NN4)return this.obj.clip.width
	else if(this.NN6) {
		if(this.obj.width)
		    return(parseInt(this.obj.width))
		else
		    return(document.getElementById(this.divname).offsetWidth)
	}
}
function getHeight() {
	if(this.IE5)return eval("document.all."+this.divname+".clientHeight")
	else if(this.NN4)return this.obj.clip.height
	else if(this.NN6) {
		if(this.obj.height)
		    return(parseInt(this.obj.height))
		else
		    return(document.getElementById(this.divname).offsetHeight)
	}
}
function showDiv() {
	this.obj.visibility="visible"
}
function hideDiv() {
	this.obj.visibility="hidden"
}
function setZindex( z ) {
//	if(this.divlink)this.divlinkobj.zIndex = z
//	this.obj.zIndex = z+1
	this.obj.zIndex = z
}
function getZindex() {
	return this.obj.zIndex
}
function warpTo(x,y) {
	if(this.active)clearTimeout(this.timer)
	if(this.IE5) {
		this.obj.pixelLeft=x
		this.obj.pixelTop=y
	} else if(this.NN4) {
		this.obj.moveTo(x,y)
	} else if(this.NN6) {
		this.obj.left = x + "px"
		this.obj.top = y + "px"
	}
}
function chaseTo( x, y ) {
	if(this.active)clearTimeout(this.timer)
	dx=x-this.getx()
	dy=this.gety()-y
	distance=Math.round(Math.sqrt(dx*dx+dy*dy))
	if(dx==0) angle=Math.PI*((dy>0)?.5:1.5)
	else angle=Math.atan(dy/dx)+(Math.PI*((dx<0)?1:(dy<0)?2:0))
	xstep=(dx>1)?Math.ceil(distance*Math.cos(angle)/this.decay):Math.floor(distance*Math.cos(angle)/this.decay)
	ystep=(dy<1)?Math.ceil(-distance*Math.sin(angle)/this.decay):Math.floor(-distance*Math.sin(angle)/this.decay)
	this.setx(this.getx()+xstep)
	this.sety(this.gety()+ystep)
	if( distance>1 ) {
		this.active=true
		this.timer=setTimeout(this.objname+".chase("+x+","+y+")", this.speed)
	} else {
		clearTimeout(this.timer)
		this.hoverflag?this.hover(x,y):this.warp(x,y)
		this.active=false
	}
}
function Hover( x, y ) {
	amp=2
	if(this.active)clearTimeout(this.timer)
	this.active=true;
	if(this.hoverstep>360)this.hoverstep=1; else this.hoverstep++;
	this.setx( x + amp*Math.sin((20*Math.cos(this.hoverstep/30))) )
	this.sety( y + amp*Math.cos((30*Math.sin(this.hoverstep/40))) )
	this.timer=setTimeout(this.objname+".hover("+x+","+y+")", this.speed)
}
function showText(content) {
	if(this.NN4) {
		this.obj.document.writeln(content)
		this.obj.document.close()
	} else if(this.NN6) {
		eval("document.getElementById(\""+this.divname+"\")"+".innerHTML = content")
	} else if(this.IE5) {
		content=content+"\n"	// You have to have this new line character for a bug on Mac IE.
		eval("document.all."+this.divname+".innerHTML = content")
	}
}
// END Layer object code ----------------------------------------------------------------------------------------

// Random generator code borrowed from http://webreference.com/js/tips/990925.html
function generate(x, y) {
  var range = y - x + 1;
  return Math.floor(Math.random() * range) + x;
}
