/*
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.html
version: 3.1.1
build: 47
*/
YUI.add("dd-constrain",function(B){var K="dragNode",M="offsetHeight",F="offsetWidth",P="host",G="tickXArray",N="tickYArray",O=B.DD.DDM,E="top",I="right",L="bottom",D="left",J="view",H=null,A=function(C){this._lazyAddAttrs=false;A.superclass.constructor.apply(this,arguments);};A.NAME="ddConstrained";A.NS="con";A.ATTRS={host:{},stickX:{value:false},stickY:{value:false},tickX:{value:false},tickY:{value:false},tickXArray:{value:false},tickYArray:{value:false},gutter:{value:"0",setter:function(C){return B.DD.DDM.cssSizestoObject(C);}},constrain:{value:J,setter:function(C){var Q=B.one(C);if(Q){C=Q;}return C;}},constrain2region:{setter:function(C){return this.set("constrain",C);}},constrain2node:{setter:function(C){return this.set("constrain",B.one(C));}},constrain2view:{setter:function(C){return this.set("constrain",J);}},cacheRegion:{value:true}};H={initializer:function(){this.get(P).on("drag:start",B.bind(this._handleStart,this));this.get(P).after("drag:align",B.bind(this.align,this));},_handleStart:function(){this.resetCache();},_regionCache:null,_cacheRegion:function(){this._regionCache=this.get("constrain").get("region");},resetCache:function(){this._regionCache=null;},_getConstraint:function(){var C=this.get("constrain"),Q=this.get("gutter"),R;if(C){if(C instanceof B.Node){if(!this._regionCache){B.on("resize",B.bind(this._cacheRegion,this),window);this._cacheRegion();}R=B.clone(this._regionCache);if(!this.get("cacheRegion")){this.resetCache();}}else{if(B.Lang.isObject(C)){R=C;}}}if(!C||!R){C=J;}if(C===J){R=this.get(P).get(K).get("viewportRegion");}B.each(Q,function(S,T){if((T==I)||(T==L)){R[T]-=S;}else{R[T]+=S;}});return R;},getRegion:function(T){var R={},S=null,C=null,Q=this.get(P);R=this._getConstraint();if(T){S=Q.get(K).get(M);C=Q.get(K).get(F);R[I]=R[I]-C;R[L]=R[L]-S;}return R;},_checkRegion:function(C){var R=C,T=this.getRegion(),S=this.get(P),U=S.get(K).get(M),Q=S.get(K).get(F);if(R[1]>(T[L]-U)){C[1]=(T[L]-U);}if(T[E]>R[1]){C[1]=T[E];}if(R[0]>(T[I]-Q)){C[0]=(T[I]-Q);}if(T[D]>R[0]){C[0]=T[D];}return C;},inRegion:function(R){R=R||this.get(P).get(K).getXY();var Q=this._checkRegion([R[0],R[1]]),C=false;if((R[0]===Q[0])&&(R[1]===Q[1])){C=true;}return C;},align:function(){var R=this.get(P),C=R.actXY,Q=this.getRegion(true);if(this.get("stickX")){C[1]=(R.startXY[1]-R.deltaXY[1]);}if(this.get("stickY")){C[0]=(R.startXY[0]-R.deltaXY[0]);}if(Q){C=this._checkRegion(C);}C=this._checkTicks(C,Q);R.actXY=C;},_checkTicks:function(V,T){var S=this.get(P),U=(S.startXY[0]-S.deltaXY[0]),R=(S.startXY[1]-S.deltaXY[1]),C=this.get("tickX"),Q=this.get("tickY");if(C&&!this.get(G)){V[0]=O._calcTicks(V[0],U,C,T[D],T[I]);}if(Q&&!this.get(N)){V[1]=O._calcTicks(V[1],R,Q,T[E],T[L]);}if(this.get(G)){V[0]=O._calcTickArray(V[0],this.get(G),T[D],T[I]);}if(this.get(N)){V[1]=O._calcTickArray(V[1],this.get(N),T[E],T[L]);}return V;}};B.namespace("Plugin");B.extend(A,B.Base,H);B.Plugin.DDConstrained=A;B.mix(O,{_calcTicks:function(W,V,S,U,T){var Q=((W-V)/S),R=Math.floor(Q),C=Math.ceil(Q);if((R!==0)||(C!==0)){if((Q>=R)&&(Q<=C)){W=(V+(S*R));if(U&&T){if(W<U){W=(V+(S*(R+1)));}if(W>T){W=(V+(S*(R-1)));}}}}return W;},_calcTickArray:function(X,Y,W,T){var Q=0,U=Y.length,S=0,R,C,V;if(!Y||(Y.length===0)){return X;}else{if(Y[0]>=X){return Y[0];}else{for(Q=0;Q<U;Q++){S=(Q+1);if(Y[S]&&Y[S]>=X){R=X-Y[Q];C=Y[S]-X;V=(C>R)?Y[Q]:Y[S];if(W&&T){if(V>T){if(Y[Q]){V=Y[Q];}else{V=Y[U-1];}}}return V;}}return Y[Y.length-1];}}}});},"3.1.1",{requires:["dd-drag"],skinnable:false});