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 - 100;
   wpObj.style.height = optimal_h - 100;
   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";
}
						

function getWindowDimensions()
{
	var inner_w;
	var inner_h;
	
	if (typeof(window.innerWidth) != 'undefined')
	{
		inner_w = window.innerWidth;
		inner_h = window.innerHeight;
	}
	else if (window.document.body != null && typeof(window.document.body.clientWidth) != 'undefined')
	{
		inner_w = window.document.body.clientWidth;
		inner_h = window.document.body.clientHeight;
	}
	else
	{
		inner_w = document.documentElement.clientHeight
		inner_h = document.documentElement.clientWidth;
	}
	var ret = {w:inner_w, h:inner_h}

	return ret;
}

resized = false;
function getOptimalDimensions(img)
{
	var wpObj = img;
    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 - 180;
	optimal_w = Math.round(optimal_h*wp_w / wp_h);
	if (!optimal_w) optimal_w=0;
   }
   return {w:optimal_w, h:optimal_h};
}

function fitImgToWindow()
{
	try
	{
		if (resized == true)
	   	{
	   		console.log('Image is ALREADY resized, skipping resize.');
	   		return;
	   	}
	   	var wpObj = $('sirImg');
	    wp_w = wpObj.width;
	   	wp_h = wpObj.height;
	   	console.log('==== resizing ' + wpObj.src);
		console.log('image size: ' + wp_w + 'x' + wp_h); 
		 
	   var inner_w = window.document.body.clientWidth ? window.document.body.clientWidth : window.innerWidth;
	   var inner_h = window.innerHeight ? window.innerHeight : window.document.body.clientHeight;
	
	   console.log('inner window: ' + inner_w + 'x' + inner_h);
	   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 - 180;
		optimal_w = Math.round(optimal_h*wp_w / wp_h);
		if (!optimal_w) optimal_w=0;
	   }
	   console.log('optimal size: ' + optimal_w + 'x' + optimal_h);						   
	   wpObj.style.display = "none";
	   wpObj.style.width = optimal_w;
	   wpObj.style.height = optimal_h;
	   wpObj.width = optimal_w;
	   wpObj.height = optimal_h;
	   wpObj.style.display = "";
	  
	   resized = true;
	   
	   //var orgObj = document.getElementById("org");
	   //orgObj.style.display = "inline";
	   //var fitObj = document.getElementById("fit");
	   //fitObj.style.display = "none";
	}
	catch (e)
	{
		console.warn('Error in fitImgToWindow: ' + e.message);
	}
}
function restoreImg()
{
	try
	{
	   if (resized == false)
	   {
	   		//console.debug('Image is not resized, skipping restore.');
	   		return;
	   }
	   //else 
	   //	console.debug('== Restoring image size');
	   var wpObj = document.getElementById("sirImg");
	   wpObj.style.width = wp_w;
	   wpObj.style.height = wp_h;
	   wpObj.width = wp_w;
	   wpObj.height = wp_h;
	  
	   
	   //var fitObj = document.getElementById("fit");
	   //fitObj.style.display = "inline";
	   //var orgObj = document.getElementById("org");
	   //orgObj.style.display = "none";
	   
	   resized = false;
   }
   catch (e)
	{
		console.warn('Error in fitImgToWindow: ' + e.message);
	}
}
function fitCurrentImageToWindow()
{
   if (resized == true)
   {
   		console.log('Image is ALREADY resized, skipping resize.');
   		return;
   }
	var wpObj = document.getElementById("lightboxImage");
    wp_w = wpObj.width;
   	wp_h = wpObj.height;
   	console.log('==== resizing ' + wpObj.src);
	console.log('image size: ' + wp_w + 'x' + wp_h); 
	 
   var inner_w = window.document.body.clientWidth ? window.document.body.clientWidth : window.innerWidth;
   var inner_h = window.innerHeight ? window.innerHeight : window.document.body.clientHeight;

   console.log('inner window: ' + inner_w + 'x' + inner_h);
   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 - 180;
	optimal_w = Math.round(optimal_h*wp_w / wp_h);
	if (!optimal_w) optimal_w=0;
   }
   console.log('optimal size: ' + optimal_w + 'x' + optimal_h);						   
   wpObj.style.display = "none";
   wpObj.style.width = optimal_w;
   wpObj.style.height = optimal_h;
   wpObj.width = optimal_w;
   wpObj.height = optimal_h;
   wpObj.style.display = "";
  
   resized = true;
   myLightbox.resizeImageContainer(optimal_w, optimal_h);
   
   //var orgObj = document.getElementById("org");
   //orgObj.style.display = "inline";
   //var fitObj = document.getElementById("fit");
   //fitObj.style.display = "none";
}
function restoreImage()
{
   if (resized == false)
   {
   		console.log('Image is not resized, skipping restore.');
   		return;
   }
   else console.log('== Restoring image size');
   var wpObj = document.getElementById("lightboxImage");
   wpObj.style.width = wp_w;
   wpObj.style.height = wp_h;
   wpObj.width = wp_w;
   wpObj.height = wp_h;
   myLightbox.resizeImageContainer(wp_w, wp_h);
   
   //var fitObj = document.getElementById("fit");
   //fitObj.style.display = "inline";
   //var orgObj = document.getElementById("org");
   //orgObj.style.display = "none";
   
   resized = false;
}

