// Copyright (c) 2006 Lukas Zeman (http://www.proteus.cz)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

EmLightBox = Class.create();

EmLightBox.prototype = {
	version: '0.9.1',

	initialize: function(options) {
		this.options             = options ? options : {};
		this.options.imgAlt      = this.options.imgAlt || 'Close to click';
		this.options.closeMsg    = this.options.closeMsg || 'Close';
		this.options.maximizeMsg = this.options.maximizeMsg || 'Maximize to the new window';
		Event.observe(window, 'load', this.initializeAnchors.bindAsEventListener(this) , false);
	},

	createElement : function(tagName, id, className, textNode, parent) {
		var el        = document.createElement(tagName);
		el.id         = id ? id : '';
		el.className  = className ? className : '';
		if (textNode) {
			el.appendChild(document.createTextNode(textNode))
		}
		return parent.appendChild(el);
	},

	show: function(event) {
		var anchor = Event.findElement(event, 'A');
		if (anchor) {
			Element.addClassName(document.body, 'emLightBox');
			Element.update(this.divTxt, anchor.title);
			this.divImg.style.backgroundImage = 'url(' + anchor.href + ')';
			this.aMax.href       = anchor.href;
			this.hideEscObserver = this.hideEsc.bindAsEventListener(this);
			Event.observe(document, 'keypress', this.hideEscObserver);
		}
		Event.stop(event);
	},

	hideEsc: function(event) {
		if (event.keyCode == Event.KEY_ESC) {
			this.hide();
			Event.stopObserving(document, 'keypress', this.hideEscObserver);
		}
	},

	hide: function() {
		Element.removeClassName(document.body, 'emLightBox');
	},

	initializeAnchors: function() {
		var reg     = new RegExp('^.+\.(jpg|jpeg|png|gif)$', 'i');
		var anchors = $A($$('A'));
		anchors.each(function(anchor) {
			if (anchor.href.match(reg) && !Element.hasClassName(anchor, 'noEmLightBox')) {
				Event.observe(anchor, 'click', this.show.bindAsEventListener(this));
			}
		}.bind(this));
		this.divBgr = this.createElement('DIV', 'emLightBox', false, false, document.body);
		this.divImg = this.createElement('DIV', 'emLightBoxImage', false, false, document.body);
		this.divTtl = this.createElement('DIV', false, 'title', false, this.divImg);
		this.divTxt = this.createElement('DIV', 'emLightBoxText', false, '', this.divTtl);
		this.aClose = this.createElement('A', false, 'close', this.options.closeMsg, this.divTtl);
		this.aMax   = this.createElement('A', 'emLightBoxMax', false, this.options.maximizeMsg, this.divTtl);
		Event.observe(this.divImg, 'click', this.hide.bindAsEventListener(this));
		this.divImg.title = this.options['imgAlt'];
		this.aMax.href    = "javascript: void(0);";
		this.aMax.target  = "_blank";
		this.aClose.href  = "javascript: void(0);";
	}
}
