// Popover, Copyright (c) 2008 Regis Gaughan, III. All Rights Reserved

var Popover = new Class({
	
	Implements: [Chain, Events, Options],

	options: {
			width:400
		},

	initialize: function(){
		this.modal = new Element('div',{'class':'popover-modal'}).setStyles({'z-index':9999,'position':'absolute','top':0,'left':0,'width':'100%','height':0,'opacity':0}).inject(document.body);
		this.popup = {};
		this.popup.box = new Element('div',{'class':'popover-box'}).setStyles({'z-index':10000,'opacity':0,'position':'absolute','top':0,'left':'50%'}).inject(document.body);
		this.popup.title = new Element('div',{'class':'popover-box-topmid'}).inject(new Element('div',{'class':'popover-box-topright'}).inject(new Element('div',{'class':'popover-box-topleft'}).inject(this.popup.box)));
		this.popup.body = new Element('div',{'class':'popover-box-midmid'}).inject(new Element('div',{'class':'popover-box-midright'}).inject(new Element('div',{'class':'popover-box-midleft'}).inject(this.popup.box)));
		new Element('div',{'class':'popover-box-bottommid'}).inject(new Element('div',{'class':'popover-box-bottomright'}).inject(new Element('div',{'class':'popover-box-bottomleft'}).inject(this.popup.box)));
		this.popup.content = new Element('div',{'class':'popover-box-body'}).inject(this.popup.body);
		this.popup.close = new Element('div',{'class':'popover-box-close','html':'(x) close'}).inject(this.popup.box);
		this.fx = {};
		this.fx.modal = new Fx.Morph(this.modal, {'link':'cancel'});
		this.fx.popup = new Fx.Morph(this.popup.box, {'link':'cancel'});
	},
	show: function(title,content,w){
	    
	    var width = (w !== false && w > 10) ? w : this.options.width;
	    this.popup.box.setStyle('width',width);
	    
		this.popup.title.set('text',title);
		this.popup.content.set('html', content);
		
		this.modal.addEvent('click',this.hide.bind(this));
		this.popup.close.addEvent('click',this.hide.bind(this));
		this.popup.close.removeClass('popover-box-close-disabled');
		this.modal.setStyles({'height':Window.getScrollSize().y,'opacity':0});
		this.modal.addClass('popover-clickable');
		var bodyscroll = Window.getScroll();
		var bodysize = Window.getSize();
		this.popup.box.setStyle('opacity',.05);
		var popupsize = this.popup.box.getCoordinates();
		var top = ((bodyscroll.y+(bodysize.y/2)-(popupsize.height/2))).toInt();
		this.popup.box.setStyles({'margin-left':(popupsize.width/2).toInt()*-1,'top':(top<0?10:top)});
		this.fx.modal.start({'opacity':.7});
	    this.fx.popup.start({'opacity':1});
	},
	hide: function(){
	    this.fx.modal.start({'opacity':0}).chain(function(){this.modal.setStyles({'height':0});}.bind(this));
	    this.fx.popup.start({'opacity':0});
	}
		
});
