// Dynamic Layer Object

function DynLayer(id,nestref,frame) {
	if (!DynLayer.set && !frame) DynLayerInit()
	this.frame = frame || self
	this.elm = this.event = this.frame.document.all[id]
	this.css = this.frame.document.all[id].style
	this.doc = document
	this.id = id
	this.nestref = nestref
	this.obj = id + "DynLayer"
	eval(this.obj + "=this")
}
function DynLayerMoveTo(x,y) {
	if (x!=null) {
		this.css.pixelLeft = x
	}
	if (y!=null) {
		this.css.pixelTop = y
	}
}
function DynLayerMoveBy(x,y) {
	this.moveTo(this.pixelLeft+x,this.pixelTop+y)
}
function DynLayerShow() {
	this.css.visibility = "visible"
}
function DynLayerHide() {
	this.css.visibility = "hidden"
}
DynLayer.prototype.moveTo = DynLayerMoveTo
DynLayer.prototype.moveBy = DynLayerMoveBy
DynLayer.prototype.show = DynLayerShow
DynLayer.prototype.hide = DynLayerHide
DynLayerTest = new Function('return true')

// DynLayerInit Function
function DynLayerInit(nestref) {
	if (!DynLayer.set) DynLayer.set = true
	for (var i=0; i<document.all.tags("DIV").length; i++) {
		var divname = document.all.tags("DIV")[i].id
		var index = divname.indexOf("Div")
		if (index > 0) {
			eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
		}
	}
	return true
}

DynLayer.nestRefArray = new Array()
DynLayer.refArray = new Array()
DynLayer.refArray.i = 0
DynLayer.set = false

// Slide Methods
function DynLayerSlideTo(endx,endy,inc,speed,fn) {
	if (endx==null) endx = this.css.pixelLeft
	if (endy==null) endy = this.css.pixelTop
	var distx = endx-this.css.pixelLeft
	var disty = endy-this.css.pixelTop
	this.slideStart(endx,endy,distx,disty,inc,speed,fn)
}
function DynLayerSlideBy(distx,disty,inc,speed,fn) {
	var endx = this.pixelLeft + distx
	var endy = this.pixelTop + disty
	this.slideStart(endx,endy,distx,disty,inc,speed,fn)
}

function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
	if (this.slideActive) return
	if (!inc) inc = 10
	if (!speed) speed = 20
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
	if (num==0) return
	var dx = distx/num
	var dy = disty/num
	if (!fn) {
		delete fn;
		fn = null;
	}
	this.slideActive = true
	this.slide(dx,dy,endx,endy,num,1,speed,fn)
}

function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
	if (!this.slideActive) return
	if (i++ < num) {
		this.moveBy(dx,dy)
		this.onSlide()
		if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
		else this.onSlideEnd()
	}
	else {
		this.slideActive = false
		this.moveTo(endx,endy)
		this.onSlide()
		this.onSlideEnd()
		eval(fn)
	}
}

function DynLayerSlideInit() {}
DynLayer.prototype.slideInit = DynLayerSlideInit
DynLayer.prototype.slideTo = DynLayerSlideTo
DynLayer.prototype.slideBy = DynLayerSlideBy
DynLayer.prototype.slideStart = DynLayerSlideStart
DynLayer.prototype.slide = DynLayerSlide
DynLayer.prototype.onSlide = new Function()
DynLayer.prototype.onSlideEnd = new Function()

// Clip Methods
function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
	if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
}
function DynLayerClipTo(t,r,b,l) {
	if (t==null) t = this.clipValues('t')
	if (r==null) r = this.clipValues('r')
	if (b==null) b = this.clipValues('b')
	if (l==null) l = this.clipValues('l')
	this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
}
function DynLayerClipBy(t,r,b,l) {
	this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
}
function DynLayerClipValues(which) {
	var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
	if (which=="t") return Number(clipv[0])
	if (which=="r") return Number(clipv[1])
	if (which=="b") return Number(clipv[2])
	if (which=="l") return Number(clipv[3])
}
DynLayer.prototype.clipInit = DynLayerClipInit
DynLayer.prototype.clipTo = DynLayerClipTo
DynLayer.prototype.clipBy = DynLayerClipBy
DynLayer.prototype.clipValues = DynLayerClipValues

// Write Method
function DynLayerWrite(html) {
	this.event.innerHTML = html
}
DynLayer.prototype.write = DynLayerWrite

// BrowserCheck Object
function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
}

is = new BrowserCheck()

// CSS Function
function css(id,left,top,width,height,color,vis,z,other) {
	if (id=="START") return '<STYLE TYPE="text/css">\n'
	else if (id=="END") return '</STYLE>'
	var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;'
	if (arguments.length>=4 && width!=null) str += ' width:'+width+'px;'
	if (arguments.length>=5 && height!=null) {
		str += ' height:'+height+'px;'
		if (arguments.length<9 || other.indexOf('clip')==-1) str += ' clip:rect(0px '+width+'px '+height+'px 0px);'
	}
	if (arguments.length>=6 && color!=null) str += ' background-color:'+color+';'
	if (arguments.length>=7 && vis!=null) str += ' visibility:'+vis+';'
	if (arguments.length>=8 && z!=null) str += ' z-index:'+z+';'
	if (arguments.length==9 && other!=null) str += ' '+other
	str += '}\n'
	return str
}
function writeCSS(str,showAlert) {
	str = css('START')+str+css('END')
	document.write(str)
	if (showAlert) alert(str)
}

//globals for HSV/RGB conversion
irpnt=new Array(2,1,0,0,3,2);
igpnt=new Array(3,2,2,1,0,0);
ibpnt=new Array(0,0,3,2,2,1);

//utility object for HSV/RGB conversion
function getrgb(){
	pqvt = new Array(4);
	with(this){
		if (s == 0){
			r = Math.floor(v);
			g = r;
			b = r;
		} else {
			nh = h / 60;
			i = Math.floor(nh);
			f = nh - i;
			pqvt[2] = v;
			pqvt[0] = v * (1 - s)
			pqvt[1] = v * (1 - s * f)
			pqvt[3] = v * (1 - s * (1 - f))
			r = Math.floor(pqvt[irpnt[i]]);
			g = Math.floor(pqvt[igpnt[i]]);
			b = Math.floor(pqvt[ibpnt[i]]);
		}
		rgb = (65536 * r + 256 * g + b).toString(16);
		while (rgb.length < 6)
			rgb = "0" + rgb;
		return "#"+rgb;
	}
}

function rgb2hsv(){
	with (this) {
		v = Math.max(r, Math.max(b, g));
		min = Math.min(r, Math.min(b, g));
		if (v == min) {
			h = 0;
			s = 0;
			return;
		}
		if (r == v) {
			h = (g - b) / (v - min);
		} else if (g == v) {
			h = 2 + (b - r) / (v - min);
		} else {
			h = 4 + (r - g) / (v - min);
		}
		h *= 60;
		while (h < 0) {
			h += 360;
		}
		s = (v - min) / v;
	}
}

function sethsv(h, s, v){
	if (h != null) {
		while (h >= 360)
			h -= 360;
		while (h < 0)
			h += 360;
		this.h = h;
	}
	if (s != null) {
		while (s > 1)
			s = 1;
		while (s < 0)
			s = 0;
		this.s = s;
	}
	if (v != null) {
		while (v > 255)
			v = 255;
		while (v < 0)
			v = 0;
		this.v = v;
	}
}

function setrgb(r, g, b){
	if (r != null) {
		if (r > 255) {
			this.r = 255;
		} else if (r < 0) {
			this.r = 0;
		} else {
			this.r = r;
		}
	}
	if (g != null) {
		if (g > 255) {
			this.g = 255;
		} else if (g < 0) {
			this.g = 0;
		} else {
			this.g = g;
		}
	}
	if (b != null) {
		if (b > 255) {
			this.b = 255;
		} else if (b < 0) {
			this.b = 0;
		} else {
			this.b = b;
		}
	}
}

function colour(r,g,b){
	this.h = 0;	//0-359
	this.s = 0;	//0-1
	this.v = 0;	//0-255
	this.r = 0;	//0-255
	this.g = 0;	//0-255
	this.b = 0;	//0-255
	this.getrgb = getrgb;
	this.rgb2hsv = rgb2hsv;
	this.setrgb = setrgb;
	this.sethsv = sethsv;
	this.setrgb(r, g, b);
	//convert on construction
	this.rgb2hsv();
}


var groupHandle = 0;

function initMouseEvents() {
	document.onmousedown = mouseDown
	document.onmousemove = mouseMove
	document.onmouseup = mouseUp
}
function mouseDown(e) {
	if (event.button!=1) return true
	var x = event.x+document.body.scrollLeft
	var y = event.y+document.body.scrollTop
	return DynMouseDown(x,y)
}
function mouseMove(e) {
	var x = event.x+document.body.scrollLeft
	var y = event.y+document.body.scrollTop
	return DynMouseMove(x,y)
}
function mouseUp(e) {
	var x = event.x+document.body.scrollLeft
	var y = event.y+document.body.scrollTop
	return DynMouseUp(x,y)
}

dragObjects = new Array();
hitObjects = new Array();
var drag = null;

function DynMouseDown(x, y) {
	for (i in dragObjects)  {
		var obj = dragObjects[i];
		if (x >= obj.css.pixelLeft && x <= obj.css.pixelLeft + obj.css.pixelWidth && y >= obj.css.pixelTop && y <= obj.css.pixelTop + obj.css.pixelHeight) {
			obj.dragOffsetX = x - obj.css.pixelLeft
			obj.dragOffsetY = y - obj.css.pixelTop
			drag = obj;
			return false
		}
	}
	return true
}

function DynMouseMove(x,y) {
	if (drag) {
		if (x >= drag.parent.style.pixelLeft &&
		 x <= drag.parent.style.pixelLeft + drag.parent.style.pixelWidth) {
			drag.moveTo(x - drag.dragOffsetX, drag.offsetTop)
			if (drag.writeDelay) {
				var value = (drag.css.pixelLeft - drag.parent.style.pixelLeft) * (drag.high - drag.low) / drag.parent.style.pixelWidth + drag.low;
				setTimeout("Write(" + drag.writeHandle + "," + value + ")", drag.writeDelay);
			}
			return false
		}
	}
	return true
}

function DynMouseUp(x,y) {
	if (drag) {
		if (drag.writeDelay == 0) {
			var value = (drag.css.pixelLeft - drag.parent.style.pixelLeft) * (drag.high - drag.low) / drag.parent.style.pixelWidth + drag.low;
			Write(drag.writeHandle, value);
		}
		delete drag;
		drag = null;
	}
	for (i in hitObjects)  {
		var obj = hitObjects[i];
		if (x >= obj.css.pixelLeft && x <= obj.css.pixelLeft + obj.css.pixelWidth && y >= obj.css.pixelTop && y <= obj.css.pixelTop + obj.css.pixelHeight) {
			Write(obj.writeHandle, !obj.value);
			return false
		}
	}
	return true
}

function makegear(i, teeth, r, teethheight, teethshape ,x, y, color, rotationStart)
{
	var factor = 100;
	return "<v:shape id='G"+i+"' style='visibility:visible;position:absolute;z-index:"+(i+2)+
		";left:"+(x)+
		";top:"+(y)+
		";height:"+r+
		";width:"+r+
		";rotation:"+rotationStart+
	";' fill='true' stroke='true' coordsize='"+r*factor+
	","+r*factor+
	"' fillcolor='"+color+
	"' strokecolor='#000000' path='"+makegearpath(teeth, r*factor/2, teethheight, teethshape)+"'>" +
	"</v:shape>"
}

function makegearpath(teeth, r, teethheight, teethshape){
	// r is outer radius
	// teethheight is height of teeth as fraction of r ; 0.1 to 0.60
	// teethshape is sharpness of teeth ; 0.03 to 0.08, smaller value is sharper
	var theta = 2*Math.PI/teeth
	var theta2 = theta/2
	var r2 = r
	var r1 = r - r * teethheight
	var A = r * teethshape
	var l1 = Math.sqrt((r1*r1+A*A))
	var l2 = Math.sqrt((r2*r2+A*A))
	var alpha = Math.atan(A/r1)
	var beta = Math.atan(A/r2)
	var phi = Math.PI/2-theta2-beta
	var s="m "+(Math.round(l2+l2*Math.cos(phi)))+","+(Math.round(l2+l2*Math.sin(phi)))+"l";
	for (i=0;i<teeth;i++){
		phi=i*theta+Math.PI/2;
		if(i>0)s+=",";
		s+=(Math.round(l2+l2*Math.cos(phi-theta2+beta)))+","+(Math.round(l2+l2*Math.sin(phi-theta2+beta)))+",";
		s+=(Math.round(l2+l1*Math.cos(phi-alpha)))+     ","+(Math.round(l2+l1*Math.sin(phi-alpha)))+",";
		s+=(Math.round(l2+l1*Math.cos(phi+alpha)))+     ","+(Math.round(l2+l1*Math.sin(phi+alpha)))+",";
		s+=(Math.round(l2+l2*Math.cos(phi+theta2-beta)))+","+(Math.round(l2+l2*Math.sin(phi+theta2-beta)));
	}
	s+="x e"
	return(s);
}


var readReq = null;

function DataReady() {
	if (readReq && readReq.readyState == 4 && readReq.status == 200) {
		var returnNode = readReq.responseXML.selectNodes("read/item");
		for (var i = 0; i < returnNode.length; ++i) {
			Changed(returnNode[i]);
		}
		delete readReq;
		readReq = null;
		setTimeout("Read()", 20);
	}
}

function Read() {
	readReq = new ActiveXObject("Microsoft.XMLHTTP");
	readReq.open("POST", host + "a.z");	
	readReq.setRequestHeader("Content-Type", "text/xml"); 		
	readReq.onreadystatechange = DataReady;
	readReq.send(xmlRead.xml);  
}

function OpenReady() {
	if (readReq && readReq.readyState == 4 && readReq.status == 200){
		var returnNode = readReq.responseXML.selectSingleNode("//handle");
		if (returnNode != null) {
			groupHandle = returnNode.text;
			xmlRead.loadXML("<read><handle>" + groupHandle + "</handle></read>");
			delete readReq;
			readReq = null;
			Read();
		}
	}
}

function initServer(tagList)
{
	initMouseEvents()
	readReq = new ActiveXObject("Microsoft.XMLHTTP");
	var vs = "<addGroup>";
	for(var j = 0; j < tagList.length; j++){
		vs += "<item><itemID>" + tagList[j] + "</itemID></item>";
	}
	vs += "</addGroup>";
	xmlRead.loadXML(vs);
	readReq.open("POST", host + "a.z");
	readReq.setRequestHeader("Content-Type", "text/xml"); 		
	readReq.onreadystatechange = OpenReady;
	readReq.send(xmlRead.xml);
}

function setpos(obj, l, t, w, h, dw, dh)
{
	if (dw == null)
		dw = w / 20;
	if (dh == null)
		dh = h / 20;
	if (l != null)
		obj.style.pixelLeft = dl + l + dw;
	if (t != null)
		obj.style.pixelTop = dt + t + dh;
	if (w != null)
		obj.style.pixelWidth = w - 2 * dw;
	if (h != null)
		obj.style.pixelHeight = h - 2 * dh;
}


function Write(writeHandle, value)
{
	xmlWrite.loadXML("<write><handle>" + groupHandle + "</handle><writeItem><itemHandle>" + writeHandle + "</itemHandle><data>" + value + "</data></writeItem></write>");
	var writeReq = new ActiveXObject("Microsoft.XMLHTTP");
	writeReq.open("POST", host + "a.z", false);
	writeReq.setRequestHeader("Content-Type", "text/xml"); 		
	writeReq.send(xmlWrite.xml);
	delete writeReq;
	writeReq = null;
}

var redColor = new colour(255,0,0);
var greenColor = new colour(0,255,0);
var blueColor = new colour(0,0,255);

function SetOff(obj, value)
{
	if (value > 0) {
		value -= 0.05;
		value = Math.max(value, 0);
		redColor.sethsv(null, value);
		obj.color2 = redColor.getrgb();
		if (value > 0) {
			setTimeout("SetOff("+obj.id+","+value+")", 20);
		}
	}
}

function SetOn(obj, value)
{
	if (value < 1) {
		value += 0.05;
		value = Math.min(value, 1);
		redColor.sethsv(null, value);
		obj.color2 = redColor.getrgb();
		if (value < 1) {
			setTimeout("SetOn("+obj.id+","+value+")", 20);
		}
	}
}
