/**
 * Popups
 * Author: Bendik Brenne <bb@freecode.no>
 * 
 */
var popup = Class.create({
	container: null,
	ready: false,
	options: {
	},
	
	initialize: function() {
		var defaultOptions = {id: false,
			classNames: '',
			hook: false,
			position: 'top',
			unique: true,
			closeOnBlur: true,
			closeBtn: 'top_right',
			left: 0,
			top: 0,
			effects: true,
			onClose: function(){},
			onShow: function(){}
		};
		this.options = Object.extend(defaultOptions, arguments[0] || { });
		
		this.effects = fcweb.effects.enabled;
		
		this.options.classNames += ' ' + this.options.position;
		
		var div = new Element('div', {className: 'popup ' + this.options.classNames}).setStyle({zIndex: '999'});
		var a = new Element('a', {href: 'javascript:;', title: 'Lukk vindu', className: 'close '});
		var wrap = new Element('div', {className: 'content_wrap'});
		var content = new Element('div', {className: 'content_body'});
		if (this.options.closeBtn != false) {
			wrap.insert(a);
			a.observe('click', this.close.bind(this));
			a.addClassName(this.options.closeBtn);
		}
		wrap.insert(content);
		div.insert(wrap);
		div.hide();
		this.container = div;
		
		if (this.options.unique) {
			document.body.select('.popup').invoke('remove');
		}
		document.body.insert(this.container);
		
		if (this.options.id) {
			div.writeAttribute('id', this.options.id);
		} else {
			div.identify();
		}
		if (!this.options.hook) {
			div.addClassName('noarrow');
		}
		
		this.position();
	},
	
	/* */
	position: function() {
		var left = 0;
		var top  = 0;
		
		if (this.options.hook) {
			left += -( this.container.getWidth() / 2);
			top  += -( this.container.getHeight());
			
			switch(this.options.position) {
			case 'top':
				left += (this.options.hook.getWidth() / 2);
				break;
			case 'bottom':
				left += (this.options.hook.getWidth() / 2);
				top  += (this.options.hook.getHeight() );
				top +=  (this.container.getHeight());
				break;
			case 'center':
				top +=  (this.options.hook.getHeight() / 2);
				left += (this.options.hook.getWidth() / 2);
				break;
			case 'left':
				left -= (this.options.hook.getWidth() / 2);
				top += ( this.container.getHeight() / 2 );
				top += (this.options.hook.getHeight() / 2);
				break;
			case 'right':
				top += ( this.container.getHeight() / 2 );
				top += (this.options.hook.getHeight() / 2);
				
				left += (this.options.hook.getWidth());
				left += (this.container.getWidth() / 2);
				break;
			}
			
			if (fcweb.ie_shit) {
				var hookTop = (this.options.hook.measure('top') + top);
				var hookLeft = (this.options.hook.measure('left') + left);
				this.container.setStyle({
					position: 'absolute',
					top:  Math.floor(hookTop) + 'px',
					left: Math.floor(hookLeft) + 'px'
				});
			} else {
				this.container.clonePosition(this.options.hook,{
					setWidth: false,
					setHeight: false,
					offsetLeft: (left + this.options.left),
					offsetTop: (top + this.options.top)
				});
			}
		} else {
			/* Center */
			var viewport = document.viewport.getDimensions();
			var left = (viewport.width/2);
			var top  = (viewport.height/2);
			left = left - ( this.container.getWidth() / 2);
			top  = top - ( this.container.getHeight() / 2);
			/* Offsets */
			left += this.options.left;
			top  +=  this.options.top;
			
			this.container.setStyle({position: 'fixed', left: left + 'px', top: top + 'px'});
		}
	},
	
	setContent: function(content, append) {
		var div = this.container.select('.content_body').first();
		if (!append)
			div.update("");
		div.insert(content);
		
		div.select('form.magicform').each(function(el){
			new MagicForm(el);
		});
	},
	
	show: function() {
		this.options.effects ? 
				this.container.appear({duration: 0.3}) : this.container.show();
		var context = this;
		if (this.options.effects) {
			setTimeout(function() {
				context._afterShow();
			},500);
		} else {
			context._afterShow();
		}
		
		if (this.options.closeOnBlur) {
			document.observe('click',function(e){
				var el = e.element();
				if (el != context.container && !el.descendantOf(context.container) && context.ready) {
					//console.log("Ready: " + context.ready);
					context.close();
				}
			});
		}
		
		if (this.options.hook) {
			this.alignViewport();
		} else {
			Event.observe(document.onresize ? document : window, "resize", this.position.bind(this));
		}
		
		/* IE fix */
		setTimeout(function() {
			context.ready = true;
		},500);
	},
	alignViewport: function() {
		var scrollOffset = document.viewport.getScrollOffsets();
		var containerPos = {top: this.container.getStyle('top').replace(/px/, ""), left: this.container.getStyle('left').replace(/px/, "")};
		if (containerPos.top < scrollOffset.top) {
			this.options.effects ? 
					new Effect.Tween(null, Math.floor(scrollOffset.top), Math.floor(containerPos.top -10), {duration:0.3}, function(p){
						scrollTo(0,p);
					})
			: scrollTo(0,Math.floor(containerPos.top -10));
		}
	},
	
	afterShow: function(){ },
	
	_afterShow: function() {
		if (fcweb.ie_shit)
			this.position();
		/*
		this.container.select('.magicform').each(function(form) {
			new MagicForm(form);
		});
		*/
	},
	close: function() {
		this.options.effects ? 
				this.container.fade({duration: 0.3}) : this.container.hide();
		Event.stopObserving(document.onresize ? document : window, "resize");
	}
});

