/*
* Copyright (C) 2009 Joel Sutherland
* Licenced under the MIT license
* http://www.newmediacampaigns.com/page/jcaption-a-jquery-plugin-for-simple-image-captions
*/
(function($) {
	
	// La part del banner:
	
	var aquestcajabanner = 0;
	//var nMax = new Array();
	//var nBannersMax = 1; // meua
	//var fotoOn = new Array(); // = 1;
	//var fotoSig = new Array(); // = 2;
	var bannerOn = 0; // meua
	var bannerSig = 0; // meua
	var ancho = 172;	
	var posIni = 0;
	var transicion = 3000; // originalment a 6000
	var tSlide = 1000;
	var comptador = 0; // meua
	var nBanners = 0;
	
	var mida = 0;
	var actiu = 0;
	
	
	$.fn.jcaption = function(settings) {
		settings = $.extend({
			wrapperElement: 'div',
			wrapperClass: 'caption',
			captionElement: 'p',
			imageAttr: 'alt',
			requireText: true,
			copyStyle: false,
			removeStyle: true,
			removeAlign: true,
			copyAlignmentToClass: false,
			copyFloatToClass: true,
			autoWidth: true,
			autoHeight: true,
			animate: false,
			show: {opacity: 'show'},
			showDuration: 200,
			hide: {opacity: 'hide'},
			hideDuration: 200	
		}, settings);			
		
		
		return $(this).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				
				var captionLabelSrc = $('#' + image.attr('rel'));
				var captionLabelHTML = !captionLabelSrc.length ? image.attr(settings.imageAttr) : captionLabelSrc.html();
				
				//if(image.attr(settings.imageAttr).length > 0 || !settings.requireText){
				if(captionLabelHTML.length > 0 || !settings.requireText){
					
					//Wrap the image with the caption div
					image.wrap("<" + settings.wrapperElement + " class='" + settings.wrapperClass + "'></" + settings.wrapperElement + ">");
					
					//Save Image Float
					var imageFloat = image.css('float');
					
					//Save Image Style
					var imageStyle = image.attr('style');
					if(settings.removeStyle) image.removeAttr('style');
					
					//Save Image Align
					var imageAlign = image.attr('align');
					if(settings.removeAlign) image.removeAttr('align');
					
					//Put Caption in the Wrapper Div
					var div = $(this).parent().append('<' + settings.captionElement + '>' + captionLabelHTML + '</' + settings.captionElement + '>');
					
					if(settings.animate){
						$(this).next().hide();
						$(this).parent().hover(
						function(){
							$(this).find('p').animate(settings.show, settings.showDuration);
						},
						function(){
							$(this).find('p').animate(settings.hide, settings.hideDuration);
						});
					}
					
					//Copy Image Style to Div
					if(settings.copyStyle) div.attr('style',imageStyle);
					
					//If there is an alignment on the image (for example align="left") add "left" as a class on the caption.  This helps deal with older Text Editors like TinyMCE
					if(settings.copyAlignmentToClass) div.addClass(imageAlign);
					
					//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());
					if(settings.autoWidth) {
						div.width(image.outerWidth());
					}
					
					if(settings.autoHeight) {
						div.height(image.outerHeight(true));
					}
					
				}
			//});
			
			// 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');
			}						
			
			aquestcajabanner++;
						
			$(this).attr("id", "cajaBanners_" + aquestcajabanner);
			
			nBanners = 0;
			$(this).children('img').each( function(index){
				$(this).wrap('<div style="position: absolute; display: block; left: ' + (parseInt(posIni) + parseInt(ancho)) + 'px" class="bannerslide" id="caixaimatge_' + aquestcajabanner + '_' + (index+1) + '" />');
				
				if(index==0){
					$("#caixaimatge_" + aquestcajabanner + '_' + (index+1)).css( {
						left : "0px"
					});
				}
				nBanners = index;
			});		
			
			if(nBanners>0){
				$(this).hover(
			            function () {
	
			            	bannerOn = 0;
			            	bannerSig = 0;
			            	bannerAnt = 0;
			            	mida = $(this).children().length;
			            	actiu = $(this).attr('id').replace(/cajaBanners_/,'');
			            	$(this).children('div.bannerslide').each(function(index){
			            		if(($(this).attr('style').indexOf('left: 0px')>-1)||($(this).attr('style').indexOf('LEFT: 0px')>-1)){
			            			//toLowerCase()
			            			lacadena = 'caixaimatge_' + actiu + '_';
			            			bannerOn = $(this).attr('id').replace(lacadena,'');
			            			//alert('banneron trobat!: ' + bannerOn);
			            		}
			            	});
	
			            	if ((parseInt(bannerOn)+1) > mida) {
			        			bannerSig = 1;
			        		}
			            	else {
			            		bannerSig = parseInt(bannerOn) + 1;
			            	}
	            			slide();
			            },
			            function () {	  
			            	
			            	$(".bannerslide").each(function(){
			            		//$(this).stop(true, false);     
			            		$(this).stop(true, true);     
			            	});
			            	
			            	clearTimeout(comptador);
			            	
			            	posicio = $("#caixaimatge_" + actiu + "_" + bannerOn).position();
	
			            	//if(Math.abs(posicio.left)<(ancho/1.5)){
			            		//alert('torne a deixar-ho com estava');
			            	
			            		/* Descomentar açò (i el true, false) per a que faça l'efecte
			            		 * de tornar a l'anterior fotograma inconclús
			            		$("#caixaimatge_" + actiu + "_" + bannerOn).css( {
			            			left	: 0
			            		});
			            		$("#caixaimatge_" + actiu + "_" + bannerSig).css( {
			            			left	: ancho
			            		});
			            		*/
			            	
			            	//}	
			            	/*
			            	else {
			            		//alert('deixe la imatge següent');
			            		$("#caixaimatge_" + actiu + "_" + bannerOn).css( {
			            			left	: "-" + ancho
			            		});
			            		$("#caixaimatge_" + actiu + "_" + bannerSig).css( {
			            			left	: 0
			            		});		            		
			            	}
			            	*/
			            }						
			     );
			}
		});
	};
	
	function slide(){	
		   	    	
		posLeft = posIni - ancho;
		$("#caixaimatge_" + actiu + "_" + bannerOn).stop().animate( {
			left : posLeft
		}, tSlide);
		$("#caixaimatge_" + actiu + "_" + bannerSig).css( {
			left	: ancho
		});
		$("#caixaimatge_" + actiu + "_" + bannerSig).stop().animate( {
			left : 0
		}, tSlide, function(){
			$("#caixaimatge_" +actiu + "_" + bannerOn).css( {
				left : "-" + ancho
			});
			bannerAnt = bannerOn;
			bannerOn = bannerSig;				
			bannerSig++;
			if (bannerSig > mida) {
				bannerSig = 1;
			}
		});				
		//
		comptador = setTimeout(slide, transicion); 
    	
	};
	
	
})(jQuery);

