/*
 *
 * Copyright (c) 2006-2010 Joan Piedra (http://joanpiedra.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */
(function($) {

/*
 * Converts image and link elements to thumbnails
 *
 * @name     $.fn.thumbs
 * @author   Joan Piedra (http://joanpiedra.com)
 * @example  $('.thumb').thumbs();
 *
 */
$.fn.thumbsHome = function(options) {
	var $thumbsHome = this;
	
	if (options == 'destroy') {
		return ThumbsHome.destroy($thumbsHome);
	}
	
	if( $thumbsHome.data('thumbsHome') ) {
		return $thumbsHome;
	}
	
	var center = {},
	defaults = {
		center: true,
		classNames: {
			center: 'thumbHome-center',
			container: 'thumbHome-container',
			icon: 'thumbHome-icon',
			img: 'thumbHome-img',
			inner: 'thumbHome-inner',
			strip: 'thumbHome-strip'
		},
		html: '<span class="%container%"><span class="%inner%"><span class="%img%"></span><span class="%strip%">%strip_content%</span><span class="%icon%"></span></span></span>',
		strip: true
	};
	
	options = $.extend(true, {}, defaults, options);
	
	return $thumbsHome.each(function(){
		var $thumbHome = $(this),
		c = options.classNames,
		clone = $thumbHome.clone(true),
		html = new String(options.html),
		centered = false,
		strip = '';
		
		for (className in c) {
			var newClassName = c[className];
			
			if ( options.center && !centered && className == 'container' ) {
				newClassName = c.container + ' ' + c.center;
				centered = true;
			}
			
			html = html.replace('%' + className + '%', newClassName);
		}
		
		if (options.strip) {
			strip = $thumbHome.is('img') ? $thumbHome.attr('alt') : $thumbHome.find('img').attr('alt');
			strip = strip != undefined ? strip : $thumbHome.attr('title');
			strip = strip != undefined ? strip : '';
		}
		
		html = html.replace('%strip_content%', strip);
		
		$thumbHome.wrap( html );
		
		if (options.center) {
			ThumbsHome.centerImg( $thumbHome );
		}
		
		var data = {
			'container': $thumbHome.parents('.' + c.container),
			'raw': clone
		};
		
		$thumbHome.data('thumbs', data);
	});
};


var ThumbsHome = {

	/*
	 * Private: Absolute positions the image in the center of the thumbnail frame
	 *
	 * @name     thumbs.centerImg
	 * @author   Joan Piedra (http://joanpiedra.com)
	 * @example  Thumbs.centerImg($thumb);
	 *
	 */
	centerImg: function($thumbHome) {
		var $img = $thumbHome.is('img') ? $thumbHome : $thumbHome.find('img'),
		css = {};
	
		$img.css( css );
	
		return $thumbHome;
	},

	/*
	 * Private: Removes all the added thumbnail html
	 *
	 * @name     thumbs.destroy
	 * @author   Joan Piedra (http://joanpiedra.com)
	 * @example  Thumbs.destroy($thumbs);
	 *
	 */
	destroy: function($thumbsHome) {
		$thumbsHome.each(function(index) {
			var $thumbHome = $(this),
			data = $thumbHome.data('thumbsHome');
			
			if (!data) {
				return;
			}
			
			data.container.after(data.raw).remove();
		});
	}

}

})(jQuery);
