﻿$(document).ready(function () {
    //Hover over the filter title
    $('.select_title, .select_title_small').hover(function () { $(this).toggleClass("select_title_hover"); });

    //Clicking over the title brings up the filter box and counts ads
    $('.select_title, select_title_small').click(function () {
        $(this).next().toggleClass('select_options_active');
        $(this).toggleClass("select_title_active");

        //Close other active menus only if I just became active
        if ($(this).hasClass('select_title_active')) {
            $('.select_title_active').not(this).trigger('click');
        }

        //Counting ads
        count_eds('');
    });

    //Range filters titles
    $('.select_options select').change(function () {
        var fromVal = $(this).parent().parent().find('[name=from]').val();
        var toVal = $(this).parent().parent().find('[name=to]').val();
        if (parseInt(fromVal) > parseInt(toVal) && fromVal != -1 && toVal != -1) {
            alert('ערך תחתון לא יכול להיות גדול יותר מערך עליון');
        }
        else {
            if (fromVal == -1 && toVal == -1) {
                $(this).parent().parent().prev().html($(this).parent().parent().prev().attr('default_text'));
            }
            else {
                $(this).parent().parent().prev().html(((fromVal != -1) ? fromVal : '') + '  -  ' + ((toVal != -1) ? toVal : ''));
            }
        }
    });


    //Hover over the filter option
    $('.select_options li').hover(function () { $(this).toggleClass("hover"); });

    //Click on Close button
    $('.select_options .filter_close').click(function () {
        $(this).parent().prev().trigger('click');
    });

    //Click on checkbox
    $('.select_options input[type=checkbox]').click(function () {
        $(this).prev().toggleClass('checked');
        filters_changed = true;

        //Display filter title
        if ($(this).parents('.select_options').find('input:checked').length == 0) {
            $(this).parents('.select_options').prev().html($(this).parents('.select_options').prev().attr("default_text"));
        }
        else {
            var items = new Array();
            $.each($(this).parents('.select_options').find('input:checked'), function (i, obj) {
                items.push($(this).prev().html());
            });
            $(this).parents('.select_options').prev().html(items.join(', '));
        }
    });

    //Alter Models list when selecting a Make
    $('#select_make_options input[type="checkbox"]').click(function () {
        if ($(this).attr('checked')) { $('[make=' + $(this).attr('id') + ']').show(); }
        else {
            $('#select_model_options input:checked[name=' + $(this).attr("id") + "]").each(function () {
                $(this).removeAttr("checked").triggerHandler("click");
            });
            $('[make=' + $(this).attr('id') + ']').hide();
        }

        if ($('#select_make_options input[type="checkbox"]:checked').length == 0) {
            $('#select_model_options .default_text').show();
        }
        else { $('#select_model_options .default_text').hide(); }
    });
});
