//выравнивает блоки по высоте в каталоге
function refresh_height_of_items_in_catalog()
{
    var $items = $("div.order div.item");
    $items = $items.filter(function(index){

        if( $(this).parent().eq(0).css('display')=='block') return true;
        return false;

    });

    $items.each(function(i){
        if( i%2 == 0 ){
            var height_now  = parseInt( $(this).outerHeight() );
            var height_next = parseInt( $items.eq(i+1).outerHeight() );

            if( height_now > height_next ) $items.eq(i+1).css('height', height_now-40 );
            if( height_now < height_next ) $(this).css('height', height_next-40 );
        }

    });
}

function compare_num_str(num) {
	var compare_str = "Smi)e";
    /*
	if (num == 1) {
		compare_str = "смайл";
	}
	else if ((num >= 2) && (num <= 4)) {
		compare_str = "смайла";
	} else {
		compare_str = "смайлов";
	}
    */
	return compare_str;
}

function total_plus(price) {
	var total_price = Math.abs($("#totalprice").val());
	var total_count = Math.abs($("#totalcount").val());
	var total_text = (total_count + 1) + " " + compare_num_str(total_count + 1) + "<br />на сумму<br />" + (total_price + price) + " руб.";
	$("#totalprice").val(total_price + price);
	$("#totalcount").val(total_count + 1);
	$("#basket_div").html(total_text);
	$("#basket_div").show();
}

function total_minus(price) {
	var total_price = Math.abs($("#totalprice").val());
	var total_count = Math.abs($("#totalcount").val());
	var total_text = (total_count - 1) + " " + compare_num_str(total_count - 1) + "<br />на сумму<br />" + (total_price - price) + " руб.";
	$("#totalprice").val(total_price - price);
	$("#totalcount").val(total_count - 1);
	$("#basket_div").html(total_text);
	if (total_count-1 == 0) {
		$("#basket_div").hide();
	}
}

//показываем всплывающий слой
function show_layer( th )
{
    th.parents().eq(1).next().show();
}
//скрываем всплывающий слой
function hide_layer()
{
    $('.pop1').hide();
}

function basket_update(cnt, price, id, th) {
    th.removeClass("on");
    th.addClass("spinner");
    $.ajax({
        type: "POST",
        url: "/basket/add_from_order_ajax.php",
        data: "PID=" + id + "&PCNT=" + cnt,
        success: function(data){
            if (data == "success") {
                var total_price = Math.abs($("#totalprice").val());
                var total_count = Math.abs($("#totalcount").val());
                var total_text = (total_count + cnt) + " " + compare_num_str(total_count + cnt) + "<br />на сумму<br />" + (total_price + price * cnt) + " руб."
                $("#totalprice").val(total_price + price * cnt);
                $("#totalcount").val(total_count + cnt);
                $("#basket_div").html(total_text);
                $("#basket_div").show();
                basket_btn_disable(id, th);
            } else {
                th.removeClass("spinner");
                th.addClass("on");
                show_layer( th );   //показываем всплывающий слой



                var total_price = Math.abs($("#totalprice").val());
                var total_count = Math.abs($("#totalcount").val());
                var total_text = (total_count + cnt) + " " + compare_num_str(total_count + cnt) + "<br />на сумму<br />" + (total_price + price * cnt) + " руб."
                $("#totalprice").val(total_price + price * cnt);
                $("#totalcount").val(total_count + cnt);
                $("#basket_div").html(total_text);
                $("#basket_div").show();
            }
        }
    });
}

function basket_btn_disable(id, th) {
    var other_sort_type;
    var count_input = th.parent().children("input");
    var curr_sort_type = th.parent().parent().parent().parent().attr("id");
    if (curr_sort_type == "sort_price") { other_sort_type = "sort_date"; }
    else { other_sort_type = "sort_price"; }
    th.removeClass("spinner");
    th.parent().children(".smileminus").removeClass("minus_on");
    th.parent().children(".smileminus").addClass("minus");
    th.parent().children(".smileplus").removeClass("plus_on");
    th.parent().children(".smileplus").addClass("plus");
    count_input.attr("disabled", "true");
    $("#" + other_sort_type + " #" + id + " .rText .smileminus").removeClass("minus_on");
    $("#" + other_sort_type + " #" + id + " .rText .smileminus").addClass("minus");
    $("#" + other_sort_type + " #" + id + " .rText .smileplus").removeClass("plus_on");
    $("#" + other_sort_type + " #" + id + " .rText .smileplus").addClass("plus");
    $("#" + other_sort_type + " #" + id + " .rText .basket_ico").removeClass("on");
    $("#" + other_sort_type + " #" + id + " .rText input").attr("disabled", "true");
}

$(document).ready(function(){
	$(".prise .smileplus").click(function(){
		if ($(this).hasClass("plus_on")) {
            var other_sort_type;
			var count_input = $(this).parent().children("input");
			var minus_btn = $(this).parent().children(".smileminus");
			var curr_price = Math.abs($(this).parent().parent().children(".priceval").val());
            var curr_sort_type = $(this).parent().parent().parent().parent().attr("id");
            var curr_id = $(this).parent().parent().attr("id");
            if (curr_sort_type == "sort_price") { other_sort_type = "sort_date"; }
            else { other_sort_type = "sort_price"; }
            $("#" + other_sort_type + " #" + curr_id + " .rText input").val(Math.abs(count_input.val()) + 1);
			count_input.val(Math.abs(count_input.val()) + 1);
	//		total_plus(curr_price);
			minus_btn.removeClass("minus");
			minus_btn.addClass("minus_on");
            $("#" + other_sort_type + " #" + curr_id + " .rText .smileminus").removeClass("minus");
            $("#" + other_sort_type + " #" + curr_id + " .rText .smileminus").addClass("minus_on");
		}
	});

	$(".prise .smileminus").click(function(){
		if ($(this).hasClass("minus_on")) {
            var other_sort_type;
			var count_input = $(this).parent().children("input");
			var curr_price = Math.abs($(this).parent().parent().children(".priceval").val());
            var curr_sort_type = $(this).parent().parent().parent().parent().attr("id");
            var curr_id = $(this).parent().parent().attr("id");
            if (curr_sort_type == "sort_price") { other_sort_type = "sort_date"; }
            else { other_sort_type = "sort_price"; }
			if (Math.abs(count_input.val()) > 0) {
                $("#" + other_sort_type + " #" + curr_id + " .rText input").val(Math.abs(count_input.val()) - 1);
				count_input.val(Math.abs(count_input.val()) - 1);
	//			total_minus(curr_price);
				if (Math.abs(count_input.val()) == 0) {
					$(this).removeClass("minus_on");
					$(this).addClass("minus");
                    $("#" + other_sort_type + " #" + curr_id + " .rText .smileminus").removeClass("minus_on");
                    $("#" + other_sort_type + " #" + curr_id + " .rText .smileminus").addClass("minus");
				}
			}
		}
	});

	$(".basket_ico").click(function(){
		if ($(this).hasClass("on")) {
		    hide_layer();//скрываем всплывающий слой
			var count_input = $(this).parent().children("input");
			var curr_price = Math.abs($(this).parent().parent().children(".priceval").val());
            var curr_sort_type = $(this).parent().parent().parent().parent().attr("id");
            var curr_id = $(this).parent().parent().attr("id");
			if (Math.abs(count_input.val()) > 0) {
				basket_update(Math.abs(count_input.val()), curr_price, curr_id, $(this));
			}
		}
	});

    //скрываем всплывающий слой, который появляется после нажатия по кнопке "купить"(корзина)
    $('.cancel_layer').click(function(){
        hide_layer();
        return false;
    });

    var all_link_text = $("#sort_price_text, #sort_new_text, #sort_for_him_text, #sort_for_her_text, #sort_for_two_text, #sort_co_text");
    var all_link_href = $("#sort_price_href, #sort_new_href, #sort_for_him_href, #sort_for_her_href, #sort_for_two_href, #sort_co_href");
    var all_div = $("div[@id*='sort_']");

    var sort_price = $("span[@id*='sort_price'], ");
    var sort_new = $("span[@id*='sort_new'], div[@id*='sort_date']");


    $("#sort_price_href a").click(function(){
        all_link_href.show();
        all_link_text.hide();
        $("#sort_price_text").show();
        $("#sort_price_href").hide();

        all_div.hide();
		$("#sort_price").show();

        refresh_height_of_items_in_catalog()
	});

	$("#sort_new_href a").click(function(){
        all_link_href.show();
        all_link_text.hide();
        $("#sort_new_text").show();
        $("#sort_new_href").hide();

        all_div.hide();
		$("#sort_date").show();

        refresh_height_of_items_in_catalog()
	});






    $("#sort_for_him_href a").click(function(){
        all_link_href.show();
        all_link_text.hide();
        $("#sort_for_him_text").show();
        $("#sort_for_him_href").hide();

        all_div.hide();
		$("#sort_for_him").show();

        refresh_height_of_items_in_catalog()
	});

    $("#sort_for_her_href a").click(function(){
        all_link_href.show();
        all_link_text.hide();
        $("#sort_for_her_text").show();
        $("#sort_for_her_href").hide();

        all_div.hide();
		$("#sort_for_her").show();

        refresh_height_of_items_in_catalog()
	});

    $("#sort_for_two_href a").click(function(){
        all_link_href.show();
        all_link_text.hide();
        $("#sort_for_two_text").show();
        $("#sort_for_two_href").hide();

        all_div.hide();
		$("#sort_for_two").show();

        refresh_height_of_items_in_catalog()
	});

    $("#sort_co_href a").click(function(){
        all_link_href.show();
        all_link_text.hide();
        $("#sort_co_text").show();
        $("#sort_co_href").hide();

        all_div.hide();
		$("#sort_co").show();

        refresh_height_of_items_in_catalog()
	});


    //выравниваем все блоки по высоте (при первом показе, остальные будут рефрешиться на событие - "click" )
    refresh_height_of_items_in_catalog();


    /*
        //было сначало
        $("#sort_price_href a").click(function(){
    		$("#sort_new_text").hide();
    		$("#sort_new_href").show();
            $("#sort_date").hide();
            $("#sort_price").show();
    		$("#sort_price_href").hide();
    		$("#sort_price_text").show();
    	});

    	$("#sort_new_href a").click(function(){
    		$("#sort_price_text").hide();
    		$("#sort_price_href").show();
            $("#sort_price").hide();
            $("#sort_date").show();
    		$("#sort_new_href").hide();
    		$("#sort_new_text").show();
    	});
        //было сначало
    */









});
