/*
 * utils
 * Copyright(c) 2009, Magic Wand.
 * 
 * This code is licensed under BSD license. Use it as you wish, 
 * but keep this copyright intact.
 */


(function($){$.current_id=0;$.fn.generateId=function(prefix){if(prefix===undefined)
prefix='userid_';return this.each(function(){var el=$(this);if(!el.attr('id')){var id=prefix+($.current_id++);el.attr('id',id);}})}
$.fn.getIdSelector=function(prefix){if(this.length==0)
return undefined;this.generateId(prefix);return'#'+this.eq(0).attr('id');}})(jQuery);

function ns(){var a=arguments,o=null,i,j,d;for(i=0;i<a.length;i++){d=a[i].split('.');o=window;for(j=0;j<d.length;j++){o[d[j]]=o[d[j]]||{};o=o[d[j]];}}
return o;}

ns('utils');utils.extend=function(subc,superc,overrides){var F=function(){};F.prototype=superc.prototype;subc.prototype=new F();subc.prototype.constructor=subc;subc.superclass=superc.prototype;if(superc.prototype.constructor==Object.prototype.constructor){superc.prototype.constructor=superc;}
if(overrides){for(var i in overrides){subc.prototype[i]=overrides[i];}}}

Offset=function(left,top){this.left=0;this.top=0;this.setup(left,top);}
Offset.prototype={setup:function(left,top){if(left instanceof Object){this.left=left.left;this.top=left.top;}else{if(left!==undefined)this.left=left;if(top!==undefined)this.top=top;}
return this;},add:function(left,top){var offset=new Offset(left,top);this.left+=offset.left;this.top+=offset.top;return this;},sub:function(left,top){var offset=new Offset(left,top);this.left-=offset.left;this.top-=offset.top;return this;},getCopy:function(){return new Offset(this);}}
Size=function(width,height){this.width=0;this.height=0;this.setup(width,height);}
Size.prototype={setup:function(width,height){if(width instanceof Object){this.width=width.width;this.height=width.height;}else{if(width!==undefined)this.width=width;if(height!==undefined)this.height=height;}
return this;},add:function(width,height){var size=new Size(width,height);this.width+=size.width;this.height+=size.height;return this;},sub:function(width,height){var size=new Size(width,height);this.width-=size.width;this.height-=size.height;return this;},getCopy:function(){return new Size(this);}}
Frame=function(left,top,width,height){this.offset=new Offset();this.size=new Size();this.setup(left,top,width,height);}
Frame.prototype={setup:function(left,top,width,height){if(left instanceof Object){this.setOffset(left);if(top)this.setSize(top);}else{this.offset.setup(left,top);this.size.setup(width,height);}
return this;},setSize:function(width,height){this.size.setup(width,height);return this;},setOffset:function(left,top){this.offset.setup(left,top);return this;},getRight:function(){return this.offset.left+this.size.width;},getBottom:function(){return this.offset.top+this.size.height;},getXC:function(){return this.offset.left+this.size.width/2;},getYC:function(){return this.offset.top+this.size.height/2;},getCenter:function(){return new Offset(this.getXC(),this.getYC());},getCopy:function(){return new Frame(this.offset,this.size);},setRight:function(right){this.offset.left=right-this.size.width;},setBottom:function(bottom){this.offset.top=bottom-this.size.height;},testX:function(x){return x>=this.offset.left&&x<=this.getRight();},testY:function(y){return y>=this.offset.top&&y<=this.getBottom();},testPoint:function(x,y){return this.testX(x)&&this.testY(y);}}

Function.prototype.createDelegate=function(obj,args,appendArgs){var method=this;return function(){var callArgs=args||arguments;if(appendArgs===true){callArgs=Array.prototype.slice.call(arguments,0);callArgs=callArgs.concat(args);}else if(typeof appendArgs=="number"){callArgs=Array.prototype.slice.call(arguments,0);var applyArgs=[appendArgs,0].concat(args);Array.prototype.splice.apply(callArgs,applyArgs);}
return method.apply(obj||window,callArgs);};}

String.prototype.entityDecode=function(){return this.replace('&gt;','>').replace('&lt;','<').replace('&quot;','"').replace('&#039;',"'").replace('&amp;','&');}

ns('utils');utils.Observable=function(config){this.listeners={};if(!this.events)
this.events={};if(config)
$.extend(this,config);this.addListeners(this.listeners);}
utils.Observable.prototype={addEvents:function(events){this.events=$.extend(this.events||{},events);},addListeners:function(listeners){var _this=this;var scope=listeners.scope?listeners.scope:window;$.each(listeners,function(name,handler){var handler_scope=scope;if(!$.isFunction(handler)){if(handler.scope)
handler_scope=handler.scope;handler=handler.handler;}
_this.addListener(name,handler,handler_scope);});},addListener:function(name,handler,scope){if(!(this.events[name]instanceof Array))
this.events[name]=[];this.events[name].push([handler,scope]);},on:function(name,handler,scope){this.addListener(name,handler,scope);},fireEvent:function(name,args){if(!(this.events[name]instanceof Array))
return true;var result=true;if(!args)
args=[];for(var i=0,l=this.events[name].length;i<l;i++){var handler=this.events[name][i][0];var scope=this.events[name][i][1];if(!scope)
scope=window;if(handler.apply(scope,args)===false)
result=false;}
return result;}}
