﻿function PopWindow() {
    this.id = "PopWidnow1";
    this.name = "PopWidnow1";
    this.parentTagId = ""; //父级id
    this.title = "我的窗口";   //标题
    this.dataText = "";    //文本型数据
    this.handler = null;   //所加载的插件
    this.defCssName = "popwindow";  //默认样式
    this.icon = "/Images/ttt.gif";    //图标
    this.winId = this.id + "_Body";
    this.closeId = this.id + "_close";
    this.maxId = this.id + "_max"; //最大化按钮元素id
    this.minId = this.id + "_min"; //最小化按钮元素id
    this.borderTopId = this.id + "_bannertopID";
    this.px = 0;
    this.py = 0;
    this.begin = false;
    this.toolBarId = "";   //工具栏
    this.run = false;     //是否运行状态
    this.focus = false;   //是否是焦点
    this.taskList = null; //工具栏列表
    this.task = null; //任务栏
    this.minShow = true;   //最小化是否显示
    this.maxShow = true;   //是否显示最大化
    this.closeShow = true; //是否显示关闭
    this.width = 591;
    this.height = 385;
    this.left = null;
    this.top = null;
    this.htmlSrc=null;      //加载html
    this.win_Index = 102;
    this.zzdiv = true;  //是否开启遮罩层
	this.zzHeight=100;
	this.zzWidth=100;
    this.closeIcon = "/Images/guangbi.gif";
    //this.eleHtml='<div class="popwindow" id="popWindow" name="popWindow" style="width:552px; height:376px;left:139px; top:34px;">'
               // +'<p><Label><span>tyrtf</span><span>测试窗口</span></Label><img src="images/close.gif" class="colose" /> </p>'
    //+'<ul class="nav_div"></ul></div>';
                this.eleHtml='<div class="popwindow" id="ww_2">'
                    +'<p><span><img src="ttt.gif" /></span><span>测试窗口</span><img src="guangbi.gif" class="colose" /></p>'
                    +'<ul class="nav_div"></ul></div>';
}

PopWindow.prototype = {
    //事件集合
    events: $.map('focus,mousedown,mouseup,click,blur,change,dblclick,error,keydown,keypress,keyup,mousemove,mouseout,mouseover,resize,scroll,select,submit'.split(','),
                function (event) { return event; }),
    //绑定所有事件,事件定义方式为:事件名称+Event
    bindEvents: function () {
        //绑定close事件
        $("#" + this.closeId).bind("click", { self: this }, this.closeClickEvent);
        //绑定最大化事件
        //$("#" + this.maxId).bind("click", { self: this }, this.maxClickEvent);
        //绑定最小化事件
        //$("#" + this.minId).bind("click", { self: this }, this.minClickEvent);

        //拽扯边框
        $("#" + this.borderTopId).bind("click", { self: this }, this.borderClickEvent);
        //
        //$("#" + this.winId).bind("click", { self: this }, this.borderClickEvent);


        for (var i = 0; i < this.events.length; i++) {
            var method = eval("this." + this.events[i] + "Event");
            //debugger;
            if (method != null && method != undefined) {
                //debugger;
                $("#" + this.id).bind(this.events[i], { self: this }, method);
            }
        }
    },
    load: function () {
		//计算页面宽度和高度
		this.zzHeight=$(document)[0].body.clientHeight+15;
		this.zzWidth=$(document)[0].body.clientWidth;
        var pop = document.getElementById(this.id);
        if (pop != null || pop != undefined) {
            $(pop).focus();
        }
        else {
            if (this.left == null || this.left == undefined) {
                this.left = (document.body.clientWidth - this.width) / 2;
                this.left += "px";
            }
            if (this.top == null || this.top == undefined) {
                this.top = (window.screen.availHeight - window.screenTop - this.height) / 2;
                if (this.top < 50) {
                    this.top = 50;
                }
                this.top += "px";
            }
            this.winId = this.id + "_Body";    //主体id
            this.closeId = this.id + "_close"; //关闭按钮id
            var ulWidth = parseInt(this.width) - 10;
            var ulHeight = parseInt(this.height) - 32;
            var ie8height = parseInt(this.height) + 35;
            if (this.zzdiv) {
                this.eleHtml = '<div id="zzdiv" style="position:absolute;left:0px;top:0px;filter:Alpha(Opacity=10);opacity:0.1;width:'+this.zzWidth+'px; height:'+this.zzHeight+'px;background-color:#000000;z-index:101; text-align:center; vertical-align:middle;"></div>';
            }
            else {
                this.eleHtml = '';
            }
            if (this.dataText != null && this.dataText != "") {
                this.eleHtml += '<div ui="dialog" class="' + this.defCssName + '" id="' + this.id + '" name="' + this.name + '" style="width:' + this.width + 'px; height:'+ie8height+'px!important; height:' + this.height + 'px;left:' + this.left + '; top:' + this.top + ';z-index:' + this.win_Index + '">'
            + '<p id="' + this.borderTopId + '"><span><img src="' + this.icon + '" /></span><span><strong>' + this.title + '</strong></span>';


                if (this.closeShow) {
                    this.eleHtml += '<img src="' + this.closeIcon + '" id="' + this.closeId + '" class="colose" /> </p>';
                }
                this.eleHtml += '<ul class="nav_div" id="' + this.winId + '" style="width:' + ulWidth + 'px; height:' + ulHeight + 'px;">' + this.dataText + '</ul></div>';
            }

            else if (this.handler != null) {
                this.handler.parentTagId = this.winId;
                this.eleHtml += '<div ui="dialog" class="' + this.defCssName + '" id="' + this.id + '" name="' + this.name + '" style="width:' + this.width + 'px; height:' + this.height + 'px;left:' + this.left + '; top:' + this.top + ';z-index:' + this.win_Index + '">'
            + '<p id="' + this.borderTopId + '" ><span><img src="' + this.icon + '" /><span><strong>' + this.title + '</strong></span>';


                if (this.closeShow) {
                    this.eleHtml += '<img src="' + this.closeIcon + '" id="' + this.closeId + '" class="colose" /> </p>';
                }
                this.eleHtml += '<ul class="nav_div" id="' + this.winId + '" style="width:' + ulWidth + 'px; height:' + ulHeight + 'px;"></ul></div>';
            }
            else if (this.htmlSrc != null) {
                this.eleHtml += '<div ui="dialog" class="' + this.defCssName + '" id="' + this.id + '" name="' + this.name + '" style="width:' + this.width + 'px; height:' + this.height + 'px;left:' + this.left + '; top:' + this.top + ';z-index:' + this.win_Index + '">'
                + '<p id="' + this.borderTopId + '"><span><img src="' + this.icon + '" /><span><span><strong>' + this.title + '</strong></span>';


                if (this.closeShow) {
                    this.eleHtml += '<img src="' + this.closeIcon + '" id="' + this.closeId + '" class="colose" /> </p>';
                }
                //this.eleHtml += '<ul class="nav_div" id="' + this.winId + '" style="width:' + ulWidth + 'px; height:' + ulHeight + 'px;"></ul></div>';
                this.eleHtml += '<iframe class="nav_div" frameborder="0" src="" id="' + this.winId + '" style="width:' + ulWidth + 'px; height:' + ulHeight + 'px" scrolling="yes"></iframe></div>';
            }

            if (this.parentTagId != null && this.parentTagId != "") {
                $("#" + this.parentTagId).append(this.eleHtml);
            }
            else {
                $("body").append(this.eleHtml);
            }
            if (this.handler != null) {
                this.handler.load();
            }
            if (this.htmlSrc != null) {
                $("#" + this.winId).attr("src", this.htmlSrc);
                //$("#" + this.winId).load(this.htmlSrc, function (data) { alert(data); });
            }
            //绑定事件
            this.bindEvents();

            new clsDrag(this.borderTopId, this.id, 'move', null, this.onDrag, null, null, false, false, null, this);

            this.run = true;
            this.focus = true;
            // this.WindowArray.push(this);
        }

    }
    ,
    onDrag: function (evt, clientX, clientY, movedX, movedY, divObj, handler, self) {
        //debugger;

        //alert(document.body.scrollHeight);
        //var a = document.getElementById("ebox_win");
        var a = divObj;
        var l, t;
        l = isNaN(parseInt(a.style.left)) ? 0 : parseInt(a.style.left);
        t = isNaN(parseInt(a.style.top)) ? 0 : parseInt(a.style.top);
        l += movedX;
        t += movedY;
        if (l < 0) {
            l = 0;
        }
        if (t < 0) {
            t = 0;
        }

        var height = window.screen.availHeight;
        var width = window.screen.availWidth;
        if (l > width - 100) {
            l = width - 100;
        }
        if (t > height - 100) {
            t = height - 100;
        }
        //debugger;
        //var vvvv=$("#"+self.id);
        a.style.left = l + 'px';
        a.style.top = t + 'px';
        //a.style.zindex=1;
        //debugger;
        //handler(self);
    },
    restLoad: function (data) {
        $("#" + this.winId).html(data);
    },


    //关闭按钮事件
    closeClickEvent: function (evt) {
        evt.data.self.close();

    },
    //最大化按钮事件
    //    maxClickEvent: function (evt) {
    //        evt.data.self.Maximize();
    //    },
    //    //最小化按钮事件
    //    minClickEvent: function (evt) {
    //        evt.data.self.Minimize();
    //    },
    //    Maximize: function () {
    //        $("#" + this.id).attr("class", "max");
    //        //this.focus=true;
    //        this.setIndex(this);

    //    },
    //    Minimize: function () {
    //        $("#" + this.id).attr("class", "min");
    //        //this.focus=false;
    //    },
    close: function () {
        this.run = false;
        this.focus = false;
        $("#" + this.id).remove();
        if (this.zzdiv) {
            $("#zzdiv").remove();
        }
        if (this.task != null) {
            this.task.Dispose();
        }
    },
    borderClickEvent: function (evt) {
        //evt.data.self.setIndex(evt);
    }
}

