/* 
 * 主类
 * version : 0.2
 * author : 熊星
 */
if (typeof KOP == "undefined" || !KOP) {
    var KOP = {};
}
KOP.rootPath;
/*注册包名*/
KOP.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=KOP;
        // KOP is implied, so it is ignored if it is included
        for (j=(d[0] == "KOP") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
}
// 获取KOP所在目录的HTTP路径
_scripts = document.getElementsByTagName("script");
for(var i=0;i<_scripts.length;i++){
    var index = _scripts[i].src.indexOf("/kop.js");
    if(index != -1){
        KOP.rootPath = _scripts[i].src.substr(0,index+1);
        break;
    }
}
// 自动创建id
KOP.maxId = 0;
KOP.getId = function(){
    return "kop" + (++KOP.maxId);
}
KOP.namespace("widget");
/***********************DOM 操作类*********************/
/**
 * DOM常用操作类
 * version 0.1
 * author : 熊星
 */
KOP.Dom={
    get : function(id){
        return ((typeof(id) == "string") ? document.getElementById(id) : id);
    },
    // 添加CLASS
    addClass : function(id,className){
        o = KOP.Dom.get(id);
        current = o.className || "";
        if(current.indexOf(className) == -1)
            o.className = current + " " + className;
    },
    // 删除CLASS
    removeClass : function(id,className){
        o = KOP.Dom.get(id);
        current = o.className || "";
        if(current.indexOf(className) != -1){
            o.className = current.replace(className,"");
        }
    },
    /**
     * 在后面插入元素
     */
    insertAfter: function(newNode, referenceNode) {
        nn = KOP.Dom.get(newNode);
        rn =  (typeof(referenceNode) == "string") ? KOP.Dom.get(referenceNode) : referenceNode;

        if (rn.nextSibling) {
            return rn.parentNode.insertBefore(nn, rn.nextSibling);
        } else {
            return rn.parentNode.appendChild(nn);
        }
    },
    /**
     * 添加事件
     */
    addEvent : function(o,eventName,eventFunction){
        obj = KOP.Dom.get(o);
        if(document.addEventListener){
           obj.addEventListener(eventName,eventFunction,false);
        }else{
           obj.attachEvent("on"+eventName,eventFunction);
        }
    },
    /*页面加载完后执行代码*/
    onLoad : function(eventFunction){
      this.addEvent(window, "load", eventFunction);
    },
    /**
     * 获取元素的X坐标和Y坐标(最下面的Y)
     */
    getXY : function(o){
        e = KOP.Dom.get(o);
        var x = e.offsetLeft, y = e.offsetTop + e.clientHeight;
        while(e=e.offsetParent){x += e.offsetLeft; y += e.offsetTop;}
        return {'x':x,'y':y};
    },
    /*获取用元素处于中间部位的XY*/
    getCenterXY : function(o){
        var doc = document;
        // 判断是否标准模式
        var cw = doc.compatMode == "BackCompat"?doc.body.clientWidth:doc.documentElement.clientWidth;
        var ch = doc.compatMode == "BackCompat"?doc.body.clientHeight:doc.documentElement.clientHeight;
        // 滚动条
        var sw = Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
        var sh = Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);

        x = (cw - o.getAttribute("width")) / 2 +sw;
        y = (ch - o.getAttribute("height")) / 2 + sh;
        return {'x':x,'y':y};
    },
    /*获取页面大小*/
    getScreen : function(){
        var xdde = document.documentElement,xdb = document.body;
        var iwidth = Math.max(xdde.clientWidth,xdb.clientWidth,xdde.scrollWidth,xdb.scrollWidth);
        var iheight = Math.max(xdde.clientHeight,xdb.clientHeight,xdde.scrollHeight,xdb.scrollHeight);

        return {'width':iwidth,'height':iheight}
    },
    /**
     * 获取物体最左上角的坐标
     */
    getPosition : function(e){
        o = KOP.Dom.get(e);
        var x = 0, y = 0;
		do{x += o.offsetLeft;y += o.offsetTop;}while((o=o.offsetParent));
		return {'x':x,'y':y};
    },
    /**
     * 重新设置坐标
     */
    setPosition:function(o,x,y){
        obj = KOP.Dom.get(o);
		obj.style.left = x + "px";
		obj.style.top = y + "px";
	},
    /**
     * 获取元素表样式信息
     */
    getStyle:function(o,stylename){
        d = KOP.Dom.get(o);
		if(d.currentStyle){
			return d.currentStyle[stylename];
		}else{
			return window.getComputedStyle(d,null).getPropertyValue(stylename);
		}
	},
    /**
     * 判断浏览器是否是IE
     */
    isIE : function(){
        return (window.navigator.appName=="Microsoft Internet Explorer"?true:false);
    },
    isIE6 : function(){
        return (window.navigator.appName=="Microsoft Internet Explorer" && window.navigator.appVersion.indexOf("MSIE 6.0")>0);
    }
}
/****************************** 常用 数字，字符串，函数操作类*************/
/**
 * 常用数字，字符串，函数操作类
 * version : 0.1
 * author : 熊星
 */
/**
 * 把某个函数绑定到某1个事件，但在该函数中使用this得到的是传过来的obj
 */
Function.prototype.bind = function(obj){
    var owner = this,args = Array.prototype.slice.call(arguments),callobj = Array.prototype.shift.call(args);
    return function(e){e=e||top.window.event||window.event;owner.apply(callobj,args.concat([e]));};
}
/*去掉空格*/
String.prototype.trim = function(){
    try {
        return this.replace(/^\s+|\s+$/g, "");
    } catch(e) {
        return this;
    }
}


