/*
*/


if (!jobsearch) { var jobsearch = {}; }

jobsearch.init_search_results = function () {
  
  $('input#include-region').click(jobsearch.__click_include_region);  
  $('#filter-mheader-company').click(function() {jobsearch.__click_filter_header('company')});
  $('#filter-mheader-title').click(function() {jobsearch.__click_filter_header('title')});
  
  $('.options-link').click(jobsearch.__click_jobpost_options);
  $('.options-box > .close').click(jobsearch.__click_jobpost_options);
  
  var alerts = new jobsearch.EmailAlerts()
  alerts.bind_callbacks();
}

jobsearch.init_alerts_manage = function() {
  $('.borra').click(function() {
    var _id = $(this).attr('id').split('-').slice(-1)[0]
    $.getJSON('/alerts/delete/?q=' + _id , 
      function(json) {
        if (json.status == 0) {
          alert('No pudimos borrar este alerta.')
        }
        else {
          $('tr#alert-' + _id).html('')
        }
      }
    );
  });
}

jobsearch.EmailAlerts = function() {
  /* handles
  */
  var self = this;
  
  this._block = '#email-alerts';
  this._linky = $(this._block + ' > .linky');
  this._form = $(this._block + ' > form');
  this._status_block = $('#alert-status');
  
  this.bind_callbacks = function() {
    self._linky.click(self._toggle); 
    self._form.submit(self._submit);
  }
  
  this._toggle = function() {
    self._status_block.html('');
    
    if (self._form.css('display') == 'none') {
      $(self._block).attr('class', 'selected');
    }
    else {
      $(self._block).attr('class', '');
    }
    self._form.toggle();
  }
  
  this._validate = function(s) {
    // validates a given email address
    var pattern = new RegExp(
      /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(s);
  }
  
  this._submit = function() {
    // submits the "create an alert form"
    var email = $(self._block + ' input[name=email]');
    if (!self._validate(email.val())) {
      self._status_block.removeClass('success').addClass('failure')
      self._status_block.html('Ese email no es válido.')
      return false;
    }
    
    var url = self._form.attr('action') + '?' + self._form.serialize()
    $.getJSON(url, self._callback)
    return false;
  }
  
  this._callback = function(json) {
    if (json.status == 1) {
      self._status_block.removeClass('failure').addClass('success')
      self._status_block.html('Listo, gracias!')
    }
    else {
      self._status_block.removeClass('success').addClass('failure')
      self._status_block.html('Encontramos un problema.')
    }
  }
}

jobsearch.__click_include_region = function() {
  /* What we bind to the include_region checkbox
  */
  var href = window.location.href
  if ($(this).attr('checked')) {
    window.location = href + '&include_region=1';
  }
  else {
    var pattern = new RegExp('&include_region=[^&]*');
    window.location = href.replace(pattern, '') + '&include_region=0';
  } 
}

jobsearch.__click_filter_header = function(filter_name) {
  /* binded to the header of the search page left nav filters
  */
  var header = $('#filter-mheader-' + filter_name),
      list = $('#filter-mlist-' + filter_name)
  
  header.html(list.css('display') == 'none' ? 'menos &raquo;' : 'más &raquo;');
  list.toggle()
}

jobsearch.__click_jobpost_options = function() {
  /* binded to each "mas opciones" jobpost link, and to its close link [x]
  */
  var post_id = $(this).attr('id').split('-').slice(-1)[0];
  $('#options-box-' + post_id).toggle();
}