$(document).ready(function(){
	prettyPhoto.init();
	prettyComments.init('textarea');
// 	$('#guia_baners').s3Slider({
// 		timeOut: 4000 
// 	});
});
  
 
  
  (function($){

    $.fn.s3Slider = function(vars) {       
        
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var mOver       = false;
        var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span");
            
        items.each(function(i) {
    
            $(items[i]).mouseover(function() {
               mOver = true;
            });
            
            $(items[i]).mouseout(function() {
                mOver   = false;
                fadeElement(true);
            });
            
        });
        
        var fadeElement = function(isMouseOut) {
            var thisTimeOut = (isMouseOut) ? (timeOut/2) : timeOut;
            thisTimeOut = (faderStat) ? 10 : thisTimeOut;
            if(items.length > 0) {
                timeOutFn = setTimeout(makeSlider, thisTimeOut);
            } else {
                console.log("Poof..");
            }
        }
        
        var makeSlider = function() {
            current = (current != null) ? current : items[(items.length-1)];
            var currNo      = jQuery.inArray(current, items) + 1
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
            var newMargin   = $(element).width() * currNo;
            if(faderStat == true) {
                if(!mOver) {
                    $(items[currNo]).fadeIn((timeOut/6), function() {
                        if($(itemsSpan[currNo]).css('bottom') == 0) {
                            $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        } else {
                            $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        }
                    });
                }
            } else {
                if(!mOver) {
                    if($(itemsSpan[currNo]).css('bottom') == 0) {
                        $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                            $(items[currNo]).fadeOut((timeOut/6), function() {
                                faderStat = true;
                                current = items[(currNo+1)];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    } else {
                        $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                        $(items[currNo]).fadeOut((timeOut/6), function() {
                                faderStat = true;
                                current = items[(currNo+1)];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    }
                }
            }
        }
        
        makeSlider();

    };  

})(jQuery);



/* ------------------------------------------------------------------------
	Class: prettyPhoto
	Use: Lightbox clone for jQuery
	Author: Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 2.1.1
------------------------------------------------------------------------- */

	prettyPhoto = {
		options : {
			'animationSpeed' : 'fast', /* fast/normal/slow */
			'padding' : 40, /* padding for each side of the picture */
			'showTitle' : false
		},
		init : function(){
			// Find all the images to overlay
			prettyPhoto.imagesArray = [];
			$("a[rel^='prettyOverlay'],a[rel^='prettyPhoto']").each(function(){
				prettyPhoto.imagesArray[prettyPhoto.imagesArray.length] = this;
				$(this).bind('click',function(){
					prettyPhoto.open(this); return false;
				});
			});
			
			$(window).scroll(function(){ prettyPhoto.centerPicture(); });
			$(window).resize(function(){ prettyPhoto.centerPicture(); prettyPhoto.resizeOverlay(); })
		},
		open : function(caller) {
			$('.publicitat').css({ display: "none" });
			$('.publicitat_h').css({ display: "none" });
			prettyPhoto.caller = caller;
			
			// Find out if the picture is part of a set
			theRel = $(caller).attr('rel');
			galleryRegExp = /\[(?:.*)\]/;
			theGallery = galleryRegExp.exec(theRel);
			
			// Calculate the number of items in the set, and the position of the clicked picture.
			prettyPhoto.setCount = 0; /* Total images in the set */
			prettyPhoto.setPosition = 0; /* Position in the set */
			prettyPhoto.arrayPosition = 0; /* Total position in the array */
			prettyPhoto.isSet = false;
			for (i = 0; i < prettyPhoto.imagesArray.length; i++){
				if($(prettyPhoto.imagesArray[i]).attr('rel').indexOf(theGallery) != -1){
					prettyPhoto.setCount++;
					if(prettyPhoto.setCount > 1) prettyPhoto.isSet = true;

					if($(prettyPhoto.imagesArray[i]).attr('href') == $(caller).attr('href')){
						prettyPhoto.setPosition = prettyPhoto.setCount;
						prettyPhoto.arrayPosition = i;
					};
				};
			};
			
			prettyPhoto.buildOverlay(prettyPhoto.isSet);

			// Display the current position
			$('div.popup span.currentText').html('<span>' + prettyPhoto.setPosition + '</span>' + '/' + prettyPhoto.setCount);

			// Position the picture in the center of the viewing area
			prettyPhoto.centerPicture();
			
			$('div.popup #fullResImageContainer').hide();
			$('.loaderIcon').show();

			// Preload the neighbour images
			prettyPhoto.preload();
		},
		next : function(){
			// Change the current position
			prettyPhoto.arrayPosition++;
			prettyPhoto.setPosition++;

			// Fade out the current picture
			$('div.popup #fullResImageContainer').fadeOut(prettyPhoto.options['animationSpeed'],function(){
				$('.loaderIcon').show();
				
				// Preload the neighbour images
				prettyPhoto.preload();
			});
			
			prettyPhoto.hideTitle();
			$('div.popup .hoverContainer').fadeOut(prettyPhoto.options['animationSpeed']);
			$('div.popup .details').fadeOut(prettyPhoto.options['animationSpeed'],function(){
				prettyPhoto.checkPosition();
			});
		},
		previous: function(){
			// Change the current position
			prettyPhoto.arrayPosition--;
			prettyPhoto.setPosition--;

			// Fade out the current picture
			$('div.popup #fullResImageContainer').fadeOut(prettyPhoto.options['animationSpeed'],function(){
				$('.loaderIcon').show();
				
				// Preload the image
				prettyPhoto.preload();
			});

			prettyPhoto.hideTitle();
			$('div.popup .hoverContainer').fadeOut(prettyPhoto.options['animationSpeed']);
			$('div.popup .details').fadeOut(prettyPhoto.options['animationSpeed'],function(){
				prettyPhoto.checkPosition();
			});
		},
		checkPosition : function(){
			// If at the end, hide the next link
			(prettyPhoto.setPosition == prettyPhoto.setCount) ? $('div.popup a.next').hide() : $('div.popup a.next').show();
			
			// If at the beginning, hide the previous link
			(prettyPhoto.setPosition == 1) ? $('div.popup a.previous').hide() : $('div.popup a.previous').show();
			
			// Change the current picture text
			$('div.popup span.currentText span').text(prettyPhoto.setPosition);
			
			if (prettyPhoto.isSet) {
				if($(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).attr('title')){
					$('div.popup .description').html(unescape($(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).attr('title')));
				}else{
					$('div.popup .description').text('');
				};
				
				if($(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).find('img').attr('alt') && prettyPhoto.options['showTitle']){
					prettyPhoto.hasTitle = true;
					$('div.prettyPhotoTitle .prettyPhotoTitleContent').html(unescape($(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).find('img').attr('alt')));
				}else{
					prettyPhoto.hasTitle = false;
				};
			}else{
				if($(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).attr('title')){
					$('div.popup .description').html(unescape($(prettyPhoto.caller).attr('title')));
				}else{
					$('div.popup .description').text('');
				};
				
				if($(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).find('img').attr('alt') && prettyPhoto.options['showTitle']){
					prettyPhoto.hasTitle = true;
					$('div.prettyPhotoTitle .prettyPhotoTitleContent').html(unescape($(prettyPhoto.caller).find('img').attr('alt')));
				}else{
					prettyPhoto.hasTitle = false;
				};
			};
		},
		centerPicture : function(){
			//Make sure the gallery is open
			if($('div.popup').size() > 0){
				
				var scrollPos = prettyPhoto.getScroll();
				
				$('div.popup').css({
					'top': ($(window).height()/2) + scrollPos['scrollTop'] - ($('div.popup').height()/2),
					'left': ($(window).width()/2) + scrollPos['scrollLeft'] - ($('div.popup').width()/2)
				});
				
				$('div.prettyPhotoTitle').css({
					'top' : $('div.popup').offset().top - 22,
					'left' : $('div.popup').offset().left + (prettyPhoto.options['padding']/2)
				});
			};
		},
		preload : function(){
			// Hide the next/previous links if on first or last images.
			prettyPhoto.checkPosition();
			
			// Set the new image
			imgPreloader = new Image();
			
			// Preload the neighbour images
			nextImage = new Image();
			if(prettyPhoto.isSet) nextImage.src = $(prettyPhoto.imagesArray[prettyPhoto.arrayPosition + 1]).attr('href');
			
			prevImage = new Image();
			if(prettyPhoto.isSet && prettyPhoto.imagesArray[prettyPhoto.arrayPosition - 1]) prevImage.src = $(prettyPhoto.imagesArray[prettyPhoto.arrayPosition - 1]).attr('href');

			$('div.popup .content_foto').css('overflow','hidden');
			
			(prettyPhoto.isSet) ? $('div.popup #fullResImage').attr('src',$(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).attr('href')) : $('div.popup #fullResImage').attr('src',$(prettyPhoto.caller).attr('href'));

			imgPreloader.onload = function(){
				var correctSizes = prettyPhoto.resize(imgPreloader.width,imgPreloader.height);
				imgPreloader.width = correctSizes['width'];
				imgPreloader.height = correctSizes['height'];
				
				// Need that small delay for the anim to be nice
				setTimeout('prettyPhoto.showimage(imgPreloader.width,imgPreloader.height,'+correctSizes["containerWidth"]+','+correctSizes["containerHeight"]+','+correctSizes["contentWidth"]+')',500);
			};
			
			(prettyPhoto.isSet) ? imgPreloader.src = $(prettyPhoto.imagesArray[prettyPhoto.arrayPosition]).attr('href') : imgPreloader.src = $(prettyPhoto.caller).attr('href');
		},
		showimage : function(width,height,containerWidth,containerHeight,contentWidth){
			$('.loaderIcon').hide();
			
			$('div.popup .content_foto').animate({'height':contentHeight},prettyPhoto.options['animationSpeed']);

			var scrollPos = prettyPhoto.getScroll();

			// Resize the holder
			$('div.popup').animate({
				'top': scrollPos['scrollTop'] + (($(window).height()/2) - (containerHeight/2)),
				'left': (($(window).width()/2) - (containerWidth/2)),
				'width':containerWidth
			},prettyPhoto.options['animationSpeed'],function(){
				$('#fullResImage').attr({
					'width':width,
					'height':height
				});

				// Show the nav elements
				prettyPhoto.shownav();

				$('div.popup .hoverContainer').height(height).width(width);

				// Fade the new image
				$('div.popup #fullResImageContainer').fadeIn(prettyPhoto.options['animationSpeed']);
			});	
		},
		shownav : function(){
			if(prettyPhoto.isSet) $('div.popup .hoverContainer').fadeIn(prettyPhoto.options['animationSpeed']);
			$('div.popup .details').fadeIn(prettyPhoto.options['animationSpeed']);

			prettyPhoto.showTitle();
		},
		showTitle : function(){
			if(prettyPhoto.options['showTitle'] && prettyPhoto.hasTitle){
				$('div.prettyPhotoTitle').css({
					'top' : $('div.popup').offset().top,
					'left' : $('div.popup').offset().left + (prettyPhoto.options['padding']/2),
					'display' : 'block'
				});
				
				$('div.prettyPhotoTitle div.prettyPhotoTitleContent').css('width','auto');
				
				if($('div.prettyPhotoTitle').width() > $('div.popup').width()){
					$('div.prettyPhotoTitle div.prettyPhotoTitleContent').css('width',$('div.popup').width() - (prettyPhoto.options['padding'] * 2));
				}else{
					$('div.prettyPhotoTitle div.prettyPhotoTitleContent').css('width','');
				};
				
				$('div.prettyPhotoTitle').animate({'top':($('div.popup').offset().top - 22)},prettyPhoto.options['animationSpeed']);
			};
		},
		hideTitle : function() {
			$('div.prettyPhotoTitle').animate({'top':($('div.popup').offset().top)},prettyPhoto.options['animationSpeed'],function() { $(this).css('display','none'); });
		},
		buildOverlay : function(){
			
			// Build the background overlay div
			backgroundDiv = "<div class='popupOverlay'></div>";
 			$('body').append(backgroundDiv);
			$('div.popupOverlay').css('height',$(document).height());
			$('.popupOverlay').bind('click',function(){
				prettyPhoto.close();
			});
			
			// Basic HTML for the picture holder
			pictureHolder = '<div class="popup popup_foto"><div class="top"><div class="left"></div><div class="middle"></div><div class="right"></div></div><div class="content_foto"><div class="loaderIcon"></div><div class="hoverContainer"><a class="next" href="#">next</a><a class="previous" href="#">previous</a></div><div id="fullResImageContainer"><img id="fullResImage" src="" /></div><div class="details clearfix"><a class="close" href="#">Close</a><p class="description"></p><p class="currentTextHolder"><span class="currentText"><span>0</span>/<span class="total">0</span></span></p></div></div><div class="bottom"><div class="left"></div><div class="middle"></div><div class="right"></div></div></div>';
			
			// Basic html for the title holder
			titleHolder = '<div class="prettyPhotoTitle"><div class="prettyPhotoTitleLeft"></div><div class="prettyPhotoTitleContent"></div><div class="prettyPhotoTitleRight"></div></div>';

			$('body').append(pictureHolder).append(titleHolder);

			$('.popup,.titleHolder').css({'opacity': 0});
			$('a.close').bind('click',function(){ prettyPhoto.close(); return false; });
			
			$('.popup .previous').bind('click',function(){
				prettyPhoto.previous();
				return false;
			});
			
			$('.popup .next').bind('click',function(){
				prettyPhoto.next();
				return false;
			});

			$('.hoverContainer').css({
				'margin-left':prettyPhoto.options['padding']/2,
				'margin-right':prettyPhoto.options['padding']/2
			});
			
			// If it's not a set, hide the links
			if(!prettyPhoto.isSet) {
				$('.hoverContainer').hide();
				$('.currentTextHolder').hide();
			};

			// Then fade it in
			$('div.popupOverlay').css('opacity',0);
			$('div.popupOverlay').fadeTo(prettyPhoto.options['animationSpeed'],0.35, function(){
				$('div.popup').fadeTo(prettyPhoto.options['animationSpeed'],1,function(){
					// To fix an IE bug
					$('div.popup').attr('style','left:'+$('div.popup').css('left')+';top:'+$('div.popup').css('top')+';');
				});
			});
		},
		resize : function(width,height){
			$('div.popup .details').width(width); /* To have the correct height */
			$('div.popup .details p.description').width(width - parseFloat($('div.popup a.close').css('width'))); /* So it doesn't overlap the button */
			
			// Get the container size, to resize the holder to the right dimensions
			contentHeight = parseFloat($('div.popup .details').height()) + parseFloat($('div.popup .details').css('margin-top')) + parseFloat($('div.popup .details').css('margin-bottom'));
			containerHeight = contentHeight + parseFloat($('div.popup .top').height()) + parseFloat($('div.popup .bottom').height());
			containerWidth = parseFloat($('div.popup .content_foto').css("padding-left")) + parseFloat($('div.popup .content_foto').css("padding-right")) + prettyPhoto.options['padding'];
			
			var newWidth = width;
			var newHeight = height;
			
			// If there's a title, take it into consideration unpon resizing
			if(prettyPhoto.options['showTitle']) containerHeight -= 22;
			
			if((containerWidth + width) > $(window).width() || (containerHeight + height) > $(window).height()) {
				// Get the original geometry and calculate scales
				var xscale=(width+containerWidth + 100)/$(window).width();
				var yscale=(height+containerHeight + 100)/$(window).height();
			
				// Recalculate new size with default ratio
				if (yscale>xscale){
					newWidth = Math.round(width * (1/yscale));
					newHeight = Math.round(height * (1/yscale));
				} else {
					newWidth = Math.round(width * (1/xscale));
					newHeight = Math.round(height * (1/xscale));
				};
			};

			// Get the container size, to resize the holder to the right dimensions
			containerHeight += newHeight;
			contentHeight += newHeight;
			containerWidth += newWidth;
		
			$('div.popup .details').width(newWidth); /* To have the correct height */
			$('div.popup .details p.description').width(newWidth - parseFloat($('div.popup a.close').css('width'))); /* So it doesn't overlap the button */

			return {
				width:newWidth,
				height:newHeight,
				containerHeight:containerHeight,
				containerWidth:containerWidth,
				contentHeight:contentHeight
			};
		},
		resizeOverlay : function() {
			$('div.popupOverlay').css({
				'height':$(document).height(),
				'width':$(window).width()
			});
		},
		getScroll : function(){
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			return {scrollTop:scrollTop,scrollLeft:scrollLeft};
		},
		close : function(){
			$('.publicitat').css({ display: "block" });
			$('.publicitat_h').css({ display: "block" });
			$('div.popup,div.prettyPhotoTitle').fadeTo(prettyPhoto.options['animationSpeed'],0, function(){
				$('div.popupOverlay').fadeTo(prettyPhoto.options['animationSpeed'],0, function(){
					$('div.popupOverlay').remove();
					$('div.popup').remove();
				});
			});
		}
	}




/* ------------------------------------------------------------------------
	Pretty Comments
	
	Developped By: Stephane Caron (http://www.no-margin-for-errors.com)
	Inspired By: The facebook textarea :)
	Version: 1.2
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */

var prettyComments = {
	version : '1.2',
	options : {
		'animate' : true,
		'animationSpeed' : 'fast', /* fast/slow/normal */
		'alreadyAnimated' : false,
		'init' : true /* DONT CHANGE */
	},
	init : function(which){
		// Create the div in which the content will be copied
		$('body').append('<div id="comment_hidden"></div>');
		
		$(which).each(function(){
			$(this).css({
				'overflow':'hidden',
				'display':'inline'
			})
			.bind('keyup',function(){ prettyComments.copyContent($(this)); });
			
			// Make sure all the content in the textarea is visible
			prettyComments.setCSS(this);
			prettyComments.copyContent($(this));
			
			$(this).height($("#comment_hidden").height());
		});
		
		prettyComments.options['init'] = false;
	},
	setCSS : function(which){
		// Init the div for the current textarea
		$("#comment_hidden").css({
			'display': 'none',
			'width': $(which).width(),
			'min-height': $(which).height(),
			'font-family': $(which).css('font-family'),
			'font-size': $(which).css('font-size'),
			'line-height': $(which).css('line-height')
		});
		
		if($.browser.msie && parseFloat($.browser.version) < 7){
			$("#comment_hidden").css('height',$(which).height());
		}
	},
	copyContent : function(which){
		// Convert the line feeds into BRs
		theValue = $(which).attr('value') || "";
		theValue = theValue.replace(/\n/g,'<br />')
		
		$("#comment_hidden").html(theValue);
		
		if(!prettyComments.options['init']){
			if($("#comment_hidden").height() > $(which).height()){
				this.expand(which);
			}else if($("#comment_hidden").height() < $(which).height()){
				this.shrink(which);
			};
		};
	},
	expand : function(which){			
		if(this.options['animate'] && !this.options['alreadyAnimated']){
			this.options['alreadyAnimated'] = true;
			$(which).animate({'height':$("#comment_hidden").height()},this.options['animationSpeed'],function(){
				prettyComments.options['alreadyAnimated'] = false;
			});
		}else if(!this.options['animate'] && !this.options['alreadyAnimated']){
			$(which).height($("#comment_hidden").height());
		};
	},
	shrink : function(which){
		this.options['alreadyAnimated'] = true;
		$(which).animate({'height':$("#comment_hidden").height()},this.options['animationSpeed'],function(){
			prettyComments.options['alreadyAnimated'] = false;
		});
	}
};



enviarEmail = {
	init : function( id_article, lloc ){
		$('.publicitat').css({ display: "none" });
		
		enviarEmail.buildOverlay( id_article, lloc );

		// Display the current position
		$('div.popup span.currentText').html('<span>' + enviarEmail.setPosition + '</span>' + '/' + enviarEmail.setCount);

		enviarEmail.center();

		$('div.popup #fullResContainer').hide();
		$('.loaderIcon').show();
		
		// Need that small delay for the anim to be nice
		setTimeout('enviarEmail.show()',500);
		
		
		$('div.popupOverlay').css({
			'height':$(document).height(),
			'width':$(window).width()
		});
		
		$(window).scroll(function(){ enviarEmail.center(); });
		$(window).resize(function(){ enviarEmail.center(); })
	},
	center : function(){
		//Make sure the gallery is open
		if($('div.popup').size() > 0){
			
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			
			$('div.popup').css({
				'top': ($(window).height()/2) + scrollTop - ($('div.popup').height()/2),
				'left': ($(window).width()/2) + scrollLeft - ($('div.popup').width()/2)
			});
		};
	},
	show : function(){
		$('.loaderIcon').hide();
		$('div.popup #fullResContainer').fadeIn('fast');
	},
	buildOverlay : function( id_article, lloc ){
		// Build the background overlay div
		backgroundDiv = "<div class='popupOverlay'></div>";
		$('body').append(backgroundDiv);
		$('div.popupOverlay').css('height',$(document).height());
		$('.popupOverlay').bind('click',function(){
			enviarEmail.close();
		});
		
		// Basic HTML
		html = '<div class="popup popup_email"><div class="top"><div class="left"></div><div class="middle"></div><div class="right"></div></div><div class="content_email"><div class="loaderIcon"></div><div id="fullResContainer">';
		
		html += '		<h4 class="verd">Enviar article</h4>\n';
		html += '		<input type="hidden" id="id_article" value="'+ id_article +'" />\n';
		html += '		<label for="nom_e">Nom del destinatari</label>\n';
		html += '		<input id="nom_e" type="text" value="" name="nom" />\n';
		html += '		<br class="p" />\n';
		html += '		<label for="email_e">Correu electrònic del destinatari</label>\n';
		html += '		<input id="email_e" type="text" value="" name="email" />\n';
		html += '		<br class="p" />\n';
		html += '		<label for="r_nom">El teu nom</label>\n';
		html += '		<input id="r_nom" type="text" value="" name="r_nom" />\n';
		html += '		<br class="p" />\n';
		html += '		<label for="r_email">El teu correu</label>\n';
		html += '		<input id="r_email" type="text" value="" name="r_email" />\n';
		html += '		<br class="p" />\n';
		html += '		<label for="comentari">Comentari</label>\n';
		html += '		<textarea id="comentari" name="comentari"></textarea><br /><br />\n';
		html += '		<input type="hidden" id="url_enviar" value="'+ lloc  +'" />\n';
		html += '		<div style="float: left;"><input class="submit" type="button" value="Enviar" name="Enviar" onclick="enviarEmail.enviar()" /></div>\n';
		html += '<div style="float: right; padding-top: 5px"><a onclick="enviarEmail.close()"><img src="/img/icones/tancar.gif" alt="tancar" /></a></div><br class="p" /></div></div><div class="bottom"><div class="left"></div><div class="middle"></div><div class="right"></div></div></div>';
		
		$('body').append(html);

		// Then fade it in
		$('div.popupOverlay').css('opacity',0);
		$('div.popupOverlay').fadeTo('fast',0.35, function(){
			$('div.popup').fadeTo('fast',1,function(){
				// To fix an IE bug
				$('div.popup').attr('style','left:'+$('div.popup').css('left')+';top:'+$('div.popup').css('top')+';');
			});
		});
	},
	resize : function(width,height){
		// Get the container size, to resize the holder to the right dimensions
		containerHeight = contentHeight + parseFloat($('div.popup .top').height()) + parseFloat($('div.popup .bottom').height());
		containerWidth = parseFloat($('div.popup .content_email').css("padding-left")) + parseFloat($('div.popup .content_email').css("padding-right"));
		
		var newWidth = width;
		var newHeight = height;
		
		if((containerWidth + width) > $(window).width() || (containerHeight + height) > $(window).height()) {
			// Get the original geometry and calculate scales
			var xscale=(width+containerWidth + 100)/$(window).width();
			var yscale=(height+containerHeight + 100)/$(window).height();
		
			// Recalculate new size with default ratio
			if (yscale>xscale){
				newWidth = Math.round(width * (1/yscale));
				newHeight = Math.round(height * (1/yscale));
			} else {
				newWidth = Math.round(width * (1/xscale));
				newHeight = Math.round(height * (1/xscale));
			};
		};

		// Get the container size, to resize the holder to the right dimensions
		containerHeight += newHeight;
		contentHeight += newHeight;
		containerWidth += newWidth;
	
		return {
			width:newWidth,
			height:newHeight
		};
	},
	close : function(){
		$('div.popup').fadeTo('fast',0, function(){
			$('div.popupOverlay').fadeTo('fast',0, function(){
				$('div.popupOverlay').remove();
				$('div.popup').remove();
			});
		});
	},
	enviar : function(){
		var email = $("#email_e").val();
		var nom = $("#nom_e").val();
		var r_email = $("#r_email").val();
		var r_nom = $("#r_nom").val();
		var comentari = $("#comentari").val();
		var id_article = $("#id_article").val();
		var url_enviar = $("#url_enviar").val();
		
		
		if( email != '' )
		{
			html = '<div class="enviarAmic"><div class="top"><div class="left"></div><div class="middle"></div><div class="right"></div></div><div class="content_email"><div class="loaderIcon"></div><div id="fullResContainer">';
			html += '<img src="/img/icones/loader.gif" alt="loader" style="margin: 150px 0px 100px 125px" />';
			html += '<br class="p" /></div></div><div class="bottom"><div class="left"></div><div class="middle"></div><div class="right"></div></div></div>';
			$(".popup").html( html );
			
			$.post( AJAX, { email: email, nom: nom, r_email: r_email, r_nom: r_nom, id_article: id_article, url_enviar: url_enviar, comentari: comentari, accio: 'enviar_art' },
			function( resposta )
			{
				// Basic HTML
				html = '<div class="enviarAmic"><div class="top"><div class="left"></div><div class="middle"></div><div class="right"></div></div><div class="content_email"><div class="loaderIcon"></div><div id="fullResContainer">';
				html += '<h4 class="verd">S\'ha enviat correctament el missatge</h4>\n';
				html += '<br class="p" /></div></div><div class="bottom"><div class="left"></div><div class="middle"></div><div class="right"></div></div></div>';
				
				$('.popup').html(html);
				$('.popup .content_email').css({height: "25px"})
				
				setTimeout('enviarEmail.close()',3000)
			});
		}
		else
		{
			alert("Has de posar un correu de destí com a mínim");
		}
	}
}

previsualitzar = {
	init : function( id_article, lloc ){
		previsualitzar.buildOverlay( id_article, lloc );

		// Display the current position
		$('div.popup span.currentText').html('<span>' + previsualitzar.setPosition + '</span>' + '/' + previsualitzar.setCount);

		previsualitzar.center();

		$('div.popup #fullResContainer').hide();
		$('.loaderIcon').show();
		
		// Need that small delay for the anim to be nice
		setTimeout('previsualitzar.show()',500);
		
		
		$('div.popupOverlay').css({
			'height':$(document).height(),
			'width':$(window).width()
		});
		
		$(window).scroll(function(){ previsualitzar.center(); });
		$(window).resize(function(){ previsualitzar.center(); })
	},
	center : function(){
		//Make sure the gallery is open
		if($('div.popup').size() > 0){
			
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			
			$('div.popup').css({
				'top': ($(window).height()/2) + scrollTop - ($('div.popup').height()/2),
				'left': ($(window).width()/2) + scrollLeft - ($('div.popup').width()/2)
			});
		};
	},
	show : function(){
		$('.loaderIcon').hide();
		$('div.popup #fullResContainer').fadeIn('fast');
	},
	buildOverlay : function( id_article, lloc ){
		// Build the background overlay div
		backgroundDiv = "<div class='popupOverlay'></div>";
		$('body').append(backgroundDiv);
		$('div.popupOverlay').css('height',$(document).height());
		$('.popupOverlay').bind('click',function(){
			previsualitzar.close();
		});
		
		var nom = $("#nom").val();
		var comentari = $("#comentari").val();
		
		// Basic HTML
		html = '<div class="popup popup_previsualitzar"><div class="top"><div class="left"></div><div class="middle"></div><div class="right"></div></div><div class="content_previsualitzar"><div class="loaderIcon"></div><div id="fullResContainer">';
		html += '		<h4 class="verd">Previsualització</h4>\n';
		
		if( comentari )
		{
			var regX = /\n/gi ;
			comentari2 = new String(comentari);
			comentari2 = comentari2.replace(regX, "<br /> \n");

			html += '		<strong class="verd">Nom:</strong> '+ nom +'<br /><br />\n';
			html += '		<strong class="verd">Comentari:</strong><br />'+ comentari2 +'<br /><br />\n';
		}
		else
		{
			html += '		<br />No hi ha cap comentari per previsualitzar<br /><br /><br />\n';
		}
		
		html += '<div style="float: right; padding-top: 5px"><a onclick="previsualitzar.close()"><img src="/img/icones/tancar.gif" alt="tancar" /></a></div><br class="p" /></div></div><div class="bottom"><div class="left"></div><div class="middle"></div><div class="right"></div></div></div>';
		$('body').append(html);
		
		

		// Then fade it in
		$('div.popupOverlay').css('opacity',0);
		$('div.popupOverlay').fadeTo('fast',0.35, function(){
			$('div.popup').fadeTo('fast',1,function(){
				// To fix an IE bug
				$('div.popup').attr('style','left:'+$('div.popup').css('left')+';top:'+$('div.popup').css('top')+';');
			});
		});
	},
	resize : function(width,height){
		// Get the container size, to resize the holder to the right dimensions
		containerHeight = contentHeight + parseFloat($('div.popup .top').height()) + parseFloat($('div.popup .bottom').height());
		containerWidth = parseFloat($('div.popup .content_previsualitzar').css("padding-left")) + parseFloat($('div.popup .content_previsualitzar').css("padding-right"));
		
		var newWidth = width;
		var newHeight = height;
		
		if((containerWidth + width) > $(window).width() || (containerHeight + height) > $(window).height()) {
			// Get the original geometry and calculate scales
			var xscale=(width+containerWidth + 100)/$(window).width();
			var yscale=(height+containerHeight + 100)/$(window).height();
		
			// Recalculate new size with default ratio
			if (yscale>xscale){
				newWidth = Math.round(width * (1/yscale));
				newHeight = Math.round(height * (1/yscale));
			} else {
				newWidth = Math.round(width * (1/xscale));
				newHeight = Math.round(height * (1/xscale));
			};
		};

		// Get the container size, to resize the holder to the right dimensions
		containerHeight += newHeight;
		contentHeight += newHeight;
		containerWidth += newWidth;
	
		return {
			width:newWidth,
			height:newHeight
		};
	},
	close : function(){
		$('div.popup').fadeTo('fast',0, function(){
			$('div.popupOverlay').fadeTo('fast',0, function(){
				$('div.popupOverlay').remove();
				$('div.popup').remove();
			});
		});
	}
}

