///////////////////////////////////////////////////////
//
//CROP 2.0
//(c) 2010 Nabatnikoff Ivan
//
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
//
//BROWSER DETECT
//
///////////////////////////////////////////////////////
var moz=((document.all)?false:true);
var ie=((document.all)?true:false);

var cropflag=false;
var side='';
var currentside=null;
var x0=0;
var y0=0;
var w=0;
var h=0;


function GetMouseCoordinates(ev)
{
 if(ev.pageX||ev.pageY) {return {x:ev.pageX,y:ev.pageY};}
 return {x:ev.clientX+document.body.scrollLeft-document.body.clientLeft,y:ev.clientY+document.body.scrollTop-document.body.clientTop};
}

function GetMouseEvent(event)
{
 ev=window.event||event;
 var pos=GetMouseCoordinates(ev);
 var button=ev.button;
 if(moz)
 {
  if(button==1) button=4;
  else
  if(button==0) button=1;
 }
 var wheelDelta;  // normalize the delta
 if(ev.wheelDelta) // IE & Opera
 wheelDelta=ev.wheelDelta/120;
 else if(ev.detail) // W3C
 wheelDelta=-ev.detail/3;
 return {x:pos.x,y:pos.y,button:button,wheelDelta:wheelDelta};
}

function MouseDown(event)
{
 if(moz) event.preventDefault();
 ev=GetMouseEvent(event);
 currentside=side;
 x0=ev.x;
 y0=ev.y;
 return false;
}

function MouseUp()
{
 currentside=null;
 return false;
}

function MouseMove(event)
{
 ev=GetMouseEvent(event); //window.event;
 if((currentside==null)||(ev.button!=1)) return false;
 dx=ev.x-x0;
 dy=ev.y-y0;
 var left=document.getElementById('left');
 var right=document.getElementById('right');
 var top=document.getElementById('top');
 var bottom=document.getElementById('bottom');
 var mainpic=document.getElementById('mainpic');
 w=(parseInt(mainpic.width)+16-parseInt(right.width)-parseInt(left.width));
 h=(parseInt(mainpic.height)+16-parseInt(bottom.height)-parseInt(top.height));
 window.status="Размер выделенной области: "+w+"X"+h;
 if(currentside.indexOf('area')!=-1)
 {
   if(w<parseInt(mainpic.width))
   {
    if(parseInt(left.width)+dx<8) { left.width=8; right.width=parseInt(mainpic.width)-w+8; }
    else
    if(parseInt(right.width)-dx<8) { right.width=8; left.width=parseInt(mainpic.width)-w+8; }
    else
    { left.width=parseInt(left.width)+dx; right.width=parseInt(right.width)-dx; x0=ev.x; }
   }
   if(h<parseInt(mainpic.height))
   {
    if(parseInt(top.height)+dy<8) { top.height=8; bottom.height=parseInt(mainpic.height)-h+8; }
    else
    if(parseInt(bottom.height)-dy<8) { bottom.height=8; top.height=parseInt(mainpic.height)-h+8; }
    else
    { top.height=parseInt(top.height)+dy; bottom.height=parseInt(bottom.height)-dy; y0=ev.y; }
   }
 }
 if(currentside.indexOf('top')!=-1)
 {
   if(h-dy<24) top.height=parseInt(mainpic.height)+16-parseInt(bottom.height)-24;
   else
   {
    if(top.height>=8) top.height=Math.max(8,parseInt(top.height)+dy);//parseInt(top.height)+dy;
    if(top.height<8) top.height=8; else {y0=ev.y;}
   }
 }
 if(currentside.indexOf('bottom')!=-1)
 {
   if(h+dy<24) bottom.height=parseInt(mainpic.height)+16-parseInt(top.height)-24;
   else
   {
    if(bottom.height>=8) bottom.height=Math.max(8,parseInt(bottom.height)-dy);
    if(bottom.height<8) bottom.height=8; else {x0=ev.x; y0=ev.y;}
   }
 }
 if(currentside.indexOf('left')!=-1) 
 {
   if(w-dx<24) left.width=parseInt(mainpic.width)+16-parseInt(right.width)-24;
   else
   {
    if(left.width>=8) left.width=Math.max(8,parseInt(left.width)+dx);
    if(left.width<8) left.width=8; else {x0=ev.x; y0=ev.y;}
   }
 }
 if(currentside.indexOf('right')!=-1) 
 {
   if(w+dx<24) right.width=parseInt(mainpic.width)+16-parseInt(left.width)-24;
   else
   {
    if(right.width>=8) right.width=Math.max(8,parseInt(right.width)-dx);
    if(right.width<8) right.width=8; else {x0=ev.x; y0=ev.y;}
   }
 }
}

function setcrop(left,top,right,bottom)
{
 if(vshow) VHide();
 if(!cropflag) flipcrop();
 document.all.left.width=left+8;
 document.all.right.width=right+8;
 document.all.top.height=top+8;
 document.all.bottom.height=bottom+8;
 window.scrollTo(0,145);
}

function addcrop()
{
 x1=document.all.left.width-8; if(x1<0) x1=0;
 y1=document.all.top.height-8; if(y1<0) y1=0;
 x2=document.all.right.width-8; if(x2<0) x2=0;
 y2=document.all.bottom.height-8; if(y2<0) y2=0;
 s='[CROP]'+x1+','+y1+','+x2+','+y2+'[/CROP]';
 document.gbform.body.value+=s;
 document.gbform.body.focus();
}

function flipcrop()
{
 var crop=document.getElementById('crop');
 var mainpic=document.getElementById('mainpic');
 crop.style.width=mainpic.width+16;
 crop.style.height=mainpic.height+16;
 cropflag=!cropflag;
 if(cropflag)
 {
  document.getElementById('cropper').style.display="";
  document.getElementById('vpanel').innerHTML='<center><a class=rlink href="#" onClick="if(cropflag) addcrop();" style="font-size: 10;">[ Вставить в комментарий ]</a></center>';
 }
 else
 {
  document.getElementById('cropper').style.display="none";
  document.getElementById('vpanel').innerHTML='';
 }
}





