﻿String.prototype.leftTrim=function(){
    return this.replace(/^\s+/g,"");
};
String.prototype.rightTrim=function(){
    return this.replace(/\s+$/g,"");
};
String.prototype.trim=function(){
    return this.replace(/(^\s+)|(\s+$)/g,"");
};
String.prototype.isInt=function(){
    var parnter=/^([+-])?0|([1-9][0-9]*)$/;
    return parnter.test(this);
};
String.prototype.isFloat=function(){
    var parnter=/^([+-])?0|([1-9][0-9]*)([.][0-9]+)?$/;
    return parnter.test(this);
};
String.prototype.isMoney=function(){
    var parnter=/^([+-])?(0|[1-9][0-9]*)(.[0-9]{1,2})?$/;
    return parnter.test(this);
};
String.prototype.isFixPhone=function(){
    var parnter=/^(0[\d]{2,3})[\d]{7,8}$/;
    return parnter.test(this);
};
String.prototype.isMobile=function(){
    var parnter=/^1[35]\d[\d]{4}[\d]{4}$/;
    return parnter.test(this);
};
String.prototype.isEmail=function(){
    var parnter=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    return parnter.test(parnter);
};
Function.prototype.bind = function(scope){
	var pointer = this;
	return function(){
		return pointer.apply(scope, arguments);
	}
}
Core={
        author:'heroyuchao',
        version:"0.0.0.1",
        $:function(id){
            return document.getElementById(id);
        },
        isIE:function(){
	        return /msie/i.test(navigator.userAgent);
        },
        isFF:function(){
            return /firefox/i.test(navigator.userAgent);
        },
        $void:function(){},
        clientRect:function(){
            var w=(document.documentElement&&document.documentElement.clientWidth)?document.documentElement.clientWidth:document.body.offsetWidth;
            var h=(document.documentElement&&document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.offsetHeight;
            return {width:w,height:h};
        },
        pageRect:function(){
            var w=document.body.offsetWidth;
            var h=document.body.offsetHeight;
            return {width:w,height:h};
        },
        domEventManager:(function(){
            return new function(){
                this.addEvent=function(node,eventType,handler){
                    if(window.attachEvent){
                        node.attachEvent("on"+eventType,handler);
                    }
                    else{
                        node.addEventListener(eventType,handler,true);
                    }
                };
                this.removeEvent=function(node,eventType,handler){
                    if(window.detachEvent){
                        node.detachEvent("on"+eventType,handler);
                    }
                    else{
                        node.removeEventListener(eventType,handler,false);
                    }
                }
            }
        })(),
        $namespace:function(){
            var a=arguments, o=null, i, j, d, rt;
            for (i=0; i<a.length; ++i) {
                d=a[i].split(".");
                rt = d[0];
                eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
                for (j=1; j<d.length; ++j) {
                    o[d[j]]=o[d[j]] || {};
                    o=o[d[j]];
                }
            }
        },
        extend:function(superClass,subClass){
            var constructor=subClass.prototype.constructor;
            subClass.prototype=new superClass();
            subClass.prototype.constructor=constructor;
            subClass.superClass=superClass;
        },
        createDelegate:function(handler,scope){
            var args=Array.apply(this, arguments);
            args.splice(0,2);
            return function(){
               if(typeof(args)=="undefined") args=[];
               return handler.apply(scope,args);
            }
        },
        id:function(){
            
        }
};
Core.Observable=function(){

    this.listeners={};
    
    this.addEvent=function(eventType,handler,config){
        if(!this.listeners[eventType]){
            this.listeners[eventType]=[];
        }
        var evt={handler:handler,config:config};
        this.listeners[eventType].push(evt);
    }
    
    this.removeEvent=function(eventType,handler){
        if(this.listeners[eventType]){
            var events=this.listeners[eventType];
            for(var i=0;i<events.length;i++){
                if(events[i].handler==handler){
                    events.splice(i,1);
                    break;
                }
            }
        }
    };
    
    this.fireEvent=function(eventType){
        var args=Array.apply(this, arguments);
        if(args.length>0){
            args.splice(0,1);
            if(this.listeners[eventType]){
                var events=this.listeners[eventType];
                for(var i=0;i<events.length;i++){
                    var scope=(events[i].config&&events[i].config.scope)?events[i].config.scope:this;
                    var delay=(events[i].config&&events[i].config.delay)?events[i].config.delay:1;
                    var handler=events[i].handler;
                    setTimeout(function(){return handler.apply(scope,args);},delay);
                }
            }
        }
    };
};


