var IE = document.all?true:false
var cDownX = cDownY = 0;
var cMouseDown = cResizeDown = false;
var cDivWidth = cDivHeight = cTotalWidth = cTotalHeight = 0;
var cDownLeft = cDownTop = 0;
var cXYp = 0.5;

var cCursorObj = cDivObj = cContainerObj = false;


if (!document.attachEvent) {
    // firefox & friends
    window.addEventListener('load', psdCropInit,true);
} else {
    // ie
    window.attachEvent('onload',psdCropInit);
}

function psdCropInit() {
	cContainerObj = document.getElementById("psdCrop");
	cDivObj = document.getElementById("cropDiv");
 	cCursorObj = document.getElementById("cropResize");
	if (cContainerObj && cDivObj && cCursorObj) psdCropStart();
}

function psdCropUpdate() {
	var f = document.getElementById("cropForm");
	if (!f) return;
	f['x'].value = parseInt(cDivObj.style.left);
	f['y'].value = parseInt(cDivObj.style.top);
	f['w'].value = parseInt(cDivWidth);
	f['h'].value = parseInt(cDivHeight);
}

function psdCropStart() {
//	var cContainerObj = document.getElementById("psdCrop");
//	var cDivObj = document.getElementById("cropDiv");
//	var cCursorObj = document.getElementById("cropResize");
	cDivObj.style.left = "0px";
	cDivObj.style.top = "0px";
	
	var container = document.createElement("div");
	if (!cContainerObj.width || !cContainerObj.height || !cCursorObj) return;
	container.id = "cropContainer";
	container.style.width = parseInt(cContainerObj.width)+"px";
	container.style.height = parseInt(cContainerObj.height)+"px";
	cContainerObj.parentNode.appendChild(container);
	container.appendChild(cContainerObj);
	container.appendChild(cCursorObj);
	
	container.appendChild(cDivObj);
	container.onmousemove = psdCropResize;
	container.onmouseup = psdCropMouseUp;
	cDivObj.onmousedown = psdCropMouseDown;
	cDivObj.onmousemove = psdCropMouseMove;
	cDivObj.onmouseup = psdCropMouseUp;
//	cDivObj.onmouseout = psdCropMouseUp;
	
	cCursorObj.onmousedown = psdCropMouseDownResize;
	cCursorObj.onmouseup = psdCropMouseUp;
	cCursorObj.onmousemove = psdCropResize;

	if(cDivObj.offsetWidth){ 
		cDivWidth=cDivObj.offsetWidth; 
		cDivHeight=cDivObj.offsetHeight; 
	} 

	else if(cDivObj.style.pixelWidth){ 
		 cDivWidth=cDivObj.style.pixelWidth; 
		 cDivHeight=cDivObj.style.pixelHeight; 
	}
	if(cContainerObj.offsetWidth){ 
		 cTotalWidth=cContainerObj.offsetWidth; 
		cTotalHeight = cContainerObj.offsetHeight; 
	} 

	else if(cContainerObj.style.pixelWidth){ 
		 cTotalWidth=cContainerObj.style.pixelWidth; 
		 cTotalHeight=cContainerObj.style.pixelHeight; 
	}
	if (cDivHeight > 0) cXYp = cDivWidth/cDivHeight;
	else cXYp = 1;
	
	cCursorObj.style.left = cDivWidth+"px";
	cCursorObj.style.top = cDivHeight+"px";

}

function psdCropMouseDown(e) {
	if (!e) e = window.event;
	
  	if (IE) { // grab the x-y pos.s if browser is IE
    	cDownX = e.clientX + document.body.scrollLeft;
 		cDownY = e.clientY + document.body.scrollTop;
  	} else {  // grab the x-y pos.s if browser is NS
    	cDownX = e.pageX;
    	cDownY = e.pageY;
  	}	

	cDownLeft = parseInt(cDivObj.style.left);
	cDownTop = parseInt(cDivObj.style.top);
	cMouseDown = true;
}

function psdCropMouseDownResize(e) {
	if (!e) e = window.event;
	
  	if (IE) { // grab the x-y pos.s if browser is IE
    	cDownX = e.clientX + document.body.scrollLeft;
 		cDownY = e.clientY + document.body.scrollTop;
  	} else {  // grab the x-y pos.s if browser is NS
    	cDownX = e.pageX;
    	cDownY = e.pageY;
  	}	

	cDownLeft = parseInt(cDivObj.style.left);
	cDownTop = parseInt(cDivObj.style.top);
	cMouseDown = cResizeDown = true;
}


function psdCropMouseMove(e) {
	if (!cMouseDown) return;
//	var cDivObj = document.getElementById("cropDiv");
//	var cCursorObj = document.getElementById("cropResize");

	if (!e) e = window.event;

	
	var mX = mY = 0;
  	if (IE) { // grab the x-y pos.s if browser is IE
    	mX = e.clientX + document.body.scrollLeft;
 		mY = e.clientY + document.body.scrollTop;
  	} else {  // grab the x-y pos.s if browser is NS
    	mX = e.pageX;
    	mY = e.pageY;
  	}

	var diffX = diffY = 0;
	diffX = mX - cDownX;
	diffY = mY - cDownY;
	var newLeft = cDownLeft+diffX;
	var newTop = cDownTop+diffY;
	if (newLeft < 0) newLeft = 0;
	if (newTop < 0) newTop = 0;

	
	
	if (newLeft+cDivWidth > cTotalWidth) newLeft = cTotalWidth - cDivWidth;
	if (newTop+cDivHeight > cTotalHeight) newTop = cTotalHeight - cDivHeight;
	
	cDivObj.style.left = newLeft+"px";	
	cDivObj.style.top = newTop+"px";
	
	cCursorObj.style.left = newLeft+cDivWidth+"px";
	cCursorObj.style.top = newTop+cDivHeight+"px";
	

//	var log = document.getElementById("log");
//	log.innerHTML = newTop;
	
	


}

function psdCropResize(e) {
	if (!cMouseDown || !cResizeDown) return;
//	var cContainerObj = document.getElementById("cropResize");
//	var cDivObj = document.getElementById("cropDiv");

	if (!e) e = window.event;

	
	var mX = mY = 0;
  	if (IE) { // grab the x-y pos.s if browser is IE
    	mX = e.clientX + document.body.scrollLeft;
 		mY = e.clientY + document.body.scrollTop;
  	} else {  // grab the x-y pos.s if browser is NS
    	mX = e.pageX;
    	mY = e.pageY;
  	}

	var diffY = 0;
	diffY = mY - cDownY;
	var newHeight = cDivHeight+diffY;
	if (newHeight < 1) newHeight = 1;
	
	if (newHeight+parseInt(cDivObj.style.top) > cTotalHeight) newHeight = cTotalHeight-parseInt(cDivObj.style.top);
	var newWidth = newHeight * cXYp;
	if (newWidth+parseInt(cDivObj.style.left) > cTotalWidth) {
		newWidth = cTotalWidth-parseInt(cDivObj.style.left);
		newHeight = Math.round(newWidth / cXYp);
	}
	
	cDownY = mY;
	cDivHeight = newHeight;
	cDivWidth = newWidth;
		
	cDivObj.style.height = newHeight+"px";
	cDivObj.style.width = newWidth+"px";

	cCursorObj.style.top = newHeight+parseInt(cDivObj.style.top)+"px";
	cCursorObj.style.left = newWidth+parseInt(cDivObj.style.left)+"px";

	
//	var log = document.getElementById("log");
//	log.innerHTML = divWidth;

}

function psdCropMouseUp(e) {
	cMouseDown = cResizeDown = false;
	psdCropUpdate();	
}