var mouseX,mouseY,draggerLeft,draggerTop,queueContainer,queueArr=[],draggers=[],isDragging=!1,isMouseDown=!1,dragger=null,clone=null,DRAG_THRESHOLD=5,showMos="pingpu",boxNo=0,dragNo=1,queueActive={background:"rgba(0,0,0,0.2)"},queueUnActive={background:"rgba(0,0,0,0.05)"},registerDrag=function(a,b){var c,d;for(queueContainer=a,b=b,$.each(a.find(".queue"),function(a,b){queueArr[a]=$(b),draggers[a]=[],elements=$(b).find(".dragger"),$.each(elements,function(b,c){draggers[a][b]=$(c)})}),c=0;c<draggers.length;c++)for(d=0;d<draggers[c].length;d++)draggers[c][d].on("mousedown",dragStart);$(document).on("mousemove",dragMove),$(document).on("mouseup",dragEnd)},dragStart=function(a){a.stopPropagation(),isMouseDown=!0,mouseX=a.clientX,mouseY=a.clientY,dragger=$(this),dragNo=Number($(a.currentTarget.offsetParent).attr("data-no")),console.log()},dragMove=function(a){var b,c;a.stopPropagation(),isMouseDown&&(b=a.clientX-mouseX,c=a.clientY-mouseY,isDragging?(clone.css({left:draggerLeft+b,top:draggerTop+c}),arrangeDragger()):(Math.abs(b)>DRAG_THRESHOLD||Math.abs(c)>DRAG_THRESHOLD)&&(clone=makeClone(dragger),draggerLeft=dragger.offset().left-parseInt(dragger.css("margin-left"))-parseInt(dragger.css("padding-left")),draggerTop=dragger.offset().top-parseInt(dragger.css("margin-top"))-parseInt(dragger.css("padding-top")),clone.css({left:draggerLeft,top:draggerTop}),queueContainer.append(clone),dragger.css("visibility","hidden"),isDragging=!0))},dragEnd=function(a){a.stopPropagation(),isDragging&&(isDragging=!1,clone.remove(),dragger.css("visibility","visible"),"11"==dragger.parent().parent().attr("data-no")&&setTimeout(function(){cs.evalScript('sendOherApp("OTHERAPPCODETOAPP_'+appPage+'.fast","'+btoa(escape("app.zuijinApp();"))+'")')},200));for(var b=0;b<queueArr.length;b++)queueArr[b].removeClass("queueActive"),queueArr[b].css(queueUnActive);isMouseDown=!1,"index"==appPageEnd&&(app.ulHeight(),FUN3.dragToBox(dragNo,boxNo+1,$(clone).attr("data-id")))},makeClone=function(a){var b=a.clone();return b.css({position:"absolute","z-index":1e5}),b},arrangeDragger=function(){var a,b,c,d,e,f,g,h,i,j,k;for(a=0;a<queueArr.length;a++)queueArr[a].removeClass("queueActive"),queueArr[a].css(queueUnActive);if(b=findQueue(),console.log("queueIn =",b),-1!=b&&(queueArr[b].addClass("queueActive"),queueArr[b].css(queueActive)),c=findHover(b),null!=c){for(d=c.hover,e=c.insert,a=0;a<draggers.length;a++)for(j=0;j<draggers[a].length;j++)draggers[a][j][0]==dragger[0]&&(f=a,g=j);if(draggers[f].splice(g,1),d){for(a=0;a<draggers.length;a++)for(j=0;j<draggers[a].length;j++)d&&draggers[a][j][0]==d[0]&&(h=a,i=j);"left"==e||"top"==e?(d.before(dragger),draggers[h].splice(i,0,dragger)):(d.after(dragger),draggers[h].splice(i+1,0,dragger))}else k=$("div.queueActive ul"),console.log(k.find("li").length),draggers[b].push(dragger),k.append(dragger)}},findQueue=function(){var g,h,i,j,k,l,m,a=-1,b=-1,c=clone.offset().top,d=clone.offset().left,e=clone.height(),f=clone.width();for(g=0;g<queueArr.length;g++)h=queueArr[g].offset().top,i=queueArr[g].offset().left,j=queueArr[g].height(),k=Math.min(h+j,c+e)-Math.max(h,c),l=Math.min(i+f,d+f)-Math.max(i,d),m=0,"liebiao"==showMos&&(m=k+l),"pingpu"==showMos&&(m=k),m>a&&(a=m,b=g);return b},findHover=function(a){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;if(console.log("queueIn",a),b="left",c="right","liebiao"==showMos&&(b="top",c="bottom"),d={hover:null,insert:c},boxNo=Number(a),-1==a)return null;for(e=-1,f=null,g=clone.offset().top,h=clone.height(),i=clone.offset().left,j=clone.width(),k=!1,l=0;l<draggers[a].length;l++)m=draggers[a][l].offset().top,n=draggers[a][l].height(),o=Math.min(m+n,g+h)-Math.max(m,g),p=draggers[a][l].offset().left,q=draggers[a][l].width(),r=Math.min(p+q,i+j)-Math.max(p,i),0>=o||0>=r||(s=o*r,h*j/3>=s||(draggers[a][l][0]!=dragger[0]?s>e&&(e=s,f=draggers[a][l]):k=!0));if(0>e){if(k)return null;if(0==draggers[a].length)return{hover:null};for(u=draggers[a].length-1;u>=0&&draggers[a][u][0]==dragger[0];)u--;return u>=0?(t=draggers[a][u],i>=t.offset().left+t.width()?{hover:t,insert:c}:d):{hover:null}}return v=(2*f.offset().left+f.width())/2,w=(2*clone.offset().left+clone.width())/2,v>w?{hover:f,insert:b}:{hover:f,insert:c}};