
$(document).ready(function()
{
    /* Objects / Index */

search_complexe(false);
q = window.location.href.split(/#/);
if(q[1]=='advanced')
    search_complexe(true);
});

/* Objects / Index */

function changeBounds(base, indice, unique)
{
    var aBound;

    var from    = '';
    var to      = '';
    var current = document.getElementById(base + indice );

    if (unique) {
        $('.' + base).each(function() { if (this.id != current.id) { this.checked = false; } });

        if (current.checked) {
            aBound = current.value.split('|');
            from   = aBound[0];
            to     = aBound[1];
        }
    } else {
        var lower;
        var upper;

        var state   = 0;
        var toCheck = new Array();

        $('.' + base).each(function() {
            if (this.checked) {
                if (state == 0) {
                    state = 1;
                    lower = this;
                    upper = this;
                } else if (state == 1) {
                    upper = this;
                } else if (state == 2 || state == 3) {
                    state = 3;
                    upper = this;
                } else {
                    this.checked = false;
                }
            } else if (state == 1 || state == 2) {
                if (this.id == current.id) {
                    state = 4;
                } else {
                    state = 2;
                    toCheck.push(this);
                }
            } else if (state == 3){
                state = 4;
            }
        });

        if (state > 2) { for (var id in toCheck) { toCheck[id].checked = true; } }

        if (state > 0) {
            aBound = lower.value.split('|');
            from   = aBound[0];

            aBound = upper.value.split('|');
            to     = aBound[1];
        }
    }
    document.getElementById(base + '_from').value = from;
    document.getElementById(base + '_to'  ).value = to;
}

function changePrice(indice, price_from, price_to) {
    var check = '#price' + indice;

    $('.price').each(function()
        {
            if ($(this).attr('id') != 'price' + indice) {
                $(this).removeAttr('checked');
            }
        }
    )
    if ($(check + ':checked').val()) {
        $('#price_from').val(price_from);
        $('#price_to').val(price_to);
    } else {
        $('#price_from').val('');
        $('#price_to').val('');
    }
}

function changePieces(indice, piece_from, piece_to) {
    var check = '#pieces' + indice;

    $('.pieces').each(function()
        {
            if ($(this).attr('id') != 'pieces' + indice) {
                $(this).removeAttr('checked');
            }
        }
    )
    if ($(check + ':checked').val()) {
        $('#piece_from').val(piece_from);
        $('#piece_to').val(piece_to);
    } else {
        $('#piece_from').val('');
        $('#piece_to').val('');
    }
}

function search_complexe(complexe)
{
    if (complexe)
    {
      $("#hideableform").show();
      $("#complexe_search").hide();
      $("#simple_search").show();
    }
    else
    {
      $("#hideableform").hide();
      $("#simple_search").hide();
      $("#complexe_search").show();
    }
}
