﻿/* Greybox Redux Written by: John Resig */
// 全屏显示
function GB_show_full(caption, url, animation) {
    var s = GB_Fun_getWindowSize();
    var ww = Math.round(s.w - (s.w / 100) * 4);
    var hh = Math.round(s.h - (s.h / 100) * 5);
    GB_show(caption, url, hh, ww, animation);
}

// 指定大小显示
function GB_show(caption, url, height, width, animation) {
    // 检查是否已经初始化过Greybox
    var check = $("#GB_overlay");
    if (check.length == 0) {
        $(document.body).append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div><span id='GB_window_Close'>点击关闭</span></div>");
        $("#GB_window_Close").click(GB_hide);
        $("#GB_window").append("<iframe id='GB_frame' src='" + url + "'></iframe>");
    }
    else {
        $("#GB_frame").get(0).src = url;
    }

    $("#GB_caption").html(caption);
    $("#GB_overlay").show();

    // 设置窗口宽高
    $("#GB_window").css({ width: width + "px", height: height + "px" });

    // 设置背景以及窗口有关位置
    GB_position();

    // 显示窗口
    if (animation) $("#GB_window").slideDown("slow");
    else $("#GB_window").show();

    // 绑定事件
    $(window).bind("resize", GB_position);
    $(window).bind("scroll", GB_position);
}

// 关闭
function GB_hide() {
    // 取消事件绑定
    $(window).unbind("resize");
    $(window).unbind("scroll");
    // 隐藏元素
    $("#GB_window").hide();
    $("#GB_overlay").hide();
}

// 获取指定对象的位置
function GB_Fun_objValue(obj) {
    var st = document.documentElement.scrollTop || document.body.scrollTop;     // 滚动条距顶部的距离
    var sl = document.documentElement.scrollLeft || document.body.scrollLeft;   // 滚动条距左边的距离
    var ch = document.documentElement.clientHeight; // 屏幕的高度
    var cw = document.documentElement.clientWidth;  // 屏幕的宽度
    var objH = $("#" + obj).height();   // 浮动对象的高度
    var objW = $("#" + obj).width();    // 浮动对象的宽度
    var objT = Number(st) + (Number(ch) - Number(objH)) / 2;
    var objL = Number(sl) + (Number(cw) - Number(objW)) / 2;
    return { "t": objT, "l": objL };
}

// 设置背景、窗口以及IFrame
function GB_position() {
    // 设置背景宽/高
    var bH = $(window).height() + $(window).scrollTop();
    var bW = $(window).width() + $(window).scrollLeft();
    $("#GB_overlay").css({ width: bW, height: bH });
    $("#GB_overlay").show();

    // 设置窗口的位置
    rr = GB_Fun_objValue("GB_window");
    $("#GB_window").css({ top: rr.t + "px", left: rr.l + "px", display: "block" });
    
    // 设置IFrame高度
    $("#GB_frame").css("height", ($("#GB_window").height() - 32) + "px");
}

// 获取Window大小
function GB_Fun_getWindowSize(doc) {
    doc = doc || document;
    var iWidth, iHeight;
    if (self.innerHeight) {
        iWidth = self.innerWidth;
        iHeight = self.innerHeight;
    } else {
        if (doc.documentElement && doc.documentElement.clientHeight) {
            iWidth = doc.documentElement.clientWidth;
            iHeight = doc.documentElement.clientHeight;
        } else {
            if (doc.body) {
                iWidth = doc.body.clientWidth;
                iHeight = doc.body.clientHeight;
            }
        }
    }
    return { "w": iWidth, "h": iHeight };
}

