/*
 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('event-synthetic',function(Y){var DOMMap=Y.Env.evt.dom_map,toArray=Y.Array,YLang=Y.Lang,isObject=YLang.isObject,isString=YLang.isString,query=Y.Selector.query,noop=function(){};function Notifier(handle,emitFacade){this.handle=handle;this.emitFacade=emitFacade;}
Notifier.prototype.fire=function(e){var args=toArray(arguments,0,true),handle=this.handle,ce=handle.evt,sub=handle.sub,thisObj=sub.context,delegate=sub.filter,event=e||{};if(this.emitFacade){if(!e||!e.preventDefault){event=ce._getFacade();if(isObject(e)&&!e.preventDefault){Y.mix(event,e,true);args[0]=event;}else{args.unshift(event);}}
event.type=ce.type;event.details=args.slice();if(delegate){event.container=ce.host;}}else if(delegate&&isObject(e)&&e.currentTarget){args.shift();}
sub.context=thisObj||event.currentTarget||ce.host;ce.fire.apply(ce,args);sub.context=thisObj;};function SyntheticEvent(){this._init.apply(this,arguments);}
Y.mix(SyntheticEvent,{Notifier:Notifier,getRegistry:function(node,type,create){var el=node._node,yuid=Y.stamp(el),key='event:'+yuid+type+'_synth',events=DOMMap[yuid]||(DOMMap[yuid]={});if(!events[key]&&create){events[key]={type:'_synth',fn:noop,capture:false,el:el,key:key,domkey:yuid,notifiers:[],detachAll:function(){var notifiers=this.notifiers,i=notifiers.length;while(--i>=0){notifiers[i].detach();}}};}
return(events[key])?events[key].notifiers:null;},_deleteSub:function(sub){if(sub&&sub.fn){var synth=this.eventDef,method=(sub.filter)?'detachDelegate':'detach';this.subscribers={};this.subCount=0;synth[method](sub.node,sub,this.notifier,sub.filter);synth._unregisterSub(sub);delete sub.fn;delete sub.node;delete sub.context;}},prototype:{constructor:SyntheticEvent,_init:function(){var config=this.publishConfig||(this.publishConfig={});this.emitFacade=('emitFacade'in config)?config.emitFacade:true;config.emitFacade=false;},processArgs:noop,on:noop,detach:noop,delegate:noop,detachDelegate:noop,_on:function(args,delegate){var handles=[],extra=this.processArgs(args,delegate),selector=args[2],method=delegate?'delegate':'on',nodes,handle;nodes=(isString(selector))?query(selector):toArray(selector);if(!nodes.length&&isString(selector)){handle=Y.on('available',function(){Y.mix(handle,Y[method].apply(Y,args),true);},selector);return handle;}
Y.Array.each(nodes,function(node){var subArgs=args.slice(),filter;node=Y.one(node);if(node){if(delegate){filter=subArgs.splice(3,1)[0];}
subArgs.splice(0,4,subArgs[1],subArgs[3]);if(!this.preventDups||!this.getSubs(node,args,null,true)){handle=this._getNotifier(node,subArgs,extra,filter);this[method](node,handle.sub,handle.notifier,filter);handles.push(handle);}}},this);return(handles.length===1)?handles[0]:new Y.EventHandle(handles);},_getNotifier:function(node,args,extra,filter){var dispatcher=new Y.CustomEvent(this.type,this.publishConfig),handle=dispatcher.on.apply(dispatcher,args),notifier=new Notifier(handle,this.emitFacade),registry=SyntheticEvent.getRegistry(node,this.type,true),sub=handle.sub;handle.notifier=notifier;sub.node=node;sub.filter=filter;if(extra){this.applyArgExtras(extra,sub);}
Y.mix(dispatcher,{eventDef:this,notifier:notifier,host:node,currentTarget:node,target:node,el:node._node,_delete:SyntheticEvent._deleteSub},true);registry.push(handle);return handle;},applyArgExtras:function(extra,sub){sub._extra=extra;},_unregisterSub:function(sub){var notifiers=SyntheticEvent.getRegistry(sub.node,this.type),i;if(notifiers){for(i=notifiers.length-1;i>=0;--i){if(notifiers[i].sub===sub){notifiers.splice(i,1);break;}}}},_detach:function(args){var target=args[2],els=(isString(target))?query(target):toArray(target),node,i,len,handles,j;args.splice(2,1);for(i=0,len=els.length;i<len;++i){node=Y.one(els[i]);if(node){handles=this.getSubs(node,args);if(handles){for(j=handles.length-1;j>=0;--j){handles[j].detach();}}}}},getSubs:function(node,args,filter,first){var notifiers=SyntheticEvent.getRegistry(node,this.type),handles=[],i,len,handle;if(notifiers){if(!filter){filter=this.subMatch;}
for(i=0,len=notifiers.length;i<len;++i){handle=notifiers[i];if(filter.call(this,handle.sub,args)){if(first){return handle;}else{handles.push(notifiers[i]);}}}}
return handles.length&&handles;},subMatch:function(sub,args){return!args[1]||sub.fn===args[1];}}},true);Y.SyntheticEvent=SyntheticEvent;Y.Event.define=function(type,config,force){if(!config){config={};}
var eventDef=(isObject(type))?type:Y.merge({type:type},config),Impl,synth;if(force||!Y.Node.DOM_EVENTS[eventDef.type]){Impl=function(){SyntheticEvent.apply(this,arguments);};Y.extend(Impl,SyntheticEvent,eventDef);synth=new Impl();type=synth.type;Y.Node.DOM_EVENTS[type]=Y.Env.evt.plugins[type]={eventDef:synth,on:function(){return synth._on(toArray(arguments));},delegate:function(){return synth._on(toArray(arguments),true);},detach:function(){return synth._detach(toArray(arguments));}};}
return synth;};},'3.3.0',{requires:['node-base','event-custom']});
