/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "enLage Images."
 * 
 * Version: 1.2 Beta, 23.02.2011
 * Joaquin Rivero
 *
 * --------------------------------------------------------------------*/

(function ($) {
    $.fn.extend({
        enLarge: function (options) {
            var defaults = {
				treatmentPNG: false, //PNG 
				largeClass: 'large',
				oldfolder : 'contentPhotos', // part of route od image
				newfolder : 'applicationImages/ResizePhotos', // App folder [ Large photos folder ]
				pngimgsrc: 'img_treat.png', // src folder of treatment png
				largeW: 400, // large image Width
				showT: 500, // timer to show
				hideT: 500, // timer to hide
				hideimgClass:'hideimg', //
				holderClass:'holder', // Classname for all new divs holder
				originalimg:'img_popup', //clasname for div of thumbnail
				treatmentClass:'pngtreatment', // classname for pngHolder
				thepngClass:'thepng' // Png Classname
            };
            var options = $.extend(defaults, options);
			var rel = 0;
            return this.each(function () {
			if (options.treatmentPNG == true) {
			rel++;
			var thesrc = $(this).find('img').not('.'+options.thepngClass).attr('src');  
			var largesrc = thesrc.replace(options.oldfolder, options.newfolder);	
			$('<div class="'+ options.holderClass+'"><div class="'+options.originalimg+'" /><div class="'+options.treatmentClass+'"/><div class="'+options.hideimgClass+'" rel="'+ rel+'" width="'+options.largeW+'"/>').appendTo(this);
			$(this).find('img').not('.'+options.thepngClass).appendTo($(this).find('.'+options.originalimg));
			$(this).find('.'+options.hideimgClass).append('<img class="'+options.largeClass+'" width="'+ options.largeW +'" src="'+largesrc+'" />');
			$(this).find('.'+options.hideimgClass).find('.'+options.largeClass)
			.load(function(){}).error(function(){
				$(this).parent().remove();
			}); // if not has a image, remove the larde div, and the script doesn't work							
			$(this).find('.'+options.treatmentClass).append('<img class="'+ options.thepngClass +'" src="'+options.pngimgsrc+'" />');		
			jQuery(this).hover(						  
							function(){
							  $(this).find('.'+options.holderClass).css({"z-index":"500"});
							  $(this).find('.'+options.hideimgClass).stop(true,true).css({"z-index":"550"}).animate({opacity:'toggle'},options.showT);
								},   function() {
							
							  $(this).find('.'+options.hideimgClass).stop(true,true).animate({opacity:'toggle'},options.showT,function(){
								$(this).css({"z-index":"0"}); //return z-index to zero	
								$(this).parent().css({"z-index":"0"});
							  }); // hide
						 });// end hover
			}// end IF
			if (options.treatmentPNG == false) {
			rel++;
			var thesrc = $(this).find('img').not('.'+options.thepngClass).attr('src');  
			var largesrc = thesrc.replace(options.oldfolder, options.newfolder);	
			$('<div class="'+ options.holderClass+'"><div class="'+options.originalimg+'" /><div class="'+options.hideimgClass+'" rel="'+ rel+'"/>').appendTo(this);
			$(this).find('img').not('.'+options.thepngClass).appendTo($(this).find('.'+options.originalimg));
			$(this).find('.'+options.hideimgClass).append('<img class="'+options.largeClass+'" width="'+ options.largeW +'" src="'+largesrc+'" />');
			$(this).find('.'+options.hideimgClass).find('.'+options.largeClass)
			.load(function(){}).error(function(){
				$(this).parent().remove();
			}); // if not has a image, remove the larde div, and the script doesn't work								
			jQuery(this).hover(						  
							function(){
							  $(this).find('.'+options.hideimgClass).stop(true,true).css({"z-index":"500"}).animate({opacity:'toggle'},options.showT);
								},   function() {
							  $(this).find('.'+options.hideimgClass).stop(true,true).animate({opacity:'toggle'},options.showT,function(){
								$(this).css({"z-index":"0"}); //return z-index to zero																			 
							  }); // hide
						 });// end hover
			}// end IF
		}); // eachfunction
        } // enlarge function
    }); // fn extend
})(jQuery);
