/**
 * NeoPopup
 *
 * An extendable, pretty, html popup class
 *
 * @uses Prototype                          1.6.0+ <http://www.prototypejs.org/>
 * @uses script.aculo.us (effects,dragdrop) 1.8.1+ <http://script.aculo.us/>
 *
 * @version 1.0.1 2008-08-25
 */


var NeoPopupRegistry = {
	activePopups: {},

	available: function() {
		for (var i in NeoPopupRegistry.activePopups) {
			if (
				NeoPopupRegistry.activePopups.hasOwnProperty(i) &&
				NeoPopupRegistry.activePopups[i].getProperty('mode') == 'modal'
			) {
				return false;
			}
		}

		return true;
	},

	register: function(Obj) {
		if (!NeoPopupRegistry.available()) {
			return -1;
		}

		var len = NeoPopupRegistry.activePopups.length;

		NeoPopupRegistry.activePopups[len] = Obj;

		return len;
	},

	unregister: function(id) {
		if (NeoPopupRegistry.activePopups.hasOwnProperty(id)) {
			delete NeoPopupRegistry.activePopups[id];
			return true;
		}

		return false;
	}
}


var NeoPopup = Class.create({
	initialize: function(id, properties) {
		this._id         = id;
		this._properties = properties ? properties : {};

		this._elContainer = null;
		this._elHeading   = null;
		this._elFooter    = null;
		this._elContent   = null;
		this._regIndex    = -1;

		if (!this._properties.hasOwnProperty('_textValues')) {
			this._properties._textValues = {};
		}
		if (!this._properties._textValues.hasOwnProperty('confirm.yes')) {
			this._properties._textValues['confirm.yes'] = 'Yes';
		}
		if (!this._properties._textValues.hasOwnProperty('confirm.no')) {
			this._properties._textValues['confirm.no'] = 'No';
		}
		if (!this._properties._textValues.hasOwnProperty('alert.close')) {
			this._properties._textValues['alert.close'] = 'Close';
		}
	},

	setText: function(id, value) {
		this._properties._textValues[id] = value;
	},

	getText: function(id) {
		return this._properties._textValues.hasOwnProperty(id) ? this._properties._textValues[id] : 'Missing text (' + id + ')';
	},

	_build: function() {
		var strWndTitle  = this.getProperty('title');
		var strClassName = this.getProperty('class');
		var strContent   = this.getProperty('content');

		this._elContainer = new Element('div', {'id': this._id, 'class': 'NeoPopup '+strClassName});

		this._elContent   = new Element('div', {'class': 'content'});
		this._elContent.update(strContent);

		this._elHeading   = new Element('div', {'class': 'heading'});
		this._elHeading.update('<h3>' + strWndTitle +'</h3>\n' + (document.all ? '' : '<ul class="controls">\n<li><a class="close" href="#">X</a></li>\n\n</ul>\n') + '<br class="clear">');

		this._elFooter    = new Element('div', {'class': 'footer'});

		this._elContainer.insert(this._elHeading);
		this._elContainer.insert(this._elContent);
		this._elContainer.insert(this._elFooter);
	},

	display: function() {
		var myself  = this;
		var offsetX = 0;
		var offsetY = 0;

		this._regIndex = NeoPopupRegistry.register(this);

		if (this._regIndex == -1) {
			return false;
		}

		this._build();
		document.body.appendChild(this._elContainer);

		//if we're using position absolute instead of fixed, we need
		//to calculate scrolling
		try {
			if (!document.all) {
				this._elContainer.style.position = 'fixed'; //attempt position fixed
			} else {
				this._elContainer.style.position = 'absolute'; //IE6 hack
			}
		} catch (e) {
			//nothing!
		}

		if (this._elContainer.style.position != 'fixed') {
			var scrollOffset = document.viewport.getScrollOffsets();
			offsetX          = scrollOffset.left;
			offsetY          = scrollOffset.top;
		}

		this._elContainer.makePositioned();
		this._elContainer.style.top  = offsetY + (document.viewport.getHeight() / 2) - (this._elContainer.getHeight() / 2) + 'px';
		this._elContainer.style.left = offsetX + (document.viewport.getWidth()  / 2) - (this._elContainer.getWidth()  / 2) + 'px';

		this._elContainer.select('.close').each(
			function(el) {
				el.observe('click', function(){myself.close(); return false;});
			}
		);
		this._elContainer.select('.confirm').each(
			function(el) {
				el.observe('click', function(){
					var cb = myself.getProperty('onconfirm');

					if (cb) {
						cb(this);
					}

					return false;
				});
			}
		);

		if (this.getProperty('mode', 'modal')) {
			this.enableScrim();
		}

		var objDrag = new Draggable(this._id, {handle: this._elHeading, starteffect: null, endeffect: null});

		return true;
	},

	confirm: function() {
		var strContent = this.getProperty('content');

		this.setProperty(
			'content',
			"<p>" + this.getProperty('content') +"</p><input type=\"button\" value=\"" +
			this.getText('confirm.yes') + "\" class=\"confirm close\"> <input type=\"button\" value=\"" +
			this.getText('confirm.no')  + "\" class=\"close\">"
		);

		return this.display();
	},

	alert: function() {
		var strContent = this.getProperty('content');

		this.setProperty(
			'content',
			"<p>" + this.getProperty('content') + "</p><div class=\"close_bg\"><a href=\"\"><input type=\"button\" value=\"" +
			this.getText('alert.close') + "\" class=\"close\"></a></div>"
		);

		return this.display();
	},

	getProperty: function(id) {
		return this._properties[id] ? this._properties[id] : null;
	},
	setProperty: function(id, value) {
		this._properties[id] = value;
		return true;
	},

	close: function() {
		var cbMyOnClose = this.getProperty('onclose');

		if (cbMyOnClose) {
			cbMyOnClose(this);
		}

		this._elContainer.remove();
		NeoPopupRegistry.unregister(this._regIndex);

		this.disableScrim();

		return false;
	},

	enableScrim: function() {
		var scrim = $('NeopPopupScrim');

		if (!scrim) {
			scrim = new Element('div', {'id': 'NeopPopupScrim'});
		}
		document.body.appendChild(scrim);

		scrim.style.width  = document.viewport.getWidth();
		scrim.style.height = document.viewport.getHeight();


	},

	disableScrim: function() {
		var scrim = $('NeopPopupScrim');

		if (scrim) {
			scrim.remove();
		}
	}
});
