/*
* Copyright (C) 2010 Anderson Costa
* Licenced under the MIT license
* http://www.mangava.com.br
*/
(function($) {
	$.fn.caption = function(settings) {
		settings = $.extend({
			triggerCaption:'.capt',
			wrapperClass: 'caption',
			captionElement: 'div',
			imageAttr: 'title',
			requireText: false,
			autoWidth: true,
			animate: true,
			position:'bottom',
			tolBottom:5,
			tolLeft:0,
			tolRight:25,
			tolTop:0,
			duration: 200
			
		}, settings);

		return $(this).find('div.'+settings.triggerCaption).each(function(){
			//Only add the caption after the image has been loaded.  This makes sure we can know the width of the caption.
			
			//$(this).bind('load', function(){

				//Make sure the captioning isn't applied twice when the IE fix at the bottom is applied
				//if($(this).data('loaded')) return false;
				//$(this).data('loaded', true);
				
				//Shorthand for the image we will be applying the caption to
				var image = $(this);
				
				//Only create captions if there is content for the caption
				if(image.attr(settings.imageAttr).length > 0 || !settings.requireText){
					
					image.parent()
					.css({
						overflow:'hidden',
						position:'relative'
					})
					.width(image.find('img:eq(0)').width())
					.height(image.find('img:eq(0)').height());
					
					image.next().addClass(settings.wrapperClass);
					
					
					//Save Image Float
					var imageFloat = image.css('float');
					
					//Save Image Style
					var imageStyle = image.attr('style');
					
					
					if(settings.animate && !image.next().is(':empty')){
						image.next().hide();
						 if(settings.position == 'bottom'){
							 image.next().css({
								 left:settings.tolLeft,
								 bottom:'0'
								 });
							 image.parent().hover(
								function(){
									image.next().animate({
											opacity:'show',
											height:'show'
										});
								},
								function(){
									image.next().animate({
										opacity:'hide',
										height:'hide'
								});
								});
						 }else if(settings.position == 'top'){
							  image.next().css({
								 left:'0',
								 top:'0'
								 });
							 image.parent().hover(
								function(){
									image.next().animate({
											opacity:'show',
											height:'show'
										});
								},
								function(){
									image.next().animate({
										opacity:'hide',
										height:'hide'
								});
								});
						 }else if(settings.position == 'left'){
							image.next().css({
								 height:image.height()+'px',
								 left:'0',
								 top:'0',
								 width:Math.floor(image.width()/2)+'px'
								 });
							 image.parent().hover(
								function(){
									image.next().animate({
											opacity:'show',
											width:'show'
										});
								},
								function(){
									image.next().animate({
										opacity:'hide',
										width:'hide'
								});
								});
						}else if(settings.position == 'right'){
						 	image.next().css({
								 height:image.height()+'px',
								 right:'0',
								 top:'0',
								 width:Math.floor(image.width()/2)+'px'
								 });
							image.parent().hover(
							function(){
								image.next().animate({
										opacity:'show',
										width:'show'
									});
							},
							function(){
								image.next().animate({
									opacity:'hide',
									width:'hide'
									});
							});
						}
					}
					
					//Copy Image Style to Div
					//if(settings.copyStyle) div.attr('style',imageStyle);
					
					//Transfers the float style from the image to the caption container
					//if(settings.copyFloatToClass) div.addClass(imageFloat);
					
					//Properly size the caption div based on the loaded image's size
					//if(settings.autoWidth) div.width(image.width());
				}
			});
			
			// Thanks to Captify for this bit!
			//if the image has already loaded (due to being cached), force the load function to be called
			if (this.complete || this.naturalWidth > 0){
				$(this).trigger('load');
			}
		//});
	}
})(jQuery);

