var SiteLogic = {
    path : "/",

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

    main : function()
    {
        $("#site-head .hmenu .item").mouseover(function(){
            $(this).find(".mask").css("width", $(this).width() + "px");
            $(this).find(".mask").show();
            $(this).attr("class", "hitem");
            $(this).find(".submenu").show();
        });
        $("#site-head .hmenu .item").mouseout(function(){
            $(this).find(".mask").hide();
            $(this).attr("class", "item");
            $(this).find(".submenu").hide();
        });

        $("#site-head .topmenu form").submit(SiteLogic.search);
        $(".product_sm a.compare").click(SiteLogic.compare_click);
        SiteLogic.show_map();
    },

    search : function()
    {
        var txt = $("input[name=search]", $(this)).val();
        txt = encodeURIComponent(txt.replace(/[^A-Za-zа-яА-Я0-9 ]+/, " "));
        window.location = SiteLogic.path + 'search/' + txt + '/';
        return false;
    },

    show_map : function()
    {
        if($("#map_canvas").size() > 0){
            var latlng = new google.maps.LatLng(50.440622,30.478678);
            var myOptions = {
              zoom: 15,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            var marker = new google.maps.Marker({position: latlng, title:"Sport-Device"});
            marker.setMap(map);
        }
    },

    compare_click : function()
    {
        var $box = $(this);
        var ref  = SiteLogic.path + "compare/";
        var pid  = $(this).attr("href").split("#")[1];
        jQuery.get(
            SiteLogic.path + "compare/add-item/"+ pid +"/",
            function(data){
                $box.replaceWith("<a href=\""+ ref +"\">Сравнить</a>");
                $("#box_compare_count").html("("+ data +")");
            }
        );
    }
};
