var Popup = Class.create({
  initialize:function(container, options) {
    this.options = {};
    Object.extend(this.options, options || {});
    
    //this.ie6 = (Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6);
    
    this.element = $(container);
    this.titlebar = this.element.down('h1.windowtitle');
    this.contentElement = this.element.down('.windowcontent');
    if (this.contentElement.down('script')) this.contentElement.down('script').remove();
    this.content = this.contentElement.innerHTML;
        
  },
  initControls:function() {
  	if (this.options.draggable) {
	    this.dragger = new Draggable(this.element, { 
	      handle: this.titlebar,
	      onEnd: function() {
	        // do something on destroy
	      }.bind(this)
	    });
    }
    
    this.element.select('.windowcontrol').each(function(item) {
      this.initControlElement(item, item.getAttribute('rel'));
    }.bind(this));
  
  },
  initControlElement:function(controlelement, controltype) {
    if (controltype == 'closebutton') {
      $(controlelement).observe('click', this.close.bind(this));
    }
  },
  open:function() {    
    this.contentElement.update(this.content);
    this.element.removeClassName('hidden');
    this.initControls();
    new Effect.ScrollTo(this.element);
    return false;
  },
  close:function() {
    this.contentElement.update('');
    this.element.addClassName('hidden');
    if (this.options.draggable) this.dragger.destroy();
  }
});
