var cropflag=false;
var currentside=null;
var x0=0;
var y0=0;
var w=0;
var h=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;
 sleft=document.all['left'].style;
 sright=document.all['right'].style;
 stop=document.all['top'].style;
 sbottom=document.all['bottom'].style;
 w=(mainpic.width+16-sright.pixelWidth-sleft.pixelWidth);
 h=(mainpic.height+16-sbottom.pixelHeight-stop.pixelHeight);
 window.status="Размер выделенной области: "+w+"X"+h;
 style=document.all[currentside].style;
 switch(currentside)
 {
  case "area":
   if(w<mainpic.width)
   {
    if(sleft.pixelWidth+dx<8) { sleft.pixelWidth=8; sright.pixelWidth=mainpic.width-w+8; }
    else
    if(sright.pixelWidth-dx<8) { sright.pixelWidth=8; sleft.pixelWidth=mainpic.width-w+8; }
    else
    { sleft.pixelWidth+=dx; sright.pixelWidth-=dx; x0=ev.x; }
   }
   if(h<mainpic.height)
   {
    if(stop.pixelHeight+dy<8) { stop.pixelHeight=8; sbottom.pixelHeight=mainpic.height-h+8; }
    else
    if(sbottom.pixelHeight-dy<8) { sbottom.pixelHeight=8; stop.pixelHeight=mainpic.height-h+8; }
    else
    { stop.pixelHeight+=dy; sbottom.pixelHeight-=dy; y0=ev.y; }
   }
   break;
  case "top":
   if(style.pixelHeight+dy>mainpic.height) style.pixelHeight=mainpic.height;
   else
   if(style.pixelHeight+dy>mainpic.height-sbottom.pixelHeight+8) style.pixelHeight=mainpic.height-sbottom.pixelHeight+8;
   else
   {
    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-dy>mainpic.height) style.pixelHeight=mainpic.height;
   else
   if(style.pixelHeight-dy>mainpic.height-stop.pixelHeight+8) style.pixelHeight=mainpic.height-stop.pixelHeight+8;
   else
   {
    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 addcrop()
{
 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+'[/CROP]';
 document.gbform.body.value+=s;
 document.gbform.body.focus();
}

function flipcrop()
{
 crop.width=mainpic.width+16;
 crop.height=mainpic.height+16;
 cropflag=!cropflag;
 if(cropflag)
 {
  document.all.cropper.style.display="";
 }
 else
 {
  document.all.cropper.style.display="none";
 }
}


