/**
 * FotoOverlay
 * Description: FotoOverlay is a jQuery plugin that collects information - such as title, description, link, and author - about your photos from the photo (img) attributes and display it on top of the photos.
 * Link: http://phalkunz.com/2009/01/18/fotooverlayfotooverlay/
 * Author: Phalkunz (http://phalkunz.com)
 * Version: 0.1
 */

(function($){

linkLabel = '&rarr;';
show_link = true;
author_label = 'Photographer:';
show_hint = true;

$(window).load(function(){
	$('img.fotooverlay').fotooverlay();
});

$.fn.fotooverlay = function() {		
		
		return this.each(function() { 
			var foto_show_hint = show_hint;			
		    var foto = $(this); 
			foto.css({
				'position': 'absolute',
				'zIndex': '1'
			});
			if(foto.attr('showhint')) {
				if (foto.attr('showhint').toLowerCase()  == 'false' || foto.attr('showhint').toLowerCase()  == '0') foto_show_hint = false;
			}
			
			// if the photo has a link
			var link = '';
			if ( foto.parent().is('a') ) {
				link = foto.parent().attr('href');
				foto.parent().removeAttr('href');
			} 
			else {
				link = foto.attr('src');
			}

			// create & setup photo frame
			var frame = $('<div class="foto-frame">');
			frame.css({
				'width': foto.width(),
				'height': foto.height()
			});
			
			// create & setup overlay
			var overlay = $('<div class="foto-overlay">');
			overlay.css({
				'position': 'absolute',
				'width': foto.width(),
				'height': foto.height(),
				'zIndex': '100'
			});
			
			// create & setup text container
			var textContainer = $('<div class="text-container">');
			textContainer.css({
				'position': 'absolute',
				'maxWidth': foto.width() - 30,
				'maxHeight': foto.height() - 40,
				'padding': '10',
				'zIndex': '300',
				'overflow': 'hidden'
			});
			
			// overlay hint
			var hint = $('<div class="fotooverlay-hint">');
			hint.css({
				'position': 'absolute',
				'zIndex': '0'
			});
			if (!foto_show_hint) hint.width(0);

			if(foto.attr('title')) textContainer.html('<span class="fotooverlay-title">' + foto.attr('title') + '</span>');
			if(show_link) textContainer.html( textContainer.html() + ' <a href="' + link + '">'+ linkLabel +'</a>' );
			if(foto.attr('description')) textContainer.html(textContainer.html() + '<p>' + foto.attr('description') + '</p>');
			
			if(foto.attr('author')) textContainer.html( textContainer.html() + '<p class="author"><span>' + author_label + '</span> ' + foto.attr('author') + '</p>');
			
			foto.before(frame);
			frame.append(foto);
			
			frame.append(overlay);
			frame.append(hint);
			frame.append(textContainer);
			
			// Setup event event handler
			frame.mouseover(function() {
				overlay.addClass('foto-overlay-hover');
				textContainer.addClass('text-container-hover');
				//hint.hide();
			});
			frame.mouseout(function() {
				overlay.removeClass('foto-overlay-hover');
				textContainer.removeClass('text-container-hover');
				//hint.show();
			});
		});
}	
})(jQuery);