//cuSel({changedEl: "select", visRows: 10});
function ajax_detail_init()
{
    $(".detail_link").click(function(event){
        event.preventDefault();
        var item_id = $(this).attr("value");
		$("#content_c").load("/catalog/ajax_detail.php?ELEMENT_ID="+item_id, function ()
            {
                init_tabs();
                ajax_detail_init();
                add_to_basket_init();
                add_to_basket_init2();
                $('#filter_return').show();
            }
		);
        
    });
}

function add_to_basket_init()
{
	$(".add_to_basket").live('click', function(event){
		var obj = $(this).parent().parent();
		$(obj).html("<p><span class=\"add_to_basket\"><img src=\"/bitrix/templates/images/basket_min.png\" alt=\"\" style=\"width:31px; height:23px;\" /></span></p><p><span class=\"add_to_basket\">Обработка</span></p>");
		$.post(
			"/catalog/ajax_add_to_basket.php",
			{ price_id: $(this).attr("value")},
			function(data){
				//alert($(obj).attr("class"));
				$(obj).html(data);
				$.post(
					"/catalog/ajax_basket_mini.php",
					{},
					function(data2){
						$("#basket_ajax").html(data2);
				});
		});
        event.preventDefault();
	});
}

function add_to_basket_init2()
{
	$(".add_to_basket2").live('click', function(event){
		var obj = $(this).parent();
		$(obj).html("<a href=\"javascript:void(0);\"><img src=\"/bitrix/templates/images/basket_min.png\" alt=\"\" style=\"width:31px; height:23px;\" class=\"add_to_basket2\" />Обработка</a>");
		$.post(
			"/catalog/ajax_add_to_basket2.php",
			{ price_id: $(this).attr("value")},
			function(data){
				//alert($(obj).attr("class"));
				$(obj).html(data);
				$.post(
					"/catalog/ajax_basket_mini.php",
					{},
					function(data2){
						$("#basket_ajax").html(data2);
				});
		});
        event.preventDefault();
	});
}

$(function(){
//	ajax_detail_init();
    add_to_basket_init();
    add_to_basket_init2();
/*    $('#filter_return').click(function () {
        $(this).hide();
        gogo_ajax(true);
    });*/
	//Добавление в корзину аяксом
//    add_to_basket_init();
    
    var ww = $('#second').width();
    var hh = Math.round(ww/8.3125);
    $('.main_banner').width(ww+'px');
    $('.main_banner').height(hh+'px');

	setInterval(function(){
		$('#special_img img').fadeOut(400, function(){
			$('#special_arrow_r').click();
		});
		$('#special_img img').fadeIn(600);
	}, 5000);
    
    $('.show_all_link').click(function(){
        $('#sort_count_f').val('0');
        $('#arrFilter_form').submit();
    });
	
	$('.yandex_href').click(function(){
		$("#yandex_name").attr("value", $(this).attr("value"));
		$("#yandex_form").submit();
	});
	
	$('#special_arrow_l').click(function(){
		var id = parseInt($('#special_img img').attr('src').substr(14,1));
		id = id - 1;
		if(id==0) id = 3;
		$('#special_img img').attr('src', 'images/special'+id+'.jpg');
	});
	$('#special_arrow_r').click(function(){
		var id = parseInt($('#special_img img').attr('src').substr(14,1));
		id = id % 3 + 1;
		$('#special_img img').attr('src', 'images/special'+id+'.jpg');
	});

	$("#search_types div").click(function(){
		$("div.search_type_current").removeClass("search_type_current");
		$(this).addClass("search_type_current");
	});

	$('table.menu td').hover(
		function(){
			$(this).children("a.menu_item_top").addClass("menu_item_top_hover");
			$(this).children("div.menu_popup").show();
		},
		function(){
			$(this).children("a.menu_item_top").removeClass("menu_item_top_hover");
			$(this).children("div.menu_popup").hide();
		}
	);

	/* $("table.filter_block span.r").live('click', function(){
		var attrib = $(this).prev().children().attr("checked");
		$(this).prev().children().attr("checked", !attrib);
	}); */

// detail tabs
    $("#tabs_header td").click(function(){
        $("div.active").removeClass("active").hide();
        $("#tabs_header td.tabs_header_current").removeClass("tabs_header_current");
        $(this).addClass("tabs_header_current");
        var bNum = ((this.id).substr(1,1));
        var toShow = "tab"+bNum;
        $("#"+toShow).show().addClass("active");
        return false;
    });
    
    if ($("div.active").length == 0){
        $("#tab1").addClass("active");
    }
    if($("div.active:first").attr("id") == "tab4"){
        $("#b4").show().addClass("tabs_header_current");
        $('.tab').hide(); $('#tab4').show();
    }else{
        $("#b1").addClass("tabs_header_current");
    };

    // Не отображать закладку Сопутствующие товары если их нет
    if ( $('#tab3 .list_item').length == 0 )
    {
        $('#tab3').hide(); $('td#b3').hide();
    }

});
