/*
Application.AutoOverlay can be used to automatically load a URL
into a modal overlay. It is initialized automatically when the
data-overlay attribute on an <a> tag is is set to true. 

These additional options can be set via data attributes on the
<a> tag:
 * data-height - the height of the loaded page within the overlay
 * data-width - the width of the loaded page within the overlay

Example #1 - using default width and height
  # This anchor tag will load google.com into a modal overlay
  # with the default size of 640px x 480px
  <a href="http://www.google.com" data-overlay="true">Google!</a>

Example #2 - specifying width and height
  # This anchor tag will load google.com into a modal overlay
  # into a container that is 1000px x 600px
  <a href="http://www.google.com" data-overlay="true" data-width="1000px" data-height="600px">Google!</a>

Example #3 - loading relative pages
  # This anchor tag will load the /people page into a modal overlay
  # into a container that is 1000px x 600px
  <a href="/people" data-overlay="true" data-width="1000px" data-height="600px">People</a>

Example #4 - loading images
  # Anything that a normal URL can load can be loaded into the modal overlay
  <a href="/some_image.jpg" data-overlay="true">Some Image</a>
  
This functionality is intended to be used by widget authors. Do NOT change
without consulting custom widgets in the system as those will need to be updated
as well. Proceed with caution if changing this API.
*/
Application.AutoOverlay = Behavior.create({
  initialize: function() {
    this.link = this.element;
    this.link.observe('click', this.showOverlay.bind(this));
  },
  
  showOverlay: function(event) {
    event.stop();

    overlay_options = { width:"640px", height: "480px" };
    width = this.link.getAttribute("data-width")
    height = this.link.getAttribute("data-height")

    if(width) overlay_options.width = width;
    if(height) overlay_options.height = height;
    
    Application.Window.loadPageInOverlay(this.link.href, overlay_options);
  }
});

Event.addBehavior({
  'a[data-overlay=true]': Application.AutoOverlay
});