
function createGenerativeLayer(layerContent,leftPos,topPos,layerWidth,layerHeight,layerBG) {

	if( document.layers ) {

		alert('layers');

		document.write( '<layer left="'+leftPos+'" top="'+topPos+'" width="'+layerWidth+'" '+(layerHeight?('height="'+layerHeight+'" '):'')

							+(layerBG?('bgcolor="'+layerBG+'" '):'')

//							+' onclick="addDragableLayer(newContent(),100,100,100,100,null)" '

							+'omouseover="this.captureEvents(Event.MOUSEDOWN);this.omousedown=dragIsDown;">'


							+layerContent

							+'</layer>' );

	} else {

		alert('divs');

		document.write( '<div style="position:absolute;left:'+leftPos+'px;top:'+topPos+'px;width:'+layerWidth+'px;'+(layerHeight?('height:'+layerHeight+'px;'):'')

							+(layerBG?('background-color:'+layerBG+';'):'')

//							+' onclick="addDragableLayer(newContent(),100,100,100,100,null)" '

//							+'" omouseover="this.onmousedown=dragIsDown;" odragstart="return false;" oselectstart="return false;"'

							+">"

							+layerContent

							+'</div>' );

	}

}

function createDragableLayers(limit) {

	for (i=0;i<limit;i++){

		document.write(createDragableLayer('object '+i,640,10+i*100,100,100,'red'));

	}

}

function createDragableLayer(layerContent,leftPos,topPos,layerWidth,layerHeight,layerBG) {

	if( document.layers ) {

		return '<layer left="'+leftPos+'" top="'+topPos+'" width="'+layerWidth+'" '+(layerHeight?('height="'+layerHeight+'" '):'')

							+(layerBG?('bgcolor="'+layerBG+'" '):'')

							+'onmouseover="this.captureEvents(Event.MOUSEDOWN);this.onmousedown=dragIsDown;">'

							+layerContent

							+'</layer>' ;

	} else {

		return '<div style="position:absolute;left:'+leftPos+'px;top:'+topPos+'px;width:'+layerWidth+'px;'+(layerHeight?('height:'+layerHeight+'px;'):'')

							+(layerBG?('background-color:'+layerBG+';'):'')

							+'" onmouseover="this.onmousedown=DRAG.dragIsDown;" ondragstart="return false;" onselectstart="return false;">'

							+layerContent

							+'</div>' ;

	}

}

function Draglayer(gridX,gridY){

	this.gridX	=	gridX;

	this.gridY	=	gridY;



}

//Draglayer.prototype.dragMousePos =
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; } } }

	//document.title=xcoord+':'+ycoord;

	return [xcoord,ycoord];

}

Draglayer.prototype.dragIsDown	=
function (e) {
	//make note of starting positions and detect mouse movements

	//alert('dragIsDown');return ;

	if	(
				(		e
				&& 	( 	e.which > 1
					|| 	e.button > 1 )
				)
			|| 	( 		window.event
				&& ( 	window.event.which > 1
					|| 	window.event.button > 1 )
				)
			) {
				return false;
	}
	window.palette			=	(this.id.indexOf('palette')>-1);

	window.msStartCoord = dragMousePos(e);

	window.lyStartCoord = this.style?[parseInt(this.style.left),parseInt(this.style.top)]:[parseInt(this.left),parseInt(this.top)];

	//if (window.msStartCoord[1]-window.lyStartCoord[1]>20){return true}

	if( document.captureEvents && Event.MOUSEMOVE ) {
		document.captureEvents(Event.MOUSEMOVE);
		document.captureEvents(Event.MOUSEUP);
		document.captureEvents(Event.DRAGDROP);
	}
	window.storeMOUSEMOVE = document.onmousemove;
	window.storeMOUSEUP = document.onmouseup;
	window.storeLayer = this;
	document.onmousemove = dragIsMove;
	document.onmouseup = dragIsMove;
	//document.ondragdrop = dd;
	//window.ondragdrop = dd;
	return false;
}

//Draglayer.prototype.grid	=
function grid(x,scale){

	scale	=	50;//(PAGE.magnification*scale)/100;

	return ((!window.palette)?scale*Math.round(x / scale):x);

}


function restorePosition(){

	if (window.storeLayer.style){

		window.storeLayer.style.left		=	window.lyStartCoord[0];
		window.storeLayer.style.top		=	window.lyStartCoord[1];

	}	else	{

		window.storeLayer.left			=	window.lyStartCoord[0];
		window.storeLayer.top			=	window.lyStartCoord[1];

	}

}
function restoreMethods(){

	alert((window.storeMOUSEMOVE)+':'+(window.storeMOUSEUP));


	if (window.storeMOUSEMOVE){

		document.onmousemove = window.storeMOUSEMOVE;

	}
	if (window.storeMOUSEUP){

		document.onmouseup = window.storeMOUSEUP;

	}

}

function inverseGrid(x){

	//scale	=	(PAGE.magnification*scale)/100;

	return ((100*x)/PAGE.magnification);

}

//Draglayer.prototype.dragIsMove	=
function dragIsMove(e) {
	var msMvCo = dragMousePos(e);

	if( !e ) {

		e = window.event ? window.event : ( new Object() );

	}
	//		layer start coord		mouse coord		mouse start coord
	var newX = grid(window.lyStartCoord[0] + ( msMvCo[0] - window.msStartCoord[0] ),10);

	var newY = grid(window.lyStartCoord[1] + ( msMvCo[1] - window.msStartCoord[1] ),20);

	//top.status='move the layer to '+newX+':'+newY;

	//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' ) {

		//restoreMethods();
		document.onmousemove = storeMOUSEMOVE;
		document.onmouseup = window.storeMOUSEUP;

		if (window.palette){

			dragIsMove2(window.lyStartCoord[0],window.lyStartCoord[1]);

			window.palette	=	false;

			//alert(window.storeLayer.style.backgroundColor+':'+msMvCo[0]+':'+msMvCo[1])

			setTimeout('PAGE.updateColor("'+window.storeLayer.style.backgroundColor+'",'+inverseScale(msMvCo[0])+','+inverseScale(msMvCo[1])+')',100);

			return;


		}	else	{

			if (window.storeLayer.id.indexOf('group')==-1){

				setTimeout('PAGE.updatePosition("'+window.storeLayer.id+'",'+inverseGrid(newX)+','+inverseGrid(newY)+')',100);

			}	else	{

				parts	=	window.storeLayer.id.split('_');

				//alert(parts[1]);

				PAGE.moveInGroup(parts[1],inverseGrid(newX),inverseGrid(newY));

			}

			return;

		}

	}

	if( navigator.product == 'Gecko' ) {

		window.setTimeout('dragIsMove2('+newX+','+newY+');',1);

	} else {

		dragIsMove2(newX,newY);

	}
}

//Draglayer.prototype.dragIsMove2	=
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;

}
var DRAG	=	new Draglayer(10,17);