var PositionMethods = {
	getPosition: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
}
Element.addMethods(PositionMethods);

Dialog = function(){
    var instances = {};
    
    var that = {};
    
    var initInstance = function(name, selector, behavior) {
        document.observe('dom:loaded', function() {
            var dialog = $$(selector).first();
            if (dialog) {
                instances[name] = new behavior(dialog);
            }
        });
    };
    that.initInstance = initInstance;
    
    var getInstance = function(name) {
        return instances[name];
    };
    that.getInstance = getInstance;
    
    return that;
    
}();

Dialog.Base = Behavior.create({
  initialize : function() {
    this.positionMode = null;
    this._bindCloseEventHandler();
    document.observe('dialog:opening', function(event) {
      // if (event.memo.dialog !== this.element) {
        this.close();
      // }
    }.bind(this));
  },
  
  openNear : function(element) {
    this.positionNear(element);
    this.open();
  },
  
  open: function() {
    if (!this.positionMode) {
      this.positionCenter();
    }
    this._fireDialogEvent('opening');
    this.element.show();    
    this._fireDialogEvent('opened');
  },
  
  positionCenter: function() {    
    this.element.addClassName('centered');
    this.positionMode = 'center';
    
    this.element.setStyle({
      marginLeft: '-' + Math.floor(this.element.getWidth()/2) + 'px',
      marginTop: '-' + Math.floor(this.element.getHeight()/2) + 'px'
    });
    
    if (Prototype.Browser.IE) {
      this.applyIEHack();
    }
  },
  
  applyIEHack: function() {
    var top = document.viewport.getScrollOffsets().top + (document.viewport.getHeight() - this.element.getHeight())/2;
    var left = document.viewport.getScrollOffsets().left + (document.viewport.getWidth() - this.element.getWidth())/2;
    this.element.setStyle({
      position: 'absolute',
      marginLeft: '0',
      marginTop: '0',
      top: top +  'px',
      left: left + 'px'
    });
  },
  
  positionNear: function(target) {
		var position = target.getPosition();
		var site = $('canvas');
		var sitePosition = site.getPosition();

		if(position[0] > ((site.getWidth() - sitePosition[0]) /2)) {
			// position to the left of element
			this.element.style.left = (position[0] - this.element.getWidth() - 26) + "px";
			this.element.addClassName('right');
		} else { 
			// position to the right of element
			this.element.style.right = "auto";
			this.element.style.left = (position[0] + target.getWidth() + 13) + "px";
			this.element.removeClassName('right');
		}
		this.element.style.top = (position[1] + Math.floor(target.getHeight() /2) - 33) + "px";
		this.positionMode = 'near';
	},
  close : function() {
    this._fireDialogEvent('closing');
    this.element.hide();
    this._fireDialogEvent('closed');
  },
  
  _fireDialogEvent : function(eventName) {
    return this.element.fire('dialog:' + eventName, {dialog: this});
  },
  _bindCloseEventHandler : function() {
    var closeAnchor = this.element.down('.header > a.close');
    closeAnchor.observe('click', (function(e) { e.stop(); this.close(); }).bindAsEventListener(this));    
  }        
});