
$(function()
{
    $('#coins_cat a.category_group').addClass('dinamyc').click(show_categories);
});

var in_process = false;

function show_categories()
{
    if (!in_process && !$(this).hasClass('selected'))
    {
        $('#coins_cat a.category_group').addClass('dinamyc').removeClass('selected');
        $(this).removeClass('dinamyc').addClass('selected');

        var cats = $('ul li a', $(this).parent());
        if (($(this).attr('href') == '?category_group_id=0') || ($(this).attr('href') == '?category_group_id=42'))
        {
            $('#td_cat').html('');
			$('#span_price').hide();
            find_coins($(this).attr('href'));
        }
        else
        {
	        if ($(this).attr('href') == '?category_group_id=128')
	        {
				$('#td_cat').html('');
				$('#cat_by_price').css('width', '35em');
				$('#span_price').show();
				
				var pricebutton = $('#findbyprice');	
				var pricehref = $('#pricehref');
				pricehref.val($(this).attr('href'));
				pricebutton.click(pricebtn_click);
	        }
	        else
	        {
				$('#span_price').hide();
				$('#cat_by_price').css('width', '');

	            $('#td_cat').html('<select id="select_cat" size="20"></select>');
	            var select = $('#select_cat');
	            select.change(select_cat_change);
	            cats.each(function()
	            {
	                select.append('<option value="' + $(this).attr('href') + '">' + $(this).text() + '</option>');
	            });
	        }
        }
    }

    return false;
}

function pricebtn_click()
{
	var minprice = $('#min_price');
	var maxprice = $('#max_price');
	minprice.attr('style', '');
	maxprice.attr('style', '');
	var err = false;
	if ((!minprice.val()) || (isNaN(minprice.val())) || (parseFloat( minprice.val() ) <= 0) || (parseFloat( minprice.val() ) > parseFloat( maxprice.val() ) ) ) {
		minprice.css('color', '#ee0000');
		minprice.focus(reset_style);
		err = true;
	}
	if ((!maxprice.val()) || ( isNaN(maxprice.val()) ) || (parseFloat( maxprice.val() ) <= 0) || (parseFloat( maxprice.val() ) < parseFloat( minprice.val() ) ) ) {
		maxprice.css('color', '#ee0000');
		maxprice.focus(reset_style);
		err = true;
	}
	if (!err)
		price_range_change($('#pricehref').val() + '&min_price=' + $('#min_price').val() + '&max_price=' + $('#max_price').val());
}

function reset_style()
{
	$(this).attr('style', '');
}

function price_range_change(href)
{
	in_process = true;
    $('#findbyprice').attr('disabled', 'disabled');

    $.ajaxSetup({
        url: href,
        cache: false,
        type: "POST"
    });
    $.get(href,
        {
            jsHttpRequest: '36'
        },
        function(data)
        {
            in_process = false;
            $('#findbyprice').removeAttr('disabled');
            $('#coins_res').html(data);
            table_prepare();
        }
    );
}

function select_cat_change()
{
    find_coins($(this).val());
}

function find_coins(href)
{
    in_process = true;
    $('#select_cat').attr('disabled', 'disabled');

    $.ajaxSetup({
        url: href,
        cache: false,
        type: "POST"
    });
    $.get(href,
        {
            jsHttpRequest: '36'
        },
        function(data)
        {
            in_process = false;
            $('#select_cat').removeAttr('disabled');
            $('#coins_res').html(data);
            table_prepare();
        }
    );
}
