﻿var fb_pathToImage = "/images/loadingAnimation.gif";
var ftop1 = 0,
ftop2 = 0,
ftop = 0,
fleft = 0,
fftop = 0;
$(document).ready(function() {
    fb_init('a.thickbox, area.thickbox, input.thickbox');
    imgLoader = new Image();
    imgLoader.src = fb_pathToImage;

});
function fb_init(domChunk) {
    $(domChunk).click(function() {
        var t = this.title || this.name || null;
        var a = this.href || this.alt;
        var g = this.rel || false;
        fb_show(t, a, g);
        this.blur();
        return false;

    });

};
function fb_show(caption, url, imageGroup) {
    try {
        ftop = 0,
        fleft = 0,
        fftop = 0,
        ftop1 = 0,
        ftop2 = 0;
        if (typeof document.body.style.maxHeight === "undefined") {
            //$("body", "html").css({height: "100%",width: "100%"});
           // $("html").css("overflow", "hidden");
            if (document.getElementById("FB_HideSelect") === null) {
                $("body").append("<iframe id='FB_HideSelect'></iframe><div id='FB_overlay'></div><div id='FB_window'></div>");
                $("#FB_overlay").click(fb_remove);

            }

        } else {
            if (document.getElementById("FB_overlay") === null) {
                $("body").append("<div id='FB_overlay'></div><div id='FB_window'></div>");
                $("#FB_overlay").click(fb_remove);

            }

        };
        if (fb_detectMacXFF()) {
            $("#FB_overlay").addClass("FB_overlayMacFFBGHack");

        } else {
            $("#FB_overlay").addClass("FB_overlayBG");

        };
        if (caption === null) {
            caption = "";

        };
        $("body").append("<div id='FB_load'><img src='" + imgLoader.src + "' /></div>");
        $('#FB_load').show();
        var baseURL;
        if (url.indexOf("?") !== -1) {
            baseURL = url.substr(0, url.indexOf("?"));
        } else {
            baseURL = url;
        };
        //ͼƬ
        var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
        var urlType = baseURL.toLowerCase().match(urlString);
        if (urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp') {
            FB_PrevCaption = "";
            FB_PrevURL = "";
            FB_PrevHTML = "";
            FB_PrevHTML1 = "";
            FB_NextCaption = "";
            FB_NextURL = "";
            FB_NextHTML = "";
            FB_NextHTML1 = "";
            FB_imageCount = "";
            FB_FoundURL = false;
            if (imageGroup) {
                FB_TempArray = $("a[rel=" + imageGroup + "]").get();
                for (FB_Counter = 0; ((FB_Counter < FB_TempArray.length) && (FB_NextHTML === "")); FB_Counter++) {
                    var urlTypeTemp = FB_TempArray[FB_Counter].href.toLowerCase().match(urlString);
                    if (! (FB_TempArray[FB_Counter].href == url)) {
                        if (FB_FoundURL) {
                            FB_NextCaption = FB_TempArray[FB_Counter].title;
                            FB_NextURL = FB_TempArray[FB_Counter].href;
                            FB_NextHTML = "<span id='FB_next'>&nbsp;&nbsp;<a href='#'>\u4E0B\u4E00\u5F20 &gt;</a></span>";
                            FB_NextHTML1 = '<a href="#" id="nextLink" title="\u4E0B\u4E00\u5F20"></a>';

                        } else {
                            FB_PrevCaption = FB_TempArray[FB_Counter].title;
                            FB_PrevURL = FB_TempArray[FB_Counter].href;
                            FB_PrevHTML = "<span id='FB_prev'>&nbsp;&nbsp;<a href='#'>&lt; \u4E0A\u4E00\u5F20</a></span>";
                            FB_PrevHTML1 = '<a href="#" title="\u4E0A\u4E00\u5F20" id="prevLink"></a>';

                        }

                    } else {
                        FB_FoundURL = true;
                        FB_imageCount = "\u56FE\u7247 " + (FB_Counter + 1) + " / " + (FB_TempArray.length);
                    }
                }
            };
            imgPreloader = new Image();
            imgPreloader.onload = function() {
                imgPreloader.onload = null;
                var pagesize = fb_getPageSize();
                var x = pagesize[0] - 150;
                var y = pagesize[1] - 150;
                var imageWidth = imgPreloader.width;
                var imageHeight = imgPreloader.height;
                if (imageWidth > x) {
                    imageHeight = imageHeight * (x / imageWidth);
                    imageWidth = x;
                    if (imageHeight > y) {
                        imageWidth = imageWidth * (y / imageHeight);
                        imageHeight = y;

                    }

                } else if (imageHeight > y) {
                    imageWidth = imageWidth * (y / imageHeight);
                    imageHeight = y;
                    if (imageWidth > x) {
                        imageHeight = imageHeight * (x / imageWidth);
                        imageWidth = x;

                    }

                };
                FB_WIDTH = imageWidth + 30;
                FB_HEIGHT = imageHeight + 60;
                $("#FB_window").append("<img id='FB_Image' src='" + url + "' width='" + imageWidth + "' height='" + imageHeight + "' alt='" + caption + "'/><div id='hoverNav'>" + FB_PrevHTML1 + FB_NextHTML1 + "</div><div id='FB_caption'>" + caption + "<div id='FB_secondLine'>" + FB_imageCount + FB_PrevHTML + FB_NextHTML + "</div></div><div id='FB_closeWindow'><a href='#' id='FB_closeWindowButton' title='\u5173\u95ED\u6216\u6309\u952E\u76D8\u9000\u51FA\u952E'>\u5173\u95ED</a></div>");

               $("#FB_closeWindowButton").click(fb_remove);
                if (! (FB_PrevHTML === "")) {
                    function goPrev() {
                        if ($(document).unbind("click", goPrev)) {
                            $(document).unbind("click", goPrev);
                        };
                        $("#FB_window").remove();
                        $("body").append("<div id='FB_window'></div>");
                        fb_show(FB_PrevCaption, FB_PrevURL, imageGroup);
                        return false;
                    };
                    $('#prevLink').height(imageHeight);
                    $("#FB_prev").click(goPrev);
                    $("#prevLink").click(goPrev);
                };
                if (! (FB_NextHTML === "")) {
                    function goNext() {
                        $("#FB_window").remove();
                        $("body").append("<div id='FB_window'></div>");
                        fb_show(FB_NextCaption, FB_NextURL, imageGroup);
                        return false;
                    };
                    $("#FB_next").click(goNext);
                    $('#nextLink').height(imageHeight);
                    $("#nextLink").click(goNext);
                };
                document.onkeydown = function(e) {
                    if (e == null) {
                        keycode = event.keyCode;

                    } else {
                        keycode = e.which;

                    };
                    if (keycode == 27) {
                        fb_remove();

                    } else if (keycode == 39) {
                        if (! (FB_NextHTML === "")) {
                            document.onkeydown = "";
                            goNext();

                        }

                    } else if (keycode == 37) {
                        if (! (FB_PrevHTML === "")) {
                            document.onkeydown = "";
                            goPrev();

                        }

                    }

                };



                fb_position();
                $("#FB_load").remove();
                $("#FB_ImageOff").click(fb_remove);
                $("#FB_window").css({
                    display: "block"

                });

            };
            imgPreloader.src = url;
            $("#FB_window").easydrag(true);

        } else {
            var queryString = url.replace(/^[^\?]+\??/, '');
            var params = fb_parseQuery(queryString);
            var fwidth = params['width'];
            var fheight = params['height'];
            if (fwidth <= 1) {
                fwidth = $("body").width() * fwidth;

            };
            if (fheight <= 1) {
                fheight = document.documentElement.clientHeight * fheight;

            };
            FB_WIDTH = (fwidth * 1) + 30 || 670;
            FB_HEIGHT = (fheight * 1) + 40 || 460;
            ajaxContentW = FB_WIDTH - 30;
            ajaxContentH = FB_HEIGHT - 45;
            if (url.indexOf('FB_iframe') != -1) {
                urlNoQuery = url.split('FB_');
                $("#FB_iframeContent").remove();
                if (params['modal'] != "true") {
                    $("#FB_window").append("<div id='FB_title'><div id='FB_ajaxWindowTitle'>" + caption + "</div><div id='FB_closeAjaxWindow'><a href='#' id='FB_closeWindowButton' title='\u5173\u95ED\u6216\u6309\u952E\u76D8\u9000\u51FA\u952E'>\u5173\u95ED</a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='FB_iframeContent' name='FB_iframeContent" + Math.round(Math.random() * 1000) + "' onload='fb_showIframe()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' > </iframe>");

                } else {
                    $("#FB_overlay").unbind();
                    $("#FB_window").append("<iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='FB_iframeContent' name='FB_iframeContent" + Math.round(Math.random() * 1000) + "' onload='fb_showIframe()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;'> </iframe>");

                }
            } else {
                if ($("#FB_window").css("display") != "block") {
                    if (params['modal'] != "true") {
                        $("#FB_window").append("<div id='FB_title'><div id='FB_ajaxWindowTitle'>" + caption + "</div><div id='FB_closeAjaxWindow'><a href='#' id='FB_closeWindowButton' title='\u5173\u95ED\u6216\u6309\u952E\u76D8\u9000\u51FA\u952E'>\u5173\u95ED</a></div></div><div id='FB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px'></div>");
                    } else {
                        $("#FB_overlay").unbind();
                        $("#FB_window").append("<div id='FB_ajaxContent' class='FB_modal' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>");
                    }
                } else {
                    $("#FB_ajaxContent")[0].style.width = ajaxContentW + "px";
                    $("#FB_ajaxContent")[0].style.height = ajaxContentH + "px";
                    $("#FB_ajaxContent")[0].scrollTop = 0;
                    $("#FB_ajaxWindowTitle").html(caption);

                }

            };
            $("#FB_closeWindowButton").click(fb_remove);
            if (url.indexOf('FB_inline') != -1) {
                $("#FB_ajaxContent").append($('#' + params['inlineId']).children());
                $("#FB_window").unload(function() {
                    $('#' + params['inlineId']).append($("#FB_ajaxContent").children());

                });
                fb_position();
                $("#FB_load").remove();
                $("#FB_window").css({
                    display: "block"

                });

            } else if (url.indexOf('FB_iframe') != -1) {
                fb_position();
                if ($.browser.safari) {
                    $("#FB_load").remove();
                    $("#FB_window").css({
                        display: "block"

                    });

                }

            } else {
                $("#FB_ajaxContent").load(url += "&random=" + (new Date().getTime()), 
                function() {
                    fb_position();
                    $("#FB_load").remove();
                    fb_init("#FB_ajaxContent a.thickbox");
                    $("#FB_window").css({
                        display: "block"

                    });

                });

            };
            $("#FB_window").easydrag(true);

        };
        if (!params['modal']) {
            $("#FB_window").setHandler('FB_title');
            document.onkeyup = function(e) {
                if (e == null) {
                    keycode = event.keyCode;

                } else {
                    keycode = e.which;

                };
                if (keycode == 27) {
                    fb_remove();

                }

            };

        }

    } catch(e) {}

};
function fb_showIframe() {
    $("#FB_load").remove();
    $("#FB_window").css({
        display: "block"

    });

};
function fb_remove() {
    $("#FB_imageOff").unbind("click");
    $("#FB_closeWindowButton").unbind("click");
    $("#FB_window").fadeOut("fast", function() {$('#FB_window,#FB_overlay,#FB_HideSelect').trigger("unload").unbind().remove();});
    $("#FB_load").remove();
    if (typeof document.body.style.maxHeight == "undefined") {
        $("body", "html").css({
            height: "auto",
            width: "auto"

        });
        $("html").css("overflow", "");

    };
    document.onkeydown = "";
    document.onkeyup = "";
    return false;

};
function fb_position() {
    $("#FB_window").css({
        width: FB_WIDTH + 'px'

    });
    $("#FB_window").fPosition({
        vpos: "middle",
        hpos: "center",
        fw: FB_WIDTH,
        fh: FB_HEIGHT

    });

};
function fb_parseQuery(query) {
    var Params = {};
    if (!query) {
        return Params;

    };
    var Pairs = query.split(/[;&]/);
    for (var i = 0; i < Pairs.length; i++) {
        var KeyVal = Pairs[i].split('=');
        if (!KeyVal || KeyVal.length != 2) {
            continue;

        };
        var key = unescape(KeyVal[0]);
        var val = unescape(KeyVal[1]);
        val = val.replace(/\+/g, ' ');
        Params[key] = val;

    };
    return Params;

};
function fb_getPageSize() {
    var de = document.documentElement;
    var w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
    var h = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
    arrayPageSize = [w, h];
    return arrayPageSize;

};
function fb_detectMacXFF() {
    var userAgent = navigator.userAgent.toLowerCase();
    if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1) {
        return true;

    }

};