// SUPPORT FUNCTIONS //

	// FIND ABSOLUTE POSITION Function
	function updateTgtPositions(tgt_id)
	{
		var nx_id = document.getElementById(tgt_id);
		
		var offset = new Object();
		offset.x = nx_id.offsetLeft;
		offset.y  = nx_id.offsetTop;
		
		while (nx_id)
		{
			if	(nx_id.offsetParent)
			{ 
				offset.x = offset.x + nx_id.offsetParent.offsetLeft;
				offset.y  = offset.y + nx_id.offsetParent.offsetTop;
				
				nx_id  = nx_id.offsetParent; 
			}
			else
			{ nx_id = false; }
		}
		
		return offset; 
	}
	// End of the above

	function convertPointsPercentage(tgt_id)
	{
		var tgt_element = document.getElementById(chld_id);
		var par_element = document.getElementById(chld_id).offsetParent;
		
		var par_width	= par_element.offsetWidth;
		var par_height = par_element.offsetHeight;
		
		var chld_x 	= tgt_element.offsetLeft;
		var chld_y 	= tgt_element.offsetTop;
		var chld_width	= tgt_element.offsetWidth;
		var chld_height = tgt_element.offsetHeight;
		
		var converted = new Object();
		converted.width	 = Math.round((chld_width/par_width)*10000)/100;
		converted.height = Math.round((chld_height/par_height)*10000)/100;
		converted.x	= Math.round((chld_x/par_width)*10000)/100;
		converted.y	= Math.round((chld_y/par_height)*10000)/100;
		
		return converted;
	}

	// TRANSFER DIV PARENT Function
	function edit_adoptEntity(tgt_id, parent_id, end_func)
	{	
		var parent_current = document.getElementById(tgt_id).offsetParent.id;
	
		if	(parent_id != parent_current)
		{
			var tgt_position = updateTgtPositions(tgt_id);
			var par_position = updateTgtPositions(parent_id);
			
			var new_pos_x	 = (tgt_position.x-par_position.x);
			var new_pos_y	 = (tgt_position.y-par_position.y);
		
			moveEntity(tgt_id, 'mov_parent');
		
			document.getElementById(tgt_id).style.left 	= new_pos_x;
			document.getElementById(tgt_id).style.top 	= new_pos_y;
			
			if	(end_func)
			{ eval(end_func); }
		}
	}
	// End of the above

	function wrapDiv(par_id, tgt_id, center)
	{
		if	(document.getElementById(tgt_id))
		{
			var tgt_element = document.getElementById(tgt_id);	
			var tgt_width	= tgt_element.offsetWidth;
			var tgt_height	= tgt_element.offsetHeight;
			
			document.getElementById(par_id).style.width 	= tgt_width+'px';
			document.getElementById(par_id).style.height 	= tgt_height+'px';
		
			if	(center)
			{ centerDiv(par_id, true, true); }
		}
	}
	
	// CENTER DIV Function
	function centerDiv(tgt_id, center_vert, center_hor)
	{
		if	(document.getElementById(tgt_id))
		{
			var tgt_element = document.getElementById(tgt_id);	
			var tgt_width	= tgt_element.offsetWidth;
			var tgt_height	= tgt_element.offsetHeight;
			
			var par_width	= tgt_element.offsetParent.offsetWidth;
			var par_height	= tgt_element.offsetParent.offsetHeight;
			
			if	(center_vert)
			{ var tgt_y = Math.round((par_height-tgt_height)/2); }
			
			if	(center_hor)
			{ var tgt_x = Math.round((par_width-tgt_width)/2); }
			
			if	(center_vert)
			{ document.getElementById(tgt_id).style.top 	= tgt_y+'px'; }
			
			if	(center_hor)
			{ document.getElementById(tgt_id).style.left 	= tgt_x+'px'; }			
		}
		else
		{ return false; }
	}
	// End of the above

// END OF SUPPORT FUNCTIONS //


// CONTROLLER FUNCTIONS //

var refMouseX;
var refMouseY;
var editQueue = new Object();
var overQueue = new Object();

function edit_cursorElement(e, tgt_id, commands)
{
	if	(!e) 
	{ var e = event; } 
	
	if	(!editQueue.id)
	{
		var tgt_element  = document.getElementById(tgt_id);
		
		refMouseX = e.clientX;
		refMouseY = e.clientY; 
		
		overQueue 			= new Object();
		overQueue.id 		= tgt_id;
		overQueue.width	 	= tgt_element.offsetWidth;
		overQueue.height 	= tgt_element.offsetHeight;
		overQueue.move_allow	 	= commands.move_allow;
		overQueue.resize_allow	 	= commands.resize_allow;
		overQueue.resize_type	 	= commands.resize_type;
	
		document.onmousemove = edit_realiseCursor;
	}
}
	
function edit_realiseCursor(e)
{
	if	(!e) 
	{ var e = event; }
	
	if	(overQueue && overQueue.id)
	{
		var tgt_position = updateTgtPositions(overQueue.id);
		var per_x		 = Math.floor(((e.clientX-tgt_position.x)/overQueue.width)*100);
		var per_y		 = Math.floor(((e.clientY-tgt_position.y)/overQueue.height)*100);
		
		if		(per_x < 8 && overQueue.resize_allow && per_y < 8)
		{ document.getElementById(overQueue.id).style.cursor = 'nw-resize'; }
		else if	(per_x < 8 && overQueue.resize_allow && per_y > 92)
		{ document.getElementById(overQueue.id).style.cursor = 'sw-resize'; }
		else if	(per_x > 92 && overQueue.resize_allow && per_y > 92)
		{ document.getElementById(overQueue.id).style.cursor = 'se-resize'; }
		else if	(per_x > 92 && overQueue.resize_allow && per_y < 8)
		{ document.getElementById(overQueue.id).style.cursor = 'ne-resize'; }
		else if	(per_y > 92 && overQueue.resize_allow && overQueue.resize_type == 'sandbox')
		{ document.getElementById(overQueue.id).style.cursor = 's-resize'; }
		else if	(per_y < 8 && overQueue.resize_allow && overQueue.resize_type == 'sandbox')
		{ document.getElementById(overQueue.id).style.cursor = 'n-resize'; }
		else if	(per_x > 92 && overQueue.resize_allow && overQueue.resize_type == 'sandbox')
		{ document.getElementById(overQueue.id).style.cursor = 'e-resize'; }
		else if	(per_x < 8 && overQueue.resize_allow && overQueue.resize_type == 'sandbox')
		{ document.getElementById(overQueue.id).style.cursor = 'w-resize'; }
		else if (overQueue.move_allow)	
		{ document.getElementById(overQueue.id).style.cursor = 'move';}
	}
}

function edit_register(e, tgt_id, commands)
{
	if	(!e) 
	{ var e = event; } 
	
	var tgt_element  = document.getElementById(tgt_id);
	
	refMouseX = e.clientX;
	refMouseY = e.clientY;
	
	editQueue		= new Object();
	editQueue.id 	= tgt_id;
	editQueue.x 	= tgt_element.offsetLeft;
	editQueue.y 	= tgt_element.offsetTop;
	editQueue.width	 	 = tgt_element.offsetWidth;
	editQueue.height 	 = tgt_element.offsetHeight;
	editQueue.endfunc	 = commands.end_func;
	editQueue.adopt		 = commands.adopt;
	editQueue.adopt_func = commands.adopt_func;
	editQueue.restrict	 = commands.restrict;
	
	editQueue.move_allow	 	= commands.move_allow;
	editQueue.resize_allow	 	= commands.resize_allow;
	editQueue.resize_type	 	= commands.resize_type;
	editQueue.measure	 		= commands.measure;
	editQueue.rules				= edit_process(e); 
	
	if		(editQueue.rules.action == 'move')
	{ document.onmousemove = edit_moveElement; }
	else if	(editQueue.rules.action == 'resize')
	{ document.onmousemove = edit_resizeElement; }
	
	if	(commands.start_func)
	{ eval(commands.start_func); }
}

function edit_process(e)
{
	if	(!e) 
	{ var e = event; } 
	
	if	(editQueue && editQueue.id)
	{
		var tgt_position = updateTgtPositions(editQueue.id);
		var per_x		 = Math.floor(((e.clientX-tgt_position.x)/editQueue.width)*100);
		var per_y		 = Math.floor(((e.clientY-tgt_position.y)/editQueue.height)*100);
		var outline		 = new Object();
		
		if		(per_x < 8 && editQueue.resize_allow && per_y < 8)
		{ outline.action = 'resize'; outline.direction = "top_left"; }
		else if	(per_x < 8 && editQueue.resize_allow && per_y > 92)
		{ outline.action = 'resize'; outline.direction = "bottom_left"; }
		else if	(per_x > 92 && editQueue.resize_allow && per_y > 92)
		{ outline.action = 'resize'; outline.direction = "bottom_right"; }
		else if	(per_x > 92 && editQueue.resize_allow && per_y < 8)
		{ outline.action = 'resize'; outline.direction = "top_right"; }
		else if	(per_y > 92 && editQueue.resize_allow && editQueue.resize_type == 'sandbox')
		{ outline.action = 'resize'; outline.direction = "bottom"; }
		else if	(per_y < 8 && editQueue.resize_allow && editQueue.resize_type == 'sandbox')
		{ outline.action = 'resize'; outline.direction = "top"; }
		else if	(per_x > 92 && editQueue.resize_allow && editQueue.resize_type == 'sandbox')
		{ outline.action = 'resize'; outline.direction = "right"; }
		else if	(per_x < 8 && editQueue.resize_allow && editQueue.resize_type == 'sandbox')
		{ outline.action = 'resize'; outline.direction = "left"; }
		else if (editQueue.move_allow)	
		{ outline.action = 'move'; outline.direction = "global"; }
		
		//document.getElementById('differenceX').innerHTML 	= outline.action;
		//document.getElementById('differenceY').innerHTML	= outline.direction;
		
		return outline;
	}
}

function edit_resizeElement(e)
{
	if	(!e) 
	{ var e = event; } 
	
	if	(editQueue && editQueue.id)
	{
		var tgt_element  = document.getElementById(editQueue.id);	
		
		var par_width	 = tgt_element.offsetParent.offsetWidth;
		var par_height	 = tgt_element.offsetParent.offsetHeight;
		
		var allow_width	 = par_width-editQueue.width;
		var allow_height = par_height-editQueue.height;
		
		var new_pos_x	 = (e.clientX-refMouseX);
		var new_pos_y	 = (e.clientY-refMouseY);
		
		if	(editQueue.rules.direction == 'bottom_right')
		{ document.getElementById(editQueue.id).style.width = editQueue.width+new_pos_x; document.getElementById(editQueue.id).style.height = editQueue.height+new_pos_y; }
		else if	(editQueue.rules.direction == 'top_right')
		{ document.getElementById(editQueue.id).style.width = editQueue.width+new_pos_x; document.getElementById(editQueue.id).style.height = editQueue.height-new_pos_y; document.getElementById(editQueue.id).style.top = editQueue.y+new_pos_y; }
		else if	(editQueue.rules.direction == 'bottom_left')
		{ document.getElementById(editQueue.id).style.width = editQueue.width-new_pos_x; document.getElementById(editQueue.id).style.left = editQueue.x+new_pos_x; document.getElementById(editQueue.id).style.height = editQueue.height+new_pos_y; }
		else if	(editQueue.rules.direction == 'top_left')
		{ document.getElementById(editQueue.id).style.width = editQueue.width-new_pos_x; document.getElementById(editQueue.id).style.left = editQueue.x+new_pos_x; document.getElementById(editQueue.id).style.height = editQueue.height-new_pos_y; document.getElementById(editQueue.id).style.top = editQueue.y+new_pos_y; }
	}
}

function edit_moveElement(e)
{
	if	(!e) 
	{ var e = event; } 
	
	if	(editQueue && editQueue.id)
	{
		var tgt_element  = document.getElementById(editQueue.id);	
		
		var par_width	 = tgt_element.offsetParent.offsetWidth;
		var par_height	 = tgt_element.offsetParent.offsetHeight;
		
		var allow_width	 = par_width-editQueue.width;
		var allow_height = par_height-editQueue.height;
		
		var new_pos_x	 = (editQueue.x+(e.clientX-refMouseX));
		var new_pos_y	 = (editQueue.y+(e.clientY-refMouseY));
		
		if	(editQueue.restrict && tgt_element.offsetParent.id == editQueue.restrict)
		{
			if		(new_pos_x < allow_width && new_pos_x > 0)
			{ document.getElementById(editQueue.id).style.left = new_pos_x; }
			else if	(new_pos_x >= allow_width)
			{ document.getElementById(editQueue.id).style.left = allow_width; }
			else if	(new_pos_x < 0)
			{ document.getElementById(editQueue.id).style.left = 0; }
		
			if		(new_pos_y < allow_height && new_pos_y > 0)
			{ document.getElementById(editQueue.id).style.top 	= new_pos_y; }
			else if	(new_pos_y >= allow_height)
			{ document.getElementById(editQueue.id).style.top 	= allow_height; }
			else if	(new_pos_y < 0)
			{ document.getElementById(editQueue.id).style.top 	= 0; }
		}
		else
		{
			document.getElementById(editQueue.id).style.left = new_pos_x;
			document.getElementById(editQueue.id).style.top  = new_pos_y;
		}
	}
}

function edit_unregister()
{	
	if	(editQueue && editQueue.id)
	{  
		if	(editQueue.adopt)
		{ edit_adoptEntity(editQueue.id, editQueue.adopt, editQueue.adopt_func); }

		if	(editQueue.endfunc)
		{ eval(editQueue.endfunc); }
	
		if	(editQueue && editQueue.id)
		{ document.onmousemove = edit_realiseCursor; }
		
		editQueue = new Object();
	}
}

// END OF CONTROLLER FUNCTIONS //

document.onmouseup	 = edit_unregister;