/*
 * jQuery Enlarge Plugin v0.2 (2011-06-10)
 * Copyright (c) 2011, Gregory Plüss (gpluess@fconnection.com)
 */

(function($)
{
	var version = '0.2';

	$.fn.enlarge = function(options)
	{
		var options = $.extend({}, $.fn.enlarge.defaults, options);

		var cutHex = function(h)
		{
			return (h.charAt(0) == "#") ? h.substring(1,7) : h;
		}

		var fixHex = function(h)
		{
			x = h.substring(0,1);
			y = h.substring(1,2);
			z = h.substring(2,3);
			return x+x+y+y+z+z;
		}

		var hex2rgba = function(h, a)
		{
			// fix h
			h = cutHex(h);

			(h.length === 3 || h.length === 6) ? h : h = 'fff';
			(h.length === 3) ? h = fixHex(h) : h;

			// fix a
			a = parseFloat(a);

			(a > 1 ? a = 1 : a);
			(a < 0 ? a = 0 : a);

			// convert values
			var r = parseInt(h.substring(0,2),16);
			var g = parseInt(h.substring(2,4),16);
			var b = parseInt(h.substring(4,6),16);

			return 'rgba('+r+','+g+','+b+','+a+')';
		}

		return this.each(function()
		{
			$(this).click(function(e)
			{
				e.preventDefault();

				var src = $(this).find('img').attr('src').slice(0, -4) + options.imageSuffix;
				var alt = $(this).find('img').attr('alt');

				var img = $('<img />',
				{
					'src':src,
					'alt':alt,
					'class':'overlay'
				}).css(
				{
					'position':'fixed',
					'z-index':9999,
					'display':'none'
				});

				var ovl = $('<div />',
				{
					'class':'overlay'
				}).css(
				{
					'position':'fixed',
					'z-index':9998,
					'background':(options.overlay ? hex2rgba(options.overlayColor, options.overlayOpacity) : 'transparent'),
					'top':0,
					'width':'100%',
					'height':'100%'
				});

				$('body').append(img);

				if (options.boxShadow)
				{
					$('img.overlay').css(
					{
						'color':'red',
						'-moz-box-shadow':options.boxShadowHOffset + ' ' + options.boxShadowVOffset + ' ' + options.boxShadowRadius + ' ' + options.boxShadowColor,
						'-webkit-box-shadow':options.boxShadowHOffset + ' ' + options.boxShadowVOffset + ' ' + options.boxShadowRadius + ' ' + options.boxShadowColor,
						'box-shadow':options.boxShadowHOffset + ' ' + options.boxShadowVOffset + ' ' + options.boxShadowRadius + ' ' + options.boxShadowColor
					});
				}

				img.load(function()
				{
					var img_ovl = $('img.overlay');

					if (typeof $(document).data('img_width') === 'undefined')
					{
						$(document).data('img_width', ($(img_ovl).width() > 0) ? $(img_ovl).width() : options.defaultWidth);
					}

					if (typeof $(document).data('img_height') === 'undefined')
					{
						$(document).data('img_height', ($(img_ovl).height() > 0) ? $(img_ovl).height() : options.defaultHeight);
					}

					$('body').append(ovl);

					$('img.overlay').css(
					{
						'top':($(window).height() - $(document).data('img_height')) / 2,
						'left':($(window).width() - $(document).data('img_width')) / 2
					}).fadeIn();

					$('img.overlay, div.overlay').live('click', function(e)
					{
						e.preventDefault();

						// destroy
						$('img.overlay, div.overlay').fadeOut('fast', function()
						{
							$(this).remove();
						});
					});
				});
			});
		});
	};

	$.fn.enlarge.defaults = {
		defaultHeight:null,
		defaultWidth:null,
		overlay:true,
		overlayColor:'#fff',
		overlayOpacity:0.75,
		boxShadow:true,
		boxShadowHOffset:0,
		boxShadowVOffset:0,
		boxShadowRadius:'15px',
		boxShadowColor:'#888',
		imageSuffix:'-big.jpg'
	};

	$.fn.enlarge.version = function() { return version; };

})(jQuery);
