$(window).load(function () {
    var ctrlForm = new ControlFormBusqueda( {
            operacionVenta : document.getElementById('operacionVenta'),
            operacionAlquiler : document.getElementById('operacionAlquiler') },
        document.getElementById('showSelectPeriodo'),
        document.getElementById('selDesdeVenta'),
        document.getElementById('selHastaVenta'),
        document.getElementById('selDesdeAlquiler'),
        document.getElementById('selHastaAlquiler')
    );
});

function ControlFormBusqueda () { return this.construct.apply(this, arguments); };
ControlFormBusqueda.prototype = {
    estadoActual : null,
    operaciones : null,
    showSelectPeriodo : null,
    selDesdeVenta : null,
    selHastaVenta : null,
    selDesdeAlquiler : null,
    selHastaAlquiler : null,

    construct : function (operaciones, showSelectPeriodo,
                selDesdeVenta, selHastaVenta, selDesdeAlquiler, selHastaAlquiler) {
        this.operaciones = operaciones;
        this.showSelectPeriodo = $(showSelectPeriodo);
        this.selDesdeVenta = $(selDesdeVenta);
        this.selHastaVenta = $(selHastaVenta);
        this.selDesdeAlquiler = $(selDesdeAlquiler);
        this.selHastaAlquiler = $(selHastaAlquiler);

        this.prepararOptions();
    },
    cambioEstado : function () {
        if ( this.operaciones.operacionVenta.checked ) {
            this.estadoActual = 'venta';
            return 1;
        }
        if ( this.operaciones.operacionAlquiler.checked ) {
                this.estadoActual = 'alquiler';
                return 2;
        }
        return 0;
    },
    prepararOptions : function () {
        if (this.operaciones && this.showSelectPeriodo) {
        	// se inicia el estado del fomulario
            switch ( this.cambioEstado() ) {
                case 1: this.formByVenta(); break;
                case 2: this.formByAlquiler(); break;
            }

            // los eventos de los options
            var tthis = this;
            $( this.operaciones.operacionVenta ).bind('click', function () {
                tthis.formByVenta();
            });
            $( this.operaciones.operacionAlquiler).bind('click', function () {
                tthis.formByAlquiler();
            });
        }
    },
    formByAlquiler : function () {
        this.showSelectPeriodo.show();

        this.selDesdeVenta.attr('disabled', true).hide();
        this.selHastaVenta.attr('disabled', true).hide();
        this.selDesdeAlquiler.attr('disabled', false).show();
        this.selHastaAlquiler.attr('disabled', false).show();
    },
    formByVenta : function () {
        this.showSelectPeriodo.hide();

        this.selDesdeVenta.attr('disabled', false).show();
        this.selHastaVenta.attr('disabled', false).show();
        this.selDesdeAlquiler.attr('disabled', true).hide();
        this.selHastaAlquiler.attr('disabled', true).hide();
    }
};