var AJAX = '/ajax/ajax.php';


function comprovaCamp ( id_camp )
{
	if( $("#"+id_camp).val() == "" )
	{
		$("#"+id_camp).addClass("comentari-error");
		$("#"+id_camp).removeClass("comentari");
	}
	else
	{
		$("#"+id_camp).addClass("comentari");
		$("#"+id_camp).removeClass("comentari-error");
	}
}

function comprovaMail ( id_camp )
{
	if ( $("#"+id_camp).val() == "" || !echeck( $("#"+id_camp).val() ) )
	{
		$("#"+id_camp).addClass("comentari-error");
		$("#"+id_camp).removeClass("comentari");
	}
	else
	{
		$("#"+id_camp).addClass("comentari");
		$("#"+id_camp).removeClass("comentari-error");
	}
}


function enviar_comentari()
{
	var error = false;
	var error_text = '';
	var id_entrada;
	var nom;
	var tmp;
	var email;
	var comentari;
	var poblacio = "";
	
	var id_entrada = $("#id_entrada").val();
	var lloc = $("#lloc").val();
	
	if( $("#email").val() ) return false;
	
	if( $("#nom").val() )
	{
		$("#nom").removeClass("comentari-error");
		$("#nom").addClass("comentari");
		var nom = $("#nom").val();
	}
	else
	{
		$("#nom").addClass("comentari-error");
		$("#nom").removeClass("comentari");
		error_text += "<li>Has de posar el teu nom</li>\n";
		error = true;
	}
		
	if( $("#poblacio").val() )
		var poblacio = $("#poblacio").val();
			
	
	if( $("#ae").val() )
	{
		var ae = $("#ae").val();

		if( echeck( ae ) )
		{
			$("#ae").removeClass("comentari-error");
			$("#ae").addClass("comentari");
		}
		else
		{
			$("#ae").addClass("comentari-error");
			$("#ae").removeClass("comentari");
			error_text += "<li>El correu elctrònic que has posat és erroni</li>\n";
			error = true;
		}
	}
	else
	{
		$("#ae").addClass("comentari-error");
		$("#ae").removeClass("comentari");
		error_text += "<li>Has de posar el correu electrònic</li>\n";
		error = true;
	}
		
	if( $("#comentari").val() )
	{
		var comentari = $("#comentari").val();
		$("#comentari").removeClass("comentari-error");
		$("#comentari").addClass("comentari");
	}
	else
	{
		$("#comentari").addClass("comentari-error");
		$("#comentari").removeClass("comentari");
		error_text += "<li>Has d'escriure un comentari</li>\n";
		error = true;
	}

	if( $("#notifica")[0].checked )
		var notifica = "si";
	else
		var notifica = "no";

 	if( !error )
	{
		$("#comentari-formulari").html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { id_entrada: id_entrada, lloc: lloc, nom: nom, poblacio: poblacio, email: ae, comentari: comentari, notifica: notifica, accio: 'enviar_comentari' },
		function( resposta )
		{
			$("#comentari-error").addClass("ocult");
			$("#comentari-formulari").fadeOut("slow");
			$("#comentari-enviat").fadeIn("slow");
		});
	}
	else
	{
		$("#comentari-error").fadeIn("slow");
		$("#comentari-error").html( "<ul>" + error_text + "</ul>");
	}
}

function genera_comentaris( pagina, id_entrada, lloc )
{
 	if( pagina && id_entrada )
	{
		$("#comentaris-cnt").html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { id_entrada: id_entrada, pagina: pagina, lloc: lloc, accio: 'genera_comentaris' },
		function( resposta )
		{
			$("#comentaris-cnt").html( resposta );
			window.location.hash = "comentaris";
		});
	}
	else
	{
		alert("Error inesperat!");
	}
	
}
function veure_comentari( id_comentari, lloc )
{
 	if( id_comentari )
	{
		$.getJSON( AJAX, { id_comentari: id_comentari, lloc: lloc, accio: 'veure_comentari' },
		function( resposta )
		{
			$("#comentari-cnt-" + resposta.id_comentari ).html( resposta.text );
		});
	}
	else
	{
		alert("Error inesperat!");
	}
}
function limitacio( numero, id_entrada )
{
 	if( numero && id_entrada )
	{
		$("#comentaris-cnt").html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { numero: numero, accio: 'limitacio' },
		function()
		{
			genera_comentaris( "1", id_entrada, lloc );
		});
	}
	else
	{
		alert("Error inesperat!");
	}
}


function echeck(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1)
		return false;

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;

	if (str.indexOf(at,(lat+1))!=-1)
		return false;

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;

	if (str.indexOf(dot,(lat+2))==-1)
		return false;

	if (str.indexOf(" ")!=-1)
		return false;

	return true;
}




enviarEmail = {
	init : function( id_article, lloc ){
		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 type="image" src="img/icones/enviar.png" onclick="enviarEmail.enviar()" style="border: none; background-color: transparent; width: 62px" /></div>\n';
		html += '<div style="float: right; padding-top: 5px"><a onclick="enviarEmail.close()"><img src="/img/icones/btnClose.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
		containerHeighhtml += newHeight;
		contentHeighhtml += 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 )
		{
			html += '		<strong class="verd">Nom:</strong> '+ nom +'<br /><br />\n';
			html += '		<strong class="verd">Comentari:</strong><br />'+ comentari +'<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/btnClose.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
		containerHeighhtml += newHeight;
		contentHeighhtml += 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();
			});
		});
	}
}


var mes_actual;
var any_actual;
var lloc;

function buildCal( mes, any )
{
	m = mes;
	y = any;
	
	var id_bloc = $('#id_bloc').val();
	
	$('#calendari').html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 40px 0px 0px 75px" />' );
	$.getJSON( AJAX, { mes: m, any: y, id_bloc: id_bloc, accio: 'cerca' },
	function( resposta )
	{
		var dies_actes = new Array();
	
		for( var i = 1; i <= 31; i++ )
		{
			dies_actes[i] = false;
		}
	
		if( resposta.dies )
		{
			for(var i = 0; i < resposta.dies.length; i++)
			{
				var tmp = resposta.dies[i];
				dies_actes[tmp] = true;
			}
		}
		
		mes_actual = m;
		any_actual = y;
	
		var mn=['gener','febrer','mar&#231;','abril','maig','juny','juliol','agost','setembre','octubre','novembre','desembre'];
		var dies=['dl','dt','dc','dj','dv','ds','dg'];
		var dim=[31,0,31,30,31,30,31,31,30,31,30,31]; /* dies dels mesos */
		var dim_ant=[31,31,0,31,30,31,30,31,31,30,31,30]; /* dies dels mesos anteriors */
		var dim_seg=[0,31,30,31,30,31,31,30,31,30,31,31]; /* dies dels mesos posteriors */
	
		var oD = new Date(y, m-1, 1);
		oD.od = oD.getDay(); /* dia de la setmana 0 - dg, 6 - ds */
	
		var data_avui = new Date();
		var avui = (y == data_avui.getFullYear() && m == data_avui.getMonth() + 1) ? data_avui.getDate() : 0;
		var setmana_avui = parseInt( (avui-1) / 7 ) + 1; /* la setmana del dia d'avui, més una setmana del mes anterior */
		setmana_actual = setmana_avui;
		
		// dies que té el febrer
		dim[1] = (((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
		dim_ant[2] = dim[1];
		dim_seg[0] = dim[1];
		
		var bloc = resposta.bloc;
		
		var html = '<table><tr><td width="20" align="center">';
		
		if( resposta.mesos_ant == 'si' )
			html += '	<a onclick="mes_anterior();"> < </a>';
			
		html += '</td><td width="140" style="text-align: center; font-size: 11px; padding-top: 2px; font-weight: bolder;">';
		html += '	<a href="'+ bloc +'/arxiu/'+ y +'/'+ m +'">'+mn[m-1]+' '+y+'</a>';
		html += '</td><td width="20" align="center">';
		
		if( resposta.mesos_pos == 'si' )
			html += '	<a onclick="mes_seguent();"> > </a>';
			
		html += '</td></tr></table>';
		
		html += '<table class="calendari" cols="7" cellpadding="3" cellspacing="0">';
		
		var dia = 1;
		var setmana = 0;
		var classes = '';
		
		/* posa els dies del mes anterior */
		for( i = ( dim_ant[m-1] - 7 - oD.od + 2 ); i <= dim_ant[m-1]; i++ )
		{
			html += '<td></td>';
			if( dia%7 == 0 )
			{
				html += '</tr><tr>';
				setmana++;
			}
			dia++;
		}
		for( i = 1; i <= dim[m-1]; i++ )
		{
			classes = '';
			var x = i;
			
			if( dia%7 == 0 ) classes += 'cal_festa';
			
			if( setmana == setmana_actual )
			{
				if( classes != '' ) classes += ' ';
				if( dia%7 == 0 ) classes += 'cal_festa';
			}
	
			if( x == avui )
			{
				if( classes != '' ) classes += ' ';
				classes += 'cal_avui';
			}
	
			if( dies_actes[x] == true )
			{
				classes += ' clletra';
				html += '<td class="'+ classes +'"><a href="'+ bloc +'/arxiu/'+ y +'/'+ m +'/'+ x +'"><strong>' + x + '</strong></a></td>';
			}
			else
				html += '<td class="'+ classes +'" unselectable="on">' + x + '</td>';
	
			if( dia%7 == 0 )
			{
				html += '</tr><tr>';
				setmana++;
			}
			dia++;
		}
		var ultim_dia = oD.od + dim[m-1]%7;
		
		
		for( i = 1; i < 14 - ultim_dia + 2; i++ )
		{
			html += '<td></td>';
			if( dia%7 == 0 )
			{
				html += '</tr><tr>';
				setmana++;
			}
			dia++;
		}
		
		html += '</tr></table>';
		
		$('#calendari').html( html );	
	});
}


function mes_anterior()
{
	if( mes_actual == 1 )
	{
		mes_actual = 12;
		any_actual--;
	}
	else mes_actual--;
	
	buildCal( mes_actual, any_actual );
}
function mes_seguent()
{
	if( mes_actual == 12 )
	{
		mes_actual = 1;
		any_actual++;
	}
	else mes_actual++;
	
	buildCal( mes_actual, any_actual );
}




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 )
		{
			html += '		<strong class="verd">Nom:</strong> '+ nom +'<br /><br />\n';
			html += '		<strong class="verd">Comentari:</strong><br />'+ comentari +'<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/btnClose.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();
			});
		});
	}
}

