var topMid = 0
var leftMid = 0
var theTop = 0
var theLeft = 0
var zIndy = 97

function createDragableLayer(layerContent,leftPos,topPos,layerWidth,layerHeight,layerBG) {
	if( document.layers ) {
		document.write( '<layer left="'+leftPos+'" top="'+topPos+'" width="'+layerWidth+'" '+(layerHeight?('height="'+layerHeight+'" '):'')+(layerBG?('bgcolor="'+layerBG+'" '):'')+'onmouseover="this.captureEvents(Event.MOUSEDOWN);this.onmousedown=dragIsDown;">'+layerContent+'</layer>' );
	} else {
		document.write( '<div style="position:absolute;left:'+leftPos+'px;top:'+topPos+'px;width:'+layerWidth+'px;'+(layerHeight?('height:'+layerHeight+'px;'):'')+(layerBG?('background-color:'+layerBG+';'):'')+'" onmouseover="this.onmousedown=dragIsDown;" ondragstart="return false;" onselectstart="return false;">'+layerContent+'</div>' );
	}
}

function dragMousePos(e) {
	//get the position of the mouse
	if( !e ) { e = window.event; } if( !e || ( typeof( e.pageX ) != 'number' && typeof( e.clientX ) != 'number' ) ) { return [0,0]; }
	if( typeof( e.pageX ) == 'number' ) { var xcoord = e.pageX; var ycoord = e.pageY; } else {
		var xcoord = e.clientX; var ycoord = e.clientY;
		if( !( ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) || ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) || window.navigator.vendor == 'KDE' ) ) {
			if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) ) {
				xcoord += document.documentElement.scrollLeft; ycoord += document.documentElement.scrollTop;
			} else if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) ) {
				xcoord += document.body.scrollLeft; ycoord += document.body.scrollTop; } } }
	return [xcoord,ycoord];
}

function dragIsDown(e) {
	toggleZIndex('movingdiv');
	//make note of starting positions and detect mouse movements
	if( ( e && ( e.which > 1 || e.button > 1 ) ) || ( window.event && ( window.event.which > 1 || window.event.button > 1 ) ) ) { return false; }
	window.msStartCoord = dragMousePos(e); window.lyStartCoord = this.style?[parseInt(this.style.left),parseInt(this.style.top)]:[parseInt(this.left),parseInt(this.top)];
	if( document.captureEvents && Event.MOUSEMOVE ) { document.captureEvents(Event.MOUSEMOVE); document.captureEvents(Event.MOUSEUP); }
	window.storeMOUSEMOVE = document.onmousemove; window.storeMOUSEUP = document.onmouseup; window.storeLayer = this;
	document.onmousemove = dragIsMove; document.onmouseup = dragIsMove; return false;
}

function dragIsDownTwo(e) {
	toggleZIndex('movingdivtwo');
	//make note of starting positions and detect mouse movements
	if( ( e && ( e.which > 1 || e.button > 1 ) ) || ( window.event && ( window.event.which > 1 || window.event.button > 1 ) ) ) { return false; }
	window.msStartCoord = dragMousePos(e); window.lyStartCoord = this.style?[parseInt(this.style.left),parseInt(this.style.top)]:[parseInt(this.left),parseInt(this.top)];
	if( document.captureEvents && Event.MOUSEMOVE ) { document.captureEvents(Event.MOUSEMOVE); document.captureEvents(Event.MOUSEUP); }
	window.storeMOUSEMOVE = document.onmousemove; window.storeMOUSEUP = document.onmouseup; window.storeLayer = this;
	document.onmousemove = dragIsMove; document.onmouseup = dragIsMove; return false;
}

function dragIsMove(e) {
	//move the layer to its newest position
	var msMvCo = dragMousePos(e); if( !e ) { e = window.event ? window.event : ( new Object() ); }
	var newX = window.lyStartCoord[0] + ( msMvCo[0] - window.msStartCoord[0] );
	var newY = window.lyStartCoord[1] + ( msMvCo[1] - window.msStartCoord[1] );
	//reset the mouse monitoring as before - delay needed by Gecko to stop jerky response (hence two functions instead of one)
	//as long as the Gecko user does not release one layer then click on another within 1ms (!) this will cause no problems
	if( e.type && e.type.toLowerCase() == 'mouseup' ) { document.onmousemove = storeMOUSEMOVE; document.onmouseup = window.storeMOUSEUP; }
	if( navigator.product == 'Gecko' ) { window.setTimeout('dragIsMove2('+newX+','+newY+');',1); } else { dragIsMove2(newX,newY); }
}

function dragIsMove2(x,y) { var oPix = ( document.childNodes ? 'px' : 0 ), theLayer = ( window.storeLayer.style ? window.storeLayer.style : window.storeLayer ); theLayer.left = x + oPix; theLayer.top = y + oPix; }

function toggleBox(szDivID,iState,theWidth,theHeight) { // 1 visible, 0 hidden
	toggleZIndex(szDivID);
    var obj = document.getElementById(szDivID);
    obj.style.visibility = iState ? "visible" : "hidden";
	setAlign(szDivID,theWidth,theHeight);
}

function setSize() {
	isIE = (document.all)? true:false
	topMid = (isIE)? document.body.offsetHeight/2 : window.innerHeight/2
	leftMid = (isIE)? (document.body.offsetWidth-20)/2 : (window.innerWidth)/2
}

function setAlign(alignID,thisWidth,thisHeight) {
	theTop = topMid-(thisHeight/2);
	theLeft = leftMid-(thisWidth/2);
	
	if (theTop <= 0) theTop = 0;
	if (theLeft <= 0) theLeft = 0;
	
    placeObject(alignID);
}

function setNoCenter(alignID,thisWidth,thisHeight) {
	newTop = topMid-(300/2);
	newLeft = leftMid-(600/2);
	
	if (newTop <= 0) newTop = 0;
	if (newLeft <= 0) newLeft = 0;
	
	theTop = newTop+thisHeight;
	theLeft = newLeft+thisWidth;
	
	placeObject(alignID);
}

function placeObject(objactID) {
	var obj = document.getElementById(objactID);
	obj.style.top = theTop;
	obj.style.left = theLeft;
}

function toggleZIndex(placeDivID) {
	zIndy = zIndy + 1

    var obj = document.getElementById(placeDivID);
	obj.style.zIndex = zIndy;
}