﻿// Simple JSON object -> string
$.toJsonString = function(obj) {
    var key, value, output = '';
    $.each(obj, function(key, value) {
        if (output != '') { output += ','; }
        output += key + ':\'' + value + '\'';
    });
    output = '{' + output + '}';
    return output;
}

// Populate form from JSON object
$.populateFilterForm = function(filterObj) {
    $.each(filterObj, function(key, value) {
        var element = $('#' + key);
        if (element && element != null) {
            if ($(element).is('input[type=checkbox]')) {
                if (unescape(value) == 'true')
                    $(element).attr('checked', true);
            }
            else {
                $(element).val(unescape(value));
            }
        }
    });
};

$.submitFilterForm = function() {
    var filterObj = {};

    $.each($('.filter'), function() {
        if ($(this).is('input[type=checkbox]')) {
            if ($(this).is(':checked')) {
                filterObj[$(this).attr('id')] = true; //if it is a checkbox we send true, otherwise we send nothing for the checkbox
            }
        }
        else if ($(this).val())
            filterObj[$(this).attr('id')] = $(this).val();
    });

    $.reloadPageWithFilter(filterObj);
};

$.reloadPageWithFilter = function(filterObj) {
    var newUrl = $.query.set("filter", $.toJsonString(filterObj)).remove("p").toString();
    newUrl = decodeURIComponent(newUrl);
    location.href = newUrl;
}

$.showSection = function(sectionElem, blnSlide) {
    if (blnSlide)
        sectionElem.children('.ctl-view').slideDown('fast');
    else
        sectionElem.children('.ctl-view').show();

    sectionElem.removeClass('off').addClass('on');
    
    sectionElem.find('a.set-link').hide();
    sectionElem.find('a.remove-link').show();
}

$.hideSection = function(sectionElem, blnSlide) {
    if (blnSlide)
        sectionElem.children('.ctl-view').slideUp('fast');
    else
        sectionElem.children('.ctl-view').hide();

    sectionElem.removeClass('on').addClass('off');
    sectionElem.find('a.set-link').show();
    sectionElem.find('a.remove-link').hide();
}

$.clearFieldValues = function(sectionElem) {
    sectionElem.find('.filter').val('');
}

$(document).ready(function() {

    // Fill form with any filter value form query string
    var filterObj;
    var filterStr = $.query.get('filter');
    if (filterStr && filterStr != null) {
        eval("filterObj=" + filterStr);
        $.populateFilterForm(filterObj);
    }

    // Hide all by default
    $.hideSection($('#filterForm .section'), false);

    // Show sections containing values
    $.each($('.filter'), function() {
        if ($(this).val()) {
            $.showSection($(this).parents('.section'), false);
        }
    });

    // Set/Remove click    
    $('.ctl-set').click(function() {
        if ($(this).parent('.section').hasClass('off')) {
            $.showSection($(this).parent('.section'), true);
        } else {
            $.hideSection($(this).parent('.section'), true);
            $.clearFieldValues($(this).parent('.section'));
        }
        return false;
    });


});
