/*
 * jQuery Lightbox Plugin
 */

(function($) {

	$.fn.Lightbox = function(options) {

		var defaults = {
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			addClass: '',
			contentType: 'html',
			contentTitle: '',
			contentValue: '',
			boxWidth: 0,
			boxHeight: 0,
			frameWidth: 0,
			frameHeight: 0,
			onOpen: '',
			onClose: '',
			triggerClick: true
		};

		var opts = $.extend(defaults, options);
		var self = this;

		function initialize() {
			set_interface();
			return false;
		};

		function set_interface() {
			$('embed, object, select').css('visibility', 'hidden');

			// Setup lightbox div's
			var strHTML = '<div id="lightbox-container"><div id="lightbox-overlay"></div><div id="lightbox"><div id="lightbox-title"></div><div id="lightbox-content"></div><div id="lightbox-close"><a href="javascript:void(0)"><span>Close</span></a></div></div></div>';
			$(strHTML).appendTo('body');

			// Set container width and height
			$('#lightbox-container').css({
				width: $(document).width(),
				height: $(document).height()
			});

			// Fade in the overlay
			$('#lightbox-overlay').css({
				backgroundColor: opts.overlayBgColor,
				opacity: opts.overlayOpacity
			}).fadeIn(200, function() {

				// Display title
				if (opts.contentTitle == '' && typeof $(self).attr('title') !== 'undefined') opts.contentTitle = $(self).attr('title');
				if (opts.contentTitle !== '') $('#lightbox-title').html(opts.contentTitle);

				// Show loading
				$('#lightbox-content').html('<div id="lightbox-loading"><span>Loading...</span></div>');

				// Center lightbox
				position_center();

				// Load content types
				load_content_type();
			});

			// Close lightbox if click on overlay or close button
			$('#lightbox-overlay, #lightbox-close a').click(function() {
				close();
				return false;
			});
		};

		function load_content_type() {
			if (opts.contentType == 'image') {
				load_image();
			}
			else if (opts.contentType == 'flash') {
				load_flash();
			}
			else if (opts.contentType == 'iframe') {
				load_iframe();
			}
			else if (opts.contentType == 'element') {
				load_element();
			}
			else {
				load_html(opts.contentValue);
			}
		};

		function load_image() {
			if (opts.contentValue !== '') {
				var img = opts.contentValue;
			}
			else {
				var img = $(self).attr('href');
			}

			// Pre-load image
			var imgPreloader = new Image();
			imgPreloader.onload = function() {
				var strHTML = '<img src="' + img + '" alt="" />';
				load_html(strHTML);
			};
			imgPreloader.src = img;
		};
		
		function load_flash() {
			if (opts.contentValue !== '') {
				var url = opts.contentValue;
			}
			else {
				var url = $(self).attr('href');
			}

			// Show flash
			var frameWidth = (opts.frameWidth == 0) ? opts.boxWidth : opts.frameWidth;
			var frameHeight = (opts.frameHeight == 0) ? opts.boxHeight : opts.frameHeight;
			var strHTML = '<object width="' + frameWidth + '" height="' + frameHeight + '"><param name="movie" value="' + url + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + url + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + frameWidth + '" height="' + frameHeight + '"></embed></object>';
			load_html(strHTML);
		};

		function load_iframe() {
			if (opts.contentValue !== '') {
				var url = opts.contentValue;
			}
			else {
				var url = $(self).attr('href');
			}

			// Show iframe
			var frameWidth = (opts.frameWidth == 0) ? opts.boxWidth : opts.frameWidth;
			var frameHeight = (opts.frameHeight == 0) ? opts.boxHeight : opts.frameHeight;
			var strHTML = '<iframe src="' + url + '" width="' + frameWidth + '" height="' + frameHeight + '" frameborder="0" allowtransparency="true"></iframe>';
			load_html(strHTML);
		};

		function load_element() {
			if (opts.contentValue !== '') {
				var el = opts.contentValue;
			}
			else {
				var el = $(self).attr('href');
			}

			// Add hash if required
			if (el.substr(0, 1) !== '#') el = '#' + el;

			// Show contents
			var strHTML = $(el).html();
			load_html(strHTML);
		};

		function load_html(strHTML) {

			// Show the final HTML
			$('#lightbox-content').html(strHTML);

			// Add extra class if necessary
			if (opts.addClass !== '') $('#lightbox-content').addClass(opts.addClass);
			
			// Add width and height
			if (opts.boxWidth > 0) $('#lightbox').width(opts.boxWidth);
			if (opts.boxHeight > 0) $('#lightbox').height(opts.boxHeight);

			// Center lightbox
			position_center();
			
			// Call on open function
			if (opts.onOpen !== '' && $.isFunction(opts.onOpen)) opts.onOpen();
		};

		function position_center() {
			$('#lightbox').css('top', ( $(window).height() - $('#lightbox').height() ) / 2 + "px");
			$('#lightbox').css('left', ( $(window).width() - $('#lightbox').width() ) / 2 + "px");
		};

		function close() {

			// Hide lightbox
			$('#lightbox').hide();

			// Fade out overlay
			$('#lightbox-overlay').fadeOut(200, function(){

				// Remove whole lightbox container
				$('#lightbox-container').remove();
				$('embed, object, select').css('visibility', 'visible');
				
				// Call on close function
				if (opts.onClose !== '' && $.isFunction(opts.onClose)) opts.onClose();
			});
		};

		// Run lightbox on click
		if (opts.triggerClick) {
			return this.click(initialize);
		}
		else {
			return initialize();
		}
	};
	
	$.Lightbox = {
		
		// Can be called via $.Lightbox.open();
		open: function(options) {
			var opts = $.extend({ triggerClick: false }, options);
			var div = document.createElement('div');
			$(div).Lightbox(opts);
		},
		
		// Can be called via $.Lightbox.close();
		close: function() {
			$('#lightbox').hide();
			$('#lightbox-overlay').fadeOut(200, function(){
				$('#lightbox-container').remove();
				$('embed, object, select').css('visibility', 'visible');
			});
		}
		
	};
	
})(jQuery);
