var currentDragger;
var pW,pH,pX,pY;

function absPosition(obj) {
      var x = y = 0;
      while(obj) {
            x += obj.offsetLeft;
            y += obj.offsetTop;
            obj = obj.offsetParent;
      }
      return {x:x, y:y};
}

function resizeXl(x){	$('newspot').style.width=parseInt($('newspot').style.width)+parseInt($('newspot').style.left)-x;    $('newspot').style.left=x;
    $('newspot_image').style.width=$('newspot').style.width;
    moveDraggers(parseInt($('newspot').style.left),parseInt($('newspot').style.top),parseInt($('newspot').style.width),parseInt($('newspot').style.height));
}
function resizeXr(x){
	$('newspot').style.width=x-parseInt($('newspot').style.left);
    $('newspot_image').style.width=$('newspot').style.width;
    moveDraggers(parseInt($('newspot').style.left),parseInt($('newspot').style.top),parseInt($('newspot').style.width),parseInt($('newspot').style.height));
}

function resizeYt(y){	$('newspot').style.height=parseInt($('newspot').style.height)+parseInt($('newspot').style.top)-y;
    $('newspot').style.top=y;
    $('newspot_image').style.height=$('newspot').style.height;
    moveDraggers(parseInt($('newspot').style.left),parseInt($('newspot').style.top),parseInt($('newspot').style.width),parseInt($('newspot').style.height));
}

function resizeYb(y){
	$('newspot').style.height=y-parseInt($('newspot').style.top);
    $('newspot_image').style.height=$('newspot').style.height;
    moveDraggers(parseInt($('newspot').style.left),parseInt($('newspot').style.top),parseInt($('newspot').style.width),parseInt($('newspot').style.height));
}

function moveDraggers(x,y,width,height){     photoPos=absPosition($('photo'));
     $('test').value=(parseInt(x)-photoPos.x-15)+":"+(parseInt(y)-photoPos.y-15)+" w="+width+" h="+height;
     $("spot_x").value=(parseInt(x)-photoPos.x-15);
     $("spot_y").value=(parseInt(y)-photoPos.y-15);
     $("spot_w").value=width;
     $("spot_h").value=height;


    if(currentDragger!='drag_l_t'){
    	$('drag_l_t').style.display='block';
		$('drag_l_t').style.top=y-8;
		$('drag_l_t').style.left=x-8;
    }

    if(currentDragger!='drag_t'){
		$('drag_t').style.display='block';
		$('drag_t').style.top=y-8;
		$('drag_t').style.left=x+parseInt(width/2);
    }

    if(currentDragger!='drag_r_t'){
		$('drag_r_t').style.display='block';
		$('drag_r_t').style.top=y-8;
		$('drag_r_t').style.left=x+width+2;
    }

    if(currentDragger!='drag_l'){
		$('drag_l').style.display='block';
		$('drag_l').style.top=y+parseInt(height/2);
		$('drag_l').style.left=x-8;
    }

    if(currentDragger!='drag_r'){
		$('drag_r').style.display='block';
		$('drag_r').style.top=y+parseInt(height/2);
		$('drag_r').style.left=x+width+2;
    }

    if(currentDragger!='drag_l_b'){
		$('drag_l_b').style.display='block';
		$('drag_l_b').style.top=y+height+2;
		$('drag_l_b').style.left=x-8;
    }

    if(currentDragger!='drag_b'){
		$('drag_b').style.display='block';
		$('drag_b').style.top=y+height+2;
		$('drag_b').style.left=x+parseInt(width/2);
    }

    if((currentDragger!='drag_l_t')&&(currentDragger!='drag_l')&&(currentDragger!='drag_r_b')){
		$('drag_r_b').style.display='block';
		$('drag_r_b').style.top=y+height+2;
		$('drag_r_b').style.left=x+width+2;
    }
}

function hideEditSpot(){	$("newspot").style.display='none';
    $("drag_l_t").style.display='none';
    $("drag_t").style.display='none';
    $("drag_r_t").style.display='none';
    $("drag_l").style.display='none';
    $("drag_r").style.display='none';
    $("drag_l_b").style.display='none';
    $("drag_b").style.display='none';
    $("drag_r_b").style.display='none';
}
function viewEditSpot(id,photoWidth,photoHeight,width,height){
	// $('test').value=photoWidth;
	photoPos=absPosition($('photo'));

    var x=photoPos.x+15;
	var y=photoPos.y+15;
    pX=x;
    pY=y;
	$(id).style.display='block';
	$(id).style.top=y;
	$(id).style.left=x;
	$(id).style.width=width;
	$(id).style.height=height;

	$(id+'_image').style.width=width;
	$(id+'_image').style.height=height;

    moveDraggers(x,y,width,height);
//----------------------------------Инициализация---------------------------------------------
    	var newspot=document.getElementById("newspot");
    	var drag_l_t=document.getElementById("drag_l_t");
    	var drag_t=document.getElementById("drag_t");
    	var drag_r_t=document.getElementById("drag_r_t");
    	var drag_l=document.getElementById("drag_l");
    	var drag_r=document.getElementById("drag_r");
    	var drag_l_b=document.getElementById("drag_l_b");
    	var drag_b=document.getElementById("drag_b");
    	var drag_r_b=document.getElementById("drag_r_b");

        var lB=x;
        var rB=x+photoWidth;
        var tB=y;
        var bB=y+photoHeight;

    	Drag.init(newspot,null,lB,rB,tB,bB);
    	Drag.init(drag_l_t,null,lB-8,rB+2,tB-8,bB+2);
    	Drag.init(drag_t,null,lB-8,rB+2,tB-8,bB+2);
    	Drag.init(drag_r_t,null,lB-8,rB+2,tB-8,bB+2);
    	Drag.init(drag_l,null,lB-8,rB+2,tB-8,bB+2);
    	Drag.init(drag_r,null,lB-8,rB+2,tB-8,bB+2);
    	Drag.init(drag_l_b,null,lB-8,rB+2,tB-8,bB+2);
    	Drag.init(drag_b,null,lB-8,rB+2,tB-8,bB+2);
    	Drag.init(drag_r_b,null,lB-8,rB+2,tB-8,bB+2);

    	newspot.onDrag=function (x,y){
            currentDragger="";
            var nextR=x+parseInt(this.style.width);
            var nextB=y+parseInt(this.style.height);
            moveDraggers(x,y,parseInt(newspot.style.width),parseInt(newspot.style.height));

    	}

    	drag_l_t.onDrag=function (x,y){
            currentDragger=this.id;
            var n_w,n_h;
			n_w=parseInt($('newspot').style.width)+parseInt($('newspot').style.left)-(x+8);
			n_h=parseInt($('newspot').style.height)+parseInt($('newspot').style.top)-(y+8);
            if((n_w>=3)&&(n_h>=3)){
            	resizeXl(x+8);
            	resizeYt(y+8);
            }else{
                drag_l_t.style.left=parseInt(newspot.style.left)-8;
                drag_l_t.style.top=parseInt(newspot.style.top)-8;
            }
    	}

    	drag_t.onDrag=function (x,y){
    		currentDragger=this.id;
    		var n_h;
			n_h=parseInt($('newspot').style.height)+parseInt($('newspot').style.top)-(y+8);
    		drag_t.style.left=parseInt(newspot.style.left)+parseInt(newspot.style.width)/2;
            if(n_h>=3){
    			resizeYt(y+8);
            }else{
               drag_t.style.top=parseInt(newspot.style.top)-8;
            }
    	}

    	drag_r_t.onDrag=function (x,y){
            currentDragger=this.id;
            var n_w,n_h;
			n_w=(x-2)-parseInt($('newspot').style.left);
			n_h=parseInt($('newspot').style.height)+parseInt($('newspot').style.top)-(y+8);
            if((n_w>=3)&&(n_h>=3)){
                resizeXr(x-2);
            	resizeYt(y+8);
            }else{
                drag_r_t.style.left=parseInt(newspot.style.left)+parseInt(newspot.style.width)+2;
                drag_r_t.style.top=parseInt(newspot.style.top)-8;
            }
    	}

        drag_l.onDrag=function (x,y){
            currentDragger=this.id;
            var n_w;
			n_w=parseInt($('newspot').style.width)+parseInt($('newspot').style.left)-(x+8);
            drag_l.style.top=parseInt(newspot.style.top)+parseInt(newspot.style.height)/2;
            if(n_w>=3){
            	resizeXl(x+8);
            }else{
                drag_l.style.left=parseInt(newspot.style.left)-8;
            }
    	}

    	drag_r.onDrag=function (x,y){
            currentDragger=this.id;
            var n_w;
			n_w=(x-2)-parseInt($('newspot').style.left);
            drag_r.style.top=parseInt(newspot.style.top)+parseInt(newspot.style.height)/2;
            if(n_w>=3){
            	resizeXr(x-2);
            }else{
                drag_r.style.left=parseInt(newspot.style.left)+parseInt(newspot.style.width)+2;
            }
    	}

    	drag_l_b.onDrag=function (x,y){
            currentDragger=this.id;
            var n_w,n_h;
			n_w=parseInt($('newspot').style.width)+parseInt($('newspot').style.left)-(x+8);
			n_h=(y-2)-parseInt($('newspot').style.top);
            if((n_w>=3)&&(n_h>=3)){
                resizeXl(x+8);
            	resizeYb(y-2);
            }else{
                drag_l_b.style.left=parseInt(newspot.style.left)-8;
                drag_l_b.style.top=parseInt(newspot.style.top)+parseInt(newspot.style.height)+2;
            }
    	}

    	drag_b.onDrag=function (x,y){
    		currentDragger=this.id;
    		var n_h;
			n_h=(y-2)-parseInt($('newspot').style.top);
    		drag_b.style.left=parseInt(newspot.style.left)+parseInt(newspot.style.width)/2;
            if(n_h>=3){
    			resizeYb(y-2);
            }else{
               drag_b.style.top=parseInt(newspot.style.top)+parseInt(newspot.style.height)+2;
            }
    	}

    	drag_r_b.onDrag=function (x,y){
            currentDragger=this.id;
            var n_w,n_h;
			n_w=(x-2)-parseInt($('newspot').style.left);
			n_h=(y-2)-parseInt($('newspot').style.top);
            if((n_w>=3)&&(n_h>=3)){
                resizeXr(x-2);
            	resizeYb(y-2);
            }else{
                drag_r_b.style.left=parseInt(newspot.style.left)+parseInt(newspot.style.width)+2;
                drag_r_b.style.top=parseInt(newspot.style.top)+parseInt(newspot.style.height)+2;
            }
    	}
}
