// (c)2010 Michael Holtstrom

var move={};move.map=Array();move.map['boxmask']=0;move.map['boxhdl boxml']=1;move.map['boxhdl boxmr']=2;move.map['boxhdl boxtm']=3;move.map['boxhdl boxbm']=4;move.map['boxhdl boxtl']=5;move.map['boxhdl boxtr']=6;move.map['boxhdl boxbr']=7;move.map['boxhdl boxbl']=8;move.data=[0,null,0,0,0,0,-1,-1,-1,-1,0,0,0,0,null,null,null];move.xtra=Array();move.ing=false;move.startMask=function(e,targ,mask)
{dbg('move.startMask');var m=move.data;m[1]=targ;m[10]=targ.offsetLeft;m[11]=targ.offsetWidth;m[12]=targ.offsetTop;m[13]=targ.offsetHeight;m[14]=mask;mask.style.left=targ.style.left;mask.style.width=targ.style.width;mask.style.top=targ.style.top;mask.style.height=targ.style.height;mask.className='boxmask';move.activate(e,0);};move.moveMask=function(e,targ,mask)
{dbg('move.moveMask');var m=move.data;m[1]=targ;m[10]=targ.offsetLeft;m[11]=targ.offsetWidth;m[12]=targ.offsetTop;m[13]=targ.offsetHeight;m[14]=mask;var elm=util.elm(e);move.activate(e,move.map[elm.className]);};move.startSimple=function(e,targ)
{dbg('move.startSimple');var m=move.data;m[1]=targ;m[10]=targ.offsetLeft;m[11]=targ.offsetWidth;m[12]=targ.offsetTop;m[13]=targ.offsetHeight;move.activate(e,0);};move.activate=function(e,act)
{dbg('move.activate '+act);var pos=util.mouse(e);move.data[0]=act;move.data[2]=pos[0];move.data[3]=pos[1];move.data[4]=0;move.data[5]=0;move.ing=true;util.add(document,'mousemove',move.move);util.add(document,'mouseup',move.terminate);};move.move=function(e)
{dbgx('move.move');if(!move.ing){return;}
e=e||window.event;var m=move.data;var pos=util.mouse(e);var x=pos[0]-m[2]+m[4];var y=pos[1]-m[3]+m[5];m[4]=0;m[5]=0;m[2]=pos[0];m[3]=pos[1];var dx=x;var dy=y;if(m[0]<1||m[0]>8)
{if(m[6]>=0&&m[10]+dx<m[6])
{x=m[6]-m[10];m[4]=dx-x;}
else if(m[7]>=0&&m[10]+m[11]+dx>m[7])
{x=m[7]-(m[10]+m[11]);m[4]=dx-x;}
if(m[8]>=0&&m[12]+dy<m[8])
{y=m[8]-m[12];m[5]=dy-y;}
else if(m[9]>=0&&m[12]+m[13]+dy>m[9])
{y=m[9]-(m[12]+m[13]);m[5]=dy-y;}
m[10]+=x;m[12]+=y;m[1].style.left=m[10]+'px';m[1].style.top=m[12]+'px';if(m[14])
{m[14].style.left=m[1].style.left;m[14].style.top=m[1].style.top;}
if(m[15]){m[15](pos);}}
else
{if(m[0]==1||m[0]==5||m[0]==8)
{if(m[6]>=0&&m[10]+dx<m[6])
{x=m[6]-m[10];m[4]=dx-x;}
else if(dx>=m[11])
{x=m[11];m[4]=dx-x;}
m[10]+=x;m[11]-=x;m[1].style.left=m[10]+'px';m[1].style.width=m[11]+'px';if(m[14])
{m[14].style.left=m[1].style.left;m[14].style.width=m[1].style.width;}}
if(m[0]==2||m[0]==6||m[0]==7)
{if(m[7]>=0&&m[10]+m[11]+dx>m[7])
{x=m[7]-(m[10]+m[11]);m[4]=dx-x;}
else if(dx+m[11]<=0)
{x=-m[11];m[4]=dx-x;}
m[11]+=x;m[1].style.width=m[11]+'px';if(m[14])
{m[14].style.width=m[1].style.width;}}
if(m[0]==3||m[0]==6||m[0]==5)
{if(m[8]>=0&&m[12]+dy<m[8])
{y=m[8]-m[12];m[5]=dy-y;}
else if(dy>=m[13])
{y=m[13];m[5]=dy-y;}
m[12]+=y;m[13]-=y;m[1].style.top=m[12]+'px';m[1].style.height=m[13]+'px';if(m[14])
{m[14].style.top=m[1].style.top;m[14].style.height=m[1].style.height;}}
if(m[0]==4||m[0]==7||m[0]==8)
{if(m[9]>=0&&m[12]+m[13]+dy>m[9])
{y=m[9]-(m[12]+m[13]);m[5]=dy-y;}
else if(dy+m[13]<=0)
{y=-m[13];m[5]=dy-y;}
m[13]+=y;m[1].style.height=m[13]+'px';if(m[14])
{m[14].style.height=m[1].style.height;}}}
return util.cancel(e);};move.terminate=function(e)
{dbg('move.terminate');if(!move.ing){return;}
move.ing=false;util.remove(document,'mousemove',move.move);util.remove(document,'mouseup',move.terminate);if(move.data[16]){move.data[16]();}
move.cleanup();e=e||window.event;return util.cancel(e);};move.cleanup=function()
{var m=move.data;m[6]=-1;m[7]=-1;m[8]=-1;m[9]=-1;m[14]=m[15]=m[16]=null;}