var app = new Object();
app.Map = Class.create({
  initialize: function(window_id, map_canvas_id) {
    this.map_canvas = $(map_canvas_id);
    this.window = new Lightbox($(window_id));
  },

  open: function(points) {
    this._using_map_api(function() {
      this.window.open();
      
      this.map.checkResize();
      
      this.map.clearOverlays();
      points.each(function(point) {
        this.map.addOverlay(new google.maps.Marker(new google.maps.LatLng(point[0], point[1])));
      }.bind(this));
      this.map.panTo(new google.maps.LatLng(points[0][0], points[0][1]));
    }.bind(this));
  },
  
  close: function() {
    this.window.close();
  },
 
  _using_map_api: function(callback) {
    if (this.map) {
      this.window.open();
      callback();
    } else {
      google.load("maps", "2", {callback: function(callback) {
        document.observe("unload", google.maps.Unload);
        this.window.open();
        this.map = new google.maps.Map2(this.map_canvas);
        this.map.setCenter(new google.maps.LatLng(31.777444, 35.234935), 5);
        this.map.addControl(new google.maps.SmallMapControl());
        this.map.addControl(new google.maps.MapTypeControl());
        this.map.setMapType(google.maps.HYBRID_MAP);
        callback();
      }.bind(this, callback)});;
    }
  }
});

document.observe("dom:loaded", function() {
  app.map = new app.Map("map_window", "map_canvas");
  
  $("search_submit").setValue(" ");
  
  $$('a.mailto').each(function(el) {
    if ($$("body")[0].className == "gob") {
      el.href = "mailto:" + "editor" + "@" + "bibliaonline.com.br"; //"getonlinebible.com"
    } else {
      el.href = "mailto:" + "editor" + "@" + "bibliaonline.com.br";
    }
  });

  var select_all = function(ev) {
    try {
      ev.element().focus();
      ev.element().select();        
    } catch(e) {};
  };
  
  $w("share_permalink share_simple_permalink").each(function(el) {
    var elem = $(el);
    if (!elem) return;
    Event.observe(elem, 'click', select_all);
    Event.observe(elem, 'dblclick', select_all);
  });
  
  $$("form.search").each(function(el) {
    Event.observe(el, "submit", function(ev) {
      try {
        var form = ev.element();
        if (form['query'].getValue().blank()) return;
        if (form['query'].getValue().strip() == form['query'].attributes["default"].value.strip()) {
          form['query'].focus();
          new Effect.Highlight(form['query']);
          return;
        }

        var options = {controller: 'search', action: 'search'};
        form.getElements().each(function(el){
          if (!(el.getValue() && el.name)) return;
          options[el.name] = el.getValue();
        });
        document.location = Routes.generate(options);
      } finally {
        ev.stop();
      }
    });
  });
  
  $$('form.search input.text').each(function(el){
    Event.observe(el, "focus", function(ev) {
      try {
        var input = ev.element();
        if (input.value.strip() == input.attributes["default"].value.strip()) input.value = '';
      } finally {}
    });

    Event.observe(el, "blur", function(ev) {
      try {
        var input = ev.element();
        if (input.value.blank()) input.value = input.attributes["default"].value;
      } finally {}
    });    
  });
    
});


