var BWflag = false;
var Initialized = false;
function InitFilters()
{
	if(Initialized) return;
	Initialized = true;
	document.all.cfoto.style.filter = "Gray";
	document.all.cfoto.filters["Gray"].enabled = BWflag;
}

function flipBW()
{
	InitFilters();
	BWflag = !BWflag;
	document.all.cfoto.filters["Gray"].enabled = BWflag;
//	if(BWflag) document.all.bw.src = "/pics/i_bw_on.gif";
//	else document.all.bw.src = "/pics/i_bw.gif";
}

var Cropflag = false;
var Gridflag = false;

var CurrentSide = null;
var x0 = 0;
var y0 = 0;

function MouseDown(side)
{
	CurrentSide = side;
	ev = window.event;
	x0 = ev.x;
	y0 = ev.y;
}

function MouseUp()
{
	CurrentSide = null;
}

function MouseMove()
{
	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":
			if(style.pixelHeight >= 8) style.pixelHeight += dy;
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "bottom":
			if(style.pixelHeight >= 8) style.pixelHeight -= dy;
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "left":
			if(style.pixelWidth >= 8) style.pixelWidth += dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "right":
			if(style.pixelWidth >= 8) style.pixelWidth -= dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
	}
}

function SetCrop(left, top, right, bottom)
{
	if(!Cropflag) flipCrop();

	document.all.left.style.pixelWidth = left + 8;
	document.all.right.style.pixelWidth = right + 8;
	document.all.top.style.pixelHeight = top + 8;
	document.all.bottom.style.pixelHeight = bottom + 8;

	window.scrollTo(0,0);
}

function AddClipping()
{
	x1 = document.all.left.style.pixelWidth - 8; if(x1 < 0) x1 = 0;
	y1 = document.all.top.style.pixelHeight - 8; if(y1 < 0) y1 = 0;
	x2 = document.all.right.style.pixelWidth - 8; if(x2 < 0) x2 = 0;
	y2 = document.all.bottom.style.pixelHeight - 8; if(y2 < 0) y2 = 0;

	s = '<!--CROP('+x1+','+y1+','+x2+','+y2+')-->';

	document.addcomm.commentbody.value += s;
	document.addcomm.commentbody.focus();
}

function flipCrop()
{
	Cropflag = !Cropflag;
	
	if(Cropflag)
	{
		document.all.cropperimg.src = "pics/i_crop_on.gif";
		document.all.cropper.style.display = "";
	}
	else
	{
		document.all.cropperimg.src = "pics/i_crop.gif";
		document.all.cropper.style.display = "none";
	}
	
}



function flipGrid()
{
	Gridflag = !Gridflag;

	if(Gridflag)
	{
		if(!Cropflag) flipCrop();
		document.all.gridimg.src = "pics/i_grid_on.gif";
		document.all.grid.style.visibility = "visible";
	}
	else
	{
		document.all.gridimg.src = "pics/i_grid.gif";
		document.all.grid.style.visibility = "hidden";
	}
}

function ifit() 
{
   var wpObj = document.getElementById("cfoto");
         wp_w = wpObj.width;
   	 wp_h = wpObj.height;
	 
	 
   var inner_w = window.document.body.clientWidth ? window.document.body.clientWidth : window.innerWidth;
   var inner_h = window.innerHeight ? window.innerHeight : window.document.body.clientHeight;

   var optimal_w  = (inner_w > wp_w) ? wp_w : inner_w;
   var optimal_h = Math.round(optimal_w*wp_h/wp_w);
      
   if (!optimal_h) optimal_h=0;
   if (optimal_h > inner_h)
     {
	optimal_h = inner_h - 35;
	optimal_w = Math.round(optimal_h*wp_w / wp_h);
	if (!optimal_w) optimal_w=0;
     }
						   
   wpObj.style.display = "none";
   wpObj.style.width = optimal_w;
   wpObj.style.height = optimal_h;
   wpObj.style.display = "inline";
  
   var orgObj = document.getElementById("org");
   orgObj.style.display = "inline";
   var fitObj = document.getElementById("fit");
   fitObj.style.display = "none";
}
						   
function irestore()
{
   var wpObj = document.getElementById("cfoto");
   wpObj.style.width = wp_w;

   wpObj.style.height = wp_h;
   var fitObj = document.getElementById("fit");
   fitObj.style.display = "inline";
   var orgObj = document.getElementById("org");
   orgObj.style.display = "none";
}
						


