// С разрешения Д.Мзареуляна // hiero.ru // edited by Benvolio

var BWflag = false;
var HFlipFlag = false;
var VFlipFlag = false;

var Initialized = false;
var scrollToTop = true;

function InitFilters()
{
	if(Initialized) return;
	Initialized = true;
	if(!Prototype.Browser.IE)
		return;
}

function flipBW()
{
	if(!Prototype.Browser.IE)
		return;
	BWflag = !BWflag;
	document.getElementById("mainpic").filters["Gray"].enabled = BWflag;
}

function flipV()
{
	if(!Prototype.Browser.IE)
		return;
	VFlipFlag = !VFlipFlag;
	document.getElementById("mainpic").filters["FlipV"].enabled = VFlipFlag;
}

function flipH()
{
	if(!Prototype.Browser.IE)
		return;
	HFlipFlag = !HFlipFlag;
	document.getElementById("mainpic").filters["FlipH"].enabled = HFlipFlag;
}

function removeFilters()
{
	if(!Prototype.Browser.IE)
		return;
	BWflag = HFlipFlag = VFlipFlag = false;
	document.getElementById("mainpic").filters["Gray"].enabled = false;
	document.getElementById("mainpic").filters["FlipH"].enabled = false;
	document.getElementById("mainpic").filters["FlipV"].enabled = false;
}

var Cropflag = false;
var Gridflag = false;

var CurrentSide = null;
var x0 = 0;
var y0 = 0;

function MouseDown(side, event)
{
	CurrentSide = side;
		if(Prototype.Browser.IE)
		{
			ev = window.event;
			x0 = ev.x;
			y0 = ev.y;
		}
		else
		{
			x0 = event.clientX;
			y0 = event.clientY;
		}
}

function MouseUp()
{
	CurrentSide = null;
}

function MouseMoveIE()
{
	ev = window.event;
	if((CurrentSide == null) || (ev.button != 1)) return;
	dx = ev.x - x0;
	dy = ev.y - y0;

	style = document.all[CurrentSide].style;
	switch (CurrentSide)
	{
		case "top_crop":
			if(style.pixelHeight >= 8) style.pixelHeight += dy;
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "bottom_crop":
			if(style.pixelHeight >= 8) style.pixelHeight -= dy;
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "left_crop":
			if(style.pixelWidth >= 8) style.pixelWidth += dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "right_crop":
			if(style.pixelWidth >= 8) style.pixelWidth -= dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
	}
}
function MouseMove(event)
{
	if(Prototype.Browser.IE)
	{
		MouseMoveIE();
		return;
	}
	if((CurrentSide == null) || (event.button != 0)) return;
	dx = event.clientX - x0;
	dy = event.clientY - y0;

	style = document.getElementById(CurrentSide);
	switch (CurrentSide)
	{
		case "top_crop":
			if(style.clientHeight >= 8) style.height =style.clientHeight+ dy;
			if(style.clientHeight < 8) style.height = 8;
			else {x0 = event.clientX; y0 = event.clientY;}
		break;
		case "bottom_crop":
			if(style.clientHeight >= 8) style.height =style.clientHeight- dy;
			if(style.clientHeight < 8) style.height = 8;
			else {x0 = event.clientX; y0 = event.clientY;}
		break;
		case "left_crop":
			if(style.clientWidth >= 8) style.width =style.clientWidth+ dx;
			if(style.clientWidth < 8) style.width = 8;
			else {x0 = event.clientX; y0 = event.clientY;}
		break;
		case "right_crop":
			if(style.clientWidth >= 8) style.width =style.clientWidth- dx;
			if(style.clientWidth < 8) style.width = 8;
			else {x0 = event.clientX; y0 = event.clientY;}
		break;
	}
}

function SetCrop(left, top, right, bottom)
{
	var l = left;
	var r = right;
	var t = top;
	var b = bottom;
	if(!Cropflag) flipCrop();
	if(Prototype.Browser.IE)
	{
		document.getElementById("left_crop").style.pixelWidth = left + 8;
		document.getElementById("right_crop").style.pixelWidth = right + 8;
		document.getElementById("top_crop").style.pixelHeight = top + 8;
		document.getElementById("bottom_crop").style.pixelHeight = bottom + 8;
	}
	else
	{
		document.getElementById("left_crop").width = left + 8;
		document.getElementById("right_crop").width = right + 8;
		document.getElementById("top_crop").height = top + 8;
		document.getElementById("bottom_crop").height = bottom + 8;
	}

	if(scrollToTop)
		window.scrollTo(0,0);
}

//added to support old remarks with the old crop function
function do_CropImage(Left, Top, Right, Bottom, BW, HFLIP, VFLIP)
{
	SetCrop(Left, Top, Right, Bottom);
}

function resetGrid()
{
	if(!Cropflag)
		return;
	scrollToTop = false;
	SetCrop(0,0,0,0);
	scrollToTop = true;
}

function AddClipping()
{
	if(!Cropflag)
	return;
	
	var ddg = document.getElementById("bottom_crop");
	crL = Prototype.Browser.IE ? document.getElementById("left_crop").style.pixelWidth : document.getElementById("left_crop").clientWidth;
	crT = Prototype.Browser.IE ? document.getElementById("top_crop").style.pixelHeight: document.getElementById("top_crop").clientHeight;
	crR = Prototype.Browser.IE ? document.getElementById("right_crop").style.pixelWidth : document.getElementById("right_crop").clientWidth;
	crB = Prototype.Browser.IE ? document.getElementById("bottom_crop").style.pixelHeight : document.getElementById("bottom_crop").clientHeight;
	if(crL=="") crL=0;
	if(crT=="") crT=0;
	if(crR=="") crR=0;
	if(crB=="") crB=0;
	x1 = parseInt(crL) - 8; if(x1 < 0) x1 = 0;
	y1 = parseInt(crT) - 8; if(y1 < 0) y1 = 0;
	x2 = parseInt(crR) - 8; if(x2 < 0) x2 = 0;
	y2 = parseInt(crB) - 8; if(y2 < 0) y2 = 0;

	s = '<!--CROP('+x1+','+y1+','+x2+','+y2+')-->';

	var el = document.getElementById("comment");
	el.value += s;
	el.focus();

}

function flipCrop()
{
	Cropflag = !Cropflag;
	if(Cropflag)
	{
//		document.all.cropperimg.src = "pics/i_crop_on.gif";
		document.getElementById("cropper").style.display = "";
	}
	else
	{
//		document.all.cropperimg.src = "pics/i_crop.gif";
		document.getElementById("cropper").style.display = "none";
	}
}

function flipGrid()
{
	Gridflag = !Gridflag;

	if(Gridflag)
	{
		if(!Cropflag) flipCrop();
//		document.all.gridimg.src = "pics/i_grid_on.gif";
		document.getElementById("grid").style.visibility = "visible";
	}
	else
	{
//		document.all.gridimg.src = "pics/i_grid.gif";
		document.getElementById("grid").style.visibility = "hidden";
	}
}

//only for firefox, to set the crop on the picture
function setCropPos(obj)
{
	if(Prototype.Browser.IE)
		return;
	var cid = document.getElementById("cropper");
	var objX = obj.offsetLeft;
	while(obj.parentNode != null)
	{
		objX += obj.offsetLeft;
		obj = obj.parentNode;
	}
	cid.style.left = objX-26;
}

function InitCropper(obj)
{
	if(Prototype.Browser.IE)
	{
		document.getElementById("mainpic").style.filter = "Gray FlipH FlipV"
		document.getElementById("mainpic").filters["Gray"].enabled = false;
		document.getElementById("mainpic").filters["FlipH"].enabled = false;
		document.getElementById("mainpic").filters["FlipV"].enabled = false;
	}
	else
		setCropPos(obj);
}
