﻿/// <reference path="jquery/jquery.js" />

$(document).ready(function() {

    setTimeout(function() {
        var container = $("#flash-element");
        if (container.size() > 0) {
            var src = container.attr("src");

            container.width($("#head").innerWidth()).height($("#head").innerHeight());
            container.flash({
                swf: src,
                width: "100%",
                height: "100%",
                params: {
                    wmode: "transparent"
                }
            });
            $("object, embed", container).css("visibility", "visible");
        }
    }, 10);

    setTimeout(function() {
        initNavBar();
    }, 10);

    setTimeout(function() {
        initFloatingBox();
    }, 10);

    setTimeout(function() {
        initLanguageSelector();
    }, 10);

    setTimeout(function() {
        initIframe();
    }, 10);

    setTimeout(function() {
        initInlineBox();
    }, 10);
});

function initInlineBox(e) {
    if (!e) {
        e = $("body");
    }
    $.nyroModalSettings({ minHeight: 80 })
    // 初始化内联弹出窗口
    $(".inlinebox", e).click(function() {
        //return;
        try {
            var v = eval("(" + $(this).attr("rel") + ")");

            if (!v.width) {
                v.width = $.window.dimension().width * 0.8;
            }
            if (!v.height) {
                v.height = $.window.dimension().height * 0.8;
            }
            //console.log("指定高宽");
            $.nyroModalManual({
                //debug: true,
                type: 'iframe',
                autoSizable: false,
                modal: true,
                width: v.width,
                height: v.height,
                //minWidth: 120,
                //minHeight: 120,
                url: this.href
            });
        }
        catch (e) {
            //console.log("不指定高宽");
            $.nyroModalManual({
                //debug: true,
                type: 'iframe',
                modal: true,
                minHeight: 80,
                url: this.href
            });
        }

        return false;
    });
}

function initIframe() {
    $("div.module-item iframe").setIframeHeight();
}

function initLanguageSelector() {
    // 美化语言版本下拉列表
    var selector = $('#infobar .culture-selector');
    if (selector.size() > 0) {
        selector.selectbox();

        // 切换语言版本
        $('#infobar .selectbox').addClass("x-fore-color infobar");
        $('#infobar .selectbox-wrapper li').click(function() {
            var targetLan = $('#infobar .culture-selector').val();
            var currentLan = $.cookie("aw_SiteCulture");

            //targetLan = targetLan;

            $.cookie("aw_SiteCulture", targetLan, { path: app_vars.cookiePath });
            window.reload();
        });
    }
    else {
        $("#infobar .culture-selector-container a").click(function() {
            var targetLan = $(this).attr("rel");
            var currentLan = $.cookie("aw_SiteCulture");

            //targetLan = targetLan;

            $.cookie("aw_SiteCulture", targetLan, { path: app_vars.cookiePath });
            window.reload();

            return false;
        });
    }
}

function initNavBar() {
    $(".nav .item a.link", "#navbar").unbind("click").filter("[target='_inline']").click(function() {
        $.nyroModalManual({
            type: 'iframe',
            autoSizable: false,
            width: 800,
            height: 600,
            minWidth: 10,
            minHeight: 10,
            url: this.href,
            hash: $.randomKey()
        });

        return false;
    }).end().find("img").each(function() {
        if ($.browser.IE6) {
            var h = ($(this).closest("a").innerHeight() - $(this).height()) / 2;
            $(this).css({
                "margin-top": h + "px",
                "margin-bottom": h + "px"
            });
        }
    });
}

function initFloatingBox() {
    var x = 1;
    var y = 2;

    var floating = $("#floating");

    var fw = floating.width();
    var bl = $("#inner").offset().left + $("#inner").outerWidth();
    var pd = ($("#inner").outerWidth() - $("#inner").innerWidth()) / 2;

    x = $(window).width() - bl;
    if (x < fw) {
        x = bl - fw - 5;
    }
    else {
        x = bl + 5;
    }
    //$(".aaa").text(pd);
    floating.floating({
        targetX: x,
        targetY: $.window.center().offsetY - floating.outerHeight() / 2 - $(document).scrollTop(),
        interval: "0"
    }).draggable();

    var cookieKey = "aw_Floating";
    if (!$.cookie(cookieKey) || $.cookie(cookieKey) == "show") {
        floating.show();
    }

    if (app_vars.design) {
        $(".closeEl", floating).remove();
    }
    else {
        //关闭浮动层
        $(".closeEl", floating).one("click", function() {
            floating.remove();
            $.cookie(cookieKey, "hidden", { path: app_vars.cookiePath });
            return false;
        });
    }
}

function resizeIFrame(iframe) {
    $(iframe).setIframeHeight();
}