var SiteLogic = {
	path          : "/eliteshina_new/",
	cart_cur_pos  : 0,
	cart_dst_pos  : 0,
	cart_acc      : (navigator.appName == "Microsoft Internet Explorer" ? 1 : 0.2),
	cart_spd      : 0,
	cart_max      : 20,
	cart_timer_id : 0,

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

	scroll_brands : function()
	{
		var left = parseInt($('.brands_frame > div:visible').css("margin-left"));
		left     = $('.brands_frame > div:visible').width() + left > 500 ? left - 500 : 0;
		$('.brands_frame > div:visible').animate({marginLeft: left + 'px'}, 1000);
	},

    show_brands : function()
    {
        var type = $(this).attr("href").split("#")[1];
        $("#brands .discs, #brands .tires").hide();
        $("#brands .title a").removeClass("selected");
        $(this).addClass("selected");
        $("#brands div." + type).show();
    },

	move_cart  : function()
	{
		var t = this;
		if(Math.abs(t.cart_dst_pos - t.cart_cur_pos) > t.cart_max){
			t.cart_spd = t.cart_spd < t.cart_max ? t.cart_spd + t.cart_acc : t.cart_spd;
			t.cart_cur_pos += ((t.cart_dst_pos > t.cart_cur_pos ? 1 : -1) * t.cart_spd);

			$("#cart_body").css("top", t.cart_cur_pos + 'px');
			t.cart_timer_id = setTimeout("SiteLogic.move_cart()", 10);
		}
		else {
			$("#cart_body").css("top", t.cart_dst_pos + 'px');
			clearTimeout(t.cart_timer_id);
			t.cart_cur_pos  = t.cart_dst_pos;
			t.cart_spd      = 0;
			t.cart_timer_id = 0;
		}
	},

	doc_scroll : function()
	{
		SiteLogic.cart_dst_pos = $(window).scrollTop() + 80;
		if(SiteLogic.cart_timer_id == 0){
			SiteLogic.move_cart();
		}
	},

	buy : function()
	{
        var type = $(this).attr("href").split(/#|_/)[1];
		var id   = $(this).attr("href").split(/#|_/)[2];
		if(parseInt($('#cart_content').css('width')) == 0){
			$('#cart_title').click();
		}
		jQuery.post(
			SiteLogic.path + 'cart/add-to-cart.html',
			{add_to_cart : 1, 'type' : type, 'item_id' : id},
			function(data){
			  $('#cart_content').html(data);
			}
		);
		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_rad]").val();
		var b = $("#select_tire select[name=tire_brand]").val();
		var s = $('#select_tire input[name=season]:checked').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;
	},

    callback_submit : function()
    {
        var data = {
            phone    : $("#callback input[name=phone]").val(),
            car      : $("#callback input[name=car]").val(),
            size     : $("#callback input[name=size]").val(),
            brands   : $("#callback input[name=brands]").val(),
            callback : 1
        };

        jQuery.post(
            SiteLogic.path + 'callback/',
            data,
            function(data){
                $("#callback form").css("display", "none");
                $("#callback").append(data);
            }
        );
        return false;
    },

    callback_show : function()
    {
        $("#callback div.result, #callback div.error").remove();
        $("#callback form").css("display", "block");

    },

    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("span").attr("class", "hselect");
                    box.parent().nextAll().find("select").attr("disabled", 1);
                }
                else {
                    box.parent().next().nextAll().find("span").attr("class", "hselect");
                    box.parent().next().nextAll().find("select").attr("disabled", 1);

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

    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.0);

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

$(function(){
	$(window).scroll(SiteLogic.doc_scroll);
	$('#brands .more_brands').click(SiteLogic.scroll_brands);
    $('#brands .title a').click(SiteLogic.show_brands);
	$('a.add_to_cart').click(SiteLogic.load_popup_box);
	$('#select_tire form').submit(SiteLogic.select_tire);
    $('#head .callback').click(function(){$('#callback').fadeIn()});
    $("#callback .close").click(function(){$('#callback').fadeOut()});
    $("#callback form").submit(SiteLogic.callback_submit);
    $("#select_bycar select").change(SiteLogic.select_bycar_change);
    $("#select_bycar form").submit(SiteLogic.select_bycar_submit);

	$('#cart_title').toggle(
	  function(){ $('#cart_content').animate({width: 100}, 400); },
	  function(){ $('#cart_content').animate({width: 0}, 400);   }
	);
	SiteLogic.doc_scroll();
});
