define(function(){function a(a){function j(a){return a&&typeof a=="object"&&typeof a.length=="number"&&!a.propertyIsEnumerable("length")?!0:!1}function k(a,b,c){var d=g[a];typeof b=="function"?b.apply(b,c):(typeof b=="string"||b instanceof String)&&d[b].apply(d,c)}function l(){var a=0;for(var c in f)f[c]==="in"&&a++;return a+b}var b=0,c=[],d=[],e={},f={},g={},h=null,i=j(a);this.onEmpty=function(a){c.push(a)},this.onFull=function(a){d.push(a)},this.checkIn=function(c,h){var j,k=!1;if(c!==undefined){if(f[c]!=="in"){e[c]=[],f[c]="in",h!==undefined&&(g[c]=h),k=!0;if(i)for(j in a)if(f[j]!=="in"){k=!1;break}}}else b++;return(l()===a||k)&&$.each(d,function(a,b){b()}),h},this.checkOut=function(a,d){if(l()===0)return;if(a===undefined)b--;else if(f[a]==="in"){f[a]="out",d!==undefined&&(g[a]=d);for(var h=0,i=e[a].length;h<i;h++)k(a,e[a][h][0],e[a][h][1])}l()===0&&$.each(c,function(a,b){b()})},this.leaveMessage=function(a,b){var c=Array.prototype.slice.call(arguments,2);f[a]==="in"?e[a].push([b,c]):k(a,b,c)}}return a})