﻿

var g_agt = navigator.userAgent.toLowerCase();
var is_opera = (g_agt.indexOf("opera") != -1);
var g_title = "";
var g_iframeno = 0;
var g_height = 57;
var act;
function exist(s) {
    return document.getElementById(s) != null;
}
function myInnerHTML(idname, html) {
    if (exist(idname)) {
        document.getElementById(idname).innerHTML = html;
    }
}
function dialog(v_w /*, v_h*/, v_title /*, v_needhead*/) {
    var width = v_w;
    //var height = v_h;
    var title = v_title;
    g_title = title;

    var sBox = '\
		<div id="dialogBox" style="display:none;z-index:19999;width:' + width + 'px;">\
			<div class=ts460 style="position:absolute;top:0px;"><img src="/images/UI/h460_t.gif" width="' + width + '" height="8" /></div>\
			<div id="setheight1" style="position:absolute;height:' + g_height + 'px;top:8px;" >\
			    <table border="0" cellpadding="0" cellspacing="0">\
			        <tr id="setheight2" style="height:' + (g_height) + 'px;">\
			            <td style="background:#000000;width:7px;filter:alpha(opacity=40); -moz-opacity:0.4;"></td>\
			             <td style="width:' + (width - 14) + 'px;">\
				             <div style="border:1px solid #565656;">\
				                 <table width="100%" border="0" cellpadding="0" cellspacing="0">\
				                 ';				                
				                //写H3 
                                if (typeof arguments[3] != "undefined") {
                                    var sClose = '<a href="###" onclick="javascript:new dialog().close();"><img src="/images/UI/btn_close1.gif" width="8" alt="关闭"></a>';
                                    if (0 == arguments[3]) {
                                        sBox += '\
					                            <tr valign="top">\
						                            <td id="dialogBody" style="height:' + (g_height - 2) + 'px;" bgcolor="#ffffff"></td>\
					                            </tr>\
			                                    ';
                                    }
                                    else {
                                        sBox += '\
						                        <tr height="24" bgcolor="#ffffff">\
							                        <td>\
								                        <div class="ts3" style="background:#ffffff;border-bottom:1px solid #ffffff;">\
									                        <div id="dialogBoxTitle" class="ts31" >' + title + '</div>\
									                        <div id="dialogClose" class="ts32" style="margin-top:5px;margin-right:5px;">' + sClose + '</div>\
								                        </div>\
							                        </td>\
						                        </tr>\
						                        <tr valign="top">\
							                        <td id="dialogBody" style="height:' + (g_height - 28) + 'px;" bgcolor="#ffffff"></td>\
						                        </tr>\
			                                    ';
                                    }
                                }
                                else {
                                    var sClose = '<a href="###" onclick="javascript:new dialog().close();"><img src="/images/UI/btn_close1.gif" width="8" alt="关闭"></a>';
                                    sBox += '\
					                        <tr height="24" bgcolor="#6b3">\
						                        <td>\
							                        <div class="ts3">\
								                        <div id="dialogBoxTitle" class="ts31">' + title + '</div>\
								                        <div id="dialogClose" class="ts32">' + sClose + '</div>\
							                        </div>\
						                        </td>\
					                        </tr>\
					                        <tr valign="top">\
						                        <td id="dialogBody" style="height:' + (g_height - 28) + 'px;" bgcolor="#ffffff"></td>\
					                        </tr>\
		                                    ';
                               }
        
    sBox += '\
		                </table>\
		                </div>\
            	    </td>\
                    <td style="background:#000000;width:7px;filter:alpha(opacity=40); -moz-opacity:0.4;"></td></tr>\
                </table>\
            </div>\
            <div id="setheight3" class="ts460" style="position:absolute;top:' + (g_height + 8) + 'px;"><img src="/images/UI/h460_b.gif" width="' + width + '" height="8" /></div>\
        </div>\
    <div id="dialogBoxShadow" style="display:none;z-index:19998;"></div>\
    ';

    var sIfram = '\
    	        <iframe id="dialogIframBG" name="dialogIframBG" frameborder="0" marginheight="0" marginwidth="0" hspace="0" vspace="0" scrolling="no" style="position:absolute;z-index:19997;display:none;"></iframe>\
	            ';

    var sBG = '\
            <div id="dialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:100%;"></div>\
	         ';
    //sBG为真正内容前的一层sIfram是一个iframe,sBox是真正内容
    this.init = function() {
        $('#dialogCase') ? $('#dialogCase').parent().children("#dialogCase").remove() : function() { };
        var oDiv = document.createElement('span');
        oDiv.id = "dialogCase";
        if (!is_opera) {
            oDiv.innerHTML = sBG + sIfram + sBox;
        }
        else {
            oDiv.innerHTML = sBG + sBox;
        }        
        document.body.appendChild(oDiv);
        this.show();
        var str = '<div class="loading" style="display:block; margin-top:5px; text-align:center;height:' + g_height + '"><img src="/images/UI/loading.gif" alt=" />加载中，请稍候……</div>';
        myInnerHTML('dialogBody', str);
//        act = setTimeout(function() {
//            this.open(_sUrl);
//        }, 30);
    }
    this.setHeight = function(height) {
        $("#setheight1").height(height);
        $("#setheight2").height(height);
        $("#dialogBody").height(height - 28);
        $("#setheight3").css("top", height + 8);
       
    }
    this.open = function(_sUrl) {
        var openIframe = "<iframe width='100%' height='100%' name='iframe_parent' id='iframe_parent' src='' frameborder='0' scrolling='no'></iframe>";
        setTimeout(function() {
            $("#iframe_parent").attr('src', _sUrl);
        }, 10);    
        myInnerHTML('dialogBody', openIframe);
    }

    this.show = function() {
        this.middle('dialogBox');
        var t = $('#dialogBox').css("top");
        var l = $('#dialogBox').css("left");
        if ($('#dialogIframBG')) {
            $('#dialogIframBG').css("top", t);
            $('#dialogIframBG').css("left", l);
            $('#dialogIframBG').width($('#dialogBox').offsetWidth);
            $('#dialogIframBG').height($('#dialogBox').offsetHeight);
            $('#dialogIframBG').css("display", "block");
        }
        if (!is_opera) {
            this.shadow();
        }
    }

    this.reset = function() {
        this.close();
    }

    this.close = function() {
        if (window.removeEventListener) {
            window.removeEventListener('resize', this.event_b, false);
            window.removeEventListener('scroll', this.event_b, false);
        }
        else if (window.detachEvent) {
            try {
                window.detachEvent('onresize', this.event_b);
                window.detachEvent('onscroll', this.event_b);
            } catch (e) { }
        }
        if ($('#dialogIframBG')) {
            $('#dialogIframBG').css("display",'none');
        }
        $('#dialogBox').css("display",'none');
        $('#dialogBoxBG').css("display",'none');
        $('#dialogBoxShadow').css("display","none");
        if (typeof (parent.onDialogClose) == "function") {
            parent.onDialogClose($('#dialogBoxTitle').innerHTML);
        }
    }

    this.shadow = function() {
        this.event_b_show();
        if (window.attachEvent) {
            window.attachEvent('onresize', this.event_b);
            window.attachEvent('onscroll', this.event_b);
        }
        else {
            window.addEventListener('resize', this.event_b, false);
            window.addEventListener('scroll', this.event_b, false);
        }
    }

    this.event_b = function() {
        var oShadow = $('#dialogBoxShadow');

        if (oShadow.css("display") != "none") {
            if (this.event_b_show) {
                this.event_b_show();
            }
        }
    }

    this.event_b_show = function() {
        var oShadow = $('#dialogBoxShadow');
        oShadow.css("position", "absolute");
        oShadow.css("display", "");
        oShadow.css("opacity", "0.2");
        oShadow.css("filter", "alpha(opacity=20)");
        oShadow.css("background", "#000");
        var sClientWidth = parent ? parent.document.body.offsetWidth : document.body.offsetWidth;
        var sClientHeight = parent ? parent.document.body.offsetHeight : document.body.offsetHeight;
        var sScrollTop = parent ? (parent.document.body.scrollTop + parent.document.documentElement.scrollTop) : (document.body.scrollTop + document.documentElement.scrollTop);
        oShadow.css("top", '0px');
        oShadow.css("left", '0px');
        oShadow.width(sClientWidth);
        oShadow.height(sClientHeight + sScrollTop);
    }

    this.middle = function(_sId) {
        var id = "#" + _sId;
        $(id).css("display", "");
        $(id).css("position", "absolute");

        var sClientWidth = parent ? parent.document.body.offsetWidth : document.body.offsetWidth;
        var sClientHeight = parent ? parent.document.body.offsetHeight : document.body.offsetHeight;
        var sScrollTop = parent ? (parent.document.body.scrollTop + parent.document.documentElement.scrollTop) : (document.body.scrollTop + document.documentElement.scrollTop);

        //alert(document.getElementById(_sId).offsetWidth);

        var sleft = (sClientWidth - document.getElementById(_sId).offsetWidth) / 2;
        var iTop = sScrollTop + 80;
        var sTop = iTop > 0 ? iTop : 0;

        $(id).css("left", sleft);
        $(id).css("top", sTop);
        //$(id).style.left = sleft + "px";
        //$(id).style.top = sTop + "px";
    }
}

function openWindow(_sUrl, _sWidth,_sHeight, _sTitle) {
    if (typeof arguments[4] != "undefined") {
        var oEdit = new dialog(_sWidth, _sTitle, arguments[4]);
    }
    else {
        var oEdit = new dialog(_sWidth, _sTitle);
    }
    oEdit.init();
    oEdit.open(_sUrl);
    oEdit.setHeight(_sHeight);
   }

   function imageViewer(imgpath) {
   	if (imgpath == null || imgpath == "") { return; }
   	if (!$("#tip")[0]) { $("<div id=\"tip\"></div>").appendTo("form"); }
   	$("#tip").html("<span><img src=\"" + imgpath + "\" /></span>");
   	if ($("#tip").height() < 40) { setTimeout("imageViewer('" + imgpath + "')", 1); }
   	var _left = (document.documentElement.clientWidth - $("#tip").width()) / 2 + document.documentElement.scrollLeft;
   	var _top = (document.documentElement.clientHeight - $("#tip").height()) / 3 + document.documentElement.scrollTop;
   	$("#tip").css({ "top": _top, "left": _left });
   	$("#tip").click(function() { $("#tip").fadeOut(); overlayOff(); });
   	$("#tip").fadeIn(); overlay();
   }

   function overlay() {
   	if (!$("#overlay")[0]) { $("<div id=\"overlay\"></div>").appendTo("body"); }
   	var _width = document.body.offsetWidth;
   	var _height = document.body.offsetHeight;
   	$("#overlay").css({ "height": _height, "width": _width });
   	$("#overlay").show();
   	$("#overlay").click(function() { $("#tip").fadeOut(); overlayOff(); });
   }
   function overlayOff() { $("#overlay").hide(); }