var SiteLogic = {
	path : "/",

	buy : function()
	{
        var link = $(this);
		var id   = $(this).attr("href").split('#')[1];
		jQuery.post(
			SiteLogic.path + 'cart/add-to-cart.html',
			{add_to_cart : 1, 'tire_id' : id},
			function(data){
			  $('#head .cart .content').html(data);
              link.attr("class", "in_cart");
			}
		);
		return false;
	},

	select_tire : function()
	{
		var w = $("#select_tire select[name=tire_width]").val();
		var h = $("#select_tire select[name=tire_height]").val();
		var d = $("#select_tire select[name=tire_diametr]").val();
		var b = $("#select_tire select[name=tire_brand]").val();
		var s = $('#select_tire select[name=season]').val();
		var u = SiteLogic.path + 'select/';

		u += (b == 0 ? "all" : b) + '/';
		u += (s == 0 ? "all" : s) + '/';
		u += (w == 0 ? "all" : w) + '/';
		u += (h == 0 ? "all" : h) + '/';
		u += (d == 0 ? "all" : d) + '/';

		window.location = u;
		return false;
	},

    search : function()
    {
        var s = $('#body .mcol .search input[name=search]').val();
     //   s = s.replace(/\//g, "-");
        s = s.replace(/^-+/, "");
        s = s.replace(/-+$/, "");
        var u = SiteLogic.path + 'search/' + encodeURIComponent(s) + '/';//escape

        window.location = u;
        return false;
    },

    select_bycar_change : function()
    {
        var box   = $(this);
        var id    = box.find("option:selected").val();

        jQuery.post(
            SiteLogic.path,
            {"load-cars-list" : 1, "type" : box.attr("name"), "id" : id},
            function(data){
                if(id == 0){
                    box.parent().nextAll().find("select").attr("disabled", 1);
                }
                else {
                    box.parent().next().nextAll().find("select").attr("disabled", 1);

                    box.parent().next().find("select").html(data);
                    box.parent().next().find("select").attr("disabled", 0);
                }
            }
        );
    },

    select_bycar_submit : function()
    {
        var mod_id = $(this).find("select[name=mod_id] option:selected").val();
        if(mod_id > 0){
            window.location = SiteLogic.path + 'auto-select/index/' + mod_id + '/';
        }
        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;
    }
};

$(function(){
    $("a.popup_box").click(SiteLogic.load_popup_box);
	$('#select_tire form').submit(SiteLogic.select_tire);
    $('#body .mcol .search form').submit(SiteLogic.search);
    $("#select_bycar select").change(SiteLogic.select_bycar_change);
    $("#select_bycar form").submit(SiteLogic.select_bycar_submit);
});

