var paged = 1;
jQuery(document).ready(function(){

    jQuery("#loading_contacto").css("display", "none");
    
    jQuery('#cboregion').change(function(){
        var estado = jQuery('#cboregion').val();
        if (estado == "0") {
            reseteaPlazas();
        }
        else {
            actualizaComboPlazas();
        }
//        filtra();
    });
    
    jQuery('#cbometros').change(function(){
        filtra();
    });
    
    
    jQuery('#tpalabras').change(function(){
//        filtra();
    });
    
    jQuery('#cboccomercial').change(function(){
//        filtra();
    });
    
	jQuery('#paginacion button').unbind('click').live('click', function(){
	    pagina = jQuery(this).val();
	    if(jQuery(this).attr('id') == "pagNext") { //btn >
		var btnNext = jQuery('button.paginaActual').parent().next('div').children('button');
		if(btnNext.attr('id') == jQuery(this).attr('id')) jQuery('#pagLast').click();
		else btnNext.click();
	    } else if(jQuery(this).attr('id') == "pagBack") { //btn <
		var btnBack = jQuery('button.paginaActual').parent().prev('div').children('button');
		if(btnBack.attr('id') == jQuery(this).attr('id')) jQuery('#pagPrev').click();
		else btnBack.click();
	    } else if(jQuery(this).attr('id') == "pagPrev") { //btn <<
		jQuery("#filtroRenta").ajaxSubmit({
			beforeSubmit: showRequest, // pre-submit callback
			success: showResponse, // post-submit callback
			url: 'pruebaproc?p=' + (--paged) + '&curr=' + (jQuery("button[id^='pagina']:first").val() - 1)
		    });
		    jQuery('#botbus').attr('disabled', 'disabled');
	    } else if(jQuery(this).attr('id') == "pagLast") { //btn >>
		jQuery("#filtroRenta").ajaxSubmit({
			beforeSubmit: showRequest, // pre-submit callback
			success: showResponse, // post-submit callback
			url: 'pruebaproc?p=' + (++paged)
		    });
		    jQuery('#botbus').attr('disabled', 'disabled');
	    } else { // btn 1, btn 2, ...
		if(pagina > 1) jQuery('#nPaginaBack').show(); //it ain't first page, so we show it
		else jQuery('#nPaginaBack').hide();//first page doesn't show 

		jQuery('#locales div').each(function() {
		    if(jQuery(this).attr('id') == 'page' + pagina) jQuery(this).show();
		    else if(jQuery(this).attr('id').indexOf('page') != -1) jQuery(this).hide();
		});
		jQuery('#paginacion button').each(function() {
		    if(jQuery(this).attr('id') == 'pagina' + pagina) jQuery(this).addClass('paginaActual');
		    else if(jQuery(this).attr('id').indexOf('pagina') != -1) jQuery(this).removeClass('paginaActual');

		if(jQuery('#paginacion button.paginaActual').parent().next('div').children('button').attr('id') == 'pagNext' //get next btn id
			&& jQuery('#pagLast').length == 0) //is displayed/exists?
			jQuery('#pagNext').hide(); //ok, it's last of last
		else
			jQuery('#pagNext').show(); 
		});
	    }
	    return false;
	});
    /*jQuery('#paginacion button').click(function(){
        pagina = jQuery(this).val();
        filtraPaginacion(pagina);
        //alert(pagina);
        return false;
    });*/
    
    jQuery('#botbus').click(function(){
        filtra();
        return false;
    });
    
    jQuery('#plazas').change(function(){
        muestraInfoPlaza();
    });

    /*jQuery("#lnkbanner_left").fancybox({
        'frameWidth': 650,
        'frameHeight': 370
    });

    
    //Google Map FancyBox.---------
    
    jQuery("#mapinfo a").fancybox({
        'frameWidth':850,
        'frameHeight': 450,
        
        
    });
    
    jQuery("#avisoLegal").fancybox({
        'frameWidth': 450,
        'frameHeight': 280
    });
    
    jQuery("#MapaFooter").fancybox({
        'frameWidth':630,
        'frameHeight': 380,
        'titleFormat':null
        
       
    });
    
    jQuery(".mapFancy").fancybox({
        'frameWidth': 700,
        'frameHeight': 310
    });
    
    
    
    jQuery("#nolistbrochure li a.fancyform").fancybox({
        'frameWidth': 505,
        'frameHeight': 240,
        'overlayOpacity': .80,
        'hideOnContentClick': false,
        'centerOnScroll': true
    
    
    });
    
    jQuery("h2.ecv a").fancybox({
        'frameWidth': 500,
        'frameHeight': 290,
        'overlayOpacity': .80,
        'hideOnContentClick': false,
        'centerOnScroll': true
    
    
    });
    
    jQuery("#enviaCurri a").fancybox({
        'frameWidth': 350,
        'frameHeight': 390,
        'overlayOpacity': .80,
        'hideOnContentClick': false,
        'centerOnScroll': true
    
    
    });
    
	
	// ixmael >
	jQuery('div#longtitle h2 a.iframe').fancybox({
		frameWidth: 550,
		frameHeight: 300
	});*/
	// < ixmael
	
    /*funciones de llamada para el mapa de flash --------------------------------------::::
     *
     */
});

function showRequest(formData, jqForm, options){
    jQuery("#loading_contacto").fadeIn();
    jQuery("#locales").empty().html('<div>Cargando resultados...</div>');
}

function showResponse(responseText, statusText){
    jQuery('#botbus').removeAttr('disabled');
    jQuery('#locales').empty();
    jQuery('#locales').append(responseText);
    jQuery("#loading_contacto").fadeOut();
/*    jQuery('#paginacion button').click(function(){
        pagina = jQuery(this).val();
        filtraPaginacion(pagina);
        //alert(pagina);
        return false;
    });*/
    jQuery("#mapinfo a.iframe").colorbox({
        width:"700", 
        height:"490",
        iframe:true
    });
}

function filtra(){
    if(jQuery('#botbus').attr('disabled') == false) {
            paged = 1;
	    jQuery("#filtroRenta").ajaxSubmit({
		beforeSubmit: showRequest, // pre-submit callback
		success: showResponse, // post-submit callback
		url: 'pruebaproc?p='+paged
//		url: 'procesa-formulario-de-renta.html'
	    });
	    jQuery('#botbus').attr('disabled', 'disabled');
    }
} 

/*function filtra(){
    if(jQuery('#botbus').attr('disabled') == false) {
	    jQuery("#filtroRenta").ajaxSubmit({
		beforeSubmit: showRequest, // pre-submit callback
		success: showResponse, // post-submit callback
		url: 'procesa-formulario-de-renta.html'
		//url: 'pruebaproc'
	    });
	    jQuery('#botbus').attr('disabled', 'disabled');
    }
} */

function filtraPaginacion(pagina){
    jQuery("#filtroRenta").ajaxSubmit({
        beforeSubmit: showRequest, // pre-submit callback
        success: showResponse, // post-submit callback
        url: 'procesa-formulario-de-renta.html?num_pagina=' + pagina
    });
}

function reseteaPlazas(){
    jQuery("#filtroRenta").ajaxSubmit({
        beforeSubmit: showRequest, // pre-submit callback
        success: showResponseReseteaPlazas, // post-submit callback
        url: 'reset-combo-ciudad.html'
    });
}

function showResponseReseteaPlazas(responseText, statusText){
    jQuery('#centroComercial').empty();
    jQuery('#centroComercial').append(responseText);
    jQuery('#locales').empty();
    jQuery("#loading_contacto").fadeOut();
}


// cambiando informacion desplegada en interior nuestras plazas :: Gerardo 30/Oct/09
function muestraInfoPlaza(){

    jQuery('#formPlazas').ajaxSubmit({
        beforeSubmit: showRequest,
        success: showResponseMuestraInfoPlazas,
        url: 'individual-plazas.html'
    });
    
}

function showResponseMuestraInfoPlazas(responseText, statusText){

    jQuery('#maincontainer').empty();
    jQuery('#maincontainer').append(responseText);
    jQuery("#maincontainer").show("slow");
}

//-----------------------------------------------------------------------------------------


function actualizaComboPlazas(){
    if(jQuery('#botbus').attr('disabled') == false) {
	    jQuery("#filtroRenta").ajaxSubmit({
		beforeSubmit: showRequest, // pre-submit callback
		success: showResponseactulizaComboPlazas, // post-submit callback
		url: 'centro-comercial-ajax.html'
	    });
	    jQuery('#botbus').attr('disabled', 'disabled');
    }
}

function showResponseactulizaComboPlazas(responseText, statusText){
    jQuery("#locales").empty().html('<div></div>');
    jQuery('#centroComercial').empty();
    jQuery('#centroComercial').append(responseText);
    jQuery("#loading_contacto").hide("slow");
	    jQuery('#botbus').removeAttr('disabled');
/*    jQuery('#cboccomercial').change(function(){
        filtra();
    });*/
}



//-----------------------------------------------------------Paginación-----------------------------------------------//







