// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


document.observe('dom:loaded', pageInit);
function pageInit() {

if ($('property_price') != null) {
  $('property_price').observe('keyup', changeSoles);
}

if ($('project_description') != null) {
  $('chars').update(230 - $F('project_description').length);
  $('project_description').observe('keyup', countChars);
}

if($("search_operation") != null){
  $("search_operation").observe('change', respondToChange);
}
elements_size = $$('#anuncios-con-servicios ul').size();
grupo_activo = 0;
if ( elements_size > 1 ) {
  new PeriodicalExecuter(function(pe) {
    toggleElements();
  }, 15);
}}

function changeSoles(event) {
  rate = $('property_price').readAttribute('c_dolar');
  $('precio_soles').value = Math.round(rate * $F('property_price'));
}

function countChars(event) {
  chars = 230 - $F('project_description').length;
  $('chars').update(chars);
  if (chars >= 0){
    $('chars').writeAttribute('class', 'positive_number');
  } else {
    $('chars').writeAttribute('class', 'negative_number');
  }
}

function update_provinces(value, prefix, home) {
  if (!prefix) { prefix = ''; }
  if (!home) { home = false; }

  if (value != '') {
    $(prefix + 'load_provinces').show();
    new Ajax.Request('/load_provinces/' + value, {
      method: 'get',
      onSuccess: function(transport) {
        data = eval(transport.responseText);
        update_select_options([prefix + 'pro_id'], data.map( function(el){ return [el.name, el.id] } ), get_district_selects(prefix, home));
        $(prefix + 'load_provinces').hide();
      }
    });
  }
}

function update_districts(value, prefix, home) {
  if (!prefix) { prefix = ''; }
  if (!home) { home = false; }

  if (value != '') {
    $(prefix + 'load_districts').show();
    new Ajax.Request('load_districts/' + value, {
      method: 'get',
      onSuccess: function(transport) {
        data = eval(transport.responseText);
        update_select_options(get_district_selects(prefix, home), data.map( function(el){ return [el.name, el.id] } ), []);
        $(prefix + 'load_districts').hide();
      }
    });
  }
}

function get_district_selects(prefix, home) {
  if (home) {
    return [prefix + 'dis_id_1', prefix + 'dis_id_2', prefix + 'dis_id_3'];
  } else {
    return [prefix + 'dis_id'];
  }
}

function verify_and_update_properties() {
  if ($('options_district').value != '' && $('options_operation').value != '' && $('options_property_type').value != '') {
    $('info_message').show();
    new Ajax.Request('map/update_properties',
        {asynchronous:true, 
         evalScripts:true, 
         parameters:'district=' + $('options_district').value + 
                    '&operation=' + $('options_operation').value + 
                    '&property_type=' + $('options_property_type').value,
         onSuccess: function(){
          $('info_message').fade();
          }
        } 
        );
  }
}

function respondToChange(event){
  val = $F('search_operation');
  opts = $A($('search_type').options);
  if (val == '3') {
    opts.each(function(opt) {
      if (opt.value != 'Apartment') {
        opt.writeAttribute('disabled','disabled');
      }else{
        opt.writeAttribute('selected','selected');
      }
    });
  }else{
    opts.each(function(opt) {
      if (opt.value != 'Apartment') {
        opt.removeAttribute('disabled');
      }
    });
  }
}

function toggleElements(){
  hideElements();
  showElements();
  toggleElementsClases();
}

function hideElements() {
  $$('.anuncios-visible').each(function(e) {
    e.fade();
  });
}

function showElements(){
  $$('.anuncios-invisible').each(function(e) {
    e.appear({duration: 3.0});
  });
}

function toggleElementsClases() {
  grupo_activo += 1;
  if(grupo_activo == 3) grupo_activo = 0;
  
  visibles = $$('.anuncios-visible');
  no_visibles = $$('.anuncios-invisible');

  visibles.each(function(e) {
    e.removeClassName('anuncios-visible');
    e.addClassName('anuncios-invisible');
  });

  no_visibles.each(function(e) {
    if(e.id == grupo_activo){
      e.removeClassName('anuncios-invisible');
      e.addClassName('anuncios-visible');
    }
  });
}


function update_select_options( target, opts_array, clear_select_list ) {
  target.each(function(ele){ clear_select_list.push(ele); }); // Include the target in the clear list

  for( k=0;k < clear_select_list.length;k++ ){
      obj = $(clear_select_list[k]);
      obj.length = 0;
      opt = new Element('option', { 'value': '' }).update("-Seleccione-");
      obj.appendChild(opt);
  }
  // Populate the new options
  target.each(function(ele){
    for(i=0;i < opts_array.length;i++){
        opt = new Element('option', { 'value': opts_array[i][1] }).update(opts_array[i][0]);
        $(ele).appendChild(opt);
    }
  });
}
