// С разрешения Д.Мзареуляна // hiero.ru // edited by Benvolio

var BWflag = false;

var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;
var initialheight=0;

var Initialized = false;
var Cropflag = false;
var Gridflag = false;

var CurrentSide = null;
var x0 = 0;
var y0 = 0;
var testtest="testtest";
var ev = null;

function init()
{
  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();
}


function falsefunc() { return false; } // used to block cascading events

function update(e)
{
  getMouseXY(e); // NS is passing (event), while IE is passing (null)

//  document.getElementById('span_browser').innerHTML = navigator.appName;
//  document.getElementById('span_winevent').innerHTML = window.event ? window.event.type : '(na)';
//  document.getElementById('span_autevent').innerHTML = e ? e.type : '(na)';
//  document.getElementById('span_mousex').innerHTML = mousex;
//  document.getElementById('span_mousey').innerHTML = mousey;
//  document.getElementById('span_grabx').innerHTML = grabx;
//  document.getElementById('span_graby').innerHTML = graby;
//  document.getElementById('span_orix').innerHTML = orix;
//  document.getElementById('span_oriy').innerHTML = oriy;
//  document.getElementById('span_elex').innerHTML = elex;
//  document.getElementById('span_eley').innerHTML = eley;
//  document.getElementById('span_algor').innerHTML = algor;
//  document.getElementById('span_dragobj').innerHTML = ev ? (ev.id ? ev.id : 'unnamed object') : '(null)';
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft;
      mousey = e.clientY + document.body.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }  


  }
}

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

function flipBW()
{
	InitFilters();
	BWflag = !BWflag;
	document.all.mainpic.filters["Gray"].enabled = BWflag;

}

function MouseDown(context,side)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  CurrentSide = side;
  ev = context;
//  ev = window.event ? window.event : context;
  ev.style.zIndex = 10; // move it to the top
  document.onmousemove = MouseMove;
  document.onmouseup = MouseUp;
//  x0 = ev.x;
//  y0 = ev.y;
  grabx = mousex;
  graby = mousey;
  elex = orix = ev.offsetLeft+ev.offsetParent.offsetLeft;
  eley = oriy = ev.offsetTop+ev.offsetParent.offsetTop;
//  elex = orix = ev.x;
//  eley = oriy = ev.y;
  x0 = elex;
  y0 = eley;

astyle = document.getElementById(CurrentSide);
//document.getElementById('span_myinfo').innerHTML = astyle.offsetHeight+"_"+astyle.scrollHeight+"_"+astyle.height+"_"+astyle.style.height+"_"+astyle.style.pixelHeight;
initialheight=astyle.offsetHeight;
initialwidth=astyle.offsetWidth;
  update();
}

function MouseUp()
{
  CurrentSide = null;
  if (ev)
  {
    ev.style.zIndex = 0;
    ev = null;
  }
  update();
  document.onmousemove = update;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}

function MouseMove(e)
{

//  if (ev)
//  {
    elex = orix + (mousex-grabx);
    eley = oriy + (mousey-graby);
//    ev.style.position = "absolute";
//    ev.style.left = (elex).toString(10) + 'px';
//    ev.style.top  = (eley).toString(10) + 'px';
//  }
//  update(e);
//  return false; // in IE this prevents cascading of events, thus text selection is disabled


//	if((CurrentSide == null) || (ev.button != 1)) return;
	dx = ev.x - x0;
//dy=ev.offsetTop+ev.offsetParent.offsetTop  -mousey;
//dx=ev.offsetLeft+ev.offsetParent.offsetLeft-mousex;
	dy=mousey-graby;
	dx=mousex-grabx;
	astyle = document.getElementById(CurrentSide);
//document.getElementById('span_myinfo').innerHTML = astyle.offsetHeight+"_"+astyle.scrollHeight+"_"+astyle.height+"_"+astyle.style.height+"_"+astyle.style.pixelHeight+"_"+dy;
//document.getElementById('span_myinfo').innerHTML = astyle.offsetWidth+"_"+astyle.scrollWidth+"_"+astyle.width+"_"+astyle.style.width+"_"+astyle.style.pixelWidth+"_"+dx;
	switch (CurrentSide)
	{
		case "toptd":
			if((initialheight+dy) >= 8) astyle.style.height=astyle.height=initialheight+dy;
			if((initialheight+dy) < 8) astyle.style.height=astyle.height = 8;
//			else {x0 = ev.x; y0 = ev.y;}
                        if (myCanvas.style.display=="") {
                         jg.clear();
                         redraw();
                        }
                        if (myCanvas2.style.display=="") {
                         jg2.clear();
                         redraw2();
                        }

		break;
		case "bottomtd":
			if((initialheight-dy) >= 8) astyle.style.height=astyle.height=initialheight-dy;
			if((initialheight-dy) < 8) astyle.style.height=astyle.height = 8;
			else {x0 = ev.x; y0 = ev.y;}
                        if (myCanvas.style.display=="") {
                         jg.clear();
                         redraw();
                        }
                        if (myCanvas2.style.display=="") {
                         jg2.clear();
                         redraw2();
                        }
		break;
		case "lefttd":
			if((initialwidth+dx) >= 8) astyle.style.width=astyle.width=initialwidth+dx;
			if((initialwidth+dx) < 8) astyle.style.width=astyle.width = 8;
			else {x0 = ev.x; y0 = ev.y;}
                        if (myCanvas.style.display=="") {
                         jg.clear();
                         redraw();
                        }
                        if (myCanvas2.style.display=="") {
                         jg2.clear();
                         redraw2();
                        }
		break;
		case "righttd":
			if((initialwidth-dx) >= 8) astyle.style.width=astyle.width=initialwidth-dx;
			if((initialwidth-dx) < 8) astyle.style.width=astyle.width = 8;
			else {x0 = ev.x; y0 = ev.y;}
                        if (myCanvas.style.display=="") {
                         jg.clear();
                         redraw();
                        }
                        if (myCanvas2.style.display=="") {
                         jg2.clear();
                         redraw2();
                        }
		break;
	}
update(e);
return false;
}

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";
	}
}

