/*
 * LostBox (for jQuery)
 * version: 1.0 (03/06/2008)
 * @requires jQuery v1.2 or later
 *
 * Copyright (c) 2008 Claudiu Andrei (www.inspireco.ro)
 *
 * Licensed under CC Attribution 3.0:
 *   http://creativecommons.org/licenses/by/3.0/
 *
 */

(function($) {
	$.lostbox = {
		settings: {
			loaderWidth 	: 300,
			inlineWidth 	: 540,
			imageTypes  	: [ 'png', 'jpg', 'jpeg', 'gif' ]
		},

		init: function(settings) {
			if (settings)
				$.extend($.lostbox.settings, settings);
			$.lostbox.settings.imageTypesRegexp = new RegExp('\.' + $.lostbox.settings.imageTypes.join('|') + '$', 'i');

			if ( ! $.lostbox.settings.loaded) {
				$.lostbox.settings.loaded = true;
			}
		},

		load: function() {
			$('#tagline .slider').show().empty().append('<div class="loading"></div>');
			$(document).bind('keydown.lostbox', keyPressed);
		},

		show: function(data) {
			$('#tagline .slider').empty().append(data);
		},

		populate: function(href) {
			if (href.match($.lostbox.settings.imageTypesRegexp))
				$.slide.populate(href);
		}
	};

	$.slide = {
		settings: {
			imageLinks 		: [],
			imageTitles 	: [],
			position 		: 0
		},

		init: function(settings, href) {
			if (settings)
				$.extend($.slide.settings, settings);

			$.slide.position(href);
		},

		position: function(href) {
			var position = $.inArray(href, $.slide.settings.imageLinks);
			if (position != -1)
				$.slide.settings.position = position;
		},

		next: function() {
			$.slide.jump($.slide.settings.position + 1);
		},

		prev: function() {
			$.slide.jump($.slide.settings.position - 1);
		},

		jump: function(position) {
			if (position >= $.slide.settings.imageLinks.length)
				position = 0;
			if (position < 0)
				position = $.slide.settings.imageLinks.length - 1;

			$.slide.settings.position = position;
			$.slide.populate($.slide.settings.imageLinks[position]);
		},

		gallery: function() {
			return ($.slide.settings.imageLinks.length > 1);
		},

		preload: function() {
			if ($.slide.gallery()) {
				var position = $.slide.settings.position;
				var imageLinks = $.slide.settings.imageLinks;

				var next = new Image();
				next.src = imageLinks[position + 1] ? imageLinks[position + 1] : imageLinks[0];
				var prev = new Image();
				prev.src = imageLinks[position - 1] ? imageLinks[position - 1] : imageLinks[imageLinks.length - 1];
			}
		},

		navigate: function() {
			$.slide.settings.started = true;
			$('#tagline .slider').append('<div class="navigation"><a class="prev" href="#"></a><a class="next" href="#"></a></div>');
			$('#tagline .next').bind('click', function() {
				$.slide.next();
				return false;
			});
			$('#tagline .prev').bind('click', function() {
				$.slide.prev();
				return false;
			});
		},

		title: function() {
			var imageTitle = $.slide.settings.imageTitles[$.slide.settings.position];

			if (imageTitle)
				$('#tagline .slider').append('<div class="image-title">'+ imageTitle +'</div>');
		},

		populate: function(href) {
			var image = new Image();
			$.lostbox.load();
			image.onload = function() {
				$.lostbox.settings.sliderWidth = image.width;
				$.lostbox.show('<img src="' + image.src + '" />');
				$.slide.title();
				if ($.slide.gallery()) {
					$.slide.navigate();
					$('#tagline .navigation').css({ 'width': image.width, 'height': image.height });
					$('#tagline .next').css({ 'width': image.width/2 - 10, 'height': image.height });
					$('#tagline .prev').css({ 'width': image.width/2 - 10, 'height': image.height });
				}
			}
			image.src = href;

			$.slide.preload();
		}
	};

	$.fn.lostbox = function(settings) {
		$.lostbox.init(settings);
		var imageList = createLinkMap(this);

		var href = $(this).attr('href');
			
		$.slide.init({ imageLinks: imageList[0], imageTitles: imageList[1] }, href);
		$.lostbox.populate(href);

	}
	
	function createLinkMap(elements) {

		var imageLinks = [];
		var imageTitles = [];
		$(elements).each(function() {
			if (this.href.match($.lostbox.settings.imageTypesRegexp) && $.inArray(this.href, imageLinks) == -1) {
				imageLinks.push(this.href);
				imageTitles.push(this.title);
			}
		});

		return [imageLinks, imageTitles];
	}

	function keyPressed(event) {
		switch(event.keyCode) {
			case 37:
			case 80:
				if ($.slide.settings.started)
					$.slide.prev();
				break;
			case 39:
			case 78:
				if ($.slide.settings.started)
					$.slide.next();
		}

		return false;
	}

	function createGallery(name) {
		var lostboxLinks = $('a[rel^="'+name+'"]');
		var distictGallery = [];
		$(lostboxLinks).each(function() {
			var value = $(this).attr('rel');
			if ($.inArray(value, distictGallery) == -1) {
				distictGallery.push(value);
				$('a[rel="'+value+'"]').lostbox();
			}
		});
	}

	$(function() {
		createGallery('slider');
	});

})(jQuery);
