
// include common file

//document.write("<script type='text/JavaScript' src='js/common.js'></script>");

var picture_folder = 'typo3conf/ext/iwshop/static/img/';
var movingObj = null;
var lastMouseCoords = null;
var corner = '';

function addHorizontalSelection(id, ratio) {

	img = document.getElementById(id);
	if(img == null) return;
	
	imgData = getObjectData(img);

	shown_h = imgData.w*ratio;
	
 	display_x = 0;
	display_y = (imgData.h - shown_h)/2.0;
	display_w = imgData.w;
	display_h = shown_h;
	
	shade_top = document.createElement('div');
	shade_top.id = id+'_shade_top';
	shade_top.style.backgroundColor = 'black';
	shade_top.style.filter = 'alpha(opacity=50)';
	shade_top.style.opacity = 0.5;
	shade_top.style.zoom = 1;
	shade_top.style.position = 'absolute';
	shade_top.style.width = display_w+'px';
	shade_top.style.height = display_y+'px';
	shade_top.style.top = imgData.y+'px';
	shade_top.style.left = imgData.x+'px';
	shade_top.style.borderBottom = 'solid 1px #C9C9C8';
	shade_top.style.backgroundImage = "url('"+picture_folder+'select-up.gif'+"')";
	shade_top.style.backgroundRepeat = 'no-repeat';
	shade_top.style.backgroundPosition = 'bottom center';
	shade_top.style.fontSize = '0px';
	shade_top.style.cursor = 's-resize';
	document.body.appendChild(shade_top);
	
	shade_top.onmousedown = function (e) { startMovingSelection(e, this.id); };
	shade_top.ondragstart = function (e) { return false; };
	shade_top.onmouseout = stopMoving;

	shade_bottom = document.createElement('div');
	shade_bottom.id = id+'_shade_bottom';
	shade_bottom.style.backgroundColor = 'black';
	shade_bottom.style.filter = 'alpha(opacity=50)';
	shade_bottom.style.opacity = 0.5;
	shade_bottom.style.zoom = 1;
	shade_bottom.style.position = 'absolute';
	shade_bottom.style.width = display_w+'px';
	shade_bottom.style.height = display_y+'px';
	shade_bottom.style.top = (imgData.y+display_h+display_y)+'px';
	shade_bottom.style.left = imgData.x+'px';
      shade_bottom.style.borderTop = 'solid 1px #C9C9C8';;
      shade_bottom.style.backgroundImage = "url('"+picture_folder+'select-down.gif'+"')";
	shade_bottom.style.backgroundRepeat = 'no-repeat';
	shade_bottom.style.backgroundPosition = 'top center';
	shade_bottom.style.fontSize = '0px';
	shade_bottom.style.cursor = 'n-resize';
	document.body.appendChild(shade_bottom);
	
	shade_bottom.onmousedown = function (e) { startMovingSelection(e, this.id); };
	shade_bottom.ondragstart = function (e) { return false; };
	shade_bottom.onmouseout = stopMoving;
	
	img.onmousedown = function (e) { startMovingSelection(e, this.id+'_shade_top'); };
	img.ondragstart = function (e) { return false; };
	img.omouseout = stopMoving;
	img.style.cursor = 'ns-resize';

}

function addVerticalSelection(id, ratio) {

	img = document.getElementById(id);
	if(img == null) return;

	imgData = getObjectData(img);

	shown_w = imgData.h*ratio;

	display_x = (imgData.w - shown_w)/2.0;
	display_y = 0;
	display_w = shown_w;
	display_h = imgData.h;

	shade_left = document.createElement('div');
	shade_left.id = id+'_shade_left';
	shade_left.style.backgroundColor = 'black';
	shade_left.style.filter = 'alpha(opacity=50)';
	shade_left.style.opacity = 0.5;
	shade_left.style.zoom = 1;
	shade_left.style.position = 'absolute';
	shade_left.style.width = (display_x-1)+'px';
	shade_left.style.height = display_h+'px';
	shade_left.style.top = imgData.y+'px';
	shade_left.style.left = imgData.x+'px';
	shade_left.style.borderRight = 'solid 1px #C9C9C8';
	shade_left.style.backgroundImage = "url('"+picture_folder+'select-left.gif'+"')";
	shade_left.style.backgroundRepeat = 'no-repeat';
	shade_left.style.backgroundPosition = 'center right';
	shade_left.style.cursor = 'e-resize';
	
	document.body.appendChild(shade_left);

	shade_left.onmousedown = function (e) { startMovingSelection(e, this.id); };
	shade_left.ondragstart = function (e) { return false; };
	shade_left.omouseout = stopMoving;

	shade_right = document.createElement('div');
	shade_right.id = id+'_shade_right';
	shade_right.style.backgroundColor = 'black';
	shade_right.style.filter = 'alpha(opacity=50)';
	shade_right.style.opacity = 0.5;
	shade_right.style.zoom = 1;
	shade_right.style.position = 'absolute';
	shade_right.style.width = (display_x-1)+'px';
	shade_right.style.height = display_h+'px';
	shade_right.style.top = imgData.y+'px';
	shade_right.style.left = (imgData.x+display_w+display_x)+'px';;
      shade_right.style.borderLeft = 'solid 1px #C9C9C8';;
      shade_right.style.backgroundImage = "url('"+picture_folder+'select-right.gif'+"')";
	shade_right.style.backgroundRepeat = 'no-repeat';
	shade_right.style.backgroundPosition = 'center left';
	shade_right.style.cursor = 'w-resize';
	document.body.appendChild(shade_right);

	shade_right.onmousedown = function (e) { startMovingSelection(e, this.id); };
	shade_right.ondragstart = function (e) { return false; };
	shade_right.omouseout = stopMoving;
	
	img.onmousedown = function (e) { startMovingSelection(e, this.id+'_shade_left'); };
	img.ondragstart = function (e) { return false; };
	img.omouseout = stopMoving;
	img.style.cursor = 'ew-resize';
}

function startMovingSelection(e, id) {
	if(!e) e = window.event;
	lastMouseCoords = mouseCoords(e);
	realID = null;
	if(id.indexOf('_shade_top') > 0) realID = id.substr(0, id.length-10);
	if(id.indexOf('_shade_bottom') > 0) realID = id.substr(0, id.length-13);
	if(id.indexOf('_shade_left') > 0) realID = id.substr(0, id.length-11);
	if(id.indexOf('_shade_right') > 0) realID = id.substr(0, id.length-12);
	if(realID == null) return;
	movingObj = realID;
	
	document.body.onmouseup = stopMoving;
	document.body.onmousemove = moveSelection;
	document.body.onmousedown = function (e) { return false; };
	document.body.ondragstart = function (e) { return false; };
}

function stopMoving() {
	document.body.onmouseup = null;
	document.body.onmousemove = null;
	document.body.onmousedown = null;
	document.body.ondragstart = null;
	movingObj = null;
}

function moveSelection(e) {
	if(!e) e = window.event;
	coords = mouseCoords(e);
	dx = coords.x-lastMouseCoords.x;
	dy = coords.y-lastMouseCoords.y;
	lastMouseCoords = coords;
	
	imgData = getObjectData(document.getElementById(movingObj));

	if( (t = document.getElementById(movingObj+'_shade_top')) != null) {
		b = document.getElementById(movingObj+'_shade_bottom');
		t_h = t.style.height.substr(0, t.style.height.length-2)*1;
		b_h = b.style.height.substr(0, b.style.height.length-2)*1;
		if(dy > 0 && b_h > 0) {
		      ddy = min(b_h, dy);
			b.style.top = imgData.y+imgData.h-b_h+ddy+'px'
			b.style.height = b_h-ddy+'px';
			t.style.height = t_h+ddy+'px';
		}
		if(dy < 0 && t_h > 0) {
		      dy *= -1;
		      ddy = min(t_h, dy);
			t.style.height = t_h-ddy+'px';
			b.style.height = b_h+ddy+'px';
   			b.style.top = imgData.y+imgData.h-b_h-ddy+'px'
   			
		}
		
		t_h = t.style.height.substr(0, t.style.height.length-2)*1;
		b_h = b.style.height.substr(0, b.style.height.length-2)*1;

		if(t_h < 2) t.style.display = 'none'; else t.style.display = 'block';
		if(b_h < 2) b.style.display = 'none'; else b.style.display = 'block';
	}
	
	if( (l = document.getElementById(movingObj+'_shade_left')) != null) {
		r = document.getElementById(movingObj+'_shade_right');
		l_w = l.style.width.substr(0, l.style.width.length-2)*1;
		r_w = r.style.width.substr(0, r.style.width.length-2)*1;
		if(dx > 0 && r_w > 0) {
		      ddx = min(r_w, dx);
			r.style.left = imgData.x+imgData.w-r_w+ddx+'px'
			r.style.width = r_w-ddx+'px';
			l.style.width = l_w+ddx+'px';

		}
		if(dx < 0 && l_w > 0) {
		      dx *= -1;
		      ddx = min(l_w, dx);
   			l.style.width = l_w-ddx+'px';
			r.style.width = r_w+ddx+'px';
			r.style.left = imgData.x+imgData.w-r_w-ddx+'px'
		}
		
		l_w = l.style.width.substr(0, l.style.width.length-2)*1;
		r_w = r.style.width.substr(0, r.style.width.length-2)*1;

	}
}

/** *************************************************************************** */

function addFreeSelect(id) {

	img = document.getElementById(id);
	if(img == null) return;
	
	imgData = getObjectData(img);
	
	img.onmousedown = function (e) { corner = '*'; startFreeSelect(e, this.id); }
	// img.onmouseout = stopFreeSelect;
	img.style.cursor = 'move';
	
	shader_left = document.createElement('div');
	shader_left.id = id+'_freeselect_left';
	shader_left.style.position = 'absolute';
// shader_left.style.width = imgData.w*0.1;
	shader_left.style.width = 0;
	shader_left.style.height = imgData.h+'px';
	shader_left.style.left = imgData.x+'px';
	shader_left.style.top = imgData.y+'px';
	shader_left.style.backgroundColor = 'black';
	shader_left.style.filter = 'alpha(opacity=50)';
	shader_left.style.opacity = 0.5;
	shader_left.style.zIndex = 50;
	shader_left.onmousedown = function (e) { corner = '*'; startFreeSelect(e, this.id); }
// shader_left.onmouseout = stopFreeSelect;
	shader_left.style.cursor = 'move';
	shader_left.style.border = 'none';
	document.body.appendChild(shader_left);
	
	shader_right = document.createElement('div');
	shader_right.id = id+'_freeselect_right';
	shader_right.style.position = 'absolute';
// shader_right.style.width = imgData.w*0.1;
	shader_right.style.width = 0;
	shader_right.style.height = imgData.h+'px';
// shader_right.style.left = imgData.x+imgData.w*0.9;
	shader_right.style.left = imgData.x+imgData.w+'px';
	shader_right.style.top = imgData.y+'px';
	shader_right.style.backgroundColor = 'black';
	shader_right.style.filter = 'alpha(opacity=50)';
	shader_right.style.opacity = 0.5;
	shader_right.style.zIndex = 50;
	shader_right.onmousedown = function (e) { corner = '*'; startFreeSelect(e, this.id); }
	shader_right.ondragstart = function (e) { return false; }
// shader_right.onmouseout = stopFreeSelect;
	shader_right.style.cursor = 'move';
	shader_right.style.border = 'none';
	document.body.appendChild(shader_right);

      shader_top = document.createElement('div');
	shader_top.id = id+'_freeselect_top';
	shader_top.style.position = 'absolute';
// shader_top.style.width = imgData.w*0.8;
	shader_top.style.width = imgData.w+'px';
// shader_top.style.height = imgData.h*0.1;
	shader_top.style.height = 0;
// shader_top.style.left = imgData.x+imgData.w*0.1;
	shader_top.style.left = imgData.x+'px';
	shader_top.style.top = imgData.y+'px';
	shader_top.style.backgroundColor = 'black';
	shader_top.style.filter = 'alpha(opacity=50)';
	shader_top.style.opacity = 0.5;
	shader_top.style.zIndex = 50;
	shader_top.style.fontSize = '0px';
	shader_top.onmousedown = function (e) { corner = '*'; startFreeSelect(e, this.id); }
	shader_top.ondragstart = function (e) { return false; }
	shader_top.style.cursor = 'move';
	shader_top.style.border = 'none';
// shader_top.onmouseout = stopFreeSelect;
	document.body.appendChild(shader_top);
	
	shader_bottom = document.createElement('div');
	shader_bottom.id = id+'_freeselect_bottom';
	shader_bottom.style.position = 'absolute';
// shader_bottom.style.width = imgData.w*0.8;
	shader_bottom.style.width = imgData.w+'px';
// shader_bottom.style.height = imgData.h*0.1;
	shader_bottom.style.height = 0;
// shader_bottom.style.left = imgData.x+imgData.w*0.1;
	shader_bottom.style.left = imgData.x+'px';
// shader_bottom.style.top = imgData.y+imgData.h*0.9;
	shader_bottom.style.top = imgData.y+imgData.h+'px';
	shader_bottom.style.backgroundColor = 'black';
	shader_bottom.style.filter = 'alpha(opacity=50)';
	shader_bottom.style.opacity = 0.5;
	shader_bottom.style.zIndex = 50;
	shader_bottom.style.fontSize = '0px';
	shader_bottom.onmousedown = function (e) { corner = '*'; startFreeSelect(e, this.id); }
	shader_bottom.ondragstart = function (e) { return false; }
	shader_bottom.style.cursor = 'move';
	shader_bottom.style.border = 'none';
// shader_bottom.onmouseout = stopFreeSelect;
	document.body.appendChild(shader_bottom);
	
	mover_TL = document.createElement('img');
	mover_TL.id = id+'_freeselect_TL';
	mover_TL.src = picture_folder+'select-corner-full.gif';
	mover_TL.style.position = 'absolute';
// mover_TL.style.top = imgData.y+imgData.h*0.1-3+'px';
	mover_TL.style.top = imgData.y-3+'px';
// mover_TL.style.left = imgData.x+imgData.w*0.1-3+'px';
	mover_TL.style.left = imgData.x-3+'px';
	mover_TL.style.zIndex = 101;
	mover_TL.style.cursor = 'nw-resize';
	mover_TL.onmousedown = function (e) { corner='TL'; startFreeSelect(e, this.id); }
	// mover_TL.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_TL);
	
	mover_T = document.createElement('img');
	mover_T.id = id+'_freeselect_T';
	mover_T.src = picture_folder+'select-corner-full.gif';
	mover_T.style.position = 'absolute';
// mover_T.style.top = imgData.y+imgData.h*0.1-3+'px';
	mover_T.style.top = imgData.y-3+'px';
	mover_T.style.left = imgData.x+imgData.w*0.5-3+'px';
	mover_T.style.zIndex = 101;
	mover_T.style.cursor = 'n-resize';
	mover_T.onmousedown = function (e) { corner='T'; startFreeSelect(e, this.id); }
	// mover_T.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_T);
	
	mover_TR = document.createElement('img');
	mover_TR.id = id+'_freeselect_TR';
	mover_TR.src = picture_folder+'select-corner-full.gif';
	mover_TR.style.position = 'absolute';
// mover_TR.style.top = imgData.y+imgData.h*0.1-3+'px';
	mover_TR.style.top = imgData.y-3+'px';
// mover_TR.style.left = imgData.x+imgData.w*0.9-3+'px';
	mover_TR.style.left = imgData.x+imgData.w-3+'px';
	mover_TR.style.zIndex = 101;
	mover_TR.style.cursor = 'ne-resize';
	mover_TR.onmousedown = function (e) { corner='TR'; startFreeSelect(e, this.id); }
	// mover_TR.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_TR);
	
	mover_L = document.createElement('img');
	mover_L.id = id+'_freeselect_L';
	mover_L.src = picture_folder+'select-corner-full.gif';
	mover_L.style.position = 'absolute';
	mover_L.style.top = imgData.y+imgData.h*0.5-3+'px';
// mover_L.style.left = imgData.x+imgData.w*0.1-3+'px';
	mover_L.style.left = imgData.x-3+'px';
	mover_L.style.zIndex = 101;
	mover_L.style.cursor = 'w-resize';
	mover_L.onmousedown = function (e) { corner='L'; startFreeSelect(e, this.id); }
	// mover_L.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_L);

	mover_R = document.createElement('img');
	mover_R.id = id+'_freeselect_R';
	mover_R.src = picture_folder+'select-corner-full.gif';
	mover_R.style.position = 'absolute';
	mover_R.style.top = imgData.y+imgData.h*0.5-3+'px';
// mover_R.style.left = imgData.x+imgData.w*0.9-3+'px';
	mover_R.style.left = imgData.x+imgData.w-3+'px';
	mover_R.style.zIndex = 101;
	mover_R.style.cursor = 'e-resize';
	mover_R.onmousedown = function (e) { corner='R'; startFreeSelect(e, this.id); }
	// mover_R.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_R);
	
	mover_BL = document.createElement('img');
	mover_BL.id = id+'_freeselect_BL';
	mover_BL.src = picture_folder+'select-corner-full.gif';
	mover_BL.style.position = 'absolute';
// mover_BL.style.top = imgData.y+imgData.h*0.9-3+'px';
	mover_BL.style.top = imgData.y+imgData.h-3+'px';
// mover_BL.style.left = imgData.x+imgData.w*0.1-3+'px';
	mover_BL.style.left = imgData.x-3+'px';
	mover_BL.style.zIndex = 101;
	mover_BL.style.cursor = 'sw-resize';
	mover_BL.onmousedown = function (e) { corner='BL'; startFreeSelect(e, this.id); }
	// mover_BL.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_BL);

	mover_B = document.createElement('img');
	mover_B.id = id+'_freeselect_B';
	mover_B.src = picture_folder+'select-corner-full.gif';
	mover_B.style.position = 'absolute';
// mover_B.style.top = imgData.y+imgData.h*0.9-3+'px';
	mover_B.style.top = imgData.y+imgData.h-3+'px';
	mover_B.style.left = imgData.x+imgData.w*0.5-3+'px';
	mover_B.style.zIndex = 101;
	mover_B.style.cursor = 's-resize';
	mover_B.onmousedown = function (e) { corner='B'; startFreeSelect(e, this.id); }
	// mover_B.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_B);

	mover_BR = document.createElement('img');
	mover_BR.id = id+'_freeselect_BR';
	mover_BR.src = picture_folder+'select-corner-full.gif';
	mover_BR.style.position = 'absolute';
// mover_BR.style.top = imgData.y+imgData.h*0.9-3+'px';
	mover_BR.style.top = imgData.y+imgData.h-3+'px';
// mover_BR.style.left = imgData.x+imgData.w*0.9-3+'px';
	mover_BR.style.left = imgData.x+imgData.w-3+'px';
	mover_BR.style.zIndex = 101;
	mover_BR.style.cursor = 'se-resize';
	mover_BR.onmousedown = function (e) { corner='BR'; startFreeSelect(e, this.id); }
	// mover_BR.onmouseout = stopFreeSelect;
	document.body.appendChild(mover_BR);
	
}

function addSquareSelect(id) {
	addFreeSelect(id);
	
	img = getObjectData( document.getElementById( id ) );
	s = min(img.w, img.h);
	
 	// alert({pageX:img.x+img.w-s, pageY:img.y+img.h-s}.pageX+' :
	// '+{pageX:img.x+img.w-s, pageY:img.y+img.h-s}.pageY);
 	TL = {x:img.x+(img.w-s)/2, y:img.y+(img.h-s)/2};
 	BR = {x:img.x+img.w-(img.w-s)/2, y:img.y+img.h-(img.h-s)/2};
 	setFreeSelectionTo(id, TL, BR, img);

	if((d = document.getElementById(id+'_freeselect_T')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_L')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_B')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_R')) != null) d.parentNode.removeChild(d);
	
	if((d = document.getElementById(id+'_freeselect_TL')) != null) {
		d.onmousedown = function (e) { corner = 'TL_X'; startFreeSelect(e, this.id); };
// d.style.position.top = ty;
// d.style.position.left = lx;
	}
	if((d = document.getElementById(id+'_freeselect_TR')) != null) {
		d.onmousedown = function (e) { corner = 'TR_X'; startFreeSelect(e, this.id); };
// d.style.position.top = ty;
// d.style.position.left = rx;
	}
	if((d = document.getElementById(id+'_freeselect_BL')) != null) {
		d.onmousedown = function (e) { corner = 'BL_X'; startFreeSelect(e, this.id); };
// d.style.position.top = by;
// d.style.position.left = lx;
	}
	if((d = document.getElementById(id+'_freeselect_BR')) != null) {
		d.onmousedown = function (e) { corner = 'BR_X'; startFreeSelect(e, this.id); };
// d.style.position.top = by;
// d.style.position.left = rx;
	}
}

function startFreeSelect(e, id) {
	if(!e) e = window.event;
	lastMouseCoords = mouseCoords(e);
	document.body.onmouseup = stopFreeSelect;
	document.body.onmousedown = function (e) { return false; };
	document.body.onmousemove = changeFreeSelect;
	document.body.ondragstart = function (e) { return false; };
	obj = document.getElementById(id);
	if(obj.tagName.toLowerCase() == 'img' && obj.id.indexOf('_freeselect_') < 0) dragObj = obj;
	else dragObj = document.getElementById( obj.id.substr(0, obj.id.lastIndexOf('_freeselect_') ) );
}

function stopFreeSelect() {
	document.body.onmousemove = null;
	document.body.onmouseup = null;
	document.body.onmousedown = null;
	document.body.ondragstart = null;
	dragObj = null;
}

function changeFreeSelect(e) {
	if(!e) e = window.event;

	coords = mouseCoords(e);
	dx = coords.x - lastMouseCoords.x;
	dy = coords.y - lastMouseCoords.y;
	lastMouseCoords = coords;
	
	if(corner == 'T' || corner == 'B') dx = 0;
	if(corner == 'L' || corner == 'R') dy = 0;

	mainID = dragObj.id;
	img = document.getElementById(mainID);
	imgData = getObjectData(img);

	var TL = {sTop:'',sLeft:'',x:0, y:0};
	var BR = {sTop:'',sLeft:'',x:0, y:0};
	TL.sTop = document.getElementById(mainID+'_freeselect_TL').style.top;
	TL.sLeft = document.getElementById(mainID+'_freeselect_TL').style.left;
	TL.x = TL.sLeft.substr(0, TL.sLeft.length-2)*1+3;
	TL.y = TL.sTop.substr(0, TL.sTop.length-2)*1+3;
	
	BR.sTop = document.getElementById(mainID+'_freeselect_BR').style.top;
	BR.sLeft = document.getElementById(mainID+'_freeselect_BR').style.left;
	BR.x = BR.sLeft.substr(0, BR.sLeft.length-2)*1+3;
	BR.y = BR.sTop.substr(0, BR.sTop.length-2)*1+3;
	
	bx = dx;
	by = dy;
	
	if( (corner == 'TL' || corner == 'L' || corner == 'BL' || corner == '*') && dx < 0) dx = -1*absMin(TL.x-imgData.x, dx);
	if( (corner == 'TR' || corner == 'R' || corner == 'BR' || corner == '*') && dx > 0) dx = min(imgData.x+imgData.w-BR.x, dx);
	if( (corner == 'TL' || corner == 'T' || corner == 'TR' || corner == '*') && dy < 0) dy = -1*absMin(TL.y-imgData.y, -dy);
	if( (corner == 'BL' || corner == 'B' || corner == 'BR' || corner == '*') && dy > 0) dy = min(imgData.y+imgData.h-BR.y, dy);
	
	// document.title = 'Moving corner '+corner;
	if(corner == 'TL' || corner == 'T' || corner == 'TR') { TL.y += dy; if(TL.y >= BR.y) TL.y = BR.y-1; }
	if(corner == 'BL' || corner == 'B' || corner == 'BR') { BR.y += dy; if(TL.y >= BR.y) BR.y = TL.y+1; }
	if(corner == 'TL' || corner == 'L' || corner == 'BL') { TL.x += dx; if(TL.x >= BR.x) TL.x = BR.x-1; }
	if(corner == 'TR' || corner == 'R' || corner == 'BR') { BR.x += dx; if(TL.x >= BR.x) BR.x = TL.x+1; }
	if(corner == '*') {
	      TL.x += dx;
	      BR.x += dx;
	      TL.y += dy;
	      BR.y += dy;
	}
	if(corner == 'TL_X') {
		TL.x = lastMouseCoords.x;
		TL.y = lastMouseCoords.y;
		w = BR.x - TL.x;
		h = BR.y - TL.y;
		if(w != h) {
			w = h = min(w, h);
			if(BR.x - w < imgData.x) w = h = BR.x - imgData.x;
			if(BR.y - h < imgData.y) w = h = BR.y - imgData.y;;
			TL.x = BR.x - w;
			TL.y = BR.y - h;
		}
	}
	if(corner == 'BR_X') {
		BR.x = lastMouseCoords.x;
		BR.y = lastMouseCoords.y;
		w = BR.x - TL.x;
		h = BR.y - TL.y;
		if(w != h) {
			w = h = min(w, h);
			if(TL.x + w > imgData.x+imgData.w) w = h = imgData.x+imgData.w - TL.x;
			if(TL.y + h > imgData.y+imgData.h) w = h = imgData.y+imgData.h - TL.y;
			BR.x = TL.x + w;
			BR.y = TL.y + h;
		}
	}
	if(corner == 'TR_X') {
		BR.x = lastMouseCoords.x;
		TL.y = lastMouseCoords.y;
		w = BR.x - TL.x;
		h = BR.y - TL.y;
		if(w != h) {
			w = h = min(w, h);
			if(TL.x + w > imgData.x+imgData.w) w = h = imgData.x+imgData.w - TL.x;
			if(BR.y - h < imgData.y) w = h = BR.y - imgData.y;
			BR.x = TL.x + w;
			TL.y = BR.y - h;
		}
	}
	if(corner == 'BL_X') {
		TL.x = lastMouseCoords.x;
		BR.y = lastMouseCoords.y;
		w = BR.x - TL.x;
		h = BR.y - TL.y;
		if(w != h) {
			w = h = min(w, h);
			if(BR.x - w < imgData.x) w = h = BR.x - imgData.x;
			if(TL.y + h > imgData.y+imgData.h) w = h = imgData.y+imgData.h - TL.y;
			TL.x = BR.x - w;
			BR.y = TL.y + h;
		}
	}
	/*
	 * if(TL.x < imgData.x) { BR.x += imgData.x-TL.x; TL.x = imgData.x; } if(TL.y <
	 * imgData.y) { BR.y += imgData.y-TL.y; TL.y = imgData.y; } if(BR.x >
	 * imgData.x+imgData.w) { TL.x -= BR.x-imgData.x+imgData.w; BR.x =
	 * imgData.x+imgData.w; } if(BR.y > imgData.y+imgData.h) { TL.y -=
	 * BR.y-imgData.y+imgData.h; BR.y = imgData.y+imgData.h; }
	 */

	setFreeSelectionTo(mainID, TL, BR, imgData);
	
	if(typeof(onCropChange) == 'function') {
		onCropChange(getCropCoords(mainID));
	}
	// document.title = 'Crop area: x='+crop.x+' :: y='+crop.y+' :: w='+crop.w+'
	// :: h='+crop.h;
}

function setFreeSelectionTo(mainID, TL, BR, imgData) {
	var d = document.getElementById(mainID + '_freeselect_left');
	if( d != null ) {
		d.style.width = Math.max(TL.x - imgData.x, 0) + 'px';
	}

	shader_right = document.getElementById(mainID+'_freeselect_right');
	shader_right.style.left = BR.x + 'px';
	shader_right.style.width = Math.max(imgData.x + imgData.w - BR.x, 0) + 'px';

	shader_top = document.getElementById(mainID+'_freeselect_top');
	shader_top.style.height = Math.max(TL.y - imgData.y) + 'px';
	shader_top.style.left = TL.x + 'px';
	shader_top.style.width = BR.x - TL.x + 'px';

	shader_bottom = document.getElementById(mainID+'_freeselect_bottom');
	shader_bottom.style.top = BR.y + 'px';
	shader_bottom.style.height = Math.max(imgData.y + imgData.h - BR.y) + 'px';
	shader_bottom.style.left = TL.x + 'px';
	shader_bottom.style.width = Math.max(BR.x - TL.x) + 'px';

	mover_TL = document.getElementById(mainID + '_freeselect_TL');
	if(mover_TL != null) {
		mover_TL.style.top = TL.y - 3 + 'px';
		mover_TL.style.left = TL.x - 3 + 'px';
	}

	mover_T = document.getElementById(mainID + '_freeselect_T');
	if(mover_T != null) {
		mover_T.style.top = TL.y - 3+'px';
		mover_T.style.left = (TL.x + BR.x) / 2 - 3 + 'px';
	}

	mover_TR = document.getElementById(mainID + '_freeselect_TR');
	if(mover_TR != null) {
		mover_TR.style.top = TL.y - 3 + 'px';
		mover_TR.style.left = BR.x - 3 + 'px';
	}

	mover_L = document.getElementById(mainID + '_freeselect_L');
	if(mover_L != null) {
		mover_L.style.top = (TL.y + BR.y) / 2 - 3 + 'px';
		mover_L.style.left = TL.x - 3 + 'px';
	}

	mover_R = document.getElementById(mainID + '_freeselect_R');
	if(mover_R != null) {
		mover_R.style.top = (TL.y + BR.y) / 2 - 3 + 'px';
		mover_R.style.left = BR.x - 3 + 'px';
	}

	mover_BL = document.getElementById(mainID + '_freeselect_BL');
	if(mover_BL != null) {
		mover_BL.style.top = BR.y - 3 + 'px';
		mover_BL.style.left = TL.x - 3 + 'px';
	}

	mover_B = document.getElementById(mainID + '_freeselect_B');
	if(mover_B != null) {
		mover_B.style.top = BR.y - 3 + 'px';
		mover_B.style.left = (TL.x + BR.x) / 2 - 3 + 'px';
	}

	mover_BR = document.getElementById(mainID + '_freeselect_BR');
	if(mover_BR != null) {
		mover_BR.style.top = BR.y - 3 + 'px';
		mover_BR.style.left = BR.x - 3 + 'px';
	}
}

function removeSelections(id) {
	stopMoving();
	stopFreeSelect();
	
	if((d = document.getElementById(id+'_shade_left')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_shade_top')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_shade_right')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_shade_bottom')) != null) d.parentNode.removeChild(d);
	
	if((d = document.getElementById(id+'_freeselect_left')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_top')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_right')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_bottom')) != null) d.parentNode.removeChild(d);
	
	if((d = document.getElementById(id+'_freeselect_TL')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_T')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_TR')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_L')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_R')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_BL')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_B')) != null) d.parentNode.removeChild(d);
	if((d = document.getElementById(id+'_freeselect_BR')) != null) d.parentNode.removeChild(d);
	
	if((i = document.getElementById(id)) != null) {
		i.style.cursor = 'auto';
		i.onmousedown = null;
	}
}

function getOffsetY(id) {
	t = document.getElementById(id+'_shade_top');
	if(t) {
            return t.style.height.substr(0, t.style.height.length-2)*1;
	}
	return -1;
}

function getOffsetX(id) {
	t = document.getElementById(id+'_shade_left');
	if(t) {
            return t.style.width.substr(0, t.style.width.length-2)*1;
	}
	return -1;
}

function getCropCoords(id) {
	img = getObjectData( document.getElementById( id ) );

	var TL = {sTop:'',sLeft:'',x:0, y:0};
	var BR = {sTop:'',sLeft:'',x:0, y:0};
	TL.sTop = document.getElementById(id+'_freeselect_TL').style.top;
	TL.sLeft = document.getElementById(id+'_freeselect_TL').style.left;
	TL.x = TL.sLeft.substr(0, TL.sLeft.length-2)*1+3;
	TL.y = TL.sTop.substr(0, TL.sTop.length-2)*1+3;

	BR.sTop = document.getElementById(id+'_freeselect_BR').style.top;
	BR.sLeft = document.getElementById(id+'_freeselect_BR').style.left;
	BR.x = BR.sLeft.substr(0, BR.sLeft.length-2)*1+3;
	BR.y = BR.sTop.substr(0, BR.sTop.length-2)*1+3;

	return {x:TL.x-img.x, y:TL.y-img.y, w:BR.x-TL.x, h:BR.y-TL.y};
}

