jQuery.win = {
    _self: null,
    content: null,
    
    /**
     * 创建一个弹出窗口
     * @param {String} title 窗口标题
     * @param {Number} width 窗口宽度
     * @param {Number} height 窗口高度
     * @param {Number} x 窗口距浏览器左编的距离
     * @param {Number} y 窗口距浏览器右边的距离
     * @param {String} contentType 指定内容的类型,有text,html,和url(默认)三种
     * @param {String} content 窗口显示的内容,将会根据指定的contentType对其解析
     */
    open: function(title, content, width, height, contentType, x, y){
        this.Title = (null != title) ? title : "标题";
        this.setSize(width, height);
        this.setSeat(x, y);
        this.contentType = (null != contentType) ? contentType : "url";
        this.content = content;
        //标识当前窗口是否被选中
        this.isSelected = false;
        //this._instance = null;
        this._title = null;
        this._md_title = null;
        this._close_btn = null;
        //this.coverall();
        //只能创建一个窗口
        if (this._self == null) {
            this.initWindow();
        }
    },
    
    //设置宽高
    setSize: function(width, height){
        if (null == this._self) {
            this.Width = (null != width) ? width : 400;
            this.Height = (null != height) ? height : 300;
        }
        else {
            this._self.css({
                width: width + "px"
                //height : height+"px"
            })
        }
    },
    
    //设置位置
    setSeat: function(x, y){
        this.X = (null != x) ? x : (this.windowWidth() / 2 - this.Width / 2 + new pos().x);
        this.Y = (null != y) ? y : (this.windowHeight() * 1 / 2 - this.Height * 3 / 5 + new pos().y);
    },
    
    initWindow: function(){
        var win_html = "<div id='win' class='win-box'>" +
 							"<iframe style='position:absolute;border:0;top:6px;left:6px;width:"+(this.Width)+"'>" +
							"</iframe>" +

							"<div class='win-top'>" +
								"<div class='wt-2'><div class='main'></div></div>" +
								"<div class='wt-1'></div>" +
								"<div class='wt-3'></div>" +
							"</div>" +
							"<div class='win-main'>" +
								"<div class='wm-2'>" +
									"<div class='main'>" +
										"<h3 class='popBoxTitle'><span>"+this.Title+"</span></h3>" +
										"<a href='#' id='winclose' onclick='return $.win.close()' class='popBoxClose'></a>" +
										"<div class='popBoxContentBox'></div>" +
									"</div>" +
								"</div>" +
								"<div class='wm-1 box-height' ></div>" +
								"<div class='wm-3 box-height'></div>" +
							"</div>" +
							"<div class='win-bottom'>" +
								"<div class='wb-2'><div class='main'></div></div>" +
								"<div class='wb-1'></div>" +
								"<div class='wb-3'></div>" +
							"</div>" +   
					"</div>";
        this._self = $(win_html)
				.appendTo(document.body)
				.addClass("win-box")
				.end()
				.css({
					width : this.Width +12+ "px",
					height : this.Height +40+ "px",
					top : this.Y + "px",
					left : this.X + "px",
					"z-index" : "9999"
				})
		;
        $(".box-height").height(this.Height+30);
		$(".popBoxContentBox").height(this.Height);
		//------------------------2009/12/4------------------------------
        /*//标题栏
        $(".popBoxTitle").mousedown(function(event){
            var offset = $.win._self.offset();
            $.win._self.X = event.clientX - offset.left;
            $.win._self.Y = event.clientY - offset.top;
            $.win._self.isSelected = true;
        }).mousemove(function(event){
            if ($.win._self.isSelected) {
                $.win._self.css({
                    top: event.clientY - $.win._self.Y,
                    left: event.clientX - $.win._self.X
                })
            }
        }).mouseup(function(event){
            $.win._self.isSelected = false;
        });*/
		
         //标题栏
        $(".popBoxTitle").mousedown(function(event){
            var offset = $.win._self.offset();
			mousePos = mouseCoords(event);
            $.win._self.X = mousePos.x - offset.left;
            $.win._self.Y = mousePos.y - offset.top;
			$("body").bind("mousemove", {}, mouseMove)
					.bind("mouseup", {}, mouseUp);
			return false;
        });
		
		function mouseUp(event) {
			$("body").unbind("mousemove", mouseMove)
				 .unbind("mouseup", mouseUp);
			//$.win._self.isSelected = false;
		}

        //获取鼠标坐标
		function mouseMove(event) {
			event = event || window.event;
			mousePos = mouseCoords(event);
			$.win._self.css({
				top: mousePos.y - $.win._self.Y,
				left: mousePos.x - $.win._self.X
			});
		}
		function mouseCoords(event) {
			if(event.pageX || event.pageY) {
				return {
					x : event.pageX,
					y : event.pageY
				};
			}
			return {
				x : event.clientX + document.body.scrollLeft - document.body.clientLeft,
				y : event.clientY + document.body.scrollTop - document.body.clientTop
			};
		}
		//--------------------------------------------
        //内容框
        this._content = $(".popBoxContentBox");
        switch (this.contentType) {
            case "text":
                this._content.text(this.content);
                break;
            case "html":
                this._content.html(this.content);
                break;
            case "url":
                this._content.html("<iframe id='win_iframe' src='" + this.content + "' style='overflow:hidden;' frameborder='0' scrolling='no'></iframe>");
                break;
        }
        $("#win_iframe").css({
            width: $.win._content.width() + "px",
            height: $.win._content.height() + "px"
        });
        //this.setSize(this.getIframeWidth()+12);
    },
    
    
    //得到iframe内部文档的宽高
    getIframeWidth: function(){
        var iframe = document.getElementById("win_iframe");
        try {
            var bWidth = iframe.contentWindow.document.body.scrollWidth;
            var dWidth = iframe.contentWindow.document.documentElement.scrollWidth;
            var width = Math.max(bWidth, dWidth);
        } 
        catch (ex) {
        }
        alert(width);
        return width;
    },
    
    //外部关闭该窗口的方法
    close: function(){
        if (null != $.win._self) {
            $.win._self.css("display", "none");
            var win = document.getElementById("win");
			//var win_iframe = document.getElementById("win_iframe");
			//win_iframe.parentNode.removeChild(win_iframe);
			$(".popBoxContentBox").empty();
            win.parentNode.removeChild(win);
            
            $.win._self = null;
        }
		return false;
    },
    
    //获取浏览器可见区域的宽高
    windowHeight: function(){
        //最后传出的值
        var windowHeight;
        // 除了IE以外的浏览器
        if (self.innerHeight) {
            windowHeight = self.innerHeight;
        }
        /* IE6 浏览器 */
        else 
            if (document.documentElement && document.documentElement.clientHeight) {
                windowHeight = document.documentElement.clientHeight;
            }
            //其他版本的IE浏览器
            else 
                if (document.body) {
                    windowHeight = document.body.clientHeight;
                }
        return windowHeight;
    },
    
    windowWidth: function(){
        //最后传出的值
        var windowWidth;
        
        // 除了IE以外的浏览器
        if (self.innerWidth) {
            windowWidth = self.innerWidth;
        }
        /* IE6 浏览器 */
        else 
            if (document.documentElement && document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            }
            // 其他版本的IE浏览器
            else 
                if (document.body) {
                    windowWidth = document.body.clientWidth;
                }
        return windowWidth;
    },

	//显示提示信息
	//参数说明: object 调用该方法的元素
	//			title 提示信息的标题
	//			content 提示信息内容
	openInfo : function(object, title, content) {
        
		var _this = $(object);
		if(_this.attr("isOpen") != undefined) {
			return false;
		}
		$.win.closeOpenInfo();
		var _title = (title!=null)?title:(_this.attr("title"));
		var _content = (content!=null)?content:(_this.attr("content"));
		var answer = document.createElement("div");
		var innerHtml = '<div class="title">'+_title+'</div>'+
						'<div class="content">'+_content+'</div>';
		var parent = object.parentNode;
		answer.innerHTML = innerHtml;
		answer.className = "helpBox";
		answer.style.position = "absolute";
		answer.style.zIndex = "99";
		var offset = _this.position();
		var x = offset.left;
		var y = offset.top + object.height;
		if((x+250) > $.win.windowWidth()) {
			x = $.win.windowWidth() - 270;
		}
		answer.style.left = x + "px";
		answer.style.top = y + "px";
		var id = Math.round(100000*Math.random());
		answer.id = id;
		parent.appendChild(answer);
		_this.attr("isOpen", id);
		setTimeout(function() {$("body").bind("click", {}, $.win.closeOpenInfo);}, 50);
	},
        
	closeOpenInfo : function() {
		var questions = $("[isOpen]");
		questions.each(function() {
			var _this = $(this);
			var elem = $("div[id="+_this.attr('isOpen')+"]").get(0);
			elem.parentNode.removeChild(elem);
			_this.removeAttr("isOpen");
		});
		$("body").unbind("click", $.win.closeOpenInfo);
	}
};

//滚卷区域宽高
var pos = function() {
	var posX,posY;
	if (window.innerHeight) {
		posX = window.pageXOffset;
		posY = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {
		posX = document.documentElement.scrollLeft;
		posY = document.documentElement.scrollTop;
	}
	else if (document.body) {
		posX = document.body.scrollLeft;
		posY = document.body.scrollTop;
	}
	this.x = posX;
	this.y = posY;
	return this;
};
window.win = $.win;
