﻿(function ($) {
    $.fn.exPopMenu = function (settings) {
        settings = jQuery.extend({
            area_height: 280,
            move_to_x: 20,
            move_to_y: 80,
            zoom_speed: 250,
            hide_speed: 250,
            bg_image_src: "/Content/Images/menu/popup_menu_bg.gif",
            over_post_fix: "_ov"
        }, settings);

        var _this = $(this);
        var items = new Array();
        var image_cache = new Array();

        var bg_layer = $("<div />").css({
            position: "absolute",
            zIndex: 100,
            top: 0,
            left: 0,
            width: "100%",
            height: "100%",
            background: "url('" + settings.bg_image_src + "') #999 50% 50%",
            opacity: 0.86
        }).hide(); //.html("<img src='" + settings.bg_image_src + "' width='1024' height='768' />").hide();
        $("body").children().before(bg_layer);

        var tmp_img = document.createElement("img");
        tmp_img.src = settings.bg_image_src;
        image_cache.push(tmp_img);

        _this.css({
            position: "absolute",
            top: 0,
            right: 0,
            zIndex: 200
        });

        _this.parent().css({
            zIndex: 255,
            position: "relative"
        });

        if ($.browser.msie && $.browser.version == "6.0") {
            $("li", _this).each(function (i) {
                $("> a", this).css({
                    width: $("img", this).width(),
                    height: $("img", this).height(),
                    display: "block"
                });
                $("> a", this)[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $("img", this).attr("src") + "', sizingMethod='scale')";
                $("img", this).hide();
            });
        }

        $("li", _this).each(function (i) {
            var img = $("img", this);
            items.push({
                src: img.attr("src"),
                width: img.attr("width"),
                height: img.attr("height"),
                org_width: img.attr("org_width"),
                org_height: img.attr("org_height"),
                margin: (img.attr("margin") == undefined) ? 0 : img.attr("margin")
            });
            var tmp_img = document.createElement("img");
            tmp_img.src = img.attr("src").substring(0, img.attr("src").lastIndexOf(".")) + settings.over_post_fix + ".png";
            image_cache.push(tmp_img);
        });

        //setMouseOverEvent();

        function setMouseOverEvent() {
            _this.mouseover(function () {
                $(this).unbind("mouseover");
                popup();
                $(document).mousemove(function (e) {
                    if (e.pageY > settings.area_height + $("#wrapper").position().top) {
                        $(this).unbind("mousemove");
                        hide();
                        setMouseOverEvent();
                    }
                });
            });
        }

        function popup() {
            bg_layer.show();

            _this.stop().animate({ right: settings.move_to_x, top: settings.move_to_y }, settings.zoom_speed);

            if ($.browser.msie && $.browser.version == "6.0") {
                $("a", _this).each(function (i) {
                    $(this)[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + items[i].src.substring(0, items[i].src.lastIndexOf(".")) + settings.over_post_fix + ".png" + "', sizingMethod='scale')";
                    $(this).stop().css("margin", 0).animate({
                        width: items[i].org_width, height: items[i].org_height
                    }, settings.zoom_speed);
                });
            } else {
                $("img", _this).each(function (i) {
                    $(this)
                        .attr("src", items[i].src.substring(0, items[i].src.lastIndexOf(".")) + settings.over_post_fix + ".png")
                        .stop().css("margin", 0).animate({
                            width: items[i].org_width, height: items[i].org_height
                        }, settings.zoom_speed);
                });
            }
        }

        function hide() {
            bg_layer.hide();

            _this.stop().animate({ right: 0, top: 0 }, settings.hide_speed);

            if ($.browser.msie && $.browser.version == "6.0") {
                $("a", _this).each(function (i) {
                    $(this)[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + items[i].src + "', sizingMethod='scale')";
                    $(this).stop().animate({
                        width: items[i].width, height: items[i].height, marginLeft: items[i].margin, marginRight: items[i].margin
                    }, settings.hide_speed);
                });
            } else {
                $("img", _this).each(function (i) {
                    $(this).attr("src", items[i].src);
                    $(this).stop().animate({
                        width: items[i].width, height: items[i].height, marginLeft: items[i].margin, marginRight: items[i].margin
                    }, settings.hide_speed);
                });
            }
        }

        function setBgSize() {
            if ($(document).height() != $(window).height()) {
                bg_layer.css("height", $(document).height());
            } else {
                bg_layer.css("height", $(window).height());
            }
        }

        setBgSize();
        $(window).resize(function () {
            setBgSize();
        });

    }
})(jQuery);
