/******************************************************************************
 * Vinculo del logo hacia el home
 */
window.onload = function () {
    var ctrlForm = new ControlFormBusqueda(
        document.getElementById('operacionVenta'),
        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,
    operacionVenta : null,
    operacionAlquiler : null,
    showSelectPeriodo : null,
    selDesdeVenta : null,
    selHastaVenta : null,
    selDesdeAlquiler : null,
    selHastaAlquiler : null,

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

        this.prepararOptions();
    },
    cambioEstado : function () {
        if (this.operacionVenta.checked && this.estadoActual != 'venta') {
            this.estadoActual = 'venta';
            return 1;
        } else {
            if (this.operacionAlquiler.checked && this.estadoActual != 'alquiler') {
                this.estadoActual = 'alquiler';
                return 2;
            }
        }
        return 0;
    },
    prepararOptions : function () {
        if (this.operacionVenta && this.operacionAlquiler && 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.operacionVenta.onclick = function () {
                if ( tthis.cambioEstado() === 1 ) {
                    tthis.formByVenta();
                }
            }
            this.operacionAlquiler.onclick = function () {
                if ( tthis.cambioEstado() === 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';
    }
};



$(window).load(function () {
    var wH = $(window).height();
    var wW = $(window).width();

    remplazarHojaDeEstilo(wW);
    redimensionFondoContenido(wW, wH);
    redimensionColumnaDatos(wH);
});
$(window).resize(function () {
    var wH = $(window).height();
    var wW = $(window).width();

    remplazarHojaDeEstilo(wW);
    redimensionFondoContenido(wW, wH);
    redimensionColumnaDatos(wH);
});
/**
 * remplaza la hoja de estilo en funci�n al ancho del navegador
 */
function remplazarHojaDeEstilo(wW) {
    var css = $('#cssDinamico');
    if (css.size() <= 0) {
    	$('head').append( $('<link id="cssDinamico" href="/public/css/css1024.css" rel="stylesheet" type="text/css" media="screen" />') );
        css = $('#cssDinamico');
    }

    css.attr('href', (wW >= 1280) ? '/public/css/css1280.css' : '/public/css/css1024.css' );
}
/**
 * modifica el tama�o del contenedor principal y la imagen de fondo para que ambos
 * se mantengan dentro del �rea de la ventana porque esta no posee scroll
 */
function redimensionFondoContenido(wW, wH) {
    $('#imgFondo').width( wW );
    $('#imgFondo').height( wH );

    $('#fondo').width( wW );
    $('#fondo').height( wH );

    $('.contenido').height( wH )
    $('.contenido').width( wW )
}

/**
 * establece el alto de la zona donde se presentan las datos para que se
 * mantenga dentro del contenedor principal sin que este no muestre scroll
 * vertical
 */
function redimensionColumnaDatos(wH) {
    $('.izquierda').height( wH - ((wH / 100) * 10) )
    $('.datos').height( wH - ((wH / 100) * 5) )
}

