/*
 *min = 1;
max = 5;
    alert( Math.floor(Math.random() * (max - min + 1)) + min );
*/
(function ($) {
    $.extend($, {
        decode : function (str) {
            var num = 0;
            var limit = 0;
            var result = {};

            if (str) {
                str = str.split(';');
                limit = str.length;
                for (num; num<limit; num++) {
                    str[num] = str[num].split(':');
                    result[str[num][0]] = str[num][1];
                }
            }

            return result;
        }
    });

    $.extend($, {
        rgt : {
            elementos : [],
            set : function (clave, valor) {
                this.elementos[clave] = valor;
                return this;
            },
            get : function (clave, defecto) {
                //if (this.elementos[clave] != 'undefined' && this.elementos[clave]) {
                if(typeof(this.elementos[clave]) != 'undefined') {
                    return this.elementos[clave];
                }
                return defecto || null;
            }
        }
    });


    // hack para solo cargar el plugIn scrollTo cuando sea necesario
    $.scrollTo = function () {
        // guardamos los argumentos
        var args = arguments;
        // cargamos el plugIn
        $.getScript('/public/js/scrollTo.js', function () {
            // al finalizar la carga aplicamos la función al objeto JQuery con los argumentos pasados al inicio
            return $.scrollTo.apply($, args);
        });
    }
})(jQuery);

$(document).ready(function () {
    var ctrlForm = new ControlFormBusqueda(
        document.getElementById('operaciones'),
        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.options[this.operaciones.selectedIndex].value === '1' ) {
            this.estadoActual = 'venta';
            return 1;
        }
        if ( this.operaciones.options[this.operaciones.selectedIndex].value === '2' ) {
                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.onchange = function () {
                if ( this.options[this.selectedIndex].value === '1' ) {
                    tthis.formByVenta();
                }
                if ( this.options[this.selectedIndex].value === '2' ) {
                    tthis.formByAlquiler();
                }
            }
        }
    },
    formByAlquiler : function () {
        this.showSelectPeriodo.className = 'visible';

        this.selDesdeVenta.disabled = true;    this.selDesdeVenta.className = 'invisibles';
        this.selHastaVenta.disabled = true;    this.selHastaVenta.className = 'invisibles';
        this.selDesdeAlquiler.disabled = false;    this.selDesdeAlquiler.className = 'visibles';
        this.selHastaAlquiler.disabled = false;    this.selHastaAlquiler.className = 'visibles';
    },
    formByVenta : function () {
        this.showSelectPeriodo.className = 'invisible';

        this.selDesdeVenta.disabled = false;    this.selDesdeVenta.className = 'visibles';
        this.selHastaVenta.disabled = false;    this.selHastaVenta.className = 'visibles';
        this.selDesdeAlquiler.disabled = true;    this.selDesdeAlquiler.className = 'invisibles';
        this.selHastaAlquiler.disabled = true;    this.selHastaAlquiler.className = 'invisibles';
    }
};
