/**

 * SqueezeBox - Expandable Lightbox

 *

 * Allows to open various content as modal,

 * centered and animated box.

 *

 * Dependencies: MooTools 1.2 trunk (04/2008)

 *

 * Inspired by

 *  ... Lokesh Dhakar	- The original Lightbox v2

 *

 * @version		1.1 rc0

 *

 * @license		MIT-style license

 * @author		Harald Kirschner <mail [at] digitarald.de>

 * @copyright	Author

 */

var SqueezeBox = {



	presets: {

		size: {x: 600, y: 450},

		sizeLoading: {x: 200, y: 150},

		marginInner: {x: 20, y: 20},

		marginImage: {x: 150, y: 200},

		handler: false,

		target: null,

		closeWithOverlay: true,

		zIndex: 65555,

		overlayOpacity: 0.7,

		classWindow: '',

		classOverlay: '',

		overlayFx: {},

		resizeFx: {},

		contentFx: {},

		ajaxOptions: {},

		parse: false, // 'rel'

		parseSecure: false,

		onOpen: $empty,

		onClose: $empty,

		onUpdate: $empty,

		onResize: $empty,

		onMove: $empty,

		onShow: $empty,

		onHide: $empty

	},



	initialize: function(presets) {

		if (this.options) return this;

		this.presets = $merge(this.presets, presets);

		this.options = {};

		this.setOptions(this.presets).build();

		this.listeners = {

			window: this.reposition.bind(this, [null]),

			close: this.close.bind(this),

			key: this.onKey.bind(this)

		};

		this.isOpen = this.isLoading = false;

		return this;

	},



	build: function() {

		this.content = new Element('div', {id: 'sbox-content'});

		this.closeBtn = new Element('a', {id: 'sbox-btn-close', href: '#'});

		this.overlay = new Element('div', {

			id: 'sbox-overlay',

			styles: {display: 'none', zIndex: this.options.zIndex}

		}).inject(document.body);

		this.win = new Element('div', {

			id: 'sbox-window',

			styles: {display: 'none', zIndex: this.options.zIndex + 2}

		}).adopt(this.closeBtn, this.content).inject(document.body);

		this.fx = {

			overlay: new Fx.Tween(this.overlay, $merge({

				property: 'opacity',

				onStart: Events.prototype.clearChain,

				duration: 250,

				link: 'cancel'

			}, this.options.overlayFx)).set(0),

			win: new Fx.Morph(this.win, $merge({

				onStart: Events.prototype.clearChain,

				unit: 'px',

				duration: 750,

				transition: Fx.Transitions.Quint.easeOut,

				link: 'cancel',

				unit: 'px'

			}, this.options.resizeFx)),

			content: new Fx.Tween(this.content, $merge({

				property: 'opacity',

				duration: 250,

				link: 'cancel'

			}, this.options.contentFx)).set(0)

		};

	},



	assign: function(to, options) {

		return to.addEvent('click', function() {

			return !SqueezeBox.fromElement(this, options);

		});

	},



	fromElement: function(from, options) {

		this.initialize();

		if (this.element) this.trash();

		this.element = $(from);

		this.setOptions($merge(this.presets, options || {}));

		if (this.element && this.options.parse) {

			var obj = this.element.getProperty(this.options.parse);

			if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);

		}

		this.assignOptions();

		this.url = ((this.element) ? (this.options.url || this.element.get('href')) : from) || '';

		if (this.options.handler) {

			var handler = this.options.handler;

			return this.setContent(handler, this.parsers[handler].call(this, true));

		}

		for (var key in this.parsers) {

			var content = this.parsers[key].call(this);

			if (content) return this.setContent(key, content);

		}

		return false;

	},



	assignOptions: function() {

		this.overlay.className = this.options.overlayClass;

		this.win.className = this.options.windowClass;

		if (Browser.Engine.trident4) this.win.addClass('sbox-window-ie6');

	},



	close: function(e) {

		if (e) e.stop();

		if (!this.isOpen) return this;

		this.fx.overlay.start(0).chain(this.toggleOverlay.bind(this));

		this.win.setStyle('display', 'none');

		this.trash();

		this.isOpen = null;

		this.fireEvent('onClose', [this.content]);

		return this;

	},



	trash: function() {

		this.element = this.image = null;

		this.options = {};

		this.removeEvents().setOptions(this.presets).callChain();

	},



	onError: function() {

		this.image = null;

		this.setContent('string', 'Error during loading');

	},



	setContent: function(handler, content) {

		if (!this.handlers[handler]) return false;

		this.content.className = 'sbox-content-' + handler;

		this.applyTimer = this.applyContent.delay(this.fx.overlay.options.duration, this, this.handlers[handler].call(this, content));

		if (this.overlay.retrieve('opacity')) return this;

		this.toggleOverlay(true);

		this.fx.overlay.start(this.options.overlayOpacity);

		return this.reposition();

	},



	applyContent: function(content, size) {

		this.applyTimer = $clear(this.applyTimer);

		this.hideContent();

		if (!content) {

			this.toggleLoading(true);

		} else {

			if (this.isLoading) this.toggleLoading(false);

			this.fireEvent('onUpdate', [this.content], 20);

		}

		this.content.empty();

		(['string', 'array', false].contains($type(content))) ? this.content.set('html', content || '') : this.content.adopt(content);

		this.callChain();

		if (!this.isOpen) {

			this.toggleListeners(true);

			this.resize(size, true);

			this.isOpen = true;

			this.fireEvent('onOpen', [this.content]);

		} else {

			this.resize(size);

		}

	},



	resize: function(size, instantly) {

		var box = document.getSize(), scroll = document.getScroll();

		this.size = $merge((this.isLoading) ? this.options.sizeLoading : this.options.size, size);

		var to = {

			width: this.size.x,

			height: this.size.y,

			left: (scroll.x + (box.x - this.size.x - this.options.marginInner.x) / 2).toInt(),

			top: (scroll.y + (box.y - this.size.y - this.options.marginInner.y) / 2).toInt()

		};

		$clear(this.showTimer || null);

		this.hideContent();

		if (!instantly) {

			this.fx.win.start(to).chain(this.showContent.bind(this));

		} else {

			this.win.setStyles(to).setStyle('display', '');

			this.showTimer = this.showContent.delay(50, this);

		}

		return this.reposition();

	},



	toggleListeners: function(state) {

		var task = (state) ? 'addEvent' : 'removeEvent';

		this.closeBtn[task]('click', this.listeners.close);

		if (this.options.closeWithOverlay) this.overlay[task]('click', this.listeners.close);

		document[task]('keydown', this.listeners.key);

		window[task]('resize', this.listeners.window)[task]('scroll', this.listeners.window);

	},



	toggleLoading: function(state) {

		this.isLoading = state;

		this.win[(state) ? 'addClass' : 'removeClass']('sbox-loading');

		if (state) this.fireEvent('onLoading', [this.win]);

	},



	toggleOverlay: function(state) {

		this.overlay.setStyle('display', (state) ? '' : 'none');

		$(document.body)[(state) ? 'addClass' : 'removeClass']('body-overlayed');

	},



	showContent: function() {

		if (this.content.get('opacity')) this.fireEvent('onShow', [this.win]);

		this.fx.content.start(1);

	},



	hideContent: function() {

		if (!this.content.get('opacity')) this.fireEvent('onHide', [this.win]);

		this.fx.content.set(0);

	},



	onKey: function(e) {

		if (e.key == 'esc') this.close();

	},



	reposition: function() {

		var size = document.getSize(), scroll = document.getScroll();

		this.overlay.setStyles({

			left: scroll.x + 'px',

			top: scroll.y + 'px',

			width: size.x + 'px',

			height: size.y + 'px'

		});

		this.win.setStyles({

			left: (scroll.x + (size.x - this.win.offsetWidth) / 2).toInt() + 'px',

			top: (scroll.y + (size.y - this.win.offsetHeight) / 2).toInt() + 'px'

		});

		return this.fireEvent('onMove', [this.overlay, this.win]);

	},



	removeEvents: function(type){

		if (!this.$events) return this;

		if (!type) this.$events = null;

		else if (this.$events[type]) this.$events[type] = null;

		return this;

	},



	parsers: {



		image: function(preset) {

			return (preset || (/\.(?:jpg|png|gif)$/i).test(this.url)) ? this.url : false;

		},



		clone: function(preset) {

			if ($(this.options.target)) return $(this.options.target);

			if (this.element && !this.element.parentNode) return this.element;

			var bits = this.url.match(/#([\w-]+)$/);

			return (bits) ? $(bits[1]) : (preset ? this.element : false);

		},



		url: function(preset) {

			return (preset || (this.url && !(/^(?:javascript|#)/i).test(this.url))) ? this.url : false;

		},



		iframe: function(preset) {

			return (preset || this.url) ? this.url : false;

		},



		string: function(preset) {

			return true;

		}



	},



	handlers: {



		image: function(url) {

			var size, tmp = new Image();

			this.image = null;

			tmp.onload = tmp.onabort = tmp.onerror = (function() {

				tmp.onload = tmp.onabort = tmp.onerror = null;

				if (!tmp.width) {

					this.onError.delay(10, this);

					return;

				}

				var box = document.getSize();

				box.x -= this.options.marginImage.x;

				box.y -= this.options.marginImage.y;

				size = {x: tmp.width, y: tmp.height};

				for (var i = 2; i--;) {

					if (size.x > box.x) {

						size.y *= box.x / size.x;

						size.x = box.x;

					} else if (size.y > box.y) {

						size.x *= box.y / size.y;

						size.y = box.y;

					}

				}

				size.x = size.x.toInt();

				size.y = size.y.toInt();

				this.image = $(tmp);

				tmp = null;

				this.image.setProperties({width: size.x, height: size.y});

				if (this.isOpen) this.applyContent(this.image, size);

			}).bind(this);

			tmp.src = url;

			if (tmp && tmp.onload && tmp.complete) tmp.onload();

			return (this.image) ? [this.image, size] : null;

		},



		clone: function(el) {

			return el.clone();

		},



		adopt: function(el) {

			return el;

		},



		url: function(url) {

			this.ajax = new Request(this.options.ajaxOptions);

			this.ajax.addEvents({

				'onSuccess': function(resp) {

					this.applyContent(resp);

					this.ajax = null;

				}.bind(this),

				'onFailure': this.onError.bind(this)

			}).send.delay(10, this.ajax, [{url: url}]);

		},



		iframe: function(url) {

			return new Element('iframe', {

				src: url,

				frameBorder: 0,

				width: this.options.size.x,

				height: this.options.size.y

			});

		},



		string: function(str) {

			return str;

		}



	},



	extend: function(properties) {

		return $extend(this, properties);

	}

};



SqueezeBox.parsers.adopt = SqueezeBox.parsers.clone;



SqueezeBox.extend(new Events($empty));

SqueezeBox.extend(new Options($empty))

SqueezeBox.extend(new Chain($empty));