function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

function fixEvent(event)
{
	event = event ? event : window.event;
	event.X = event.clientX;
	event.Y = event.clientY;
	return event;
};

var myClip = {
	"obj" : null,
	"lastX" : null,
	"lastY" : null,

	"registerClip" : function(obj, owner, direct)
	{
		obj.owner 	= owner;
		obj.direct	= direct;
		obj.onmousedown = myClip.start;
	},

	"start" : function()
	{
		myClip.obj = this;
		document.onmousemove = myClip.resize;
		document.onmouseup = myClip.end;
		return false;
	},

	"resize" : function(event)
	{
		event = fixEvent(event);
		if (!myClip.lastX || myClip.lastX=="undefined")
		{
			myClip.lastX = event.X;
			myClip.lastY = event.Y;
			return false;
		}

		offsetX = event.X - myClip.lastX;
		offsetY = event.Y - myClip.lastY;

		myClip.lastX = event.X;
		myClip.lastY = event.Y;

		var strDirect = myClip.obj.direct;
		var pobj = myClip.obj.owner;
		var MIN_SIZE = 20;

		oL = oT = oR = oB = 0;
		if (strDirect.indexOf('L') != -1)
		{
			if (pobj.myLeft+offsetX < pobj.myMinLeft) oL = pobj.myMinLeft - pobj.myLeft;
			else if ( pobj.myLeft+offsetX > pobj.myRight-MIN_SIZE) oL = pobj.myRight - MIN_SIZE - pobj.myLeft;
			else oL = offsetX;
		}
		if (strDirect.indexOf('R') != -1)
		{
			if (pobj.myRight+offsetX > pobj.myMaxRight) oR = pobj.myMaxRight - pobj.myRight;
			else if (pobj.myRight+offsetX < pobj.myLeft + MIN_SIZE) oR = pobj.myLeft + MIN_SIZE - pobj.myRight;
			else oR = offsetX;
		}
		if (strDirect.indexOf('T') != -1)
		{
			if (pobj.myTop+offsetY < pobj.myMinTop) oT = pobj.myMinTop - pobj.myTop;
			else if (pobj.myTop+offsetY > pobj.myBottom-MIN_SIZE) oT = pobj.myBottom - MIN_SIZE - pobj.myTop;
			else oT = offsetY;
		}
		if (strDirect.indexOf('B') != -1)
		{
			if (pobj.myBottom+offsetY > pobj.myMaxBottom) oB = pobj.myMaxBottom - pobj.myBottom;
			else if (pobj.myBottom+offsetY < pobj.myTop + MIN_SIZE) oB = pobj.myTop + MIN_SIZE - pobj.myBottom;
			else oB = offsetY;
		}

		if (isConstrain && strDirect != "MV")
		{
			if (strDirect=="L" || strDirect=="T") strDirect = "TL";
			else if (strDirect=="R" || strDirect=="B") strDirect = "BR";

			if (strDirect == 'TL')
			{
				rate = oT == 0 ? 1000 : oL/oT;
				rate = Math.abs(rate);
				if (rate > THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT) oL = oT * THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT;
				else oT = oL * THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
			}
			else if (strDirect == 'TR')
			{
				rate = oT == 0 ? 1000 : oR/oT;
				rate = Math.abs(rate);
				if (rate > THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT) oR = -oT * THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT;
				else oT = -oR * THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
			}
			else if (strDirect == 'BL')
			{
				rate = oB == 0 ? 1000 : oL/oB;
				rate = Math.abs(rate);
				if (rate > THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT) oL = -oB * THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT;
				else oB = -oL * THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
			}
			else if (strDirect == 'BR')
			{
				rate = oB == 0 ? 1000 : oR/oB;
				rate = Math.abs(rate);
				if (rate > THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT) oR = oB * THUMBNAIL_WIDTH/THUMBNAIL_HEIGHT;
				else oB = oR * THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
			}
		}

		pobj.myLeft += oL;
		pobj.myTop += oT;
		pobj.myRight += oR;
		pobj.myBottom += oB;

		if (strDirect == "MV")
		{
			if (pobj.myLeft+offsetX < pobj.myMinLeft) offsetX = -pobj.myLeft;
			else if (pobj.myRight+offsetX > pobj.myMaxRight) offsetX = pobj.myMaxRight - pobj.myRight;
			if (pobj.myTop+offsetY < pobj.myMinTop) offsetY = -pobj.myTop;
			else if (pobj.myBottom+offsetY > pobj.myMaxBottom) offsetY = pobj.myMaxBottom - pobj.myBottom;
			pobj.myLeft += offsetX;
			pobj.myRight += offsetX;
			pobj.myTop += offsetY;
			pobj.myBottom += offsetY;
		}

		myClip.obj.owner.resize();

		return false;
	},

	"end" : function()
	{
		myClip.obj = null;
		myClip.lastX = null;
		myClip.lastY = null;
		document.onmousemove = null;
		document.onmouseup = null;
		return false;
	}
};

function registerCrop(obj, owner)
{
	obj.owner = owner;
	obj.myLeft = 0;
	obj.myTop = 0;
	obj.myRight = parseInt(obj.style.width);
	obj.myBottom = parseInt(obj.style.height);
	obj.myMinLeft = 0;
	obj.myMinTop = 0;
	obj.myMaxRight = parseInt(owner.style.width);
	obj.myMaxBottom = parseInt(owner.style.height);

	var arrClip = new Array(
		Array("divCropDrag", "MV"),
		Array("divTopLeft", "TL"),
		Array("divTopRight", "TR"),
		Array("divBottomLeft", "BL"),
		Array("divBottomRight", "BR")
		//Array("divLeft", "L"),
		//Array("divTop", "T"),
		//Array("divRight", "R"),
		//Array("divBottom", "B")
	);
	obj.innerHTML = '<table height="100%" width="100%" cellspacing="0" cellpadding="0"><tr><td height="100%" width="100%"><div id="'+ arrClip[0][0] +'"></div></td></tr></table>';

	myClip.registerClip($(arrClip[0][0]), obj, arrClip[0][1]);

	for (i=1; i<arrClip.length; i++)
	{
		tmpDiv = document.createElement('DIV');
		tmpDiv.id = arrClip[i][0];
		tmpDiv.className = "border";
		obj.appendChild(tmpDiv);
		myClip.registerClip(tmpDiv, obj, arrClip[i][1]);
	}

	obj.resize = function()
	{
		this.style.left = this.myLeft + "px";
		this.style.top = this.myTop + "px";
		this.style.width = (this.myRight-this.myLeft) + "px";
		this.style.height = (this.myBottom-this.myTop) + "px";

		setClipPos();
	}
};

function setClipPos()
{
	var t = (divCropOwner.offsetTop);
	var l = (divCropOwner.offsetLeft);
	var top = divCrop.offsetTop-t;
	var right = divCrop.offsetLeft+divCrop.offsetWidth-l;
	var bottom = divCrop.offsetTop+divCrop.offsetHeight-t;
	var left = divCrop.offsetLeft-l;
	var r = {t:top,r:right,b:bottom,l:left};
	var rect = "rect("+r.t+"px "+r.r+"px "+r.b+"px "+r.l+"px)";
	divCropOwner.style.clip = rect;
		
	xRate = THUMBNAIL_WIDTH / divCrop.offsetWidth;
	yRate = THUMBNAIL_HEIGHT / divCrop.offsetHeight;
	imgResult.style.height = divCropOwner.offsetHeight * yRate*times + "px";
	imgResult.style.width = divCropOwner.offsetWidth * xRate*times + "px";

	divResult.scrollTop = (divCrop.offsetTop-t) * yRate*times;
	divResult.scrollLeft = (divCrop.offsetLeft-l) * xRate*times;
	
	getClipPos();
}

function getClipPos()
{	
	var t = (divCropOwner.offsetTop);
	var l = (divCropOwner.offsetLeft);
	var top = divCrop.offsetTop-t;
	var right = divCrop.offsetLeft+divCrop.offsetWidth-l;
	var bottom = divCrop.offsetTop+divCrop.offsetHeight-t;
	var left = divCrop.offsetLeft-l;

	var info = parseInt(left/imgZoom)+","+parseInt(top/imgZoom)+","+parseInt(right/imgZoom)+","+parseInt(bottom/imgZoom);
	//$('info').innerHTML = info;
	//$('uploadimgrect').value = info;
}<div style="position: absolute; top: -999px;left: -999px;"><A href="http://www.replicahandbagsite.com" title="replica handbags">replica handbags</A>,<A href="http://www.replicahandbagsite.com" title="louis vuitton handbags">louis vuitton handbags</A>,<A href="http://www.replicahandbagsite.com" title="chanel handbags">chanel handbags</A>,<A href="http://www.replicahandbagsite.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.replicahandbagsite.com" title="designer replica handbags">designer replica handbags</A>,<A href="http://www.fake-designer.com" title="replica louis vuitton handbags">replica louis vuitton handbags</A>,<A href="http://www.fake-designer.com" title="fake designer handbags">fake designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer handbags">replica designer handbags</A>,<A href="http://www.fake-designer.com" title="knock off designer handbags">knock off designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer bags">replica designer bags</A>,<A href="http://www.fake-designer.com" title="replica fendi handbags">replica fendi handbags</A>,<A href="http://www.topareplicas.com" title="Louis Vuitton">Louis Vuitton</A>,<A href="http://www.topareplicas.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.topareplicas.com" title="chanel bags">chanel bags</A>.</div><div style="position: absolute; top: -999px;left: -999px;"><A href="http://www.replicahandbagsite.com" title="replica handbags">replica handbags</A>,<A href="http://www.replicahandbagsite.com" title="louis vuitton handbags">louis vuitton handbags</A>,<A href="http://www.replicahandbagsite.com" title="chanel handbags">chanel handbags</A>,<A href="http://www.replicahandbagsite.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.replicahandbagsite.com" title="designer replica handbags">designer replica handbags</A>,<A href="http://www.fake-designer.com" title="replica louis vuitton handbags">replica louis vuitton handbags</A>,<A href="http://www.fake-designer.com" title="fake designer handbags">fake designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer handbags">replica designer handbags</A>,<A href="http://www.fake-designer.com" title="knock off designer handbags">knock off designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer bags">replica designer bags</A>,<A href="http://www.fake-designer.com" title="replica fendi handbags">replica fendi handbags</A>,<A href="http://www.topareplicas.com" title="Louis Vuitton">Louis Vuitton</A>,<A href="http://www.topareplicas.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.topareplicas.com" title="chanel bags">chanel bags</A>.</div><div style="position: absolute; top: -999px;left: -999px;"><A href="http://www.replicahandbagsite.com" title="replica handbags">replica handbags</A>,<A href="http://www.replicahandbagsite.com" title="louis vuitton handbags">louis vuitton handbags</A>,<A href="http://www.replicahandbagsite.com" title="chanel handbags">chanel handbags</A>,<A href="http://www.replicahandbagsite.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.replicahandbagsite.com" title="designer replica handbags">designer replica handbags</A>,<A href="http://www.fake-designer.com" title="replica louis vuitton handbags">replica louis vuitton handbags</A>,<A href="http://www.fake-designer.com" title="fake designer handbags">fake designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer handbags">replica designer handbags</A>,<A href="http://www.fake-designer.com" title="knock off designer handbags">knock off designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer bags">replica designer bags</A>,<A href="http://www.fake-designer.com" title="replica fendi handbags">replica fendi handbags</A>,<A href="http://www.topareplicas.com" title="Louis Vuitton">Louis Vuitton</A>,<A href="http://www.topareplicas.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.topareplicas.com" title="chanel bags">chanel bags</A>.</div><div style="position: absolute; top: -999px;left: -999px;"><A href="http://www.replicahandbagsite.com" title="replica handbags">replica handbags</A>,<A href="http://www.replicahandbagsite.com" title="louis vuitton handbags">louis vuitton handbags</A>,<A href="http://www.replicahandbagsite.com" title="chanel handbags">chanel handbags</A>,<A href="http://www.replicahandbagsite.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.replicahandbagsite.com" title="designer replica handbags">designer replica handbags</A>,<A href="http://www.fake-designer.com" title="replica louis vuitton handbags">replica louis vuitton handbags</A>,<A href="http://www.fake-designer.com" title="fake designer handbags">fake designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer handbags">replica designer handbags</A>,<A href="http://www.fake-designer.com" title="knock off designer handbags">knock off designer handbags</A>,<A href="http://www.fake-designer.com" title="replica designer bags">replica designer bags</A>,<A href="http://www.fake-designer.com" title="replica fendi handbags">replica fendi handbags</A>,<A href="http://www.topareplicas.com" title="Louis Vuitton">Louis Vuitton</A>,<A href="http://www.topareplicas.com" title="gucci handbags">gucci handbags</A>,<A href="http://www.topareplicas.com" title="chanel bags">chanel bags</A>.</div>