///////////////////////////////////////////////////////////////////////////////
//	Copyright (C) 2001-2004 YaWah.com. All rights reserved.
//
//	This work contains valuable  confidential and proprietary information.
//	Disclosure, use or  reproduction without the  written authorization of
//    YaWah.com is prohibited.
//
//	There is a theory which states that if ever anyone discovers exactly what
//	this code is for and why it is here, it will instantly disappear and be
//	replaced by something even more bizarre and inexplicable.
//
//	There is another which states that this has already happened.
//
//	Version 04-jan-2004
//

var initialized = false;
var cropImg;
var srcX, srcY, srcW, srcH;
var ulLayer, urLayer, dlLayer, drLayer, topLayer, bottomLayer, leftLayer, rightLayer;

var cTop = 0.0;
var cLeft = 0.0;
var cBottom = 1.0;
var cRight = 1.0;
var inCropDrag = false;
var updateCropForm = true;

function makeImageLayer(name, src)
{
	var newLayer = addLayer(name);
 	setLayerHTML(newLayer, getImgTag(name + '-img', src));
	showLayer(newLayer);
	return newLayer;
}

function cropRound5(number)
{
  	return Math.round(number*10000)/10000;
}

function updateCropRect()
{
	inCropDrag  = true;

	x1 = getLayerLeft(ulLayer) + 6;
	x2 = getLayerLeft(drLayer) + 3;
	y1 = getLayerTop(ulLayer) + 6;
	y2 = getLayerTop(drLayer) + 3;
	
	var html = getImgTag('ltop-img', 'images/frames/crop-hor.gif', x2 - x1 - 4, 3);
	
	var w1 = x2 - x1 - 4 ;
	setLayerHTML(topLayer,  '<table border="0" cellspacing="0" cellpadding="0" background="images/frames/ant-r.gif" height="3" width="' + w1 + '"><tr><td></td></tr></table>');
	moveLayerTo(topLayer, x1 + 2 , y1 - 3);
	setLayerHTML(bottomLayer,  '<table border="0" cellspacing="0" cellpadding="0" background="images/frames/ant-l.gif" height="3" width="' + w1 + '"><tr><td></td></tr></table>');
	moveLayerTo(bottomLayer, x1 + 2 , y2);
	
	var h1 = y2 - y1 - 4;
	setLayerHTML(leftLayer,  '<table border="0" cellspacing="0" cellpadding="0" background="images/frames/ant-u.gif" height="' + h1 + '" width="3"><tr><td></td></tr></table>');
	moveLayerTo(leftLayer, x1 - 3 , y1 + 2);
	setLayerHTML(rightLayer,  '<table border="0" cellspacing="0" cellpadding="0" background="images/frames/ant-d.gif" height="' + h1 + '" width="3"><tr><td></td></tr></table>');
	moveLayerTo(rightLayer, x2 , y1 + 2);
	
	cTop  = cropRound5((1.0 * (y1 - srcY - 1)) / srcH);
	cLeft = cropRound5((1.0 * (x1 - srcX - 1)) / srcW);
	cBottom = cropRound5((1.0 * (y2 - srcY + 1)) / srcH);
	cRight = cropRound5((1.0 * (x2 - srcX + 1)) / srcW);
		
	if(updateCropForm  && document.cropForm)
	{
		document.cropForm.top.value = cTop;
		document.cropForm.left.value = cLeft;
		document.cropForm.bottom.value = cBottom;
		document.cropForm.right.value = cRight;	
	}

	inCropDrag = false;
}

function getPos(el)
{
	for (var lx=0,ly=0;el!=null;
		lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}

function makeCropHandles(img)
{
	cropImg = img;

	if(!initialized)
	{
		ulLayer = makeImageLayer('lul', 'images/frames/crop-ul.gif');
		urLayer = makeImageLayer('lur', 'images/frames/crop-ur.gif');
		dlLayer = makeImageLayer('ldl', 'images/frames/crop-dl.gif');
		drLayer = makeImageLayer('ldr', 'images/frames/crop-dr.gif');
		topLayer = makeImageLayer('ltop', 'images/frames/ant-u.gif');
		bottomLayer = makeImageLayer('lbot', 'images/frames/ant-d.gif');
		leftLayer = makeImageLayer('llef', 'images/frames/ant-l.gif');
		rightLayer = makeImageLayer('lrig', 'images/frames/ant-r.gif');
	}
	
	myImgDim = getPos(cropImg)
	srcX = myImgDim.x;
	srcY = myImgDim.y;
	srcW = cropImg.width;
	srcH = cropImg.height;

	w1 = eval(srcW) * (cRight - cLeft);
	x1 = eval(srcX) + (cLeft * srcW);
	h1 = eval(srcH) * (cBottom - cTop);
	y1 = eval(srcY) + (cTop * srcH);

	moveLayerTo(ulLayer, x1 - 5, y1 - 5);
	moveLayerTo(urLayer, x1 - 4 + w1, y1 - 5);
	moveLayerTo(dlLayer, x1 - 5 , eval(y1) + eval(h1)- 4);
	moveLayerTo(drLayer, x1 - 4 +  eval(w1) , eval(y1) + eval(h1) - 4);
		
	Drag.init(ulLayer, null, srcX - 5, srcX - 17 + eval(srcW), srcY - 5, srcY - 15 + eval(srcH));
	Drag.init(urLayer, null, eval(srcX) + 8, srcX - 4 + eval(srcW), srcY - 5, srcY - 15 + eval(srcH));
	Drag.init(dlLayer, null, srcX - 5, srcX - 17 + eval(srcW), eval(srcY) + 8, eval(srcY) + eval(srcH)- 4);
	Drag.init(drLayer, null, eval(srcX) + 8, srcX - 4 + eval(srcW), eval(srcY) + 8, eval(srcY) + eval(srcH)- 4);
		
	updateCropRect();
	ulLayer.onDrag = function(x, y) 
	{ 
		y2 = getLayerTop(dlLayer);
		if(y2 - 13 <= y)
		{
			y2 = y + 13;
			moveLayerTo(drLayer, getLayerLeft(drLayer), y2);
		}			
		moveLayerTo(dlLayer, x, y2);
		
		x2 = getLayerLeft(urLayer);
		if(x2 - 13 <= x)
		{
			x2 = x + 13;
			moveLayerTo(drLayer, x2, getLayerTop(drLayer));
		}
		moveLayerTo(urLayer, x2, y);
		updateCropRect();
	}

	urLayer.onDrag = function(x, y) 
	{ 
		y2 = getLayerTop(drLayer);
		if(y2 - 13 <= y)
		{
			y2 = y + 13;
			moveLayerTo(dlLayer, getLayerLeft(dlLayer), y2);
		}			
		moveLayerTo(drLayer, x, y2);
		
		x2 = getLayerLeft(ulLayer);
		if(x2 + 13 >  x)
		{
			x2 = x - 13;
			moveLayerTo(dlLayer, x2, getLayerTop(dlLayer));
		}
		moveLayerTo(ulLayer, x2, y);
		updateCropRect();
	}

	dlLayer.onDrag = function(x, y) 
	{ 
		y2 = getLayerTop(ulLayer);
		if(y2 + 13 > y)
		{
			y2 = y - 13;
			moveLayerTo(urLayer, getLayerLeft(urLayer), y2);
		}			
		moveLayerTo(ulLayer, x, y2);
		
		
		x2 = getLayerLeft(drLayer);
		if(x2 - 13 <  x)
		{
			x2 = x + 13;
			moveLayerTo(urLayer, x2, getLayerTop(urLayer));
		}
		moveLayerTo(drLayer, x2, y);				
		updateCropRect();			
	}

	drLayer.onDrag = function(x, y) 
	{ 
		y2 = getLayerTop(urLayer);
		if(y2 + 13 > y)
		{
			y2 = y - 13;
			moveLayerTo(ulLayer, getLayerLeft(ulLayer), y2);
		}			
		moveLayerTo(urLayer, x, y2);
		
		x2 = getLayerLeft(dlLayer);
		if(x2 + 13 >  x)
		{
			x2 = x - 13;
			moveLayerTo(ulLayer, x2, getLayerTop(ulLayer));
		}
		moveLayerTo(dlLayer, x2, y);
		updateCropRect();
	}								
	initialized = true;
}

