var SiteLogic = {
    path : "/",

    init : function(path)
    {
        SiteLogic.path = path;
        $(SiteLogic.main);
    },

    main : function()
    {
        $("#site-search .search form").submit(SiteLogic.search_submit);
        $("a.popup_box").click(SiteLogic.load_popup_box);
    },

    search_submit : function()
    {
        var $form = $(this);

        window.location = SiteLogic.path + 'search/' +
            $("select.width option:selected", $form).val() + "-" +
            $("select.height option:selected", $form).val() + "-" +
            $("select.diametr option:selected", $form).val() + "-" +
            $("select.season option:selected", $form).val() + "/";
        return false;
    },

    unload_popup_box : function()
    {
        var $over = $("body > .popup-overlay");
        var $box  = $("body > .popup-content")

        $over.fadeTo(400, 0, function(){$over.hide()});
        $box.fadeTo(400, 0, function(){$box.hide()});
    },

    load_popup_box : function()
    {
        var $over = $("body > .popup-overlay");
        var $box  = $("body > .popup-content")

        if($over.size() == 0){
            $("body").append(
                "<div class=\"popup-overlay\" style=\"display: none; position: fixed; width: 100%; height: 100%; left: 0px; top: 0px; z-index: 5; background: #000000; opacity: 0.5; filter: alpha(opacity = 50);\" onclick=\"SiteLogic.unload_popup_box()\"></div>"
            );
            $over = $("body > .popup-overlay");
        }

        if($box.size() == 0){
            $("body").append(
                "<div class=\"popup-content\" style=\"display: none; position: fixed; width: 500px; height: 400px; z-index: 10; background: #FFFFFF; padding: 10px;\">" +
                  "<a href=\"#\" style=\"float: right; width: 16px; height: 16px; font: bold 18px/16px Verdana; color: #AAAAAA; text-decoration: none;\" onclick=\"SiteLogic.unload_popup_box()\">X</a>" +
                  "<div id=\"popup-box\" class=\"body\" style=\"font: 12px/15px Verdana; color: #444444;\"></div>" +
                "</div>"
            );
            $box = $("body > .popup-content");
        }

        $box.css("left", (($over.width() - 500) / 2)  + "px");
        $box.css("top",  (($over.height() - 400) / 2) + "px");

        $over.fadeTo(400, 0.5);
        $box.fadeTo(400, 1);

        jQuery.get(
            $(this).attr("href"),
            function(data){
                $(".body", $box).html(data);
            }
        );
        return false;
    }
};
