$(document).ready(function(){
	$('textarea').prettyComments();
	$("a[rel^='prettyPhoto']").prettyPhoto();
		
   $("ul#titulars_breus").liScroll();
});
  
 
 
 /* ------------------------------------------------------------------------
	Class: prettyPhoto
	Use: Lightbox clone for jQuery
	Author: Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 2.2.3
------------------------------------------------------------------------- */

	$.fn.prettyPhoto = function(settings) {
		// global Variables
		var caller = 0;
		var doresize = true;
		var imagesArray = [];
	
		$(window).scroll(function(){ _centerPicture(); });
		$(window).resize(function(){ _centerPicture(); _resizeOverlay(); });
		$(document).keyup(function(e){
			switch(e.keyCode){
				case 37:
					if (setPosition == 1) return;
					changePicture('previous');
					break;
				case 39:
					if (setPosition == setCount) return;
					changePicture('next');
					break;
				case 27:
					close();
					break;
			};
	    });
 
	
		settings = jQuery.extend({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 40, /* padding for each side of the picture */
			opacity: 0.35, /* Value between 0 and 1 */
			showTitle: true, /* true/false */
			allowresize: true, /* true/false */
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
		}, settings);
	
		$(this).each(function(){
			var hasTitle = false;
			var isSet = false;
			var setCount = 0; /* Total images in the set */
			var setPosition = 0; /* Position in the set */
			var arrayPosition = 0; /* Total position in the array */
			
			imagesArray[imagesArray.length] = this;
			$(this).bind('click',function(){
				open(this); return false;
			});
		});
	
		function open(el) {
			caller = $(el);
		
			// 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.
			isSet = false;
			setCount = 0;
			for (i = 0; i < imagesArray.length; i++){
				if($(imagesArray[i]).attr('rel').indexOf(theGallery) != -1){
					setCount++;
					if(setCount > 1) isSet = true;

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

			// Display the current position
			$('div.popup p.currentTextHolder').text(setPosition + settings.counter_separator_label + setCount);

			// Position the picture in the center of the viewing area
			_centerPicture();
		
			$('div.popup #fullResImageContainer').hide();
			$('.loaderIcon').show();
			
			_preload();
		};
	
		showimage = function(width,height,containerWidth,containerHeight,contentHeight,contentWidth,resized){
			$('.loaderIcon').hide();
			var scrollPos = _getScroll();

			if($.browser.opera) {
				windowHeight = window.innerHeight;
				windowWidth = window.innerWidth;
			}else{
				windowHeight = $(window).height();
				windowWidth = $(window).width();
			};

			$('div.popup .content').animate({'height':contentHeight,'width':containerWidth},settings.animationSpeed);

			projectedTop = scrollPos['scrollTop'] + ((windowHeight/2) - (containerHeight/2));
			if(projectedTop < 0) projectedTop = 0 + $('div.prettyPhotoTitle').height();

			// Resize the holder
			$('div.popup').animate({
				'top': projectedTop,
				'left': ((windowWidth/2) - (containerWidth/2)),
				'width': containerWidth
			},settings.animationSpeed,function(){
				$('#fullResImage').attr({
					'width':width,
					'height':height
				});

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

				// Fade the new image
				$('div.popup #fullResImageContainer').fadeIn(settings.animationSpeed);

				// Show the nav elements
				_shownav();
			
				// Fade the resizing link if the image is resized
				if(resized) $('a.expand,a.contract').fadeIn(settings.animationSpeed);
			});
		};
	
		function changePicture(direction){
			if(direction == 'previous') {
				arrayPosition--;
				setPosition--;
			}else{
				arrayPosition++;
				setPosition++;
			};

			// Allow the resizing of the images
			if(!doresize) doresize = true;

			// Fade out the current picture
			$('div.popup .hoverContainer,div.popup .details').fadeOut(settings.animationSpeed);
			$('div.popup #fullResImageContainer').fadeOut(settings.animationSpeed,function(){
				$('.loaderIcon').show();
			
				// Preload the image
				_preload();
			});

			_hideTitle();
			$('a.expand,a.contract').fadeOut(settings.animationSpeed,function(){
				$(this).removeClass('contract').addClass('expand');
			});
		};
	
		function close(){
			$('div.popup,div.prettyPhotoTitle').fadeOut(settings.animationSpeed, function(){
				$('div.prettyPhotoOverlay').fadeOut(settings.animationSpeed, function(){
					$('div.prettyPhotoOverlay,div.popup,div.prettyPhotoTitle').remove();
				
					// To fix the bug with IE select boxes
					if($.browser.msie && $.browser.version == 6){
						$('select').css('visibility','visible');
					};
				});
			});
		};
	
		function _checkPosition(){
			// If at the end, hide the next link
			if(setPosition == setCount) {
				$('div.popup a.next').css('visibility','hidden');
				$('div.popup a.arrow_next').addClass('disabled').unbind('click');
			}else{ 
				$('div.popup a.next').css('visibility','visible');
				$('div.popup a.arrow_next.disabled').removeClass('disabled').bind('click',function(){
					changePicture('next');
					return false;
				});
			};
		
			// If at the beginning, hide the previous link
			if(setPosition == 1) {
				$('div.popup a.previous').css('visibility','hidden');
				$('div.popup a.arrow_previous').addClass('disabled').unbind('click');
			}else{
				$('div.popup a.previous').css('visibility','visible');
				$('div.popup a.arrow_previous.disabled').removeClass('disabled').bind('click',function(){
					changePicture('previous');
					return false;
				});
			};
		
			// Change the current picture text
			$('div.popup p.currentTextHolder').text(setPosition + settings.counter_separator_label + setCount);
		
			var $c = (isSet) ? $(imagesArray[arrayPosition]) : $(caller);

			if($c.attr('title')){
				$('div.popup .description').show().html(unescape($c.attr('title')));
			}else{
				$('div.popup .description').hide().text('');
			};
		
			if($c.find('img').attr('alt') && settings.showTitle){
				hasTitle = true;
				$('div.prettyPhotoTitle .prettyPhotoTitleContent').html(unescape($c.find('img').attr('alt')));
			}else{
				hasTitle = false;
			};
		};
	
		function _fitToViewport(width,height){
			hasBeenResized = false;
		
			$('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 = height + parseFloat($('div.popup .details').height()) + parseFloat($('div.popup .details').css('margin-top')) + parseFloat($('div.popup .details').css('margin-bottom'));
			contentWidth = width;
			containerHeight = height + parseFloat($('div.prettyPhotoTitle').height()) + parseFloat($('div.popup .top').height()) + parseFloat($('div.popup .bottom').height());
			containerWidth = width + settings.padding;
		
			// Define them in case there's no resize needed
			imageWidth = width;
			imageHeight = height;

			if($.browser.opera) {
				windowHeight = window.innerHeight;
				windowWidth = window.innerWidth;
			}else{
				windowHeight = $(window).height();
				windowWidth = $(window).width();
			};
		
			if( ((containerWidth > windowWidth) || (containerHeight > windowHeight)) && doresize && settings.allowresize) {
				hasBeenResized = true;
			
				if((containerWidth > windowWidth) && (containerHeight > windowHeight)){
					// Get the original geometry and calculate scales
					var xscale =  (containerWidth + 200) / windowWidth;
					var yscale = (containerHeight + 200) / windowHeight;
				}else{
					// Get the original geometry and calculate scales
					var xscale = windowWidth / containerWidth;
					var yscale = windowHeight / containerHeight;
				}

				// Recalculate new size with default ratio
				if (yscale<xscale){
					imageWidth = Math.round(width * yscale * 0.9);
					imageHeight = Math.round(height * yscale * 0.9);
				} else {
					imageWidth = Math.round(width * xscale * 0.9);
					imageHeight = Math.round(height * xscale * 0.9);
				};
			
				// Define the new dimensions
				contentHeight = imageHeight + parseFloat($('div.popup .details').height()) + parseFloat($('div.popup .details').css('margin-top')) + parseFloat($('div.popup .details').css('margin-bottom'));
				contentWidth = imageWidth;
				containerHeight = imageHeight + parseFloat($('div.prettyPhotoTitle').height()) + parseFloat($('div.popup .top').height()) + parseFloat($('div.popup .bottom').height());
				containerWidth = imageWidth + settings.padding;
			
				$('div.popup .details').width(contentWidth); /* To have the correct height */
				$('div.popup .details p.description').width(contentWidth - parseFloat($('div.popup a.close').css('width'))); /* So it doesn't overlap the button */
			};

			return {
				width:imageWidth,
				height:imageHeight,
				containerHeight:containerHeight,
				containerWidth:containerWidth,
				contentHeight:contentHeight,
				contentWidth:contentWidth,
				resized:hasBeenResized
			};
		};
	
		function _centerPicture(){
			//Make sure the gallery is open
			if($('div.popup').size() > 0){
			
				var scrollPos = _getScroll();
			
				if($.browser.opera) {
					windowHeight = window.innerHeight;
					windowWidth = window.innerWidth;
				}else{
					windowHeight = $(window).height();
					windowWidth = $(window).width();
				};
			
				if(doresize) {
					projectedTop = (windowHeight/2) + scrollPos['scrollTop'] - ($('div.popup').height()/2);
					if(projectedTop < 0) projectedTop = 0 + $('div.prettyPhotoTitle').height();
					
					$('div.popup').css({
						'top': projectedTop,
						'left': (windowWidth/2) + scrollPos['scrollLeft'] - ($('div.popup').width()/2)
					});
			
					$('div.prettyPhotoTitle').css({
						'top' : $('div.popup').offset().top - $('div.prettyPhotoTitle').height(),
						'left' : $('div.popup').offset().left + (settings.padding/2)
					});
				};
			};
		};
	
		function _shownav(){
			if(isSet) $('div.popup .hoverContainer').fadeIn(settings.animationSpeed);
			$('div.popup .details').fadeIn(settings.animationSpeed);

			_showTitle();
		};
	
		function _showTitle(){
			if(settings.showTitle && hasTitle){
				$('div.prettyPhotoTitle').css({
					'top' : $('div.popup').offset().top - 22,
					'left' : $('div.popup').offset().left + (settings.padding/2),
					'display' : 'none'
				});
			
				$('div.prettyPhotoTitle div.prettyPhotoTitleContent').css('width','auto');
			
				if($('div.prettyPhotoTitle').width() > $('div.popup').width()){
					$('div.prettyPhotoTitle div.prettyPhotoTitleContent').css('width',$('div.popup').width() - (settings.padding * 2));
				}else{
					$('div.prettyPhotoTitle div.prettyPhotoTitleContent').css('width','');
				};
			
				$('div.prettyPhotoTitle').fadeIn(settings.animationSpeed);
			};
		};
	
		function _hideTitle() {
			$('div.prettyPhotoTitle').fadeOut(settings.animationSpeed);
		};
	
		function _preload(){
			// Hide the next/previous links if on first or last images.
			_checkPosition();
		
			// Set the new image
			imgPreloader = new Image();
		
			// Preload the neighbour images
			nextImage = new Image();
			if(isSet && setPosition > setCount) nextImage.src = $(imagesArray[arrayPosition + 1]).attr('href');
			prevImage = new Image();
			if(isSet && imagesArray[arrayPosition - 1]) prevImage.src = $(imagesArray[arrayPosition - 1]).attr('href');

			$('div.popup .content').css('overflow','hidden');
		
			if(isSet) {
				$('div.popup #fullResImage').attr('src',$(imagesArray[arrayPosition]).attr('href'));
			}else{
				$('div.popup #fullResImage').attr('src',$(caller).attr('href'));
			};

			imgPreloader.onload = function(){
				var correctSizes = _fitToViewport(imgPreloader.width,imgPreloader.height);
				imgPreloader.width = correctSizes['width'];
				imgPreloader.height = correctSizes['height'];
			
				// Need that small delay for the anim to be nice
				setTimeout('showimage(imgPreloader.width,imgPreloader.height,'+correctSizes["containerWidth"]+','+correctSizes["containerHeight"]+','+correctSizes["contentHeight"]+','+correctSizes["contentWidth"]+','+correctSizes["resized"]+')',500);
			};
		
			(isSet) ? imgPreloader.src = $(imagesArray[arrayPosition]).attr('href') : imgPreloader.src = $(caller).attr('href');
		};
	
		function _getScroll(){
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			return {scrollTop:scrollTop,scrollLeft:scrollLeft};
		};
	
		function _resizeOverlay() {
			$('div.prettyPhotoOverlay').css({
				'height':$(document).height(),
				'width':$(window).width()
			});
		};
	
		function _buildOverlay(){
		
			// Build the background overlay div
			backgroundDiv = "<div class='prettyPhotoOverlay'></div>";
			$('body').append(backgroundDiv);
			$('div.prettyPhotoOverlay').css('height',$(document).height()).bind('click',function(){
				close();
			});
		
			// Basic HTML for the picture holder
			popup = '<div class="popup"><div class="top"><div class="left"></div><div class="middle"></div><div class="right"></div></div><div class="content"><a href="#" class="expand" title="Expand the image">Expand</a><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><div class="nav"><a href="#" class="arrow_previous">Previous</a><p class="currentTextHolder">0'+settings.counter_separator_label+'0</p><a href="#" class="arrow_next">Next</a></div></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(popup).append(titleHolder);

			$('.popup,.titleHolder').css({'opacity': 0});
			$('.popup,.prettyPhotoTitle').addClass(settings.theme);
			$('a.close').bind('click',function(){ close(); return false; });
			$('a.expand').bind('click',function(){
			
				// Expand the image
				if($(this).hasClass('expand')){
					$(this).removeClass('expand').addClass('contract');
					doresize = false;
				}else{
					$(this).removeClass('contract').addClass('expand');
					doresize = true;
				};
			
				_hideTitle();
				$('div.popup .hoverContainer,div.popup #fullResImageContainer').fadeOut(settings.animationSpeed);
				$('div.popup .details').fadeOut(settings.animationSpeed,function(){
					_preload();
				});
			
				return false;
			});
		
			$('.popup .previous,.popup .arrow_previous').bind('click',function(){
				changePicture('previous');
				return false;
			});
		
			$('.popup .next,.popup .arrow_next').bind('click',function(){
				changePicture('next');
				return false;
			});

			$('.hoverContainer').css({
				'margin-left': settings.padding/2
			});
		
			// If it's not a set, hide the links
			if(!isSet) {
				$('.hoverContainer,.nav').hide();
			};


			// To fix the bug with IE select boxes
			if($.browser.msie && $.browser.version == 6){
				$('body').addClass('ie6');
				$('select').css('visibility','hidden');
			};

			// Then fade it in
			$('div.prettyPhotoOverlay').css('opacity',0).fadeTo(settings.animationSpeed,settings.opacity, function(){
				$('div.popup').css('opacity',0).fadeIn(settings.animationSpeed,function(){
					// To fix an IE bug
					$('div.popup').attr('style','left:'+$('div.popup').css('left')+';top:'+$('div.popup').css('top')+';');
				});
			});
		};
	};




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

eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('$.C.D=4(b){b=E.F({8:i,o:\'G\',j:H,5:i,p:q},b);$(\'I\').J(\'<t K="1"></t>\');k c=4(a){$("#1").2({\'L\':\'M\',\'N\':-u,\'O\':-u,\'v\':$(a).v(),\'P-0\':$(a).0(),\'l-w\':$(a).2(\'l-w\'),\'l-x\':$(a).2(\'l-x\'),\'z-0\':$(a).2(\'z-0\')});3($.A.Q&&R($.A.S)<7){$("#1").2(\'0\',$(a).0())}};k d=4(a){m=$(a).T(\'U\')||"";m=m.V(/\\n/g,\'<B />\');$("#1").W(m+\'<B />\');3(!b.p){3($("#1").0()>$(a).0()){3($(\'#1\').0()>b.j){$(a).2(\'9-y\',\'r\')}h{$(a).2(\'9-y\',\'s\');e(a)}}h 3($("#1").0()<$(a).0()){3($(\'#1\').0()>b.j){$(a).2(\'9-y\',\'r\')}h{$(a).2(\'9-y\',\'s\');f(a)}}}};k e=4(a){3(b.8&&!b.5){b.5=q;$(a).8({\'0\':$("#1").0()},b.o,4(){b.5=i})}h 3(!b.8&&!b.5){$(a).0($("#1").0())}};k f=4(a){3(b.8&&!b.5){b.5=q;$(a).8({\'0\':$("#1").0()},b.o,4(){b.5=i})}h{$(a).0($("#1").0())}};$(6).X(4(){$(6).2({\'9\':\'s\'}).Y(\'Z\',4(){d($(6))});c(6);d($(6));3($("#1").0()>b.j){$(6).2({\'9-y\':\'r\',\'0\':b.j})}h{$(6).0($("#1").0())};b.p=i})};',62,62,'height|comment_hidden|css|if|function|alreadyAnimated|this||animate|overflow||||||||else|false|maxHeight|var|font|theValue||animationSpeed|init|true|scroll|hidden|div|10000|width|family|size||line|browser|br|fn|prettyComments|jQuery|extend|fast|500|body|append|id|position|absolute|top|left|min|msie|parseFloat|version|attr|value|replace|html|each|bind|keyup'.split('|'),0,{}))



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

		// 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, idioma ){
		// Build the background overlay div
		backgroundDiv = "<div class='prettyPhotoOverlay'></div>";
		$('body').append(backgroundDiv);
		$('div.prettyPhotoOverlay').css('height',$(document).height()).bind('click',function(){
			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 += '		<input type="hidden" id="idioma_val" value="'+ idioma +'" />\n';

		if( idioma == 'catala' )
		{
			html += '		<h4 class="clletra">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 += '		<div style="float: left;"><input type="image" src="img/contingut/enviar_'+ idioma +'.png" onclick="enviarEmail.enviar()" style="border: none; background-color: transparent; width: 48px" /></div>\n';
			html += '<div style="float: right; padding-top: 5px"><a onclick="enviarEmail.close()"><img src="/img/icones/tancar.png" 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>';
		}
		else
		{
			html += '		<h4 class="blau">Enviar articulo</h4>\n';
			html += '		<input type="hidden" id="id_article" value="'+ id_article +'" />\n';
			html += '		<label for="nom_e">Nombre/s</label>\n';
			html += '		<input id="nom_e" type="text" value="" name="nom" />\n';
			html += '		<br class="p" />\n';
			html += '		<label for="email_e">E-mail(s)</label>\n';
			html += '		<input id="email_e" type="text" value="" name="email" />\n';
			html += '		<br class="p" />\n';
			html += '		<label for="r_nom">Tu nombre</label>\n';
			html += '		<input id="r_nom" type="text" value="" name="r_nom" />\n';
			html += '		<br class="p" />\n';
			html += '		<label for="r_email">Tu e-mail</label>\n';
			html += '		<input id="r_email" type="text" value="" name="r_email" />\n';
			html += '		<br class="p" />\n';
			html += '		<label for="comentari">Comentario</label>\n';
			html += '		<textarea id="comentari" name="comentari"></textarea><br /><br />\n';
			html += '		<div style="float: left;"><input type="image" src="img/contingut/enviar_'+ idioma +'.png" onclick="enviarEmail.enviar()" style="border: none; background-color: transparent; width: 48px" /></div>\n';
			html += '<div style="float: right; padding-top: 5px"><a onclick="enviarEmail.close()"><img src="/img/icones/tancar.png" 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.prettyPhotoOverlay').css('opacity',0).fadeTo('fast',0.35, function(){
			$('div.popup').css('opacity',0).fadeIn('fast',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.prettyPhotoOverlay').fadeTo('fast',0, function(){
				$('div.prettyPhotoOverlay').remove();
				$('div.popup').remove();
			});
		});
	},
	enviar : function(){
		var email = $("#email_e").val();
		var idioma = $("#idioma_val").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/loading.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="blau">';
				
				if( idioma == 'catala' )
					html += '		S\'ha enviat correctament el missatge<br />\n';
				else
					html += '		Su mensaje se ha enviado correctamente<br />\n';

				html += '</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("Falta posar el teu correu electrònic\nFalta poner el correo electrónico");
		}
	}
}

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='prettyPhotoOverlay'></div>";
		$('body').append(backgroundDiv);
		$('div.prettyPhotoOverlay').css('height',$(document).height()).bind('click',function(){
			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="blau">Previsualització</h4>\n';
		
		if( comentari )
		{
			var regX = /\n/gi ;
			comentari2 = new String(comentari);
			comentari2 = comentari2.replace(regX, "<br /> \n");

			html += '		<strong class="blau">Nom:</strong> '+ nom +'<br /><br />\n';
			html += '		<strong class="blau">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.png" 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.prettyPhotoOverlay').css('opacity',0).fadeTo('fast',0.35, function(){
			$('div.popup').css('opacity',0).fadeIn('fast',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.prettyPhotoOverlay').fadeTo('fast',0, function(){
				$('div.prettyPhotoOverlay').remove();
				$('div.popup').remove();
			});
		});
	}
}



jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.04
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).width();
				});
				$strip.width(stripWidth);			
				var defTiming = stripWidth/settings.travelocity;
				var totalTravel = stripWidth+containerWidth;								
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};