var previewCropImage = false;
var imageCropDataForm = false;
var cropImageWithAjax = false;
var imageCropValsIni = false;

var cropImageScale = 1;
var ICcroppingimage = 0;
var CIXpos = 1;
var CIYpos = 1;
var imageid = 0;
var imageCropMaxHeight = 1000;
var imageCropMaxWidth = 1000;

var CIx0 = 0;
var CIx1 = 0;
var CIy0 = 0;
var CIy1 = 0;

var path = ''
var appid = ''
var table = '';
var mode = ''
var xierpaevent = ''

function setCropXierpaValues(p, a, i, t, m, e, x, y, w, h) {
/*
 * Sets up Xierpa-specific values.
 */
	path = p;
	appid = a;
	imageid = i;
	table = t;
	mode = m;
	xierpaevent = 'cropimage';
	//CIx0 = x;
	//CIx1 = y;
	//CIy0 = w;
	//CIy1 = h;
	initImageCrop();
}

function sendImageCropData2AjaxHandler(event, x, y, w, h) {
/**
 *
 */
	dimensions = x + ',' + y + ',' + w + ',' + h;
	target = '';

	if (event == 1) {
		AjaxDispatcher.ajaxlink(path, appid, table, imageid, mode, dimensions, xierpaevent, target);
	}
}

function initImageCrop() {
/*
 *
 */
	document.onmousedown = imageCropCoordCheck;
	document.onmousemove = imageCropMoveHandler;

	imageCropMarkX = objectXpos('imagecropmarkdiv2image');
	imageCropMarkY = objectYpos('imagecropmarkdiv2image');

	imageCropMarker = document.createElement('div');
	imageCropMarker.setAttribute('id','theimagecropmarker');
	imageCropMarker.style.position = 'absolute';
	imageCropMarker.style.left = '-1px';
	imageCropMarker.style.top = '-1px';
	imageCropMarker.style.width = '0px';
	imageCropMarker.style.height = '0px';
	imageCropMarker.style.borderWidth = '1px';
	imageCropMarker.style.borderStyle = 'solid';
	imageCropMarker.style.borderColor = '#ffcc00';
	imageCropMarker.style.fontSize = '2px';
	imageCropMarker.style.zIndex = '2000';
	imageCropMarker.style.visibility = 'visible';

	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && imageCropMarker.attachEvent) {
		imageCropMarker.attachEvent("onclick", imageCropDeselectArea);
	} else {
		imageCropMarker.addEventListener('click', imageCropDeselectArea, true);
	}

	document.body.appendChild(imageCropMarker);
	imageCropMarkX = objectXpos('imagecropmarkdiv2image');
	imageCropMarkY = objectYpos('imagecropmarkdiv2image');
	imageCropPreview = findobject('theimagecroppreview');
	imageCropImage = findobject('theimagecropcontainer');

	if (!imageCropImage)
		return

	imageCropMaxWidth = parseInt(imageCropImage.style.width);
	imageCropMaxHeight = parseInt(imageCropImage.style.height);

	if (!cropImageWithAjax) {
		icFormDetecter = findobject('imageCropSecretDetecter');
		imageCropDataForm = icFormDetecter.form;
		if (imageCropDataForm) {
			imageCropInputFields = findobject('imageCropVals');
			imageCropInputRadios = findobject('imageCropWhicharea');
			ICwhich = 0;

			if (imageCropInputRadios.length) {
				for (i = 0; i < imageCropInputRadios.length; i++) {
					if (imageCropInputRadios[i].checked) {
						ICwhich = i;
					}
				}
			}
			imageCropShowArea(ICwhich);
		} else {
			alert('Could not find the form to collect cropping data.\n ImageCropping will not work.');
		}
	} else if (imageCropValsIni) {
		imageCropShowAreaFromValue(imageCropValsIni);
	}
}

function imageCropInputRadiosObject(v) {
	if (imageCropInputRadios.length && (imageCropInputRadios.length > v))
		return imageCropInputRadios[v];
	else
		return imageCropInputRadios;
}

function imageCropInputFieldsObject(v) {
	if (imageCropInputFields.length && (imageCropInputFields.length > v)) {
		return imageCropInputFields[v];
	} else {
		return imageCropInputFields;
	}
}

function imageCropSetMaxArea(v, crops) {
	imageCropInputFieldsObject(v).value = crops;
	imageCropShowArea(v);
}

function imageCropMoveHandler(e) {
/*
 * Show bounding box when moving.
 */
	if (ICcroppingimage == 1) {

		if (is.ns) {
			CIXpos = e.pageX;
			CIYpos = e.pageY;
		}

		if (is.ie) {
			CIXpos = parseInt(event.x);
			CIYpos = parseInt(event.y);
		}

		CIXpos -= imageCropMarkX;
		CIYpos -= imageCropMarkY;
		CIxw = CIXpos - CIx0;
		CIx1 = CIx0;

		if (CIxw < 0) {
			CIxw = (-1) * CIxw;
			CIx1= CIx0 - CIxw;
		}

		CIyh = CIYpos - CIy0;
		CIy1 = CIy0;

		if (CIyh < 0) { 
			CIyh = (-1) * CIyh;
			CIy1 = CIy0 - CIyh;
		}

		if ((CIy1 + CIyh <= imageCropMaxHeight) & (CIx1 + CIxw <= imageCropMaxWidth) & (CIx1 >= 0) & (CIy1 >= 0))
			imageCropReShow();
	}
}

function imageCropShowArea(v) {
	ICwhich = v;
	imageCropInputRadiosObject(ICwhich).checked = true;
	thisvalue = imageCropInputFieldsObject(ICwhich).value;
	if (thisvalue!="") {
		imageCropShowAreaFromValue(thisvalue);
		}
	else imageCropDeselectArea();
}

function imageCropShowAreaFromValue(thisvalue) {
	a = 0;
	b = "";
	c = new Array(20, 40, 30, 100);

	for (i = 0; i < thisvalue.length; i++) {
		if (thisvalue.charAt(i) != ",")
			b = b + thisvalue.charAt(i);

		if ((thisvalue.charAt(i) == ",") || (i == thisvalue.length - 1)) {
			c[a] = b;
			b = "";
			a++;
		}
	}

	c[0] = parseInt(c[0] / cropImageScale);
	c[1] = parseInt(c[1] / cropImageScale);
	c[2] = parseInt(c[2] / cropImageScale);
	c[3] = parseInt(c[3] / cropImageScale);

	if (c[0] < 0) { c[0] = 0; }
	if (c[0] > imageCropMaxWidth) { c[0] = imageCropMaxWidth; }
	if (c[1] < 0) { c[1] = 0; }
	if (c[1] > imageCropMaxHeight) { c[1] = imageCropMaxHeight; }
	if (c[2] < 0) { c[2] = 1; }
	if (c[2] > imageCropMaxWidth) { c[2] = imageCropMaxWidth;}
	if (c[3] < 0) { c[3] = 1; }
	if (c[3] > imageCropMaxHeight) { c[3] = imageCropMaxHeight;}
	if (c[0] + c[2] > imageCropMaxWidth) { c[2] = imageCropMaxWidth - c[0];}
	if (c[1] + c[3] > imageCropMaxHeight) { c[3] = imageCropMaxHeight - c[1];}

	if (typeof(window.ICwhich) != 'undefined') {
		imageCropInputFieldsObject(ICwhich).value = parseInt(cropImageScale * c[0]) + ',' + parseInt(cropImageScale * c[1]) + ',' + parseInt(cropImageScale * c[2]) + ',' + parseInt(cropImageScale * c[3]);
	}

	CIx1 = c[0];
	CIy1 = c[1];
	CIxw = c[2];
	CIyh = c[3];
	imageCropReShow();
	previewCropImagePreview(imageCropDataForm);
}

function imageCropMarkerHide() {
	showhideobject(imageCropMarker,'','hide');
	imageCropMarker.style.width = '0px';
	imageCropMarker.style.height = '0px';
}

function imageCropDeselectArea() {
	if (ICcroppingimage == 0) {
		imageCropMarkerHide();
		imageCropInputFieldsObject(ICwhich).value = '';
	} else {
		imageCropCheckArea();
	}
}

function imageCropSave() { 
	f = imageCropDataForm;
	if (f['saveImageCropPath'] && f['saveImageCropWidth'])
		getform2htmlpageupdate('/_root/_lib2/imagecroppreview/-/gethtmlupdate', false, f, false, false); 
}

function previewCropImagePreview() { 
	if (imageCropDataForm) {
		imageCropDataForm['imageCropAction'].value = 'previewimage';
		if (previewCropImage)
			getform2htmlpageupdate('/_root/_lib2/imagecroppreview/-/gethtmlupdate',
				'theimagecroppreview', imageCropDataForm, false, false);
		}
}

function imageCropCheckArea() {
	select = (typeof(window.ICwhich) != 'undefined') ? true : false;

	if (ICcroppingimage == 1) {
		ICcroppingimage = 0;
		x = parseInt(cropImageScale * CIx1);
		y = parseInt(cropImageScale * CIy1);
		w = parseInt(cropImageScale * CIxw);
		h = parseInt(cropImageScale * CIyh)

		if (cropImageWithAjax && window.sendImageCropData2AjaxHandler) { 
			sendImageCropData2AjaxHandler(1, x, y, w, h, imageid, table, mode); 
		} else if (select&&imageCropDataForm) {
			thisvalue = x+','+y+','+w+','+h;
			imageCropInputFieldsObject(ICwhich).value = thisvalue;
		} else alert('Expected js function "sendImageCropData2AjaxHandler(event, x, y, w, h, i, t, m)" is not found or cropImageWithAjax is false')

		previewCropImagePreview(imageCropDataForm);

	} else {
		ICcroppingimage = 1;
	}

	if (ICcroppingimage == 1) {
		imageCropMarker.style.width = '0px';
		imageCropMarker.style.height = '0px';
		CIx0 = clickx - imageCropMarkX;
		CIy0 = clicky - imageCropMarkY;
		imageCropMarker.style.left = CIx0 + 'px';
		imageCropMarker.style.top = CIy0 + 'px';
		x = parseInt(cropImageScale * CIx0);
		y = parseInt(cropImageScale * CIy0);

		if (cropImageWithAjax && window.sendImageCropData2AjaxHandler) { 
			sendImageCropData2AjaxHandler(0, x, y); 
		} else if (select&&imageCropDataForm){
			imageCropInputFieldsObject(ICwhich).value = x + ',' + y;
		}
	}
	showhideobject(imageCropMarker,'','show');
}

function imageCropReShow() {
	imageCropMarker.style.left = (imageCropMarkX + CIx1) + 'px';
	imageCropMarker.style.top = (imageCropMarkY + CIy1) + 'px';
	imageCropMarker.style.width = CIxw + 'px';
	imageCropMarker.style.height = CIyh + 'px';
}

function imageCropCoordCheck(e) {
	if (is.ns) {
		clickx = e.pageX;
		clicky = e.pageY;
	} else if (is.ie) {
		clickx = parseInt(event.x);
		clicky = parseInt(event.y);
	}
}

function imageCropValuesCollect(f) {
	collect = f['imageCropValues']
}

function load() {
	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
		window.attachEvent("onload", initImageCrop);
	} else {
		window.addEventListener('load', initImageCrop, true);
	}
}

