﻿////阴影类
//构造函数：
//boderTemplateEl:半透明边框模板实例
var boxShadow = function(boderTemplateEl, isShowMask)
{
    //用于存放内容的Div
    this.containerID = "cid" + Math.ceil( Math.random() * 100000);
    this.mainTable = document.createElement("table");
    this.mainTable.style.position = "absolute";
    this.mainTable.style.top = "20px";
    this.mainTable.style.left = "20px";
    this.mainTable.style.boder = "0px";
    this.mainTable.cellpadding = "0";
    this.mainTable.cellspacing = "0";
    this.mainTable.insertRow(-1);
    this.mainTable.rows[0].insertCell(-1);
    this.mainTable.rows[0].cells[0].valign = "top";
    this.boderList = {};
    boxShadow.baseZIndex += 10;
    this.currentBaseZIndex = boxShadow.baseZIndex;
    //this.testid = id + "_";
    
    var relativeEl = document.createElement("div");
    relativeEl.style.position = "relative";
    this.mainTable.rows[0].cells[0].appendChild(relativeEl);
    
    var containerObj = document.createElement("div");
    containerObj.id = this.containerID;
    this.mainTable.id = this.containerID + "_tb";
    this.mainTable.style.zIndex = this.currentBaseZIndex;
    relativeEl.appendChild(containerObj);
    
    this.bIsIE6 = window.navigator["userAgent"].indexOf("MSIE 6.0") > -1;
    
    var icount = 1;
    for(var borderEl in boderTemplateEl)
    {
        var divEl = document.createElement("div");
        this.boderList[borderEl] = divEl;
        divEl.id = this.containerID+"_"+borderEl;
        if(this.bIsIE6)
        {
            divEl.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(nabled=true, sizingMethod=scale, src=\""+boderTemplateEl[borderEl].pic+"\")";
        }
        else
        {
            divEl.style.backgroundImage = "url("+ boderTemplateEl[borderEl].pic +")";
        }
        divEl.style.position = "absolute";
        divEl.style.fontSize = "1px";
        
        divEl.style.zIndex = this.currentBaseZIndex + icount;
        for(var attr in boderTemplateEl[borderEl])
        {
            if(attr == "pic")
            {
                continue;
            }
            if(this.bIsIE6 &&  attr.toLowerCase() == "right")
            {
                try
                {
                    divEl.style[attr] = parseInt(boderTemplateEl[borderEl][attr]) + 0 + "px";
                    divEl.defaultRight = parseInt(boderTemplateEl[borderEl][attr]);
                }catch(e){}
                continue;
            }
            
            if(this.bIsIE6 &&  attr.toLowerCase() == "bottom")
            {
                try
                {
                    divEl.style[attr] = parseInt(boderTemplateEl[borderEl][attr]) + 2 + "px";
                }catch(e){}
                continue;
            }
            var a = "";
            
            divEl.style[attr] = boderTemplateEl[borderEl][attr];
        }
        relativeEl.appendChild(divEl);
    }
}



//绑定到Dom对象
//obj:要绑定到的Dom对象。
boxShadow.prototype.bind = function(obj)
{

    //需要移除的外框
    var elNeedToRemove = null;
    var findEl = obj;
    while(findEl != document.body)
    {
        findEl = findEl.parentNode;
        
        if(findEl.tagName.toLowerCase() == "table" &&  /cid[\d]+\_tb/i.test(findEl.id))
        {
            elNeedToRemove = findEl;
        }
    }
    
    document.body.appendChild(this.mainTable);
    var afterObj = document.getElementById(this.containerID);
    if(this.boderList["elLeft"] && this.bIsIE6)
    {
        this.boderList["elLeft"].style.height = obj.clientHeight + "px";
    }
    if(this.boderList["elRight"] && this.bIsIE6)
    {
        this.boderList["elRight"].style.height = obj.clientHeight + "px";
    }
    obj.style.position = "static";
    obj.style.display = "block";
    this.mainTable.style.left = !obj.style.left?"100px":parseInt(obj.style.left.replace("px","")) - 3 + "px";
    this.mainTable.style.top = !obj.style.top?"100px":parseInt(obj.style.top.replace("px","")) - 3 + "px";
    obj.style.left = "1px";
    obj.style.top = "1px";
    
    if(this.bIsIE6)
    {
        this.ie6IntervalSetBorderHeight = window.setInterval
        (
            function(cid, innerObj)
            {
                return function()
                {
                    var lel = document.getElementById(cid + "_elLeft");
                    var rel = document.getElementById(cid + "_elRight");
                    var trel = document.getElementById(cid + "_elTopRight");
                    var brel = document.getElementById(cid + "_elBottomRight");
                    
                    if(innerObj.offsetHeight != lel.clientHeight)
                    {
                        lel.style.height = innerObj.offsetHeight + "px";
                    }
                    if(innerObj.offsetHeight != rel.clientHeight)
                    {
                        rel.style.height = innerObj.offsetHeight + "px";
                    }
                    rel.style.right = rel.defaultRight + ((innerObj.offsetWidth + 1) %  2) + "px";
                    trel.style.right = trel.defaultRight + ((innerObj.offsetWidth + 1) %  2) + "px";
                    brel.style.right = brel.defaultRight + ((innerObj.offsetWidth + 1) %  2) + "px";
                    //alert(trel.defaultRight);
                    //alert();
                }
            }(this.containerID, obj)
            ,35
        );
    }
    afterObj.parentNode.insertBefore(obj,afterObj);
    if(elNeedToRemove)
    {
        elNeedToRemove.parentNode.removeChild(elNeedToRemove);
    }
}

///设置关闭按钮
//closeButton: 设置点击关闭的按钮
boxShadow.prototype.setCloseButton = function(closeButton)
{
    this.closeEventHanlder = this.doClose(this.mainTable.id + "", this);
    if(window.addEventListener)
    {
        closeButton.addEventListener("click",this.closeEventHanlder,false);
    }
    else
    {
        closeButton.attachEvent("onclick",this.closeEventHanlder);
    }
}

///设置拖动条
//dragEl:接收拖动事件的DOM
boxShadow.prototype.setDragHanlder = function(dragEl)
{
    this.dragWindowMousedownHandler = this.dragBarMouseDown(this);
    this.dragWindowMouseUpHandler = this.dragBarMouseUp(this);
    this.dragWindowMouseMoveHandler = this.dragBarMouseMove(this);
    this.dragElement = dragEl;
    if(window.addEventListener)
    {
        dragEl.addEventListener("mousedown", this.dragWindowMousedownHandler, false);
        document.body.addEventListener("mouseup", this.dragWindowMouseUpHandler, false);
        document.body.addEventListener("mousemove",  this.dragWindowMouseMoveHandler, false)
    }
    else
    {
        dragEl.attachEvent("onmousedown", this.dragWindowMousedownHandler);
        document.body.attachEvent("onmouseup", this.dragWindowMouseUpHandler);
        document.body.attachEvent("onmousemove",  this.dragWindowMouseMoveHandler);
    }
}

//private
boxShadow.prototype.doClose = function(cid, obj)
{
    return function(e)
    {
        
        var evt = e || evt;
        var closeButton = evt.srcElement || evt.target;
        if(window.detachEvent)
        {
            closeButton.detachEvent("onclick",obj.closeEventHanlder);
            if(obj.dragWindowMouseUpHandler)
            {
                document.body.detachEvent("onmouseup",obj.dragWindowMouseUpHandler);
                document.body.detachEvent("onmousemove",obj.dragWindowMouseMoveHandler);
                obj.dragElement.detachEvent("onmousedown", obj.dragWindowMousedownHandler);
            }
        }
        else
        {
            closeButton.removeEventListener("click",obj.closeEventHanlder,false);
            if(obj.dragWindowMouseUpHandler)
            {
                document.body.removeEventListener("mouseup",obj.dragWindowMouseUpHandler,false);
                document.body.removeEventListener("mousemove",obj.dragWindowMouseMoveHandler,false);
                obj.dragElement.removeEventListener("mousedown", obj.dragWindowMousedownHandler,false);
            }
        }
        if(obj.ie6IntervalSetBorderHeight) 
        {
            window.clearInterval(obj.ie6IntervalSetBorderHeight);
        }
        
        
        
//        this.dragWindowMousedownHandler = this.dragBarMouseDown(this);
//        this.dragWindowMouseUpHandler = this.dragBarMouseUp(this);
//        this.dragWindowMouseMoveHandler = this.dragBarMouseMove(this);
//        this.dragElement = dragEl;
        
        document.getElementById(cid).style.display = "none";
    }
}

//private
boxShadow.prototype.dragBarMouseDown = function(obj)
{
    
    return function(e)
    {
        var evt = e || window.event; 
        obj.isDragStart = true;
        obj.startMouseX = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        obj.startMouseY = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        
        obj.startTbX = obj.mainTable.offsetLeft;// + document.body.scrollLeft + document.documentElement.scrollLeft;
        obj.startTbY = obj.mainTable.offsetTop;// + document.body.scrollTop + document.documentElement.scrollTop;
    }
}

//private
boxShadow.prototype.dragBarMouseUp = function(obj)
{
    return function()
    {
        obj.mainTable.focus();
        obj.isDragStart = false;
    }
}
//private
boxShadow.prototype.dragBarMouseMove = function(obj)
{
    return function(e)
    {
        if(!obj.isDragStart) return;
        
        if(!obj.startTbX) return;
        
        var evt = e || window.event; 
        obj.currentMouseX = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        obj.currentMouseY = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        
        var newTop = obj.startTbY + (obj.currentMouseY - obj.startMouseY);
        var newLeft = obj.startTbX + (obj.currentMouseX - obj.startMouseX);
        
        if(newTop < 0) newTop = 0;
        if(newLeft < 0) newLeft = 0;
        if(newLeft + obj.mainTable.offsetWidth > document.body.offsetWidth) newLeft = document.body.offsetWidth - obj.mainTable.offsetWidth;
        
        
        
        obj.mainTable.style.top = newTop + "px";
        obj.mainTable.style.left = newLeft + "px";
    }
}

///半透明边框模板类
var boderTemplate = function()
{
    this.elTopLeft = {pic:"images/cl_boder_topleft.png",left:"-29px",top:"-26px",width:"29px",height:"26px"};
    this.elTop = {pic:"images/cl_boder_top.png",left:"0px",top:"-26px",height:"26px", width:"100%"};
    this.elTopRight = {pic:"images/cl_boder_topright.png",right:"-33px",top:"-26px",width:"33px",height:"26px"};
    this.elLeft = {pic:"images/cl_boder_left.png",left:"-29px",top:"0px",width:"29px", height:"100%"};
    this.elRight = {pic:"images/cl_boder_right.png",right:"-33px",top:"0px",width:"33px", height:"100%"};
    this.elBottomLeft = {pic:"images/cl_boder_bottomleft.png",left:"-29px",bottom:"-35px",width:"29px",height:"35px"};
    this.elBottom = {pic:"images/cl_boder_bottom.png",bottom:"-35px",left:"0px",height:"35px", width:"100%"};
    this.elBottomRight = {pic:"images/cl_boder_bottomright.png",right:"-33px",bottom:"-35px",width:"33px",height:"35px"};
}


///半透明边框模板类（白色背景，橙色边框）
var boderTemplate_whiteBgOrangeBorder = function()
{
    this.elTopLeft = {pic:"images/wo_boder_topleft.png",left:"-7px",top:"-8px",width:"7px",height:"8px"};
    this.elTop = {pic:"images/wo_boder_top.png",left:"0px",top:"-8px",height:"8px", width:"100%"};
    this.elTopRight = {pic:"images/wo_boder_topright.png",right:"-9px",top:"-8px",width:"9px",height:"8px"};
    this.elLeft = {pic:"images/wo_boder_left.png",left:"-7px",top:"0px",width:"7px", height:"100%"};
    this.elRight = {pic:"images/wo_boder_right.png",right:"-9px",top:"0px",width:"9px", height:"100%"};
    this.elBottomLeft = {pic:"images/wo_boder_bottomleft.png",left:"-7px",bottom:"-9px",width:"7px",height:"9px"};
    this.elBottom = {pic:"images/wo_boder_bottom.png",bottom:"-9px",left:"0px",height:"9px", width:"100%"};
    this.elBottomRight = {pic:"images/wo_boder_bottomright.png",right:"-9px",bottom:"-9px",width:"9px",height:"9px"};
}

//z index开始计数
boxShadow.baseZIndex = 900




