
	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, April 2006
	
	Update log:
		August, 9th, 2006: Added a fix to the preserve aspect ratio feature ( Thanks to Urko Benito)
		August, 11th, 2006: If fixed ratio is set, resize crop area initially
		
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/	

	
	
	var crop_script_alwaysPreserveAspectRatio = false;
	var crop_script_fixedRatio = false;	 
										 
	
	var crop_script_browserIsOpera = navigator.userAgent.indexOf('Opera')>=0?true:false;
	var cropDiv_left = false;
	var cropDiv_top = false;
	var cropDiv_right = false;
	var cropDiv_bottom = false;  
	var cropDiv_dotted = false;
	
	var crop_currentResizeType = false;
	
	var cropEvent_posX;
	var cropEvent_posY;
	
	var eventCount = 10;
	
	var cropEvent_eventX;
	var cropEvent_eventY;
	var crop_resizeCounter = -1;
	var crop_moveCounter = -1;
	
	var crop_imageDiv = false;
	var imageDiv_currentWidth = false;
	var imageDiv_currentHeight = false;
	var imageDiv_currentLeft = false;
	var imageDiv_currentTop = false;
	
	var smallSquare_tl;
	var smallSquare_tc;
	var smallSquare_tr;
	var smallSquare_lc;
	var smallSquare_rc;
	var smallSquare_bl;
	var smallSquare_bc;
	var smallSquare_br;
	
	var offsetSmallSquares = Math.floor(smallSquareWidth/2);
	
	var cropScriptAjaxObjects = new Array();
	var preserveAspectRatio = false;
	
	function setFakeEvents(el)
	{
		el.onselectstart = function() { return true; };
		el.ondragstart = function() { return false; };
	}

	var cropWidthRatio = false;	
	function crop_createDivElements()
	{
		crop_imageDiv= document.getElementById('imageContainer');
		setFakeEvents(crop_imageDiv);
		
		cropDiv_left = document.createElement('DIV');
		cropDiv_left.className = 'crop_transparentDiv'; 
		cropDiv_left.style.visibility = 'visible';
		cropDiv_left.style.left = '0px';
		cropDiv_left.style.top = '0px';
		cropDiv_left.style.height = crop_imageHeight + 'px';
		cropDiv_left.style.width = '0px';
		cropDiv_left.innerHTML = '<span></span>';
		crop_imageDiv.appendChild(cropDiv_left);
		setFakeEvents(cropDiv_left);
		
		cropDiv_top = document.createElement('DIV');
		cropDiv_top.className = 'crop_transparentDiv'; 
		cropDiv_top.style.visibility = 'visible';
		cropDiv_top.style.left = '0px';
		cropDiv_top.style.top = '0px';
		cropDiv_top.style.height = '0px';
		cropDiv_top.style.width = crop_imageWidth + 'px';
		cropDiv_top.innerHTML = '<span></span>';
		crop_imageDiv.appendChild(cropDiv_top);
		setFakeEvents(cropDiv_top);
		
		cropDiv_right = document.createElement('DIV');
		cropDiv_right.className = 'crop_transparentDiv'; 
		cropDiv_right.style.visibility = 'visible';
		cropDiv_right.style.left = (crop_imageWidth) + 'px';
		cropDiv_right.style.top = '0px';
		cropDiv_right.style.height = crop_imageHeight + 'px';
		cropDiv_right.style.width = '0px';		
		cropDiv_right.innerHTML = '<span></span>';
		crop_imageDiv.appendChild(cropDiv_right); 
		setFakeEvents(cropDiv_right);
		
		cropDiv_bottom = document.createElement('DIV');
		cropDiv_bottom.className = 'crop_transparentDiv'; 
		cropDiv_bottom.style.visibility = 'visible';
		cropDiv_bottom.style.left = '0px';
		cropDiv_bottom.style.top = (crop_imageHeight) + 'px';
		cropDiv_bottom.style.height = '0px';
		cropDiv_bottom.style.width = crop_imageWidth + 'px';
		cropDiv_bottom.innerHTML = '<span></span>';
		crop_imageDiv.appendChild(cropDiv_bottom); 
		setFakeEvents(cropDiv_bottom);
		
		cropDiv_dotted = document.createElement('DIV');
		cropDiv_dotted.className='crop_dottedDiv';
		cropDiv_dotted.style.left = '0px';
		cropDiv_dotted.style.top = '0px';
		cropDiv_dotted.style.width = (crop_imageWidth-(cropToolBorderWidth*2)) + 'px';
		cropDiv_dotted.style.height = (crop_imageHeight-(cropToolBorderWidth*2)) + 'px';
		cropDiv_dotted.innerHTML = '<div></div>'; 
		cropDiv_dotted.style.cursor = 'move';
		setFakeEvents(cropDiv_dotted);
		
		if(crop_script_browserIsOpera && navigator.userAgent.indexOf('9.') < 0 && navigator.userAgent.indexOf('10.') < 0)
		{		    
			var div = cropDiv_dotted.getElementsByTagName('DIV')[0];
			div.style.backgroundColor='transparent';
			cropDiv_bottom.style.backgroundColor='transparent';
			cropDiv_right.style.backgroundColor='transparent';
			cropDiv_top.style.backgroundColor='transparent';
			cropDiv_left.style.backgroundColor='transparent';
		}
		
		cropDiv_dotted.onmousedown = cropScript_initMove;
		
		smallSquare_tl = document.createElement('IMG');
		smallSquare_tl.src = '/static/images/small_square.gif';
		smallSquare_tl.style.position = 'absolute';
		smallSquare_tl.style.left = (-offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_tl.style.top = (-offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';		
		smallSquare_tl.style.cursor = 'nw-resize';
		smallSquare_tl.id = 'nw-resize';
		smallSquare_tl.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_tl);
		setFakeEvents(smallSquare_tl);
		
		smallSquare_tr = document.createElement('IMG');
		smallSquare_tr.src = '/static/images/small_square.gif';
		smallSquare_tr.style.position = 'absolute';
		smallSquare_tr.style.left = (crop_imageWidth - offsetSmallSquares - (cropToolBorderWidth*2)) + 'px';
		smallSquare_tr.style.top = (-offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';		
		smallSquare_tr.style.cursor = 'ne-resize';
		smallSquare_tr.id = 'ne-resize';
		smallSquare_tr.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_tr);
		setFakeEvents(smallSquare_tr);
		
		smallSquare_bl = document.createElement('IMG');
		smallSquare_bl.src = '/static/images/small_square.gif';
		smallSquare_bl.style.position = 'absolute';
		smallSquare_bl.style.left = (-offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_bl.style.top = (crop_imageHeight - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_bl.style.cursor = 'sw-resize';
		smallSquare_bl.id = 'sw-resize';
		smallSquare_bl.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_bl);
		setFakeEvents(smallSquare_bl);
		
		smallSquare_br = document.createElement('IMG');
		smallSquare_br.src = '/static/images/small_square.gif';
		smallSquare_br.style.position = 'absolute';
		smallSquare_br.style.left = (crop_imageWidth - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_br.style.top = (crop_imageHeight - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_br.style.cursor = 'se-resize';
		smallSquare_br.id = 'se-resize';
		smallSquare_br.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_br);
		setFakeEvents(smallSquare_br);
		
		smallSquare_tc = document.createElement('IMG');
		smallSquare_tc.src = '/static/images/small_square.gif';
		smallSquare_tc.style.position = 'absolute';
		smallSquare_tc.style.left = (Math.floor(crop_imageWidth/2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_tc.style.top = (-offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_tc.style.cursor = 'n-resize';
		smallSquare_tc.id = 'n-resize';
		smallSquare_tc.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_tc);
		setFakeEvents(smallSquare_tc);
		
		smallSquare_bc = document.createElement('IMG');
		smallSquare_bc.src = '/static/images/small_square.gif';
		smallSquare_bc.style.position = 'absolute';
		smallSquare_bc.style.left = (Math.floor(crop_imageWidth/2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_bc.style.top = (crop_imageHeight - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_bc.style.cursor = 's-resize';
		smallSquare_bc.id = 's-resize';
		smallSquare_bc.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_bc);
		setFakeEvents(smallSquare_bc);
		
		smallSquare_lc = document.createElement('IMG');
		smallSquare_lc.src = '/static/images/small_square.gif';
		smallSquare_lc.style.position = 'absolute';
		smallSquare_lc.style.left = (-offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_lc.style.top = (Math.floor(crop_imageHeight/2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_lc.style.cursor = 'w-resize';
		smallSquare_lc.id = 'w-resize';
		smallSquare_lc.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_lc);	
		setFakeEvents(smallSquare_lc);
		
		smallSquare_rc = document.createElement('IMG');
		smallSquare_rc.src = '/static/images/small_square.gif';
		smallSquare_rc.style.position = 'absolute';
		smallSquare_rc.style.left = (crop_imageWidth - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_rc.style.top = (Math.floor(crop_imageHeight/2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_rc.style.cursor = 'e-resize';
		smallSquare_rc.id = 'e-resize';
		smallSquare_rc.onmousedown = cropScript_initResize;
		cropDiv_dotted.appendChild(smallSquare_rc);
		setFakeEvents(smallSquare_rc);
		
		crop_imageDiv.appendChild(cropDiv_dotted); 
	}

	var oldMidX = -1;
	var oldMidY = -1;
	var preserveMidX = false;
	var preserveMidY = false;

	function cropScript_initMove(e)
	{
		if(document.all)e=event;
		
		if (e.target) source = e.target;
			else if (e.srcElement) source = e.srcElement;
			if (source.nodeType == 3) 
				source = source.parentNode;

		if(source.id && source.id.indexOf('resize')>=0)return;	
		
		imageDiv_currentLeft = cropDiv_dotted.style.left.replace('px','')/1;
		imageDiv_currentTop = cropDiv_dotted.style.top.replace('px','')/1;
		imageDiv_currentWidth = cropDiv_dotted.style.width.replace('px','')/1;
		imageDiv_currentHeight = cropDiv_dotted.style.height.replace('px','')/1;		
		cropEvent_eventX = e.clientX;
		cropEvent_eventY = e.clientY;	
		
		preserveMidX = false;
		preserveMidY = false;
		
		crop_moveCounter = 0;
		cropScript_timerMove();
		return false;
	}
	
	function cropScript_timerMove()
	{
		if(crop_moveCounter>=0 && crop_moveCounter<eventCount){
			crop_moveCounter++;
			setTimeout('cropScript_timerMove()',1);
			return;
		}
	}
	
	function cropScript_initResize(e)
	{
		if(document.all) e = event;
		
		cropDiv_dotted.style.cursor = 'default';
		crop_currentResizeType = this.id;
		
		cropEvent_eventX = e.clientX;
		cropEvent_eventY = e.clientY;
		crop_resizeCounter = 0;
		imageDiv_currentWidth = cropDiv_dotted.style.width.replace('px','')/1;
		imageDiv_currentHeight = cropDiv_dotted.style.height.replace('px','')/1;
		imageDiv_currentLeft = cropDiv_dotted.style.left.replace('px','')/1;
		imageDiv_currentTop = cropDiv_dotted.style.top.replace('px','')/1;
				
		cropWidthRatio = cropDiv_dotted.offsetWidth / cropDiv_dotted.offsetHeight;		
		
		if(crop_script_fixedRatio)	cropWidthRatio = crop_script_fixedRatio;
		
		if(document.all){
			var div = cropDiv_dotted.getElementsByTagName('DIV')[0];
			div.style.display='none';
		}
		
		var x1 = cropDiv_dotted.style.left.replace('px','')/1.0;
		var desiredWidth = cropDiv_dotted.style.width.replace('px','')/1.0;
		var y1 = cropDiv_dotted.style.top.replace('px','')/1.0;
		var desiredHeight = cropDiv_dotted.style.height.replace('px','')/1.0;
		oldMidX = x1 + desiredWidth / 2.0;
		oldMidY = y1 + desiredHeight / 2.0;
		if (crop_script_alwaysPreserveAspectRatio && (crop_currentResizeType=='s-resize' || crop_currentResizeType=='n-resize'))
		{
			preserveMidX = true;
		}
		else
		{
			preserveMidX = false;
		}
		if (crop_script_alwaysPreserveAspectRatio && (crop_currentResizeType=='w-resize' || crop_currentResizeType=='e-resize'))
		{
			preserveMidY = true;
		}
		else
		{
			preserveMidY = false;
		}
		
		cropScript_timerResize();
		return false;
	}
	
	function cropScript_timerResize()
	{
		if(crop_resizeCounter>=0 && crop_resizeCounter<eventCount){
			crop_resizeCounter = crop_resizeCounter + 1;
			setTimeout('cropScript_timerResize()',1);
			return;
		}
	}

	function diff(a, b)
	{
		return Math.abs(a-b);
	}
	
	function getHfromW(width, aspectRatio)
	{
		return Math.round(width / aspectRatio);
	}
	
	function getWfromH(height, aspectRatio)
	{
		return Math.round(height * aspectRatio);
	}
	
	var mouseMoveEventInProgress = false;
	function cropScript_mouseMove(e)
	{
		if(mouseMoveEventInProgress)return false;
		if(crop_moveCounter<eventCount && crop_resizeCounter<eventCount)return false;
		if(document.all)mouseMoveEventInProgress = true;
		if(document.all)e = event;	

		if(crop_resizeCounter==eventCount){	
			
			if(e.ctrlKey || crop_script_alwaysPreserveAspectRatio) preserveAspectRatio=true; else preserveAspectRatio = false;
			
			var cropMinHeight = crop_minimumWidthHeight;
			var cropMinWidth = Math.round(cropMinHeight * (preserveAspectRatio ? cropWidthRatio : 1));
			
			var deltaX = e.clientX - cropEvent_eventX;
			var deltaY = e.clientY - cropEvent_eventY;
			
			if(deltaX == 0 && deltaY == 0) 
			{	
				cropDiv_dotted.style.cursor = 'default';
				if (document.all) mouseMoveEventInProgress = false;
				return false;
			}
			
			if (preserveAspectRatio)
			{
				var x1 = cropDiv_dotted.style.left.replace('px','')/1.0;
				var desiredWidth = cropDiv_dotted.style.width.replace('px','')/1.0;
				var x2 = x1 + desiredWidth;
				var y1 = cropDiv_dotted.style.top.replace('px','')/1.0;
				var desiredHeight = cropDiv_dotted.style.height.replace('px','')/1.0;
				var y2 = y1 + desiredHeight;
				
				if(crop_currentResizeType=='e-resize')
				{								
					desiredWidth = Math.max(cropMinWidth, imageDiv_currentWidth + deltaX);
					if (x1 + desiredWidth > crop_imageWidth)
					{
						desiredWidth = crop_imageWidth - x1;
					}
					x2 = x1 + desiredWidth;
					
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					y1 = oldMidY - desiredHeight/2;
					y2 = oldMidY + desiredHeight/2;

					desiredHeight = diff(y1, y2);
					if (y1 < 0)
					{
						y1 = 0;
						desiredHeight = (oldMidY - 0) * 2;			            
						y2 = oldMidY + desiredHeight/2;
					}
					if (y2 > crop_imageHeight)
					{
						y2 = crop_imageHeight;
						desiredHeight = (crop_imageHeight - oldMidY) * 2; 
						y1 = oldMidY - desiredHeight/2;
					}
					desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
					x2 = x1 + desiredWidth;
				}
				else
				if(crop_currentResizeType=='s-resize')
				{								
					desiredHeight = Math.max(cropMinHeight, imageDiv_currentHeight + deltaY);
					if (y1 + desiredHeight > crop_imageHeight)
					{
						desiredHeight = crop_imageHeight - y1;
					}
					y2 = y1 + desiredHeight;
					
					desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
					x1 = oldMidX - desiredWidth/2;
					x2 = oldMidX + desiredWidth/2;

					desiredWidth = diff(x1, x2);
					if (x1 < 0)
					{
						x1 = 0;
						desiredWidth = (oldMidX - 0) * 2;			            
						x2 = oldMidX + desiredWidth/2;
					}
					if (x2 > crop_imageWidth)
					{
						x2 = crop_imageWidth;
						desiredWidth = (crop_imageWidth - oldMidX) * 2; 
						x1 = oldMidX - desiredWidth/2;
					}
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					y2 = y1 + desiredHeight;
				}
				else
				if(crop_currentResizeType=='w-resize')
				{								
					desiredWidth = Math.max(cropMinWidth, imageDiv_currentWidth - deltaX);
					if (x2 - desiredWidth < 0)
					{
						desiredWidth = x2;
					}
					x1 = x2 - desiredWidth;
					
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					y1 = oldMidY - desiredHeight/2;
					y2 = oldMidY + desiredHeight/2;

					desiredHeight = diff(y1, y2);
					if (y1 < 0)
					{
						y1 = 0;
						desiredHeight = (oldMidY - 0) * 2;			            
						y2 = oldMidY + desiredHeight/2;
					}
					if (y2 > crop_imageHeight)
					{
						y2 = crop_imageHeight;
						desiredHeight = (crop_imageHeight - oldMidY) * 2; 
						y1 = oldMidY - desiredHeight/2;
					}
					desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
					x1 = x2 - desiredWidth;
				}
				else
				if(crop_currentResizeType=='n-resize')
				{								
					desiredHeight = Math.max(cropMinHeight, imageDiv_currentHeight - deltaY);
					if (y2 - desiredHeight < 0)
					{
						desiredHeight = y2;
					}
					y1 = y2 - desiredHeight;
					
					desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
					x1 = oldMidX - desiredWidth/2;
					x2 = oldMidX + desiredWidth/2;

					desiredWidth = diff(x1, x2);
					if (x1 < 0)
					{
						x1 = 0;
						desiredWidth = (oldMidX - 0) * 2;			            
						x2 = oldMidX + desiredWidth/2;
					}
					if (x2 > crop_imageWidth)
					{
						x2 = crop_imageWidth;
						desiredWidth = (crop_imageWidth - oldMidX) * 2; 
						x1 = oldMidX - desiredWidth/2;
					}
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					y1 = y2 - desiredHeight;
				}
				else
				if(crop_currentResizeType=='se-resize')
				{
					var dx1 = deltaX;
					var dx2 = getWfromH(deltaY, cropWidthRatio);
					var dx = Math.max(dx1, dx2);
					desiredWidth = Math.max(cropMinWidth, imageDiv_currentWidth + dx);
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					x2 = x1 + desiredWidth;
					y2 = y1 + desiredHeight;
					if (x2 > crop_imageWidth)
					{
						desiredWidth -= x2 - crop_imageWidth;
						x2 -= x2 - crop_imageWidth;
						desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
						y2 = y1 + desiredHeight;
					}
					if (y2 > crop_imageHeight)
					{
						desiredHeight -= y2 - crop_imageHeight;
						y2 -= y2 - crop_imageHeight;
						desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
						x2 = x1 + desiredWidth;
					}
				}
				else
				if(crop_currentResizeType=='sw-resize')
				{
					var dx1 = -deltaX;
					var dx2 = getWfromH(deltaY, cropWidthRatio);
					var dx = Math.max(dx1, dx2);
					desiredWidth = Math.max(cropMinWidth, imageDiv_currentWidth + dx);
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					x1 = x2 - desiredWidth;
					y2 = y1 + desiredHeight;
					if (x1 < 0)
					{
						desiredWidth -= -x1;
						x1 += -x1;
						desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
						y2 = y1 + desiredHeight;
					}
					if (y2 > crop_imageHeight)
					{
						desiredHeight -= y2 - crop_imageHeight;
						y2 -= y2 - crop_imageHeight;
						desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
						x1 = x2 - desiredWidth;
					}
				}
				else
				if(crop_currentResizeType=='ne-resize')
				{
					var dx1 = deltaX;
					var dx2 = -getWfromH(deltaY, cropWidthRatio);
					var dx = Math.max(dx1, dx2);
					desiredWidth = Math.max(cropMinWidth, imageDiv_currentWidth + dx);
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					x2 = x1 + desiredWidth;
					y1 = y2 - desiredHeight;
					if (x2 > crop_imageWidth)
					{
						desiredWidth -= x2 - crop_imageWidth;
						x2 -= x2 - crop_imageWidth;
						desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
						y1 = y2 - desiredHeight;
					}
					if (y1 < 0)
					{
						desiredHeight -= -y1;
						y1 += -y1;
						desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
						x2 = x1 + desiredWidth;
					}
				}
				else
				if(crop_currentResizeType=='nw-resize')
				{
					var dx1 = -deltaX;
					var dx2 = -getWfromH(deltaY, cropWidthRatio);
					var dx = Math.max(dx1, dx2);
					desiredWidth = Math.max(cropMinWidth, imageDiv_currentWidth + dx);
					desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
					x1 = x2 - desiredWidth;
					y1 = y2 - desiredHeight;
					if (x1 < 0)
					{
						desiredWidth -= -x1;
						x1 += -x1;
						desiredHeight = getHfromW(desiredWidth, cropWidthRatio);
						y1 = y2 - desiredHeight;					    
					}
					if (y1 < 0)
					{
						desiredHeight -= -y1;
						y1 += -y1;
						desiredWidth = getWfromH(desiredHeight, cropWidthRatio);
						x1 = x2 - desiredWidth;
					}
				}
				
				cropDiv_dotted.style.left = Math.round(x1) + 'px';
				cropDiv_dotted.style.top = Math.round(y1) + 'px';
				cropDiv_dotted.style.width = Math.round(desiredWidth) + 'px';
				cropDiv_dotted.style.height = Math.round(desiredHeight) + 'px';
				
			}
			else
			{				
				if(crop_currentResizeType=='e-resize' || crop_currentResizeType=='ne-resize' || crop_currentResizeType == 'se-resize'){
				cropDiv_dotted.style.width = Math.max(cropMinWidth,(imageDiv_currentWidth + deltaX)) + 'px';													
				}
				if(crop_currentResizeType=='s-resize' || crop_currentResizeType=='sw-resize' || crop_currentResizeType == 'se-resize'){				
					cropDiv_dotted.style.height = Math.max(cropMinHeight,(imageDiv_currentHeight + deltaY)) + 'px';				
				}
				if(crop_currentResizeType=='w-resize' || crop_currentResizeType=='sw-resize' || crop_currentResizeType=='nw-resize'){								
					var tmpTop = cropDiv_dotted.style.left.replace('px','')/1;
					var newTop = Math.max(0,(imageDiv_currentLeft + e.clientX - cropEvent_eventX));				
					if((newTop + cropMinWidth)> (cropDiv_dotted.style.left.replace('px','')/1 + cropDiv_dotted.style.width.replace('px','')/1))
					{
						newTop = (cropDiv_dotted.style.left.replace('px','')/1 + cropDiv_dotted.style.width.replace('px','')/1) - cropMinWidth;	
					}		
					cropDiv_dotted.style.left = newTop + 'px';
					cropDiv_dotted.style.width = Math.max(cropMinWidth,(cropDiv_dotted.style.width.replace('px','')/1 + tmpTop - cropDiv_dotted.style.left.replace('px','')/1)) + 'px';				
				}
				if(crop_currentResizeType=='n-resize' || crop_currentResizeType=='nw-resize' || crop_currentResizeType=='ne-resize'){
					var tmpTop = cropDiv_dotted.style.top.replace('px','')/1;
					var newTop = Math.max(0,(imageDiv_currentTop + e.clientY - cropEvent_eventY));				
					if((newTop + cropMinHeight)> (cropDiv_dotted.style.top.replace('px','')/1 + cropDiv_dotted.style.height.replace('px','')/1))
					{
						newTop = (cropDiv_dotted.style.top.replace('px','')/1 + cropDiv_dotted.style.height.replace('px','')/1) - cropMinHeight;	
					}				
					cropDiv_dotted.style.top = newTop + 'px';
					cropDiv_dotted.style.height = Math.max(cropMinHeight,(cropDiv_dotted.style.height.replace('px','')/1 + tmpTop - cropDiv_dotted.style.top.replace('px','')/1)) + 'px';					
				}
				if((cropDiv_dotted.offsetHeight + (cropToolBorderWidth*2) + cropDiv_dotted.style.top.replace('px','')/1) > crop_imageHeight)
					cropDiv_dotted.style.height = (crop_imageHeight - cropDiv_dotted.style.top.replace('px','')/1 - (cropToolBorderWidth*2)) + 'px';
			
				if((cropDiv_dotted.offsetWidth + (cropToolBorderWidth*2) + cropDiv_dotted.style.left.replace('px','')/1) > crop_imageWidth)
					cropDiv_dotted.style.width = (crop_imageWidth - cropDiv_dotted.style.left.replace('px','')/1 - (cropToolBorderWidth*2)) + 'px';	
				
			}
			
			if(!crop_script_fixedRatio && !e.ctrlKey)
			{
				cropWidthRatio = cropDiv_dotted.offsetWidth / cropDiv_dotted.offsetHeight;	
			}
		}
		
		if(crop_moveCounter==eventCount)
		{
			var tmpLeft = imageDiv_currentLeft + e.clientX - cropEvent_eventX;
			if(tmpLeft<0) tmpLeft=0;
			if((tmpLeft + imageDiv_currentWidth + (cropToolBorderWidth*2))>crop_imageWidth)tmpLeft = crop_imageWidth - imageDiv_currentWidth - (cropToolBorderWidth*2);
			cropDiv_dotted.style.left = tmpLeft + 'px';
			var tmpTop = imageDiv_currentTop + e.clientY - cropEvent_eventY;
			if(tmpTop<0) tmpTop=0;
			if((tmpTop + imageDiv_currentHeight + (cropToolBorderWidth*2))>crop_imageHeight)tmpTop = crop_imageHeight - imageDiv_currentHeight - (cropToolBorderWidth*2);
			cropDiv_dotted.style.top = tmpTop + 'px';			
		}
		
		repositionSmallSquares();		
		resizeTransparentSquares();
		if(updateFormValuesAsYouDrag) cropScript_updateFormValues();
		mouseMoveEventInProgress = false;
		return false;		
	}
	
	function repositionSmallSquares()
	{
		var l = cropDiv_dotted.style.left.replace('px','')/1;
		var t = cropDiv_dotted.style.top.replace('px','')/1;
		var w = cropDiv_dotted.style.width.replace('px','')/1;
		var h = cropDiv_dotted.style.height.replace('px','')/1;
		var midLeft = (preserveMidX) ? (oldMidX - l) : (w/2);
		var midTop = (preserveMidY) ? (oldMidY - t) : (h/2);		
		smallSquare_tc.style.left = (Math.floor(midLeft + cropToolBorderWidth) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_bc.style.left = (Math.floor(midLeft + cropToolBorderWidth) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_tr.style.left = (w + (cropToolBorderWidth*2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_rc.style.left = (w + (cropToolBorderWidth*2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_br.style.left = (w + (cropToolBorderWidth*2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';

		smallSquare_br.style.top = (h + (cropToolBorderWidth*2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_bc.style.top = (h + (cropToolBorderWidth*2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_bl.style.top = (h + (cropToolBorderWidth*2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_lc.style.top = (Math.floor(midTop + cropToolBorderWidth/2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';
		smallSquare_rc.style.top = (Math.floor(midTop + cropToolBorderWidth/2) - offsetSmallSquares -(cropToolBorderWidth*2)) + 'px';		
	}
	
	function resizeTransparentSquares()
	{		
		cropDiv_left.style.width = cropDiv_dotted.style.left.replace('px','')/1<0?'0px':cropDiv_dotted.style.left;
		cropDiv_right.style.width = Math.max(0,crop_imageWidth - (cropToolBorderWidth*2) - (cropDiv_dotted.style.width.replace('px','')/1 + cropDiv_dotted.style.left.replace('px','')/1)) + 'px';
		cropDiv_right.style.left = Math.round(cropDiv_dotted.style.width.replace('px','')/1 + (cropToolBorderWidth*2) + cropDiv_dotted.style.left.replace('px','')/1) + 'px';
		cropDiv_bottom.style.height = Math.max(0,crop_imageHeight - (cropToolBorderWidth*2) - (cropDiv_dotted.style.height.replace('px','')/1 + cropDiv_dotted.style.top.replace('px','')/1)) + 'px';
		cropDiv_bottom.style.top = (cropDiv_dotted.style.height.replace('px','')/1 + (cropToolBorderWidth*2) + cropDiv_dotted.style.top.replace('px','')/1) + 'px';
		cropDiv_top.style.height = cropDiv_dotted.style.top;
		
		cropDiv_bottom.style.left = cropDiv_dotted.style.left;
		cropDiv_bottom.style.width = (cropDiv_dotted.style.width.replace('px','')/1 + (cropToolBorderWidth*2)) + 'px' ;
		cropDiv_top.style.left = cropDiv_dotted.style.left;
		cropDiv_top.style.width = (cropDiv_dotted.style.width.replace('px','')/1 + (cropToolBorderWidth*2)) + 'px' ;		
		if(cropDiv_left.style.width=='0px')cropDiv_left.style.visibility='hidden';else cropDiv_left.style.visibility='visible';
		if(cropDiv_right.style.width=='0px')cropDiv_right.style.visibility='hidden';else cropDiv_right.style.visibility='visible';
		if(cropDiv_bottom.style.width=='0px')cropDiv_bottom.style.visibility='hidden';else cropDiv_bottom.style.visibility='visible';
		
	}
	
	function cropScript_updateFormValues()
	{
		document.getElementById('input_crop_x').value = Math.round(cropDiv_dotted.style.left.replace('px','')/1 * (crop_originalImageWidth/crop_imageWidth));
		document.getElementById('input_crop_y').value = Math.round(cropDiv_dotted.style.top.replace('px','')/1 * (crop_originalImageHeight/crop_imageHeight));
		document.getElementById('input_crop_width').value = Math.round((cropDiv_dotted.style.width.replace('px','')/1 + (cropToolBorderWidth*2)) * (crop_originalImageWidth/crop_imageWidth));
		document.getElementById('input_crop_height').value = Math.round((cropDiv_dotted.style.height.replace('px','')/1 + (cropToolBorderWidth*2)) * (crop_originalImageHeight/crop_imageHeight));			
	}
	
	function cropScript_stopResizeMove(e)
	{		
		crop_resizeCounter = -1;
		crop_moveCounter = -1;
		cropDiv_dotted.style.cursor = 'move';
		cropScript_updateFormValues();
		computeMultiplier();
		
		var element;
		if (document.all)	
			element = event.srcElement;
		else
			element = e.target;
		
		if (element.name!='res')
			filterResolutions();

		if(document.all){
			var div = cropDiv_dotted.getElementsByTagName('DIV')[0];
			div.style.display='block';
		}
	}
	
	function cropScript_setCropSizeByInput()
	{
		var obj_x = document.getElementById('input_crop_x');
		var obj_y = document.getElementById('input_crop_y');
		var obj_width = document.getElementById('input_crop_width');
		var obj_height = document.getElementById('input_crop_height');
		
		obj_x.value = obj_x.value.replace(/[^0-9]/gi,'');
		obj_y.value = obj_y.value.replace(/[^0-9]/gi,'');
		obj_width.value = obj_width.value.replace(/[^0-9]/gi,'');
		obj_height.value = obj_height.value.replace(/[^0-9]/gi,'');
		
		if(obj_x.value.length==0)obj_x.value=0;
		if(obj_y.value.length==0)obj_y.value=0;
		if(obj_width.value.length==0)obj_width.value=crop_originalImageWidth;
		if(obj_height.value.length==0)obj_height.value=crop_originalImageHeight;
		
		if(obj_x.value>(crop_originalImageWidth-crop_minimumWidthHeight))obj_x.value = crop_originalImageWidth-crop_minimumWidthHeight;
		if(obj_y.value>(crop_originalImageHeight-crop_minimumWidthHeight))obj_y.value = crop_originalImageHeight-crop_minimumWidthHeight;

		if(obj_width.value/1 > crop_originalImageWidth)obj_width.value = crop_originalImageWidth - obj_x.value/1;
		if(obj_height.value/1 > crop_originalImageHeight)obj_height.value = crop_originalImageHeight - obj_y.value/1;
				
		if(obj_x.value/1 + obj_width.value/1 > crop_originalImageWidth) obj_width.value = crop_originalImageWidth - obj_x.value; 
		if(obj_y.value/1 + obj_height.value/1 > crop_originalImageHeight) obj_height.value = crop_originalImageHeight - obj_y.value;  
		
		cropDiv_dotted.style.left = Math.round(obj_x.value/1 * (crop_imageWidth / crop_originalImageWidth)) + 'px';
		cropDiv_dotted.style.top = Math.round(obj_y.value/1 * (crop_imageHeight / crop_originalImageHeight)) + 'px';
		cropDiv_dotted.style.width = Math.floor((obj_width.value/1 - (cropToolBorderWidth*2))  * (crop_imageWidth / crop_originalImageWidth)) + 'px';
		var forIE = document.all?1:0;
		cropDiv_dotted.style.height = Math.floor((obj_height.value/1 - (cropToolBorderWidth*2)) * (crop_imageHeight / crop_originalImageHeight) - forIE) + 'px';

		repositionSmallSquares();		
		resizeTransparentSquares();
	}
	

	
	function cropScript_setBasicEvents()
	{
		setFakeEvents(document.documentElement);
		
		document.documentElement.onmousemove = cropScript_mouseMove;		
		document.documentElement.onmouseup = cropScript_stopResizeMove;
		
		document.getElementById('input_crop_x').onblur = cropScript_setCropSizeByInput;
		document.getElementById('input_crop_y').onblur = cropScript_setCropSizeByInput;
		document.getElementById('input_crop_width').onblur = cropScript_setCropSizeByInput;
		document.getElementById('input_crop_height').onblur = cropScript_setCropSizeByInput;
		
		
	}

	function cropScript_validatePercent()
	{
		this.value=this.value.replace(/[^0-9]/gi,'');
		if(this.value.length==0)this.value='1';
		if(this.value/1>crop_maximumPercent)this.value='100';
		if(this.value/1<crop_minimumPercent)this.value=crop_minimumPercent
	}
	
	function crop_initFixedRatio()
	{
		var ow = parseInt(document.getElementById('input_crop_width').value);
		var oh = parseInt(document.getElementById('input_crop_height').value);
		var ox = parseInt(document.getElementById('input_crop_x').value);
		var oy = parseInt(document.getElementById('input_crop_y').value);

		if(crop_script_fixedRatio > ow/oh ){
			var cw = Math.round(oh * crop_script_fixedRatio);
			if(cw > crop_originalImageWidth){
				var ch = Math.round(crop_originalImageWidth / crop_script_fixedRatio);
				document.getElementById('input_crop_width').value = crop_originalImageWidth;
				document.getElementById('input_crop_y').value = Math.round(
					Math.min(Math.max(0 , oy + (oh - ch)/2), crop_originalImageHeight - ch));
				document.getElementById('input_crop_height').value  = ch;
				document.getElementById('input_crop_x').value = 0;
			}
			else
			{
				document.getElementById('input_crop_x').value = Math.round(
					Math.min(Math.max(0 , ox + (ow - cw)/2), crop_originalImageWidth - cw));
				document.getElementById('input_crop_width').value = cw;
			}
		}else{
			var ch = Math.round(ow / crop_script_fixedRatio);
			if(ch > crop_originalImageHeight){
				var cw = Math.round(crop_originalImageHeight * crop_script_fixedRatio);
				document.getElementById('input_crop_height').value = crop_originalImageHeight;
				document.getElementById('input_crop_x').value = Math.round(
					Math.min(Math.max(0 , ox + (ow - cw)/2), crop_originalImageWidth - cw));
				document.getElementById('input_crop_width').value  = cw;
				document.getElementById('input_crop_y').value = 0;
			}
			else
			{
				document.getElementById('input_crop_y').value = Math.round(
					Math.min(Math.max(0 , oy + (oh - ch)/2), crop_originalImageHeight - ch));
				document.getElementById('input_crop_height').value = ch;
			}
		}
		
			cropScript_setCropSizeByInput();
		filterResolutions();
	}
	
	function init_imageCrop()
	{
		cropScript_setBasicEvents();
		crop_createDivElements();
		cropScript_updateFormValues();
		if(crop_script_fixedRatio && crop_script_alwaysPreserveAspectRatio){
			crop_initFixedRatio();
		}
	}