/*
	Programmer: Lukasz Czerwinski
	CodeCanyon: http://codecanyon.net/user/Lukasz_Czerwinski
*/

(function($){
$.fn.preloader = function(settings){
	//Global variables 
	var el;
	//Default settings
	settings = jQuery.extend({
		Speed		: 360,				//Speed animations 
		Style		: "default",		//Style preloader
		Height		: 0,				//(false) Default height for IMG
		Width		: 0,				//(false) Default width for IMG
		Title		: 1					//(true) Short description
	}, settings);
	//Basic element
	el = $(this);
	//Hide IMG
	el.hide();
	//Wrap the IMG
	el.wrap("<div class='preloader'></div");
	//Add div with preloader
	el.parent(".preloader").append("<div class='"+settings.Style+"'></div>");
	//Load
	el.each(function(){
		//This IMG
		var image_ = $(this);
		//Get the SRC
		var srcImg = image_.attr("src");
		var img = new Image();
		//Set sizes
		el.parent(".preloader").css({
			height	: (settings.Height) ? settings.Height : image_.attr("height")+2,
			width	: (settings.Width) ? settings.Width  : image_.attr("width")+2
		});
		//Size IMG
		(settings.Height) ? image_.attr("height", settings.Height) : image_.attr("height", image_.attr("height"));
		(settings.Width) ? image_.attr("Width", settings.Width) : image_.attr("width", image_.attr("width"));
		//Add description  
		if(settings.Title) {
			image_.parent(".preloader").append("<div class='alt'>"+image_.attr("alt")+"</div>");
		}
		//Load the IMG
		img.onload = function () {
			//Remove div with preloader
			image_.parent(".preloader").children("."+settings.Style).fadeOut(settings.Speed, function(){
				$(this).remove();
				image_.fadeIn(settings.Speed);
				//IE
				img.onload = function() {};
			});
		};
		img.src = srcImg;
	}); 
	
	//Show and hide description 
	el.parent(".preloader").hover(
		function(){
			$(this).children(".alt").stop(true, true).delay(150).slideDown(200);
		},
		function(){
			$(this).children(".alt").stop(true, true).delay(150).slideUp(200);
		});
}
})(jQuery); //The end 
