
var ua = navigator.userAgent.toLowerCase();
var isOpera = ua.indexOf("opera") != -1; 
var isIE = ((ua.indexOf("msie") != -1) && !isOpera && (ua.indexOf("webtv") == -1)); 
var isGecko = ua.indexOf("gecko") != -1;
var isNetscape = ua.indexOf("netscape") != -1;
var isFirefox = ua.indexOf("firefox") != -1;

var DisplayPhotoFormat="0";
var DisplayPhotoFormat1="0";
var PhotoHaveFullFormat=0;
var PhotoHaveFormat1=0;
var DesplFormat=0;
var ImageWidth1=0, ImageHeight1=0, ImageWidth2=0, ImageHeight2=0, FullImageWidth=0, FullImageHeight=0;

function gebi(id) {return document.getElementById(id);}
function absPosition(obj) {
	this.x = 0;
	this.y = 0;
    while(obj) {
		this.x += obj.offsetLeft;
		this.y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return {x:this.x,y:this.y};
}
function defPosition(event) {
	if (document.attachEvent != null) {
		this.x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		this.y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (!document.attachEvent && document.addEventListener) {
		this.x = event.clientX + window.scrollX;
		this.y = event.clientY + window.scrollY;
	}
	return {x:this.x,y:this.y};
}
function countSubstrings(_str, _sub) {
	var count = 0;
	while (_str.indexOf(_sub) != -1) {
		_str = _str.substring(_str.indexOf(_sub) + _sub.length, _str.length);
		count++;
	}
	return count;
}

var BlackWhiteOn=false;

function blackFilter() {
	if(isIE) {
		Filterflag = false;
	    gebi("FilterDiv").style.display = "none";
	    FiltresProp.style.display="none";
	    
	    BlackWhiteOn = !BlackWhiteOn;

		gebi("td_pic_container").style.filter = (document.forms["crop"].bw.value) ? "" : "Gray";
		document.forms["crop"].bw.value = (document.forms["crop"].bw.value) ? "" : "1";
		return false;
	} else return true;
}
function flipvFilter() {
	if(isIE) {
		var cmd = document.forms["crop"].cmd;
		var steps = countSubstrings(cmd.value, "v") % 2;
		gebi("mainpic").style.filter = (steps) ? "" : "FlipV";
		document.forms["crop"].cmd.value += "v";
		return false;
	} else return true;
}
function fliphFilter() {
	if(isIE) {
		var cmd = document.forms["crop"].cmd;
		var steps = countSubstrings(cmd.value, "h") % 2;
		gebi("pic_container").style.filter = (steps) ? "" : "FlipH";
		document.forms["crop"].cmd.value += "h";
		return false;
	} else return true;
}

var rotateArray = [0, 90, 180, 270];
var _rotateArray = [0, -90, -180, -270];
function rotate(side) {
	var cmd = document.forms["crop"].cmd;
	var flipCount = (countSubstrings(cmd.value, "h") + countSubstrings(cmd.value, "v")) % 2;
	var side_count = countSubstrings(cmd.value, side) + 1;
	var unside_count = countSubstrings(cmd.value, ((side == "r") ? "l" : "r"));
	var steps = (side_count - unside_count) % 4;
	if (flipCount) {
		var flag = (side == "l" && steps > 0) || (side == "r" && steps < 0);
	}
	else var flag = ((side == "r" && steps > 0) || (side == "l" && steps < 0));
	var arr = (flag) ? rotateArray : _rotateArray;
	steps = Math.abs(steps);
	gebi("mainpic").rotation = arr[steps];
	cmd.value += side;
	clearCrop();
	Cropflag = false;
	Filterflag = false;
	gebi("FilterDiv").style.display = "none";
	AddInComment.style.display="none";
	FiltresProp.style.display="none";
	return false;
}

var aOperations = new Array();
aOperations["CROP"] = "";

var bLoaded = false;
function resize_handler() {
	var imageToEdit = gebi("mainpic");
	var divToMove = gebi("cropper");
	var divToMove1 = gebi("FilterDiv");
	
	var _x = 8;
	var _y = 8;
	divToMove.style.left = absPosition(imageToEdit).x - _x;
	divToMove.style.top = absPosition(imageToEdit).y - _y;
	divToMove.style.width = imageToEdit.offsetWidth + 2 * _x;
	divToMove.style.height = imageToEdit.offsetHeight + 2 * _y;
	
	divToMove1.style.left = absPosition(imageToEdit).x;
	divToMove1.style.top = absPosition(imageToEdit).y;
	divToMove1.style.width = imageToEdit.offsetWidth;
	divToMove1.style.height = imageToEdit.offsetHeight;

}

var Cropflag = false;
var Gridflag = false;

function flipCrop() {
	Cropflag = !Cropflag;
	if(Cropflag)
	{
	    gebi("cropper").style.display = "";
        AddInComment.style.display = "";
	}
	else
	{
	    clearCrop();
	    AddInComment.style.display="none";
	}
	resize_handler();
	Filterflag = false;
	gebi("FilterDiv").style.display = "none";
	FiltresProp.style.display="none";
}
function clearCrop() {
	gebi("top").style.height = 8;
	gebi("bottom").style.height = 8;
	gebi("left").style.width = 8;
	gebi("right").style.width = 8;
	gebi("top_left").style.height = 8; gebi("top_left").style.width = 8;
	gebi("bottom_left").style.height = 8; gebi("bottom_left").style.width = 8;
	gebi("top_right").style.height = 8; gebi("top_right").style.width = 8;
	gebi("bottom_right").style.height = 8; gebi("bottom_right").style.width = 8;
	initMovingCenter(false);
	gebi("cropper").style.display = "none";
	aOperations["CROP"] = "";
}
function flipGrid() {
	Gridflag = !Gridflag;
	if(Gridflag) {
		if(!Cropflag) flipCrop();
		gebi("grid").src = "images/btn_golden_rule_on.gif";
		gebi("grid").style.visibility = "visible";
	} else {
		gebi("grid").src = "images/btn_golden_rule.gif";
		gebi("grid").style.visibility = "hidden";
	}
}

var CurrentSide = null;
var x0 = 0;
var y0 = 0;
function MouseDown(side, ev) {
	CurrentSide = side;
	ev = ev || window.event;
	x0 = defPosition(ev).x;
	y0 = defPosition(ev).y;
}
function MouseUp() {
	CurrentSide = null;
}
function initMovingCenter(flag) {
	resizeState = flag;
	gebi("mdlblck").style.cursor = (flag) ? "move" : "";
}
function MouseMove(ev) {
	if (CurrentSide == null) return;
	
	var mainPc = gebi("mainpic");
	var ee = mainPc.offsetHeight;
	var tp = gebi("top");
	var bt = gebi("bottom");
	var md = gebi("mdlblck");

	ev = ev || window.event;
	dx = defPosition(ev).x - x0;
	dy = defPosition(ev).y - y0;
	
	var curObj = gebi(CurrentSide);
	if (CurrentSide != "mdlblck") initMovingCenter(true);
	try {
	switch (CurrentSide) {
		case "mdlblck":
			if (!resizeState) return;
			if (dy >= 0 && (gebi("top").offsetHeight + gebi("bottom").offsetHeight + 5 + dy) >= ee) return;
			if (dy < 0  && (gebi("top").offsetHeight + 5 + gebi("bottom").offsetHeight - dy) >= ee) return;
			if (gebi("top").offsetHeight + dy >= 8 && gebi("bottom").offsetHeight - dy >= 8) {
				gebi("top").style.height = gebi("top").offsetHeight + dy;
				gebi("bottom").style.height = gebi("bottom").offsetHeight - dy;
			}
			if (gebi("top").offsetHeight < 8) gebi("top").style.height = 8;
			else if (gebi("bottom").offsetHeight < 8) gebi("bottom").style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			if (gebi("left").offsetWidth + dx >= 8 && gebi("right").offsetWidth - dx >= 8) {
				gebi("left").style.width = gebi("left").offsetWidth + dx;
				gebi("right").style.width = gebi("right").offsetWidth - dx;
			}
			if (gebi("left").offsetWidth < 8) gebi("left").style.width = 8;
			else if (gebi("right").offsetWidth < 8) gebi("right").style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "top":
			if (dy >= 0 && (tp.offsetHeight + bt.offsetHeight + 5 + dy) >= ee) return;
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight + dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "bottom":
			if (dy < 0 && (tp.offsetHeight + 5 + bt.offsetHeight - dy) >=ee) return;
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight - dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "left":
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth + dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "right":
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth - dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "top_left":
			if (dy >= 0 && (tp.offsetHeight + bt.offsetHeight + 5 + dy) >= ee) return;
			curObj = gebi("top");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight + dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("left");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth + dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "bottom_left":
			if (dy < 0 && (tp.offsetHeight + 5 + bt.offsetHeight -dy) >=ee) return;
			curObj = gebi("bottom");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight - dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("left");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth + dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
			
		case "top_right":
			if(dy >= 0 && (tp.offsetHeight + bt.offsetHeight + 5 + dy) >= ee) return;
			curObj = gebi("top");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight + dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("right");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth - dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
			
		case "bottom_right":
			if (dy < 0 && (tp.offsetHeight + 5 + bt.offsetHeight -dy) >=ee) return;
			curObj = gebi("bottom");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight - dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("right");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth - dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;	
	} 
	} catch (all) {}
	AddClipping2();
}
function AddClipping2() {
	var top = (gebi("top").offsetHeight - 8 < 0) ? 0 : gebi("top").offsetHeight - 8;
	var right = (gebi("right").offsetWidth - 8 < 0) ? 0 : gebi("right").offsetWidth - 8;
	var bottom = (gebi("bottom").offsetHeight - 8 < 0) ? 0 : gebi("bottom").offsetHeight - 8;
	var left = (gebi("left").offsetWidth - 8 < 0) ? 0 : gebi("left").offsetWidth - 8;
	var result = top + "," + right + "," + bottom + "," + left;
	aOperations["CROP"] = result;
}

