﻿/*
*author: lionelpang
*/
var XJS;
/*
*eXtensible JavaScript , XJS
*/
(function(){
	var _undefined, //private undefined object
        _userAgent = navigator.userAgent.toLowerCase(); //browser information object
	XJS = {
		isObject : function(object)//判断是不是对象
		{
			return typeof(object)=='object';
		},
		isArray : function(object)//判断是不是数组
		{
			return object && object.constructor==Array;
		},
		isFunction: function(object)//判断是不是函数
		{
			return typeof(object)=='function';
		},
		isClass: function(object)
        {
            return typeof(object)=='function' && object.classType=='class';
        },
        isInterface: function(object)
        {
            return typeof(object)=='function' && object.classType=='interface';
        },
	//browser information	 (use jquery's code)
		browser:{
			version: (_userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
			safari: /webkit/.test( _userAgent ),
			opera: /opera/.test( _userAgent ),
			msie: /msie/.test( _userAgent ) && !/opera/.test( _userAgent ),
			mozilla: /mozilla/.test( _userAgent ) && !/(compatible|webkit)/.test( _userAgent )
		},
	//is or not standard mode
		isStandardMode:document.compatMode=="CSS1Compat"?true:false,
		addEvent:function(){
			if(document.addEventListener)
			{
				return function(el, evt, handler, capture){
					return el.addEventListener(evt, handler, capture||false);
				};
			}
			else if(document.attachEvent)
			{
				return function(el, evt, handler, capture){
					return el.attachEvent('on'+evt, handler);
				};
			}
		}(),
		removeEvent:function(){
			if(document.removeEventListener)
			{
				return function(el, evt, handler, capture){
					return el.removeEventListener(evt, handler, capture||false);
				};
			}
			else if(document.detachEvent)
			{
				return function(el, evt, handler, capture){
					return el.detachEvent('on'+evt, handler);
				};
			}

		}(),
    //cancel event's bubble mode
        stopBubble:function(){
			var evt = arguments[0] || window.event;
			if(evt){
				if(evt.stopPropagation)
					evt.stopPropagation();
				else	
                    evt.cancelBubble = true;
			}
	    }        
	};
    XJS.fireEvent = function(){
        if(XJS.browser.msie)
        {
            return function(el, evt_name){
                el.fireEvent('on'+evt_name);
            };
        }
        else
        {
            return function(el, evt_name){
                var _event = document.createEvent("Events");
				_event.initEvent(evt_name, true, false);
				el.dispatchEvent(_event);
            };
        }
    }();
//类继承
	var _extend_fn = function(subClass)
	{
		return function(superClass)
		{
			if((XJS.isClass(superClass) || XJS.isInterface(superClass)) && !(subClass._defined || subClass._implemented || subClass._extended) && subClass.classType == superClass.classType)
			{
				subClass.prototype = new superClass();
				subClass.prototype.superclass = superClass;
				subClass.prototype.parent = superClass.prototype;
				subClass.prototype.constructor = subClass;
				subClass._extended = true;
			}				
			return subClass;
		};
	};
//接口继承		
	var _implement_fn = function(subClass){
		return function(interfaceClasses)
		{
			if(!(subClass._defined || subClass._implemented))
			{
				var _a_interfaces = (XJS.isArray(interfaceClasses) && interfaceClasses) || [interfaceClasses];
				for(var i=0;i<_a_interfaces.length;++i)
				{
					if(XJS.isClass(_a_interfaces[i]) || XJS.isInterface(_a_interfaces[i]))
					{
						_override_fn(_a_interfaces[i].prototype, subClass.prototype,false);
						_override_fn(_a_interfaces[i], subClass,false);
					}
				}
				subClass._implemented = true;
			}
			return subClass;
		};
	};
//属性或方法重写		
	var _override_fn = function(src, dest, isdef){
		var _regexp = new RegExp([
			'^', (!isdef && '(initialize)|')||'', '(constructor)|(superclass)|(parent)|(extend)|(_extended)|(implement)|(_implemented)|(define)|(_defined)$'
		].join(''));
		for(var p_m in src)
		{
			if(!_regexp.test(p_m))
				dest[p_m] = src[p_m];
		}
	};
//类的定义
	var _define_fn = function(subClass)
	{
		return function(defineBody)
		{					
			if(XJS.isObject(defineBody.public))
			{
				_override_fn(defineBody.public, subClass.prototype, true);
			}
			if(subClass.classType=='class' && XJS.isObject(defineBody.static))
			{
				_override_fn(defineBody.static, subClass, true);
			}
			subClass._defined = true;
			return subClass;
		};
	};
    var _create_fn = function(type){
        var _class;
        switch(type){
            case 'interface':
				_class = function(){
				};
				_class.extend = _extend_fn(_class);		
				_class.define = _define_fn(_class);
                _class.classType = 'interface';
                break;
            case 'class':
            default:
				_class = function()	{
					XJS.isFunction(this.initialize) && this.initialize.apply(this, arguments); 
				};
				_class.extend = _extend_fn(_class);		
				_class.define = _define_fn(_class);
                _class.implement = _implement_fn(_class);
                _class.classType = 'class';
        }
        return _class;
    };
	XJS.Class = function ()//创建类 
	{
		return _create_fn('class');
	};
    XJS.Interface = function()  //创建接口
    {
        return _create_fn('interface');
    };
})();
