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

Event.observe(window, "load", function() {
  $$(".ajax-link").each(function(link) {
    link.observe("click", function(ev) {
      if (confirm(link.readAttribute('conf_msg'))) {
        new Ajax.Request(link.readAttribute('href'), {
          method: link.readAttribute('data-method'),
        });
      }
      ev.stop();
    });
  });
});

function closeSiteMsg(h) {
  document.cookie = "msg_hash=" + h + "; path=/; max-age=" + (60*60*24*365);
  if (typeof(Prototype) == "undefined") {
    var e = document.getElementById("msgBox");
    e.parentNode.removeChild(e);
  } else {
    $("msgBox").hide();
  }
}

function citySelector(country_select, city_label, city_select) {
  new Form.Element.Observer(country_select, 0.5, function(el, val) {
    if (val == "United States") {
      $(city_label).update(G._("State"));
    } else {
      $(city_label).update(G._("City"));
    }
    new Ajax.Request('/companies/cities_list', {
      method     : 'get',
      parameters : {'country': val},
      onSuccess  : function(transport) {
        $(city_select).update(transport.responseText);
      }
    });
  });
}

function singleSubmit(selector) {
  if (!selector) { selector = "input[type=submit]"; }
  $$(selector).reject( function(a) { return a.hasClassName('no-single-submit'); } ).invoke("observe", 'click', function(ev) {
    if (!ev.dont_submit){
      this.writeAttribute("old", this.value);
      this.value = G._('Submitting ...');
      this.disabled = true;
      this.addClassName('disabled_button');
      var restore = function() {
        this.enable();
        this.value = this.readAttribute("old");
        this.removeClassName('disabled_button');
      }.bindAsEventListener(this);
      setTimeout(restore, 60 * 1000);
      ev.stop();
      this.up("form").submit();
    }
  });
}

/*function rotateTweets(tweets) {
    var c = $("tweet_content");
    var i = 0;
    var f = function() {
	var t = tweets[i++ % tweets.length];
	c.update(t);
    };
    setInterval(f, 10000);
}*/

Event.observe(window, 'load', function() {
  if ($('jobs_search_query')){
    $('jobs_search_query').observe('submit', function(ev){
      if ($F('jobs_query_text').blank()){
        ev.stop();
        return false;
      }
    });
  }
});

Ajax.Responders.register({
  onCreate   : function() {
    $('ajax-loader-new').show();
  },
  onComplete : function(request, transport) {
    $('ajax-loader-new').hide();
  }
});

function quantity_price(list_price, qtt) {
  ordered_list = list_price.keys().sort( function (a,b) { return a-b });
  choice = ordered_list.reject(function(idx){return idx < qtt});
  if (choice.first() ==  null) {
    choice = ordered_list.last();
  }
  else {
    choice = choice.first();
  }
  return list_price.get(choice);
}
