function package(packageName) {
	var parts = packageName.split(".");
	if (!eval("window." + parts[0])) {
		eval("window." + parts[0] + " = {}");
	}
	
	var s = parts[0];
	for (var i=1; i<parts.length; i++) {
		s += "." + parts[i];
		if (!eval(s)) {
			eval(s + " = {}");
		}
	}
}

String.prototype.trim = function()
{
    // 用正则表达式将前后空格
    // 用空字符串替代。
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

if (typeof Array.prototype.pop != "function") { // IE 5.x fix from Igor Poteryaev.
    Array.prototype.pop = function() {
        if (this.length === 0) {return UNDEFINED;}
        return this[--this.length];
    };
}
    
Array.prototype.remove = function(index) {
   	if (index >= this.length || index < 0) {
 		return null;
   	}
   	    	
   	var deleteObject = this[index];
   	
  	for (var i=index; i<this.length-1; i++) {
  		this[i] = this[i+1];
  	}
  	this.length--;

  	return deleteObject;
}
    
if (typeof Array.prototype.push != "function") { // IE 5.x fix from Igor Poteryaev.
    Array.prototype.push = function() {
        for (var i = 0; i < arguments.length; ++i) {this[this.length] = arguments[i];}
        return this.length;
    };
}

Date.prototype.format = function(format) //author: meizz
{
  var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
  }
  if(/(y+)/.test(format)) {
  	format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  }
  
  for(var k in o) {
  	if(new RegExp("("+ k +")").test(format)) {
    	format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
  	}
  }
  return format;
}

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;	

    elements.push(element);
  }

  return elements;
}

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}

Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}

Object.extend = function(destination, source) {
  for (property in source) {
  	if (property != "isInstanceOf") {
    	destination[property] = source[property];
    }
  }

  return destination;
}

Function.prototype.extend = function(source) {
	var isInstanceOf = this.prototype.isInstanceOf;
	
	this.prototype.isInstanceOf = function(theClassType) {
		var ret = false;
		if (typeof isInstanceOf == "function") {
			ret = isInstanceOf.apply(this, [theClassType]);
		}
		
		if (!ret) {
			if (typeof source.isInstanceOf == "function") {
				ret = source.isInstanceOf(theClassType);
			}
		}
		return ret;
	}
		
	Object.extend(this.prototype, source);
		
	//TODO: clear source 
	var sysFunction = ["toString", "valueOf", "toLocaleString"]; 
	
	for (var i=0; i<sysFunction.length; i++) {
		var p = sysFunction[i];
		var f = source[p];
		if (typeof f == "function") {
			this.prototype[p] = f;
		}		
	}
	

	this.$super = function(bindTarget) {
		var superFuncs = {};
		for (var p in source) {
			var f = source[p];
			if (typeof f == "function") {
				superFuncs[p] = f.bind(bindTarget);
			}
		}
		
		for (var i=0; i<sysFunction.length; i++) {
			var p = sysFunction[i];
			var f = source[p];
			if (typeof f == "function") {
				superFuncs[p] = f.bind(bindTarget);
			}		
		}
		return superFuncs;
	}
	
	return this;
}
Function.prototype.extendClass = Function.prototype.extend;

Function.prototype.extendInstance = function(source, noSuper) {
	if (!noSuper) {
		var $super = {};
		
		for (var p in source) {
			var f = source[p];
			if (typeof f == "function") {
				$super[p] = f;
			}
		}
			
		var sysFunction = ["toString", "valueOf", "toLocaleString"]; 
		
		for (var i=0; i<sysFunction.length; i++) {
			var p = sysFunction[i];
			var f = source[p];
			if (typeof f == "function") {
				$super[p] = f;
			}		
		}
		
		source.$super = $super;
	}
	
	var Constructor = this;
	
	source.isInstanceOf = function(theClassType) {	
		var ret = (theClassType == Constructor);
		if (!ret && (typeof $super.isInstanceOf == "function")) {
			ret = $super.isInstanceOf(theClassType);
		}
		return ret;
	}			

	Constructor.isClassOf = function(instance) {
   	if (typeof instance.isInstanceOf == "function") {
   		return instance.isInstanceOf(Constructor);
   	}
   	else {
   		return false;
   	}
	}
	
	return source;
}


function ClassBuilder() {
	var _controlsCreated = [];

	function ClassBuilder() {
	}
	
	function initialize() {
		this.$super = {};
		if (this.constructor.$super) {
			this.$super = this.constructor.$super(this);
		}
		
		if (typeof this.initialize == "function") {
			this.initialize.apply(this, arguments);
		}
	}
	
	with (ClassBuilder) {
		prototype.create = function(classType) {
			var _id = null;
			var _index = 0;
	    function Constructor() {
	      _controlsCreated.push(this);
	      _id = "aw" + _index;
	      _index = _controlsCreated.length - 1;
	      initialize.apply(this, arguments);
	    }
	    
	    classType.isClassOf = function(instance) {
	    	if (typeof instance.isInstanceOf == "function") {
	    		return instance.isInstanceOf(classType);
	    	}
	    	else {
	    		return false;
	    	}
	    }
	    
	    
	    with (Constructor) {
		    prototype.isInstanceOf = function(theClassType) {
		    	return (theClassType == classType);
		    }
		    
				prototype.getId = function() {
					return _id;
				}
				prototype.setId = function(theId) {
					_id = theId;
				}	
				prototype.getIndex = function() {
					return _index;
				}	    
	  	}
	    
	    return Constructor;
		}
		
		
		prototype.refreshAll = function() {
			for (var i=0; i<_controlsCreated.length; i++) {
				var control = _controlsCreated[i];
				if (typeof control.registerEventListeners == "function") {
					control.registerEventListeners();
				} 
			}
		}

		//释放资源，防止内存泄露
		prototype.releaseAll = function() {
			for (var i=0; i<_controlsCreated.length; i++) {
				var control = _controlsCreated[i];
				if (control && typeof control.releaseAll == "function") {
					control.releaseAll();
				} 
			}
		}
		
		prototype.getObjectOf = function(el) {
			if (typeof el.aw != "undefined") {
				return _controlsCreated[new Number(el.aw)];
			}
		}
	}
	return new ClassBuilder();
}

var classBuilder = new ClassBuilder();

ClassBuilder.releaseAll = function() {
	classBuilder.releaseAll();
	window.detachEvent("onunload", ClassBuilder.releaseAll);
}

ClassBuilder.refreshAll = function() {
	classBuilder.refreshAll();
	window.detachEvent("onload", ClassBuilder.refreshAll);
}

//自动释放资源
window.attachEvent("onunload", ClassBuilder.releaseAll);

try {
window.attachEvent("onload", ClassBuilder.refreshAll);
}
catch (ex) {
	alert(ex.description);
}
