/**
 * SqueezeBox - Expandable Lightbox
 * 
 * Allows to open various content as modal,
 * centered and animated box.
 * 
 * Inspired by 
 *  ... Lokesh Dhakar	- The original Lightbox v2
 *  ... Cody Lindley	- ThickBox
 * 
 * @version		1.0rc0
 * 
 * @license		MIT-style license
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @copyright	Author
 */
var SqueezeBox = {

	presets: {
		size: {x: 590, y: 410},
		sizeLoading: {x: 200, y: 150},
		marginInner: {x: 20, y: 20},
		marginImage: {x: 150, y: 200},
		handler: false,
		adopt: null,
		closeWithOverlay: true,
		zIndex: 65555,
		overlayOpacity: 0.7,
		classWindow: '',
		classOverlay: '',
		onOpen: Class.empty,
		onClose: Class.empty,
		onUpdate: Class.empty,
		onResize: Class.empty,
		onMove: Class.empty,
		onShow: Class.empty,
		onHide: Class.empty,
		ajaxOptions: {}
	},

	initialize: function(options) {
		if (this.options) return this;
		this.setOptions(this.presets = $merge(this.presets, options));

		this.build();
		this.listeners = {
			'window': this.reposition.bind(this, [null]),
			'close': this.close.bindWithEvent(this),
			'key': this.onKey.bindWithEvent(this)};
		this.isOpen = this.isLoading = false;
		return this;
	},

	build: function() {
		this.overlay = new Element('div', {'id': 'sbox-overlay',
			'styles': {'display': 'none', 'z-index': this.options.zIndex}});
		this.content = new Element('div', {'id': 'sbox-content'});
		this.btnClose = new Element('a', {'id': 'sbox-btn-close', 'href': 'javascript:void(null);'});
		this.window = new Element('div', {'id': 'sbox-window', 'styles': {'display': 'none', 'z-index': this.options.zIndex + 2}})
			.adopt(this.btnClose).adopt(this.content)
		$(document.body).adopt(this.overlay).adopt(this.window);

		this.fx = {
			'overlay': new Fx.Style(this.overlay, 'opacity', {duration: 250, wait: false}).set(0),
			'window': new Fx.Styles(this.window, {duration: 750, wait: false}),
			'content': new Fx.Style(this.content, 'opacity', {duration: 250, wait: false,
				onComplete: window.ie ? this.content.setStyle.bind(this.content, ['filter', '']) : Class.empty}).set(0)
		};
	},

	fromElement: function(el, options) {
		this.initialize();
		this.element = $(el);
		if (this.element && this.element.rel) options = $merge(options || {}, Json.evaluate(this.element.rel));
		this.setOptions(this.presets, options);
		this.assignOptions();
		this.url = (this.element ? (this.options.url || this.element.href) : el) || '';

		if (this.options.handler) {
			var handler = this.options.handler;
			return this.setContent(handler, this.parsers[handler].call(this, true));
		}

		var res = false;
		for (var handler in this.parsers) {
			if ((res = this.parsers[handler].call(this))) return this.setContent(handler, res);
		}
		return this;
	},

	assignOptions: function() {
		this.overlay.setProperty('class', this.options.classOverlay);
		this.window.setProperty('class', this.options.classWindow);
	},

	close: function() {
		if (!this.isOpen) return this;
		this.fx.overlay.start(0).chain(this.toggleOverlay.bind(this));
		this.window.setStyle('display', 'none');
		this.destroyImage();
		this.toggleListeners();
		this.isOpen = null;
		this.fireEvent('onClose', [this.content]);
		this.callChain();
		return this;
	},

	onError: function() {
		this.setContent('Error during loading');
	},

	destroyImage: function() {
		if (this.image) {
			Element.prototype.removeEvents.call(this.image);
			this.image = null;
		};
	},

	setContent: function(handler, content) {
		this.content.setProperty('class', 'sbox-content-' + handler);
		this.applyTimer = this.applyContent.delay(this.fx.overlay.options.duration, this, [this.handlers[handler].call(this, content)]);
		if (this.overlay.opacity) return this;
		this.toggleOverlay(true);
		this.fx.overlay.start(this.options.overlayOpacity);
		this.reposition();
		return this;
	},

	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.setHTML('')[['string', 'array', false].test($type(content)) ? 'setHTML' : '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 sizes = window.getSize();
		this.size = $merge(this.isLoading ? this.options.sizeLoading : this.options.size, size);
		var to = {
			'width': this.size.x, 'height': this.size.y,
			'left': (sizes.scroll.x + (sizes.size.x - this.size.x - this.options.marginInner.x) / 2).toInt(),
			'top': (sizes.scroll.y + (sizes.size.y - this.size.y - this.options.marginInner.y) / 2).toInt()};
		$clear(this.showTimer);
		this.hideContent();
		if (!instantly) this.fx.window.start(to).chain(this.showContent.bind(this))
		else {
			this.window.setStyles(to).setStyle('display', '');
			this.showTimer = this.showContent.delay(50, this);
		}
		this.reposition(sizes);
	},

	toggleListeners: function(state) {
		var task = state ? 'addEvent' : 'removeEvent';
		this.btnClose[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);
		window[task]('scroll', this.listeners.window);
	},

	toggleLoading: function(state) {
		this.isLoading = state;
		this.window[state ? 'addClass' : 'removeClass']('sbox-loading');
		if (state) this.fireEvent('onLoading', [this.window]);
	},

	toggleOverlay: function(state) {
		this.overlay.setStyle('display', state ? '' : 'none');
		$(document.body)[state ? 'addClass' : 'removeClass']('body-overlayed');
	},

	showContent: function() {
		if (this.content.opacity) this.fireEvent('onShow', [this.window]);
		this.fx.content.start(1);
	},

	hideContent: function() {
		if (!this.content.opacity) this.fireEvent('onHide', [this.window]);
		this.fx.content.stop().set(0);
	},

	onKey: function(e) {
		switch (e.key) {
			case 'esc': this.close(); break;
		}
	},

	reposition: function(sizes) {
		sizes = sizes || window.getSize();
		this.overlay.setStyles({
			'left': sizes.scroll.x, 'top': sizes.scroll.y,
			'width': sizes.size.x, 'height': sizes.size.y});
		this.window.setStyles({
			'left': (sizes.scroll.x + (sizes.size.x - this.window.offsetWidth) / 2).toInt(),
			'top': (sizes.scroll.y + (sizes.size.y - this.window.offsetHeight) / 2).toInt()});
		this.fireEvent('onMove', [this.overlay, this.window, sizes]);
	},

	parsers: {
		'image': function(preset) {
			return (preset || this.url.test(/\.(jpg|jpeg|png|gif|bmp)$/i)) ? this.url : false;
		},
		'adopt': function(preset) {
			if ($(this.options.adopt)) return $(this.options.adopt);
			if (preset || ($(this.element) && !this.element.parentNode)) return $(this.element);
			var bits = this.url.match(/#([\w-]+)$/);
			return bits ? $(bits[1]) : false;
		},
		'url': function(preset) {
			return (preset || (this.url && !this.url.test(/^javascript:/i))) ? this.url: false;
		},
		'string': function(preset) {
			return true;
		}
	},

	handlers: {
		'image': function(url) {
			this.image = new Element('img');
			Element.prototype.addEvents.call(this.image, {
				'load': function() {
					var win = {x: window.getWidth() - this.options.marginImage.x, y: window.getHeight() - this.options.marginImage.y};
					var size = {x: this.image.width, y: this.image.height};
					for (var i = 0; i < 2; i++)
						if (size.x > win.x) { size.y *= win.x / size.x; size.x = win.x;
						} else if (size.y > win.y) { size.x *= win.y / size.y; size.y = win.y; }
					size = {x: parseInt(size.x), y: parseInt(size.y)};
					this.image = window.khtml ? new Element('img', {'src': this.image.src}) : $(this.image);
					this.image.setProperties({'id': 'sbox-image', 'width': size.x, 'height': size.y});
					this.applyContent(this.image, size);
					this.destroyImage();
				}.bind(this),
				'error': this.onError.bind(this),
				'abort': this.onError.bind(this)
			});
			(function() {
				this.src = url;
			}).delay(10, this.image);
		},
		'adopt': function(el) {
			return el.clone();
		},
		'url': function(url) {
			this.ajax = new Ajax(url, this.options.ajaxOptions);
			this.ajax.addEvent('onSuccess', function(resp) {
				this.applyContent(resp);
				this.ajax = null;
			}.bind(this));
			this.ajax.addEvent('onFailure', this.onError.bind(this));
			this.ajax.request.delay(10, this.ajax);
		},
		'string': function(str) {
			return str;
		}
	},

	extend: $extend
};

SqueezeBox.extend(Events.prototype);
SqueezeBox.extend(Options.prototype);
SqueezeBox.extend(Chain.prototype);

SqueezeBox.parsers.swf = function(preset) {
			return (preset || this.url.test(/\.swf/)) ? this.url : false;
		}

		SqueezeBox.handlers.swf = function(url) {
			var size = this.options.size;
			var swfobj = new SWFObject(url, 'sbox-swf', size.x, size.y, 9, '#ffffff');
			return swfobj.getSWFHTML();
		}

		window.addEvent('domready', function() {

			$ES('a.boxed').each(function(el) {
				el.addEvent('click', function(e) {
					e.stop();
					if (SqueezeBox.fromElement(el)) e.stop();
				}.bindWithEvent(el));
			});

			SqueezeBox.initialize({
				height: 350,
				width: 400,
				ajaxOptions: {
					method: 'get'
				}
			});

			$$('.panel-toggler').each(function(el) {
				var target = el.getLast().setStyle('display', 'none');
				el.getFirst().addEvent('click', function() {
					target.style.display = (target.style.display == 'none') ? '' : 'none';
				});
			});
		});

		var openSmooth = function() {
			var from = $('myGallery').clone(), smooth, crsl;
			SqueezeBox.fromElement(from, {size: {x: 465, y: 350}}).chain(function() {
				smooth = new gallery(this.content.getFirst(), {
					timed: false
				});
				// dirty hacked ;)
				$(document.body).addClass('jdGallery');
				crsl = smooth.carouselContainer.element;
				crsl.injectAfter(SqueezeBox.window);
				crsl.setStyles({'z-index': SqueezeBox.options.zIndex + 3});
			}).chain(function() {
				$(document.body).removeClass('jdGallery');
				crsl.remove();
			});
		}