bw=new checkBrowser();
movemarker=false;
sizemarker=false;
var win_scroll=false;
var offsetX,offsetY,baseW,baseH
//	Set up parameters
topbar=18; // height of top bar
basebar=16; // height of the base bar
minmargin=6; // width of right margin without scrollers
maxmargin=15; // width of right margin with scrollers
panelcolor='#DDDDDD' // the surround colour of the panel
activecolour='#FFFFFF' // the colour the last dragged/clicked window becomes
baseCol='#EEEEEE' // the initial colour of the content area
borderCol='#888888' // the colour of the border

currentpanel=-1;
objs=new Array()
bases=new Array()
contents=new Array()
upbuttons=new Array()
downbuttons=new Array()
maxbuttons=new Array()
resizebuttons=new Array()
win=new Array()

function windowz(t,x,y,z,w,h,ref,txt) {
	//	Store values from definitions for use later
	this.x=x;this.y=y;this.z=z;
	this.w=w;this.h=h;
	this.prew=w;this.preh=h;
	this.prex=x;this.prey=y;
	this.ref=ref;this.txt=txt;
	this.status="normal";
	this.marginright=minmargin;
	//	Write style sheet for windows
	bw.ns4?extra='':extra='border:1px solid '+borderCol+';';
	wincss+='#win'+ref+' {position:absolute;left:0px;top:0px;overflow:hidden;'+extra+';visibility:hidden;}'
	wincss+='#win'+ref+'base {position:absolute;left:2px;top:'+(topbar+1)+'px;overflow:hidden;'+extra+'}'
	wincss+='#win'+ref+'content {position:absolute;left:0px;top:0px;width:95%;}'
	wincss+='#up'+ref+' {position:absolute;left:0px;top:0px;visibility:hidden;}'
	wincss+='#down'+ref+' {position:absolute;left:0px;top:0px;visibility:hidden;}'
	wincss+='#resize'+ref+' {position:absolute;left:0px;top:0px;visibility:hidden;}'
	wincss+='#maxi'+ref+' {position:absolute;left:0px;top:0px;visibility:hidden;}'
	//	Write <div>s for windows
	outxt+='<div id="win'+ref+'">'
	outxt+='<table cellpadding="0" cellspacing="0" border="0"><tr><td><img src="images/win_g_spacer.gif" width="3" height="'+topbar+'"></td><td valign="center" class="debug_header"><img src="images/win_survey_icon.gif" align=absmiddle> '+t+'</td></tr></table>'
	outxt+='<div id="win'+ref+'base">'
	outxt+='<img src="images/win_g_spacer.gif" width="1" height="1" border="0">'
	bw.ns4?outxt+='<div id="win'+ref+'content" class="debug_content">'+rewrite(w,txt)+'</div>':outxt+='<div id="win'+ref+'content" class="debug_content">'+txt+'</div>';
	outxt+='</div>'
	outxt+='<div id="up'+ref+'"><a href="#" onmouseover="win_scroll=true;flow('+ref+',1)" onmouseout="win_scroll=false"><img src="images/win_b_up.gif" width="9" height="9" alt="" border="0"></a></div>'
	outxt+='<div id="down'+ref+'"><a href="#" onmouseover="win_scroll=true;flow('+ref+',-1)" onmouseout="win_scroll=false"><img src="images/win_b_down.gif" width="9" height="9" alt="" border="0"></a></div>'
	outxt+='<div id="resize'+ref+'"><img src="images/win_b_resize.gif" width="7" height="7"></div>'
	outxt+='<div id="maxi'+ref+'"><table cellpadding="0" cellspacing="0" border="0"><tr>'
	outxt+='<td><a href="javascript:minimise('+ref+')" onfocus="if (this.blur) this.blur()"><img src="images/win_b_min.gif" width="7" height="7" alt="" border="0"></a></td>'
	outxt+='<td><img src="images/win_g_spacer.gif" width="3" height="1"></td>'
	outxt+='<td><a href="javascript:maximise('+ref+')" onfocus="if (this.blur) this.blur()"><img src="images/win_b_max.gif" width="7" height="7" alt="" border="0"></a></td>'
	outxt+='</tr></table></div>'
	outxt+='</div>'
	return this;
}

function setupobjs() {
	for (i=0;i<win.length;i++) {
		//	Create objs of all the window components
		objs[i]=new objLayer("win"+win[i].ref)
		bases[i]=new objLayer("win"+win[i].ref+"base","win"+win[i].ref)
		contents[i]=new objLayer("win"+win[i].ref+"content","win"+win[i].ref+"base","win"+win[i].ref)
		upbuttons[i]=new objLayer("up"+win[i].ref,"win"+win[i].ref)
		maxbuttons[i]=new objLayer("maxi"+win[i].ref,"win"+win[i].ref)
		downbuttons[i]=new objLayer("down"+win[i].ref,"win"+win[i].ref)
		resizebuttons[i]=new objLayer("resize"+win[i].ref,"win"+win[i].ref)

		//	Set the surround colours
		objs[i].changeBack(panelcolor)
		bases[i].changeBack(baseCol)

		//	Position the window
		objs[i].moveTo(win[i].x,win[i].y)

		//	Set the size of the window
		objs[i].clipTo(0,win[i].w,win[i].h,0)
		objs[i].setSize(win[i].w,win[i].h)

		//	Set the size of the content area
		bases[i].clipTo(0,win[i].w-win[i].marginright,win[i].h-(topbar+basebar+2),0)
		bases[i].setSize(win[i].w-win[i].marginright,win[i].h-(topbar+basebar+2))
		//	Position the content
		contents[i].moveTo(5,5)
		//	Test whether scrollbars are required
		checkifscroll(i)
		// make them visible
		objs[i].show();
		objs[i].changeAlpha(70);
	}
	//	Start the event tracking
	setTimeout("debug_init()",50)
}

function debug_init() {
	if (bw.ns4) {
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	}
	if (bw.ff) {
		document.addEventListener("onmousedown", engage, false); 
		document.addEventListener("onmousemove", dragIt, false); 
		document.addEventListener("onmouseup", release, false); 
	}
	document.onmousedown = engage;
	document.onmousemove = dragIt;
	document.onmouseup = release;
	window.onscroll = doScroll;
}

function doScroll(evt) {
	var topMargin = 5;
	var leftMargin = 5;
	var buttomMargin = 5;
	var rightMargin = 5;

	for (i=0;i<objs.length;i++) {
		if(objs[i].x + objs[i].w > document.body.scrollLeft + document.body.clientWidth ) {
			objs[i].moveTo(document.body.scrollLeft + document.body.clientWidth - objs[i].w - rightMargin, objs[i].y);
		}
		if( objs[i].x < document.body.scrollLeft ) {
			objs[i].moveTo(document.body.scrollLeft + leftMargin , objs[i].y);
		}

		if(objs[i].y + objs[i].h > document.body.scrollTop + document.body.clientHeight ) {
			objs[i].moveTo(objs[i].x , document.body.scrollTop + document.body.clientHeight - objs[i].h - buttomMargin);
		}
		if( objs[i].y < document.body.scrollTop ) {
			objs[i].moveTo(objs[i].x , document.body.scrollTop + topMargin);
		}
	}
}

function checkmarker(e) {
	//	Get the position of the mouse
	if (bw.ns4 || bw.ff) {
		locx=e.pageX;
		locy=e.pageY;
	} else {
		locx=event.clientX;
		locy=event.clientY;
	}
	locx += document.body.scrollLeft;
	locy += document.body.scrollTop;

	//	Is the user grabbing the top bar? And which panel is it?
	for (i=0;i<objs.length;i++) {
		tempx = objs[i].x;
		tempy = objs[i].y;
		if ((locx > tempx ) && (locx < (objs[i].w)+(tempx)) && (locy>tempy) && (locy<(topbar)+(tempy))) {
			movemarker=true;
			currentpanel=i;
		}
	}
	
	//	Is the user grabbing the bottom corner? And which panel is it?
	for (i=0;i<objs.length;i++) {
		tempx = objs[i].x;
		tempy = objs[i].y;
		if ((locx<(objs[i].w)+(tempx)) && (locx>(objs[i].w)+(tempx)-10) && (locy<(objs[i].h)+(tempy)) && (locy>(objs[i].h)+(tempy)-10)) {
			sizemarker=true;
			currentpanel=i;
		}
	}
}

function dragIt(evt) {
	//	Set how far the user has dragged from the original position
	//alert("drag");
	if (bw.ns4 || bw.ff) {
		xamt = evt.pageX - offsetX
		yamt = evt.pageY - offsetY
	} else {
		xamt=window.event.clientX - offsetX
		yamt=window.event.clientY - offsetY
	}
	xamt += document.body.scrollLeft;
	yamt += document.body.scrollTop;

	//alert(1);
	//	If the user is moving the window, simply move the parent object
	if (movemarker) {
		objs[currentpanel].moveTo(xamt, yamt);
		return false;
	//	If the user is resizing however..
	} else if (sizemarker) {
		xamt -= document.body.scrollLeft;
		yamt -= document.body.scrollTop;

		//	Make sure the window doesn't go too small (40,40)
		if (xamt<(baseW-40)*-1) {xamt=(baseW-40)*-1}
		if (yamt<(baseH-40)*-1) {yamt=(baseH-40)*-1}
		//	Call the adjusting function
		adjustSize(currentpanel,baseW+xamt,baseH+yamt);
		//	Allow the mouse event to continue
		return false;
	}
}

function engage(evt) {
	//	Check whether the user is clicking on a window area (top or resize area)
	checkmarker(evt);
	//	If clicking on the top bar..
	if (movemarker) {
			objs[currentpanel].changeAlpha(40);
		//	Set the offset variables to the location of the click (within the object under NS)
		if (bw.ns4 || bw.ff) {
			offsetX = evt.pageX - objs[currentpanel].x;
			offsetY = evt.pageY - objs[currentpanel].y;
		} else {
			offsetX = window.event.offsetX;
			offsetY = window.event.offsetY;
		}
	//	If clicking on the resize area..
	} else if (sizemarker) {
		//	Set the offset variables to the location of the click
		if (bw.ns4 || bw.ff) {
			offsetX = evt.pageX;
			offsetY = evt.pageY;
		} else {
			offsetX = window.event.clientX;
			offsetY = window.event.clientY;
		}


		//	Set up panel ready for resizing
		setforsize(currentpanel)
		//	Update the status
		win[currentpanel].status="normal";
	}
	if (movemarker || sizemarker) {
		//	Set the clicked windows z-order to the highest to bring it to the front
		setZ(currentpanel)
	}
}

function release(evt) {
	//alert(objs[0].x);
	//alert(objs[0].y);

	//	Reset the variables
	movemarker = false;
	if (currentpanel!=-1) objs[currentpanel].changeAlpha(70);
	//	If the user resized, do we need scrollbars?
	if (sizemarker) {
		checkifscroll(currentpanel)
	}
	sizemarker = false;
}

function checkifscroll(n) {
	//	Find the height available to the content (window height minus top and bottom bars)
	windowsize=win[n].h-(topbar+basebar+2+5);
	//	Update the stored value for the current height of the content
	contents[n].updateDim();
	//	Store the current height of the content
	contentsize=contents[n].h;
	//	Is the content longer than the available height?
	if (contentsize>windowsize) {
		//	Create a margin to fit the scrollbar in
		win[n].marginright=maxmargin;
		//	Adjust the amount of room available to the content to accommodate scrollbar
		bases[n].clipTo(0,win[n].w-win[n].marginright,win[n].h-(topbar+basebar+2),0)
		bases[n].setSize(win[n].w-win[n].marginright,win[n].h-(topbar+basebar+2))
		//	Position the scrollbar buttons within the newly sized window
		upbuttons[n].moveTo(win[n].w-12,(topbar+1))
		downbuttons[n].moveTo(win[n].w-12,win[n].h-(basebar+10))
		//	Show the scrollbar buttons
		upbuttons[n].show();
		downbuttons[n].show();
	} else {
		//	Set the margin to the size without scrollbar
		win[n].marginright=minmargin;
	}
	//	Position the maximise button and show it
	maxbuttons[n].moveTo(win[n].w-20,2);
	maxbuttons[n].show();
	// Position resize indicator
	resizebuttons[n].moveTo(win[n].w-10,win[n].h-10);
	resizebuttons[n].show();
	if (bw.ns4) {contents[n].writeIt(rewrite((bases[n].w)-10,win[n].txt))}
}

function setZ(n) {
	//Set the z and highlight the current panel
	for (i=0;i<objs.length;i++) {
		if (i==n) {
			objs[i].setZIndex(22);
			win[i].z=22;
			bases[i].changeBack(activecolour)
		} else {
			win[i].z+=-1;
			objs[i].setZIndex(win[i].z);
			bases[i].changeBack(baseCol)
		}
	}
}

function setforsize(n) {
	//	Store the current width and height of the window
	baseW=objs[n].w;
	baseH=objs[n].h;
	//	Hide the scrollbar buttons
	upbuttons[n].hide();
	downbuttons[n].hide();
	//	Hide the scrollbar area whilst resizing
	win[n].marginright=minmargin;
	//	Reset the content back to top of scroll
	contents[n].moveTo(5,5)
	//	Hide the maximise button
	maxbuttons[n].hide();
	// Hide the resize button
	resizebuttons[n].hide();
}

function adjustSize(n,w,h) {
	//	Set the parent objects dimensions to the new dimensions
	objs[n].clipTo(0,w,h,0)
	objs[n].setSize(w,h)
	//	Set the inner objects dimensions based on the parent object
	bases[n].clipTo(0,w-win[n].marginright,h-(topbar+basebar+2),0)
	bases[n].setSize(w-win[n].marginright,h-(topbar+basebar+2))
	//	Store the new dimensions in the win object
	win[n].w=w;
	win[n].h=h;
}

function rewrite(w,t) {
	//	Create a table for NS content to flow correctly
	txt='<table cellpadding="0" cellspacing="0" border="0" width="'+w+'"><tr><td>'
	txt+=t;
	txt+='</td></tr></table>'
	return txt
}

function flow(n,d) {
	//	Can we scroll up?
	if (contents[n].y>=4 && d==1) {d=0}
	//	Find the height available to the content (window height minus top and bottom bars and margins)
	windowsize=win[n].h-(topbar+basebar+2+5);
	//	Update the stored value for the current height of the content
	contents[n].updateDim();
	//	Store the current height of the content
	contentsize=contents[n].h;
	//	Find the max amount of scroll
	scrollextent=(contentsize-windowsize)*-1;
	//	Can we scroll down?
	if (contents[n].y<=scrollextent && d==-1) {d=0}
	//	Move the contents and repeat on a timeout
	contents[n].moveBy(0,d)
	if (win_scroll) {setTimeout("flow("+n+","+d+")",10)}
}

function maximise(n) {
	//	Set the panel to the front
	setZ(n);
	//	Prepare the panel for resizing (hide scrollbar/buttons)
	setforsize(n);
	moveTmpLeft = document.body.scrollLeft + document.body.clientWidth - 180 - 5;
	moveTmpTop = document.body.scrollTop + 5;
	moveTmpWidth = 180;
	moveTmpHeight = document.body.clientHeight - 10;

	//	Are we maximising or return back to size?
	if (win[n].status=="normal") {
		//	Store the return sizes in case req'd later
		win[n].prex=objs[n].x;win[n].prey=objs[n].y;
		win[n].prew=win[n].w;win[n].preh=win[n].h;
		//	Update the status
		win[n].status="maxed"
		//	Run the loop
		maxloop(n,moveTmpLeft,moveTmpTop,moveTmpWidth,moveTmpHeight);
	} else if (win[n].status=="maxed") {
		//	Update the status
		win[n].status="normal"
		//	Run the loop
		maxloop(n,win[n].prex,win[n].prey,win[n].prew,win[n].preh)
	} else if (win[n].status="minied") {
		//	Update the status
		win[n].status="maxed"
		//	Run the loop
		maxloop(n,moveTmpLeft,moveTmpTop,moveTmpWidth,moveTmpHeight);
	}
}

function minimise(n) {
	//	Set the panel to the front
	setZ(n);
	//	Prepare the panel for resizing (hide scrollbar/buttons)
	setforsize(n);

	moveTmpLeft = document.body.scrollLeft + document.body.clientWidth - 150 - 10;
	moveTmpTop = document.body.scrollTop + 10;

	//	Are we minimising or return back to size?
	if (win[n].status=="normal") {
		//	Store the return sizes in case req'd later
		win[n].prex=objs[n].x;win[n].prey=objs[n].y;
		win[n].prew=win[n].w;win[n].preh=win[n].h;
		//	Update the status
		win[n].status="minied"
		//	Run the loop
		maxloop(n,moveTmpLeft,moveTmpTop,150,(topbar+basebar+3))
	} else if (win[n].status=="minied") {
		//	Update the status
		win[n].status="normal"
		//	Run the loop
		maxloop(n,win[n].prex,win[n].prey,win[n].prew,win[n].preh)
	} else if (win[n].status=="maxed") {
		//	Update the status
		win[n].status="minied"
		//	Run the loop
		maxloop(n,moveTmpLeft,moveTmpTop,150,(topbar+basebar+3))
	}
}

function maxloop(n,destx,desty,destw,desth) {
	//	Determine distance to travel to corner and apply damper
	xdist=(objs[n].x-destx)/4
	ydist=(objs[n].y-desty)/4
	//	Determine size to achieve and apply damper
	wdelta=(win[n].w-destw)/4
	hdelta=(win[n].h-desth)/4
	//	Move panel if still some distance away
	if ((xdist*xdist)>2) {
		objs[n].moveBy((-1*xdist),0)
	}
	if ((ydist*ydist)>2) {
		objs[n].moveBy(0,(-1*ydist))
	}
	//	Resize panel if still short of target size
	if ((wdelta*wdelta)>2) {
		newW=win[n].w+(-1*wdelta);
		if (newW<0) {newW=0};
		adjustSize(n,newW,win[n].h)
	}
	if ((hdelta*hdelta)>2) {
		newH=win[n].h+(-1*hdelta);
		if (newH<0) {newH=0};
		adjustSize(n,win[n].w,newH)
	}
	//	More moving/resizing req'd?
	if (((xdist*xdist)>4 || (ydist*ydist)>4) || ((wdelta*wdelta)>2 || (hdelta*hdelta)>2)) {
		setTimeout("maxloop("+n+","+destx+","+desty+","+destw+","+desth+")",10)
	} else {
		//	Once resizing stops - set final size correctly and place in final position
		adjustSize(n,destw,desth)
		objs[n].moveTo(destx,desty)
		//	do we need scrollbars/buttons?
		checkifscroll(n)
	}
}
