/*
 * jLouvre - inline gallery - jQuery plugin
 * Examples and documentation at: http://labs.dreamweb.sk/jlouvre
 * Version: 1.0 (10/10/2009)
 * Copyright (c) 2009 Juraj Ivan
 * Requires: jQuery v1.3+
*/

(function($){
$.fn.extend(
{
	jlouvre: function(options)
	{
		var defaults = {
			clickToClose: true,
			image: '#image',
			caption: '#caption',
			thumb: '.thumb',
			thumbs: '#thumbs',
			controls: '#controls',
			controlPrev: '#control_prev',
			controlPrevTxt: 'Previous',
			controlNext: '#control_next',
			controlNextTxt: 'Next',
			controlClose: '#control_close',
			controlCloseTxt: 'Close'
		}
		
		var config = $.extend(defaults, options);
		
		return this.each(function()
		{
			var $this = $(this);
	
			var html  = '<div id="controls">';
				html += '<a href="javascript:void(0)" id="'+config.controlPrev.substr(1)+'">'+config.controlPrevTxt+'</a>';
				html += '<a href="javascript:void(0)" id="'+config.controlNext.substr(1)+'">'+config.controlNextTxt+'</a>';
				html += '<a href="javascript:void(0)" id="'+config.controlClose.substr(1)+'">'+config.controlCloseTxt+'</a>';
				html += '</div>';
				html += '<div id="'+config.image.substr(1)+'" class="loading"></div>';
				html += '<div id="caption"></div>';

			$this.append(html);
			$(config.controls).hide();
			$(config.image).hide();
			$(config.caption).hide();
			
			if(window.location.hash.substr(2) != '')
			{
				load_image(window.location.hash.substr(2));
			}
			
			$(config.thumb).click(function(event)
			{
				$('html, body').animate(0, 500);
				load_image($(this).attr('id'));
				return false;
			});
			
			$(config.image+' > img').live('click', function()
			{
				if(config.clickToClose) load_all(); else load_next(window.location.hash.substr(2));
			});
		
			$(config.controlPrev).click(function()
			{
				load_prev(window.location.hash.substr(2));
			});
		
			$(config.controlNext).click(function()
			{
				load_next(window.location.hash.substr(2));
			});
			
			$(config.controlClose).click(function()
			{
				load_all();
			});
	
			function load_image(id)
			{
				var $el = $('#'+id);
			
				if($el.length)
				{
					window.location.hash = '/'+id;
					
					if(first_image(id)) $(config.controlPrev).addClass('disabled'); else $(config.controlPrev).removeClass('disabled');
					if(last_image(id))  $(config.controlNext).addClass('disabled'); else $(config.controlNext).removeClass('disabled');
					
					$(config.thumbs).hide();
					$(config.caption).html('').hide();
					$(config.controls).show();
					$(config.image).html('').addClass('loading').show();		
					
					var img = new Image();
						src = $el.attr('href');
						caption = $el.attr('title');

					$(img).load(function()
					{		
						$(this).hide();
						$(config.image).removeClass('loading').html(this);
						$(config.image).css('min-height', $(img).height()+1);
						if(caption.length) $('#caption').html(caption).show();
						$(this).fadeIn();
					}).attr('src', src);
				}
			}
			
			function first_image(id)
			{
				return $('#'+id).parent().prev().length == 0 ? true : false;
			}
			
			function last_image(id)
			{
				return $('#'+id).parent().next().length == 0 ? true : false;
			}
			
			function load_prev(id)
			{
				prev = $('#'+id).parent().prev().find('a').attr('id');
				
				if($('#'+prev).length) load_image(prev);
			}
			
			function load_next(id)
			{
				next = $('#'+id).parent().next().find('a').attr('id');
				
				if($('#'+next).length) load_image(next);
			}
			
			function load_all()
			{
				window.location.hash = '/';
				
				$(config.controls).hide();
				$(config.image).html('').addClass('loading').hide();
				$(config.caption).html('').hide();
				$(config.thumbs).show();
			}
			
			$(window).keydown(function(event)
			{
				var keycode = (window.event) ? event.keyCode : event.keyCode;
				var esc = (window.event) ? 27 : event.DOM_VK_ESCAPE;
				
				switch(keycode)
				{
					case esc:
					case 27: load_all(); break;
					case 37: load_prev(window.location.hash.substr(2)); break;
					case 39: load_next(window.location.hash.substr(2)); break;
				}
			});
		});
		
		return this;
	}
});
})(jQuery);
