﻿/*
================================
   Author:     binchen.roc
   DateCreated: 2007.09.10
   DateUpdated: 2007.11.23
   
   var p = new popWin([width],[left],[top],[name]);
   
   只读属性
   --------------------------------
    title                 页头内容
    desc                  描述
    body                  主体内容
    footer                页脚内容
    width                 窗口宽度
    height                窗口高度
    left                  定位style.left
    top                   定位style.top
    name                  窗口名称，定义不同的名称可以打开多个窗口
    popWinContainer       窗口容器[TABLE]对象
    titleContainer        标题容器[DIV]对象，下同。p.titleContainer.style.backgroundColor = 'red';
    descContainer
    bodyContainer
    footerContainer       
    popWinClose           关闭按钮[标签IMG]对象，注：请不要重写该对象的onclick事件，有需要可以使用closeCallback
    titleBgColor
    descBgColor
    bodyBgColor
    footerBgColor
    
    事件
    --------------------------------
    closeCallback         定义关闭后的回调事件。p.closeCallback = function(){ };
    fillCallback
        
    方法
    -------------------------------
    fill()                用属性title、desc、body、footer的数据填充窗口
    close()               关闭窗口
    setTitle(String)      设置页头内容
    setDesc(String)
    setBody(String)
    setFooter(String)
    setPosition(L,T)      设置窗口的位置L:left,T:top，null代表不改变当前值
    setSize(width,height) 设置窗口的大小。
    setDraggable(options) 设置拖拽。options -> {handle:[Object],onStart:[event],onDrag:[event],onEnd:[event]}
    setBackgroundColor(titleBgColor,descBgColor,bodyBgColor,footerBgColor); null表示不变更;setBackgroundColor(); ->默认背景色
    
    全局
    -------------------------------
    getPopWinInstance(name)     按窗口名称找回实例。
                                var p = getPopWinInstance('newPopWin');//找回名称为newPopWin的窗口的实例
                                p.close();
   
   
   Example:
   
        var p = new popWin(300,100,100,'newPopWin');
        popup.title = "title";
        // popup.title = null; 或不设定将不出现标题，以下同(出body外)。
        popup.desc = "desc";
        popup.body = "body";
        popup.footer = "footer";
        
        popup.fill();//将前面的数据设置填充到窗口
        
        popup.close();//关闭
        
        var p2 = getPopWinInstance('newPopWin');//找回名称为newPopWin的窗口的实例
        p2.setBody('XX');
        p2.closeCallback = function(){
            //定义关闭后的回调事件
        };
        p2.close();//关闭
        
        t.setTitle('s');//设置标题为字符s
        t.setTitle();//设置空值，既是隐藏标题栏
        
        t.setDraggable({handle:t.titleContainer,onEnd:function(){
            alert('end');
        }});
                
==================================
*/
var popWins = [];
function popWin(width,left,top,winName){
    var t = this;
    this.rudeLeft=left;
    this.rudeTop=top;
    this.title = null;
    this.desc = null;
    this.body = null;
    this.footer = null;
    this.width = 400;
    this.height = '';
    this.left = 100;
    this.top = 100;
    this.name = '';
    var wz = getWindowSize();
    if(width) this.width = width;
    if(left){
        if((parseInt(left)+parseInt(this.width)) > wz.width) this.left = parseInt(wz.width-this.width);
        else this.left = left;
    }else{
        this.left=(document.documentElement.clientWidth-this.width)/2+document.documentElement.scrollLeft;
    }
    if(top) 
        this.top = top;
    if(winName) this.name = winName;
    popWins = popWins.reject(function(d) { return d.name == t.name });
    popWins.push(this);
    this.init();
}
popWin.prototype = {
    init:function(){
        setAllSelectVisibility('hidden');
        this.responseStyle();
        this.responseHTML();
        this.setPosition(this.left,this.top);
        this.popWinClose.onclick = this.close.bind(this);
        this.setDraggable({handle:this.titleContainer});
    },
    responseHTML:function(){
        if(!$('popWin_Tb'+this.name)){
            var s = '<table cellpadding="0" cellspacing="0" id="popWin_Tb'+this.name+'" class="popWin_Tb" style="width:'+this.width+'px"><tr><td id="popWin_Tb1'+this.name+'" colspan="2" style="width:'+this.width+'px" class="popWin_CellWide popWin_Top">&nbsp;</td><td class="popWin_Cell"></td></tr><tr><td colspan="2" class="popWin_Main"><div id="popWin_Con'+this.name+'" class="popWin_Con"><img src="/Images/close.gif" id="popWin_Close'+this.name+'" class="popWin_Close" alt=""/><div class="popWin_TitleCon" id="popWin_TitleCon'+this.name+'"><div id="popWin_Title'+this.name+'" class="popWin_Title"><img src="/Images/wait1.gif" alt="" /></div></div><div class="popWin_DescCon" id="popWin_DescCon'+this.name+'"><div id="popWin_Desc'+this.name+'" class="popWin_Desc"><img src="/Images/wait1.gif" alt="" /></div></div><div class="popWin_ContentCon" id="popWin_ContentCon'+this.name+'"><div id="popWin_Content'+this.name+'" class="popWin_Content"><img src="/Images/wait1.gif" alt="" /></div></div><div class="popWin_FooterCon" id="popWin_FooterCon'+this.name+'"><div id="popWin_Footer'+this.name+'" class="popWin_Footer"><img src="/Images/wait1.gif" alt="" /></div></div></div></td><td class="popWin_Cell popWin_Shadow"></td></tr><tr><td class="popWin_Cell"></td><td id="popWin_Tb2'+this.name+'" style="width:'+this.width+'px" class="popWin_CellWide popWin_Shadow"></td><td class="popWin_Cell popWin_Shadow"></td></tr></table>';
            new Insertion.Bottom(document.body,s);
        }
        this.popWinContainer = $('popWin_Tb'+this.name);
        this.popCon = $('popWin_Con'+this.name);
        this.titleContainer = $('popWin_Title'+this.name);
        this.descContainer = $('popWin_Desc'+this.name);
        this.bodyContainer = $('popWin_Content'+this.name);
        this.footerContainer = $('popWin_Footer'+this.name);
        this.popWinClose = $('popWin_Close'+this.name);
    },
    responseStyle:function(){
        if(!$('popWinStyle')){
            var h = document.getElementsByTagName('HEAD').item(0);
            var sty = document.createElement('link');
            sty.href = '/style/popWin.css';
            sty.id = 'popWinStyle';
            sty.rel = 'stylesheet';
            sty.type = 'text/css';
            h.appendChild(sty);
        }
    },
    close:function(){
        if(this.popWinContainer){
            this.popWinContainer.remove();
            var t = this;
            popWins = popWins.reject(function(d) { return d==t });
        }
        setAllSelectVisibility('visible');
        this.closeCallback();
    },
    closeCallback:function(){},
    fill:function(){
        this.setTitle(this.title);
        this.setDesc(this.desc);
        this.setBody(this.body);
        this.setFooter(this.footer);
        this.fillCallback();
        
        if(this.rudeTop==null){
            this.height=this.popWinContainer.clientHeight;
            var top=(document.documentElement.clientHeight-this.height)/2+document.documentElement.scrollTop;
            this.top=Math.round(top);
            this.setPosition();
        }
    },
    fillCallback:function(){},
    setSize:function(w,h){
        if(w){
            this.width = w;
            this.popWinContainer.style.width = w+'px';
            $('popWin_Tb1'+this.name).style.width = w+'px';
            $('popWin_Tb2'+this.name).style.width = w+'px';
        }
        if(h){
            this.height = h;
            $('popWin_ContentCon'+this.name).setStyle({'height':h+'px','overflow':'auto'});
        }else{
            this.height = '';
            $('popWin_ContentCon'+this.name).setStyle({'height':'','overflow':'hidden'});
        }
    },
    setPosition:function(L,T){
        if(L) this.left = L;
        if(T) this.top = T;
        this.popWinContainer.style.position = 'absolute';
        this.popWinContainer.style.left = this.left+'px';
        this.popWinContainer.style.top = this.top+'px';
    },
    setTitle:function(s){
        if(s){
            this.title = s;
            this.titleContainer.show();
            $('popWin_Tb1'+this.name).style.backgroundColor = this.titleBgColor;
            this.titleContainer.update(this.title);
            this.bodyContainer.style.paddingTop = '';
        }else{
            this.titleContainer.hide();
            $('popWin_Tb1'+this.name).style.backgroundColor = '#fff';
            this.bodyContainer.style.paddingTop = '0';
        }
    },
    setDesc:function(s){
        if(s){
            this.desc = s;
            this.descContainer.show();
            this.descContainer.update(this.desc);
        }else{
            this.descContainer.hide();
        }
    },
    setBody:function(s){
        if(s){
            this.body = s;
            this.bodyContainer.show();
            this.bodyContainer.update(this.body);
        }
    },
    setFooter:function(s){
        if(s){
            this.footer = s;
            this.footerContainer.show();
            this.footerContainer.update(this.footer);
        }else{
            this.footerContainer.hide();
        }
    },
    setDraggable:function(options){
        if(options){
            if(this.dragHandle && this.dragHandle == options.handle){
                this.dg.destroy();
            }
            this.dragHandle = options.handle;
            this.dg = new Draggable(this.popWinContainer,{
                handle:options.handle,
                onStart:options.onStart,
                onDrag:options.onDrag,
                onEnd:options.onEnd
            });
        }
    },
    setBackgroundColor:function(titleBgColor,descBgColor,bodyBgColor,footerBgColor){
        if(titleBgColor) this.titleBgColor = titleBgColor;
        else this.titleBgColor = '#E4EDF3';
        $('popWin_TitleCon'+this.name).style.backgroundColor = this.titleBgColor;
        $('popWin_Tb1'+this.name).style.backgroundColor = this.titleBgColor;
            
        if(descBgColor) this.descBgColor = descBgColor;
        else this.descBgColor = '#fff';
        $('popWin_DescCon'+this.name).style.backgroundColor = descBgColor;
        
        if(bodyBgColor) this.bodyBgColor = bodyBgColor;
        else this.bodyBgColor = '#fff';
        $('popWin_ContentCon'+this.name).style.backgroundColor = bodyBgColor;
        
        if(footerBgColor) this.footerBgColor = footerBgColor;
        else this.footerBgColor = '#E4EDF3';
        $('popWin_FooterCon'+this.name).style.backgroundColor = this.footerBgColor;
    }
};
function getPopWinInstance(name){
    var wo = null;
    popWins.each(function(w){
        if(w.name == name)
        wo = w;
    });
    return wo;
}