/*
 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 Code licensed under the BSD License:
 http://developer.yahoo.com/yui/license.html
 version: 3.3.0
 build: 3167
 */
YUI.add('dd-delegate',function(Y){var Delegate=function(o){Delegate.superclass.constructor.apply(this,arguments);},CONT='container',NODES='nodes',_tmpNode=Y.Node.create('<div>Temp Node</div>');Y.extend(Delegate,Y.Base,{_bubbleTargets:Y.DD.DDM,dd:null,_shimState:null,_handles:null,_onNodeChange:function(e){this.set('dragNode',e.newVal);},_afterDragEnd:function(e){Y.DD.DDM._noShim=this._shimState;this.set('lastNode',this.dd.get('node'));this.get('lastNode').removeClass(Y.DD.DDM.CSS_PREFIX+'-dragging');this.dd._unprep();this.dd.set('node',_tmpNode);},_delMouseDown:function(e){var tar=e.currentTarget,dd=this.dd;if(tar.test(this.get(NODES))&&!tar.test(this.get('invalid'))){this._shimState=Y.DD.DDM._noShim;Y.DD.DDM._noShim=true;this.set('currentNode',tar);dd.set('node',tar);if(dd.proxy){dd.set('dragNode',Y.DD.DDM._proxy);}else{dd.set('dragNode',tar);}
dd._prep();dd.fire('drag:mouseDown',{ev:e});}},_onMouseEnter:function(e){this._shimState=Y.DD.DDM._noShim;Y.DD.DDM._noShim=true;},_onMouseLeave:function(e){Y.DD.DDM._noShim=this._shimState;},initializer:function(cfg){this._handles=[];var conf=Y.clone(this.get('dragConfig')||{}),cont=this.get(CONT);conf.node=_tmpNode.cloneNode(true);conf.bubbleTargets=this;if(this.get('handles')){conf.handles=this.get('handles');}
this.dd=new Y.DD.Drag(conf);this.dd.after('drag:end',Y.bind(this._afterDragEnd,this));this.dd.on('dragNodeChange',Y.bind(this._onNodeChange,this));this.dd.after('drag:mouseup',function(){this._unprep();});this._handles.push(Y.delegate(Y.DD.Drag.START_EVENT,Y.bind(this._delMouseDown,this),cont,this.get(NODES)));this._handles.push(Y.on('mouseenter',Y.bind(this._onMouseEnter,this),cont));this._handles.push(Y.on('mouseleave',Y.bind(this._onMouseLeave,this),cont));Y.later(50,this,this.syncTargets);Y.DD.DDM.regDelegate(this);},syncTargets:function(){if(!Y.Plugin.Drop||this.get('destroyed')){return;}
var items,groups,config;if(this.get('target')){items=Y.one(this.get(CONT)).all(this.get(NODES));groups=this.dd.get('groups');config=this.get('dragConfig');if(config&&'groups'in config){groups=config.groups;}
items.each(function(i){this.createDrop(i,groups);},this);}
return this;},createDrop:function(node,groups){var config={useShim:false,bubbleTargets:this};if(!node.drop){node.plug(Y.Plugin.Drop,config);}
node.drop.set('groups',groups);return node;},destructor:function(){if(this.dd){this.dd.destroy();}
if(Y.Plugin.Drop){var targets=Y.one(this.get(CONT)).all(this.get(NODES));targets.unplug(Y.Plugin.Drop);}
Y.each(this._handles,function(v){v.detach();});}},{NAME:'delegate',ATTRS:{container:{value:'body'},nodes:{value:'.dd-draggable'},invalid:{value:'input, select, button, a, textarea'},lastNode:{value:_tmpNode},currentNode:{value:_tmpNode},dragNode:{value:_tmpNode},over:{value:false},target:{value:false},dragConfig:{value:null},handles:{value:null}}});Y.mix(Y.DD.DDM,{_delegates:[],regDelegate:function(del){this._delegates.push(del);},getDelegate:function(node){var del=null;node=Y.one(node);Y.each(this._delegates,function(v){if(node.test(v.get(CONT))){del=v;}},this);return del;}});Y.namespace('DD');Y.DD.Delegate=Delegate;},'3.3.0',{requires:['dd-drag','event-mouseenter'],skinnable:false,optional:['dd-drop-plugin']});
