// requires jQuery
var Popup = function() {};

Popup.prototype = {

  options: {
    popup_frame_name : 'popup',
    overlay_name : "popup_overlay",
    height : 400,
    width : 545,
    max_height : 0,
    max_width : 960,
    oncomplete : null
  },

  initialize: function(options) {
    this.setOptions(options);
    this.createPopup();
    this.loadLinks();
  },
  
  setOptions: function(options) {

  },

  popup_frame : null,

  overlay : null,

  createPopup: function() {
    this.popup_frame = $('<div id="'+this.options.popup_frame_name+'" class="popup" style="z-index:1100;position:absolute;position:fixed;display:none;overflow:auto;"></div>');
    this.overlay = $('<div id="'+this.options.overlay_name+'" style="display:none;height:9999px;top:0;left:0;z-index:1000;position:absolute;width:100%;background: transparent url(/images/popup/black-70.png) repeat;"></div>');
    var pop = this;
    this.overlay.click(function() {
      pop.close();
    });
    $(document.body).append(this.overlay);
    $(document.body).append(this.popup_frame);
  },

  loadLinks: function() {
    var pop = this;
    $('a.popuplink').each(function(i) {
      $(this).click(function() {
        pop.popup();
        return false;
      });
      /*if (this.rel) {
        $(this.rel).setStyle('visibility', 'hidden');
      }*/
    });
  },

  popup: function() {
    var link = $(this);

    if (link.rel) {
      this.rel_popup(link.rel);
    } else {
      var pop = this;
      $.ajax({
        url: link.href,
        type: 'GET',
        success: function(html) {
          pop.popup_frame.html("<div class='top'></div><div class='mid'>"+html+"<div id='pClose'><a href='' onclick='popups.close();return false;'>CLOSE</a></div></div><div class='bottom'></div>");
          try {
            pop.show();
            pop.movePopup();
          } catch(e) {alert(e)}
//          eval(js);
          //custom onComplete
          if (pop.options.oncomplete) {
            pop.options.oncomplete();
          }
        }
      });
      
    }
  },

  open: function(link, request_method, data_source) {
    if (typeof(request_method) == 'undefined') {
      request_method = 'get';
    }
    var pop = this;
    var req = new Request.HTML({
      url: link,
      data: data_source,
      type: request_method,
      success: function(html) {
        pop.popup_frame.set('html', "<div class='top'></div><div class='mid'>"+html+"<div id='pClose'><a href='' onclick='popups.close();return false;'>CLOSE</a></div></div><div class='bottom'></div>");
        pop.show();
        pop.movePopup();
        eval(js);
        //custom onComplete
        if (pop.options.oncomplete) {
          pop.options.oncomplete();
        }
      }.bind(this)
    }).send();
  },
  rel_popup: function(frame) {
    frame = $(frame);
    frame.inject(this.popup_frame);
    frame.setStyle('visibility', 'visible');
    this.show();
    this.movePopup();
  },

  show: function() {
    this.overlay.css('height', $(document).height());
    this.popup_frame.show();
    this.overlay.show();
  },

  movePopup: function() {
    if (this.options.width && this.options.height) {
      this.popup_frame.css('width', this.options.width);
      this.popup_frame.css('height', this.options.height);
      var x = ($(document).width() - this.options.width)/2;
      var y = ($(document).height() - this.options.height)/2;
    } else {
      var size = {x:this.popup_frame.width(), y:this.popup_frame.height(0)};
      if (this.options.max_width > 0 && size.x > this.options.max_width) {
        var x = ($(document).width() - this.options.max_width)/2;
      } else {
        var x = ($(document).width() - size.x)/2;
      }
      if (this.options.max_height > 0 && size.y > this.options.max_height) {
        var y = ($(document).height() - this.options.max_height)/2;
      } else {
        var y = ($(document).height() - size.y)/2;
      }
      if (y < 1) { // went negative. the div is too big
        y = 50;
        this.popup_frame.css('height', $(document).height() - 100);
      }
      if (x < 1) {
        x = 100;
        this.popup_frame.css('width', $(document).width() - 200);
      }

    }
    this.popup_frame.css('left', x);
    this.popup_frame.css('top', y);
  },

  close: function() {
    this.overlay.hide();
    this.popup_frame.hide();
  }
};

var popups;
$(document).ready(function () {
  popups = new Popup();
  popups.initialize({});
});
