function Lightbox (options) {
	Lightbox.prototype.initialize(options);
}

Lightbox.prototype = {
	config: null,

	defaults: {
		name: '',
		width: null,
		height: null,
		close_button: true,
		overlay: true,
		content: {
			type: 'html',
			src: '#'
		},

		transition_speed: 300,
		delay: 500,
		overlay_opacity: 0.80,

		after_close: function () { }
	},

	initialize: function (options) {
		this.settings = $.extend({}, this.defaults, options);
		this.renderMarkup();
		cufonCall();
		this.cacheSelectors();
		this.attachEvents();
		this.loadContent();

		if (this.settings['open'] == true) {
			this.open();
		}
	},

	renderMarkup: function () {
		var markup = '';

		// remove the existing lightbox
		if (typeof this.$overlay != 'undefined') this.$overlay.remove();
		if (typeof this.$shell != 'undefined') this.$shell.remove();

		if (this.settings.overlay == true) {
			markup += '<div id="lightbox_' + this.settings['name'] + '_overlay" class="lightbox_overlay" style="display:none; width:100%; height:100%; position:absolute; top:0; left:0; z-index:999999"></div>';
		}

		markup += '<div id="lightbox_' + this.settings['name'] + '" class="lightbox" style="display:none; position:absolute; z-index:9999999">';

		if (this.settings.close_button == true) {
			markup += '<div id="lightbox_' + this.settings['name'] + '_close" class="lightbox_close">Close X</div>';			
		}

		markup += '<div id="lightbox_' + this.settings['name'] + '_container" class="lightbox_container"></div></div>';

		$('body').append(markup);
	},

	cacheSelectors: function () {
		this.selector = {
			overlay: '#lightbox_' + this.settings['name'] + '_overlay',
			shell: '#lightbox_' + this.settings['name'],
			close: '#lightbox_' + this.settings['name'] + '_close',
			container: '#lightbox_' + this.settings['name'] + '_container'
		};

		this.$html = $('html');
		this.$overlay = $(this.selector.overlay);
		this.$close = $(this.selector.close);
		this.$shell = $(this.selector.shell);
		this.$close = $(this.selector.close);
		this.$container = $(this.selector.container);
		this.$overlay.height($(document).height());
	},

	attachEvents: function () {
		var _self = this;

		this.$shell.draggable();

		if (this.$close.length > 0) {
			this.$close.click(function () {
				_self.close();
			});
		}
	},

	loadContent: function () {
		var _self = this;
		switch (this.settings['content']['type']) {
			case 'text': this.$container.append('<p>' + this.settings['content']['src'] + '</p>');
				_self.resize();
				break;

			default: this.$container.load(this.settings['content']['src'], function () {
				if (_self.settings.overlay == true) {
					_self.$overlay.show().fadeTo(_self.settings.transition_speed, _self.settings.overlay_opacity);
				}
				_self.$shell.fadeIn(_self.settings.transition_speed);
				_self.resize();
				_self.center();
			});
				break; // ajax loading
		}
	},

	resize: function () {
		if (this.settings['width'] == null) {
			this.$shell.width(this.$container.width());
		} else {
			this.$shell.width(this.settings['width']);
		}

		if (this.settings['height'] == null) {
			this.$shell.height(this.$container.height());
		} else {
			this.$shell.height(this.settings['height']);
		}
		
		if (this.$container.width() > 0)
		{
			if(this.$container.find('object').length > 0){
				this.$container.css({ 
					'width': this.$container.find('object').width()+'px'
				});
			}else{
				this.$container.css({ 
					'width': this.$container.width()+'px'
				});
			}
			this.$shell.css({ 
				'width': this.$container.outerWidth(true)+'px'
			});
		}
	},

	center: function () {
		this.$shell.css({ 'top': (this.$html.scrollTop() + ($(window).height() / 2) - (this.$container.height() / 2)) + 'px',
			'left': ($(window).width() / 2) - (this.$container.width() / 2) + 'px'
		});
	},

	open: function () {

	},

	close: function () {
		var _self = this;
		if (this.$overlay)
			this.$overlay.fadeOut(this.settings.transition_speed, function () { $(this).remove() });
		if (this.$shell)
			this.$shell.fadeOut(this.settings.transition_speed, function () { $(this).remove() });

		this.settings.after_close.call(this);
	}
};

$(function () {
	$('a[rel^=lightbox]').click(function (e) {
		e.preventDefault();
		var $link = $(this);
		var options = $link.attr('rel').split('|');
		var settings = {
			name: options[1],
			width: options[2],
			height: options[3],
			overlay: ($.inArray('overlay', options) != -1) ? true : false,
			content: { src: $link.attr('href') + ' #' + options[1] + '_content' },

			after_close: function () {
				if (typeof (setFocusOnFlash) != 'undefined') {
					setFocusOnFlash();
				}
			}
		};
		Lightbox(settings);
	});
});
