var isIE, overlay;

Event.observe(window, 'load', initializePopupBoxes, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var PopupBox = Class.create();
PopupBox.prototype = {

	initialize: function(ctrl) {
		this.link         = $(ctrl)
		this.content      = $(ctrl.rel);
		this.body         = $('os-page');
		this.actionsReady = false;
		Event.observe(ctrl, 'click', this.show.bindAsEventListener(this), false);
		ctrl.onclick = function() {return false;};
	 	this.body.style.position = 'relative';
	   
	 	if (this.content) {
			this.content.parentNode.removeChild(this.content);
			this.body.appendChild(this.content);
			this.content.style.position = 'absolute';
			this.content.style.zIndex = '2001';
			this.content.popupBox = this;
		}
	},

	show: function() {
		var lpos = getPos(this.link, this.body);
		var lsiz = getSize(this.link);
		var csiz = getSize(this.content);
		lpos[0] -= csiz[0] - lsiz[0];
		if (this.content.xOffset != null) {lpos[0] += this.content.xOffset;}
		if (this.content.yOffset != null) {lpos[1] += this.content.yOffset;}
		this.content.style.left = lpos[0] + 'px'
		this.content.style.top  = lpos[1] + 'px'
		if (overlay.show(this)) {
			this.link.addClassName('popped');
			if (isIE) {
				this.content.show();
				this.finishShow();
			} else {
				Effect.BlindDown(this.content, {duration:0.15, afterFinish:this.finishShow.bindAsEventListener(this)});
			}
			this.prepareActions();
		}
	},

	hide: function() {
		this.link.show();
		if (isIE) {
			this.content.hide();
			this.finishHide();
		} else {
			Effect.BlindUp(this.content, {duration:0.15, afterFinish:this.finishHide.bindAsEventListener(this)});
		}
	},

	toggle: function() {
		if (this.content.visible()) {this.hide();}
		else {this.show();}
	},

	finishHide: function() {
		this.link.removeClassName('popped');
		overlay.hide(this);
	},

	finishShow: function() {
		// this.link.hide();
	},

	prepareActions: function() {
		if (!this.actionsReady) {
			actions = $$('#' + this.content.id + ' .pb-action')
			for (i = 0; i < actions.length; i++) {
				Event.observe(actions[i], 'click', this[actions[i].rel].bindAsEventListener(this), false);
				actions[i].onclick = function() {return false;};
			}
			this.actionsReady = true;
		}
	}
}

var PopupOverlay = Class.create();
PopupOverlay.prototype = {
	initialize: function() {
		this.ol = $(document.createElement('div'));
		this.ol.id = 'pb-overlay';
		this.ol.style.display = 'none';
		this.ol.style.position = 'absolute';
		this.ol.style.top = '0';
		this.ol.style.left = '0';
		this.ol.style.width = '100%';
		this.ol.style.height = '100%';
		this.ol.style.zIndex = '2000';
		$('os-page').appendChild(this.ol);
		Event.observe(this.ol, 'click', this.dismiss.bindAsEventListener(this), false);
	},

	show: function(box) {
		if ((this.box == null) || (this.box == box)) {
			if (isIE) {
				this.ol.style.height = this.ol.parentNode.offsetHeight + 'px';
			}
			this.box = box;
			this.ol.show();
			return true;
		} else {
			return false;
		}
	},

	hide: function(box) {
		if (this.box == box) {
			this.ol.hide();
			this.box = null;
		}
	},

	dismiss: function() {
		if (this.box) {this.box.hide();}
	}
}

function initializePopupBoxes() {
	isIE = (navigator.userAgent.match('MSIE')) ? true : false;
	if (overlay == null) {
		overlay = new PopupOverlay();
	}
	boxes = $$('.pb-box');
	for(i = 0; i < boxes.length; i++) {
		if (boxes[i].popupBox != null) {
			boxes[i].parentNode.removeChild(boxes[i]);
		}
	}
	links = $$('.pb-link');
	for(i = 0; i < links.length; i++) {
		valid = new PopupBox(links[i]);
	}
}
