var Class = {
//创建类
	create : function () {
		return function () {
			this.initialize.apply(this, arguments);
		};
	}
};

var $A = function (a) {
//转换数组
	return a ? Array.apply(null, a) : new Array;
};

var $ = function (id) {
//获取对象
	return document.getElementById(id);
};

var Try = {
//检测异常
	these : function () {
		var returnValue, arg = arguments, lambda, i;
	
		for (i = 0 ; i < arg.length ; i ++) {
			lambda = arg[i];
			try {
				returnValue = lambda();
				break;
			} catch (exp) {}
		}
	
		return returnValue;
	}
	
};

Object.extend = function (a, b) {
//追加方法
	for (var i in b) a[i] = b[i];
	return a;
};

Object.extend(Object, {

	addEvent : function (a, b, c, d) {
	//添加函数
		if (a.attachEvent) a.attachEvent(b[0], c);
		else a.addEventListener(b[1] || b[0].replace(/^on/, ""), c, d || false);
		return c;
	},
	
	delEvent : function (a, b, c, d) {
		if (a.detachEvent) a.detachEvent(b[0], c);
		else a.removeEventListener(b[1] || b[0].replace(/^on/, ""), c, d || false);
		return c;
	},
	
	reEvent : function () {
	//获取Event
		return window.event ? window.event : (function (o) {
			do {
				o = o.caller;
			} while (o && !/^\[object[ A-Za-z]*Event\]$/.test(o.arguments[0]));
			return o.arguments[0];
		})(this.reEvent);
	}
	
});

Function.prototype.bind = function () {
//绑定事件
	var wc = this, a = $A(arguments), o = a.shift();
	return function () {
		wc.apply(o, a.concat($A(arguments)));
	};
};
/////////////////////////////////////////

//绝对坐标
function getLTWH(o){function getCurrentStyle(style){var number=parseInt(o.currentStyle[style]);return isNaN(number)?0:number;}function getComputedStyle(style){return parseInt(document.defaultView.getComputedStyle(o,null).getPropertyValue(style));}var oLTWH={"left":o.offsetLeft,"top":o.offsetTop,"width":o.offsetWidth,"height":o.offsetHeight};while(true){o=o.offsetParent;if(o==(document.body&&null))break;oLTWH.left+=o.offsetLeft;oLTWH.top+=o.offsetTop;if(CDrag.IE){oLTWH.left+=getCurrentStyle("borderLeftWidth");oLTWH.top+=getCurrentStyle("borderTopWidth");}else{oLTWH.left+=getComputedStyle("border-left-width");oLTWH.top+=getComputedStyle("border-top-width");}}return oLTWH;}
function XYwin(v) { var z=(navigator.appVersion.indexOf("MSIE") != -1)?Array(document.body.clientHeight,document.body.clientWidth):Array(window.innerHeight,window.innerWidth); return z[v]; }

var bkExtend = function(){
	var args = arguments;
	if (args.length == 1) args = [this, args[0]];
	for (var prop in args[1]) args[0][prop] = args[1][prop];
	return args[0];
};

function bkClass() { }
bkClass.prototype.construct = function() {};
bkClass.extend = function(def) {
  var classDef = function() {
      if (arguments[0] !== bkClass) { return this.construct.apply(this, arguments); }
  };
  var proto = new this(bkClass);
  bkExtend(proto,def);
  classDef.prototype = proto;
  classDef.extend = this.extend;      
  return classDef;
};

function $BK(elm) {if(typeof(elm) == "string"){elm = document.getElementById(elm);}return (elm && !elm.appendTo) ? bkExtend(elm,createElm.prototype) : elm;}
var createElm=bkClass.extend({
	construct:function(elm){elm=document.createElement(elm);elm = $BK(elm);return elm;},
	appendTo:function(elm){elm.appendChild(this);return this;},
	appendBefore:function(elm){elm.parentNode.insertBefore(this,elm);return this;},
	setContent:function(c){this.innerHTML = c;return this;},
	setStyle : function(st){var elmStyle = this.style;for(var itm in st){switch(itm){
				case 'float':elmStyle['cssFloat'] = elmStyle['styleFloat'] = st[itm];break;
				case 'opacity':elmStyle.opacity = st[itm];elmStyle.filter = "alpha(opacity=" + Math.round(st[itm]*100) + ")";break;
				case 'className':this.className = st[itm];break;
				default:elmStyle[itm] = st[itm];	
	}}return this;},
	addClass : function(cls){this.className =cls;return this;},	
	removeClass : function(){this.className =' ';return this;},
	remove : function(){this.parentNode.removeChild(this);return this;},	
	setAttributes : function(at){for(var itm in at){this[itm] = at[itm];}return this;}
});
////////////////////////////////////////////////////
var CDrag = Class.create();
CDrag.IE = /MSIE/.test(window.navigator.userAgent);

CDrag.load = function (obj_string, func, time) {
//加载对象
	var index = 0, timer = window.setInterval(function () {
		try {
			if (eval(obj_string + ".loaded")) {
				window.clearInterval(timer);
				func(eval("new " + obj_string));
			}
		} catch (exp) {}

		if (++ index == 20) window.clearInterval(timer);
	}, time + index * 3);
};

CDrag.database = {
//数据存储
	json : null,
	
	parse : function (id) {
	//查找资源
		var wc = this, json = wc.json, i;
		for (i in json) {
			if (json[i].id == id)
				return json[i];
		}
	}
};

CDrag.Ajax = Class.create();

Object.extend(CDrag.Ajax, {

	getTransport: function() {
		return Try.these(
			function () { return new ActiveXObject('Msxml2.XMLHTTP') },
			function () { return new ActiveXObject('Microsoft.XMLHTTP') },
			function () { return new XMLHttpRequest() }
		) || false;
	}
	
});

CDrag.Ajax.prototype = {

	initialize : function (url) {
	//初始化
		var wc = this;
		wc.ajax = CDrag.Ajax.getTransport();
	},
	
	load : function (func) {
		var wc = this, ajax = wc.ajax;
		if (ajax.readyState == 4 && ajax.status == 200)
			func(ajax.responseText);
	},
	
	send : function (url, func) {
		var wc = this, ajax = wc.ajax;
		var querys = url;
		if(arguments.length==3){
			var method='post';
			var postStr=arguments[2];
		}else{
			var method="get";
			var postStr=null;
		}
		ajax.open(method, querys, true);//setRequestHeader(),要放到 open()的后面
		if(method=='post') ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.onreadystatechange = wc.load.bind(wc, func);
		ajax.send(postStr);
	}
};

CDrag.Table = Class.create();

CDrag.Table.prototype = {
//列的拖拽暂时不考虑

	initialize : function () {
	//初始化
		var wc = this;
		wc.items = []; //创建列组
	},
	
	add : function () {
	//添加列
		var wc = this, id = wc.items.length, arg = arguments;
		return wc.items[id] = new CDrag.Table.Cols(id, wc, arg[0]);
	}
};
String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,'')}
