/* 7.7 js Document */
/* author: marc */

//CONSTANTS
var FOTO=1;
var ENTREVISTA=2;
var VIDEO=3;
//VARIABLES GLOBALS
var bolMenu=0;
		
var intFoto=0;
var intFotos=0;
var strReportatge='';
var intTipus=0;
var intPaginaThumbs=0;
var intPaginesThumbs=0;
var intThumbsPagina=7;
var bolMenuInicial=1;
var bolFotoCargant=0;
var intIdioma=0;

var imgCover=0;
var imgCoverNew=1;
var interval;

var photoWidth=0;
var photoHeight=0;
var PIMGPortada;
var strSrcImatgesPortada;

//FUNCIONS
function intro(){
	
}

function canviarIdioma(){
	var strIdioma;
	if(intIdioma==2){
		intIdioma=0;
	}else{
		intIdioma=intIdioma + 1;
	}
	switch(intIdioma){
		case 0:
			strIdioma="Idioma";
		break;
		case 1:
			strIdioma="Idioma";
		break;
		case 2:
			strIdioma="Language";
		break;	
		
	}
	$('#idioma_li').html("<li class='sub_menu_li' id='idioma_li'>" + strIdioma + "</li>");
}

function zeroDavant(intNumero){
	var strNumero = '0';
	if (intNumero){
		if (typeof intNumero == 'number') {
			if (intNumero>0){
				if (intNumero<10){
					strNumero = '0' + intNumero;
				}else{
					strNumero=intNumero;
				}
			}
		}
	}
	return strNumero;
}

//funció de navegació del slideshow d'imatges amb les fletxes
function canviFoto(novaFoto){
	var strSrc='';
	var strContador='';
	var strPeuFoto='';
	var strPhoto='';
	
	var objImg = new Image();
	if(novaFoto){
		strSrc='numeros/' + strNumRevista + '/' + strReportatge + '/fotos/' + strReportatge + '_' + zeroDavant(novaFoto) + '.jpg';		
		strContador='<p>'+zeroDavant(novaFoto)+' l '+zeroDavant(intFotos)+'</p>';	
		$('#mini_control_numero').html(strContador);	
	}
	objImg.onload = function(){
		photoWidth=$(this).attr('width');
		photoHeight=$(this).attr('height');
		var fonsWidth=$('#fons').width();
		var fonsHeight=$('#fons').height();
		var newPhotoWidth=photoWidth;
		var newPhotoHeight=photoHeight;
		//if(photoWidth>=fonsWidth){
			newPhotoWidth=fonsWidth;
			newPhotoHeight=(photoHeight*newPhotoWidth)/photoWidth;
		//}
		if(newPhotoHeight>=fonsHeight){
			newPhotoHeight=fonsHeight;
			newPhotoWidth=(photoWidth*newPhotoHeight)/photoHeight;
		}
		$('#fons').fadeOut('normal', function(){
			var left=(fonsWidth/2) - newPhotoWidth/2;
			var top=(fonsHeight/2 - newPhotoHeight/2);
			
			strPhoto+='<img id="bg_img" alt="photo" src="'+strSrc+'"/>';
			$('#fons').html(strPhoto);
			$('#bg_img').width(newPhotoWidth).height(newPhotoHeight).css({'top':top, 'left':left});
			$('#fons').fadeIn('normal', function(){
				$('.peu_foto_text').css('display', 'none');
				strPeuFoto = '#peu_foto_' + strReportatge + '_' + zeroDavant(novaFoto);		
				$(strPeuFoto).css('display', 'block');
				bolFotoCargant=0;
			});
		});
	}
	bolFotoCargant=1;
	objImg.src=strSrc;
	delete objImg;
}

function canviPagina(novaFoto){
	var strContador='';
	strContador='<p>' + zeroDavant(novaFoto) + ' l ' + zeroDavant(intFotos) + '</p>';	
	$('#entrevista_numero').html(strContador);
	$('.menu_entrevista_cos').css('display', 'none');
	$('#menu_entrevista_cos_' + zeroDavant(novaFoto)).css('display', 'block');
	$('#menu_entrevista').fadeIn('slow', function(){
		bolMenu=5;
	});
}

function fotoAnterior(){
	if (bolFotoCargant!=1){
		if (intFoto > 1){
			intFoto--;
			canviFoto(intFoto);
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function fotoSeguent(){
	if (bolFotoCargant!=1){	
		if (intFoto < intFotos){
			intFoto++;
			canviFoto(intFoto);
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function paginaAnterior(){
	if (intFoto > 1){
		intFoto--;
		canviPagina(intFoto);
	}else{
		return false;
	}
}

function paginaSeguent(){
	if (intFoto < intFotos){
		intFoto++;
		canviPagina(intFoto);
	}else{
		return false;
	}
}

function canviThumbs(){
			
	var intNumImatgesCarregades=0;
	var strThumbSrc= new String;
	var PIMG=new Array;
	
	var intThumbInicial=intThumbsPagina*(intPaginaThumbs-1)+1;
	var intThumbs=intFotos-(intThumbInicial-1);
	
	if (intThumbs>intThumbsPagina) intThumbs=intThumbsPagina;
	
	for(var i=0;i<intThumbs;i++){
		strThumbSrc='numeros/' + strNumRevista + '/' + strReportatge + '/thumbs/' + strReportatge + '_' + zeroDavant(intThumbInicial) + '.jpg';
		PIMG[i]=new Image();
		PIMG[i].onload = function(){
			intNumImatgesCarregades++;
			if(intNumImatgesCarregades==intThumbs){
				intThumbInicial=intThumbsPagina*(intPaginaThumbs-1)+1;
				$('.thumb:visible').fadeOut(300);
				setTimeout(function(){
					for(var j=1;j<=intThumbs;j++){
						strThumbSrc='numeros/' + strNumRevista + '/' + strReportatge + '/thumbs/' + strReportatge + '_' + zeroDavant(intThumbInicial) + '.jpg';				
						strThumb='#thumb' + j;
						$(strThumb + ' >img').removeAttr('src').attr('src', strThumbSrc);
						$(strThumb).fadeIn(300);
						intThumbInicial++;
					}
				},200);
			}
		}
		PIMG[i].src=strThumbSrc;		
		intThumbInicial++;
	}	
	delete strSrcImatges;
	delete PIMG;
}

function thumbsAnterior(){
	if (intPaginaThumbs>1){
		intPaginaThumbs--;
		canviThumbs();
	}		
}

function thumbsSeguent(){	
	if (intPaginaThumbs<intPaginesThumbs){
		intPaginaThumbs++;
		canviThumbs();
	}		
}

function amagarControls(){
	$('.thumb').css('display', 'none');
	$('.menu_report_text_seccio').contents().css('display', 'none');
	$('.menu_report_text_seccio').css('display', 'none');	
	$('.menu_report_text').contents().css('display', 'none');
	$('.menu_report_text').css('display', 'none');		
	$('#sub_menu_report .sub_menu_li').css('background', 'none').css('color', '#FFF');
	$('.peu_foto_fons').css('display', 'none');
	$('#control_foto_esquerra').css('display', 'none');
	$('#control_foto_dret').css('display', 'none');
	$('.barra_inferior').css('display', 'none');
}

//funció per a carregar els reports i veure'ls

function carregarReportatge(strReportatge){
	switch(strNumRevista){
		case '01':
			switch(strReportatge){
				case 'report1':
					intFotos=21;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=19;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report5':
					intFotos=9;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=26;
					intTipus=FOTO;
				break;
				case 'report7':
					intFotos=30;
					intTipus=FOTO;
				break;
			}
		break;
		case '02':
			switch(strReportatge){
				case 'report1':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=19;				
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=17;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=13;
					intTipus=FOTO;
				break;
				case 'report5':
					intFotos=6;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=30;
					intTipus=FOTO;
				break;
				case 'report7':
					intFotos=15;
					intTipus=FOTO;
				break;
			}
		break;
		case '03':
			switch(strReportatge){
				case 'report1':
					intFotos=32;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=45;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=17;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report5':
					intFotos=11;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report7':
					intFotos=29;
					intTipus=FOTO;
				break;
			}
		break;
		case '04':
			switch(strReportatge){
				case 'report1':
					intFotos=25;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=11;
					intTipus=FOTO;
				break;
				case 'report5':
					intFotos=6;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=24;
					intTipus=FOTO;
				break;
				case 'report7':
					intFotos=49;
					intTipus=FOTO;
				break;
				case 'report8':
					intFotos=32;
					intTipus=FOTO;
				break;
			}
		break;
		case '05':
			switch(strReportatge){
				case 'report1':
					intFotos=41;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=20;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=16;
					intTipus=FOTO;
				break;
				case 'report5':
					//pagines de la entrevista
					intFotos=11;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=1;
					intTipus=VIDEO;
				break;
			}		
		break;
		case '06':
			switch(strReportatge){
				case 'report1':
					intFotos=30;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=24;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=21;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=53;
					intTipus=FOTO;
				break;
				case 'report5':
					//pagines de la entrevista
					intFotos=6;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=1;
					intTipus=VIDEO;
				break;
			}		
		break;
		case '07':
			switch(strReportatge){
				case 'report1':
					intFotos=23;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=16;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=30;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=16;
					intTipus=FOTO;
				break;
				case 'report5':
					//pagines de la entrevista
					intFotos=9;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=1;
					intTipus=VIDEO;
				break;
			}		
		break;
		case '08':
			switch(strReportatge){
				case 'report1':
					intFotos=26;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=25;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=30;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=27;
					intTipus=FOTO;
				break;
				case 'report5':
					//pagines de la entrevista
					intFotos=3;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=1;
					intTipus=VIDEO;
				break;
			}		
		break;
		case '09':
			switch(strReportatge){
				case 'report1':
					intFotos=27;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=27;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=34;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=28;
					intTipus=FOTO;
				break;
				case 'report5':
					//pagines de la entrevista
					intFotos=5;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=1;
					intTipus=VIDEO;
				break;
			}		
		break;
		case '10':
			switch(strReportatge){
				case 'report1':
					intFotos=27;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=27;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=23;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=30;
					intTipus=FOTO;
				break;
				case 'report5':
					//pagines de la entrevista
					intFotos=4;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=1;
					intTipus=VIDEO;
				break;
			}		
		break;

	}
	
	window.clearInterval(interval);
	
	switch(intTipus){
		case FOTO:
			intFoto=1;
			intPaginaThumbs=1;
			intPaginesThumbs=intFotos/intThumbsPagina;
			$('#barra_inferior_controls_report').load('html_estructura/barra_inferior_controls_foto_' + strIdioma + '.html',function(){																															
				$('#peu_foto_report').load('numeros/' + strNumRevista + '/' + strReportatge + '/html/peus_' + strIdioma + '.php', function(){
					$('#menu_report').load('html_estructura/menu_report_foto_' + strIdioma + '.html',function(){
						$('#memoria').load('numeros/' + strNumRevista + '/' + strReportatge + '/html/memoria_' + strIdioma + '.php', function(){
							$('#perfil').load('numeros/' + strNumRevista + '/' + strReportatge + '/html/perfil_' + strIdioma + '.php', function(){
								$('#llicencia').load('html/llicencia_' + strIdioma + '.html', function(){																											
									$('#fons').load('html_estructura/fons_foto.html', function(){
										canviFoto(1);
										canviThumbs();
										$('.controls').css('display', 'block');
										$('#menu_report').delay(1000).fadeIn('fast', function(){
											$('#barra_inferior').fadeIn('slow');
											bolMenu=3;
										});
									});
								});
							});				
						});
					});
				});
			});
			
		break;
		case ENTREVISTA:
			intFoto=1;
			$('#barra_inferior_controls_report').load('html_estructura/barra_inferior_controls_entrevista.html',function(){
				$('#barra_inferior').fadeIn('slow');
				$('#menu_report').load('html_estructura/menu_report_entrevista_' + strIdioma + '.html',function(){
					$('#menu_entrevista_titol').load('numeros/' + strNumRevista + '/' + strReportatge + '/html/entrevista_titol_' + strIdioma + '.php',function(){
						$('#menu_entrevista_cos').load('numeros/' + strNumRevista + '/' + strReportatge + '/html/entrevista_cos_' + strIdioma + '.php',function(){
							$('#fons').load('html_estructura/fons_foto.html', function(){
								canviFoto(1);
								canviPagina(1);
								$('.controls').css('display', 'none');
								$('#menu_report').fadeIn('slow', function(){
									bolMenu=3;
								});
							});
						});
					});
				});
			});
		break;
		case VIDEO:
			$('#barra_inferior_controls_report').empty();
			$('#barra_inferior').fadeIn('slow');
			$('#fons').load('html_estructura/fons_video.html', function(){
				$('#bg_vid_fons').load('numeros/' + strNumRevista + '/' + strReportatge + '/video/video.html', function(){
					$('#menu_report').load('html_estructura/menu_report_video_' + strIdioma + '.html',function(){
						$('#memoria').load('numeros/' + strNumRevista + '/' + strReportatge + '/html/memoria_' + strIdioma + '.php',function(){
							$('#perfil').load('numeros/' + strNumRevista + '/' + strReportatge + '/html/perfil_' + strIdioma + '.php',function(){
								$('#llicencia').load('html/llicencia_' + strIdioma + '.html', function(){
									$('.controls').css('display', 'none');
									//$('#bg_vid').children().css('z-index', '-1');
									//$('#menu_report').css('z-index', '9');
									$('#menu_report').fadeIn('slow', function(){
										bolMenu=3;
									});
								});
							});
						});
					});
				});
			});	
		break;
	}
	
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-19012570-1']);
	_gaq.push(['_setLocalRemoteServerMode']);
	_gaq.push(['_trackPageview']);
	
	(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
}

function amagarMenus(){
	switch(bolMenu){
		case 1:			
			$('#menu_portada').fadeOut('normal');
		break;
		case 2:
			$('#sub_menu_info .sub_menu_li').css('background', 'none').css('color', '#FFF');
			$('#menu_info').fadeOut('normal');
		break;
		case 3:
			setTimeout(function(){
				$('#menu_report').fadeOut('normal');
			},500);
		break;		
		case 4:
			$('#peu_foto_li').css('background', 'none').css('color', '#FFF');
			$('#peu_foto').fadeOut('normal');
		break;
		case 5:
			$('#menu_entrevista').fadeOut('normal');
		break;
		case 6:
			$('#idioma_li').css('background', 'none').css('color', '#FFF');
			$('#menu_idioma').fadeOut('normal');
		break;
	}
	bolMenu=0;
}

function iniciarEvents(){
	//obrir del menu principal
 	$('#logo_img').mouseenter(function(){
		if(bolMenu!=1){
			amagarMenus();
			bolMenu=1;
			$('#menu_portada').stop(false,true).fadeIn('slow');
		}
	});
	
	//tancar menu principal
	$('#menu_portada').mouseleave(function(){
		amagarMenus();
	});
	
	$('#subscripcio_li').mouseenter(function(){
		amagarMenus();				 	
		$('#subscripcio_li').css('background', '#00FF00').children().css('color', '#000');
		$('#subscripcio_li').stop(false,true).fadeIn('slow');
	});
	
	$('#subscripcio_li').mouseleave(function(){
		$('#subscripcio_li').css('background', '#000').children().css('color', '#FFF');
		$('#subscripcio_li').stop(false,true).fadeIn('slow');
	});

	$('#peu_foto').mouseleave(function(){
		amagarMenus();
	});
	
	$('#boto_info').mouseenter(function(){
		if(bolMenu!=3){
			amagarMenus();
			bolMenu=3;
			$('#menu_report').stop(false,true).fadeIn('slow');
		}
	});

	$('#menu_report').mouseleave(function(){
		amagarMenus();
	});
	
	var sub_menu_info_timeout = 0;
	$('#sub_menu_info .sub_menu_li').mouseenter(function(){
		if( sub_menu_info_timeout !=0) clearTimeout(sub_menu_info_timeout);
		var aquest = this;
		sub_menu_info_timeout = setTimeout(function(){
			if(bolMenu!=2){
				amagarMenus();
				bolMenu=2;
				$(aquest).css('background', '#FFFF00').css('color', '#000');
				$('#menu_info').stop(false,true).fadeIn('slow');	
			}
			if(bolMenu==2){
				$('#sub_menu_info .sub_menu_li').css('background', 'none').css('color', '#FFF');
				$(aquest).css('background', '#FFFF00').css('color', '#000');
				$('.menu_info_text').css('display', 'none');
				$('#menu_info_'+ $(aquest).attr('name')).css('display', 'block');
			}
		},50);
	},function(){
		clearTimeout(sub_menu_info_timeout);
	});
	
	$('#idioma_li').mouseenter(function(){
		if(bolMenu!=6){
			amagarMenus();
			bolMenu=6;
			$('#idioma_li').css('background', '#FFFF00').css('color', '#000');
			$('#menu_idioma').stop(false,true).fadeIn('slow');
		}
	});
		
	$('#menu_idioma').mouseleave(function(){
		amagarMenus();
	});
	
	$('.numero_a').mouseenter(function(){
		$(this).css('color', '#00FF00');		
	});
	
	$('.numero_a').mouseleave(function(){
		$(this).css('color', '#FFF');		
	});
		
	$('#menu_info').mouseleave(function(){
		amagarMenus();
	});
	
	$('.idioma_a').mouseenter(function(){
		$(this).css('background', '#FFFF00').css('color', '#000');
	});
	
	$('.idioma_a').mouseleave(function(){
		$(this).css('background', '#000').css('color', '#FFF');
	});
	
	var bolMenuParat=1;
	$('.menu_portada_report_titol').mousemove(function(){
		var aquest = this;
		var strTitol = $(this).attr('name');
		var strNumero = strTitol.substring(strTitol.length-1,strTitol.length);
		var strCos= '#menu_portada_report_cos_' + strNumero;
		var strElement= '#menu_portada_element_' + strNumero;
		if(bolMenuParat){
			bolMenuParat=0;
			if(bolMenuInicial){
				bolMenuInicial=0;
				$(aquest).slideUp('fast', function(){
					$(strElement).css('height', '160px');
					$(strCos).slideDown('normal', function(){
						bolMenuParat=1;	
					});
				});
			}else{
				$('.menu_portada_report_cos:visible').slideUp('fast',function(){
					$('.menu_portada_element').css('height', '15px');
					$('.menu_portada_report_titol:hidden').css('display', 'block');						
					$(aquest).slideUp('fast', function(){
						$(strElement).css('height', '160px');
						$(strCos).slideDown('normal', function(){
							bolMenuParat=1;	
						});
					});
				});
			}
		}
	});
	
	
														
	//obrir report desde click al menu principal
	$('.menu_portada_report_cos_img').click(function(){
		strReportatge = $(this).attr('name');
		$('#menu_portada').fadeOut('normal', function(){
			bolMenu=0;
			carregarReportatge(strReportatge);
		});
	});
	
	$('.menu_portada_blog').mouseenter(function(){
		$(this).children('.menu_portada_a').css('color', '#00FF00');	
	});
	
	$('.menu_portada_blog').mouseleave(function(){
		$(this).children('.menu_portada_a').css('color', '#FFF');
	});
	
	

	//control esquerra
	$('#control_foto_esquerra').click(function(){
		fotoAnterior();
	});
	//control dret
	$('#control_foto_dret').click(function(){
		fotoSeguent();
	});	
	
	$('#control_foto_esquerra').mouseenter(function(){
		$('#mini_control_esquerra_img').removeAttr('src').attr('src','img/control_esquerra_on.gif');
	});
	
	$('#control_foto_dret').mouseenter(function(){
		$('#mini_control_dret_img').removeAttr('src').attr('src','img/control_dret_on.gif');
	});
	
	$('#control_foto_esquerra').mouseleave(function(){		
		$('#mini_control_esquerra_img').removeAttr('src').attr('src','img/control_esquerra.gif');
	});
	
	$('#control_foto_dret').mouseleave(function(){			
		$('#mini_control_dret_img').removeAttr('src').attr('src','img/control_dret.gif');
	});
}

function imgCoverChange(){
	var strPhoto;
	
	imgCover=imgCoverNew;
	imgCoverNew++;
	if(strNumRevista=='10'){
		if(imgCoverNew>9){
			window.clearInterval(interval);
		}		
	}
	photoWidth=$(PIMGPortada[imgCover]).attr('width');
	photoHeight=$(PIMGPortada[imgCover]).attr('height');
	var fonsWidth=$('#fons').width();
	var fonsHeight=$('#fons').height();
	var newPhotoWidth=photoWidth;
	var newPhotoHeight=photoHeight;
	//if(photoWidth>=fonsWidth){
		newPhotoWidth=fonsWidth;
		newPhotoHeight=(photoHeight*newPhotoWidth)/photoWidth;
	//}
	if(newPhotoHeight>=fonsHeight){
		newPhotoHeight=fonsHeight;
		newPhotoWidth=(photoWidth*newPhotoHeight)/photoHeight;
	}
	//$('#fons').fadeOut('slow', function(){
		var left=(fonsWidth/2) - newPhotoWidth/2;
		var top=(fonsHeight/2 - newPhotoHeight/2);
		
		strPhoto='<img id="bg_img_new" alt="photo" src="'+strSrcImatgesPortada[imgCover]+'" style="display:none;"/>';
		$('#fons').append(strPhoto);
		$('#bg_img_new').width(newPhotoWidth).height(newPhotoHeight).css({'top':top, 'left':left});
		$('#bg_img_new').fadeIn(666, function(){
			strPhoto='<img id="bg_img" alt="photo" src="'+strSrcImatgesPortada[imgCover]+'" />';
			$('#fons').html(strPhoto);
			$('#bg_img').width(newPhotoWidth).height(newPhotoHeight).css({'top':top, 'left':left});
		});
	//});
}

function coverMultiple(){
	strSrcImatgesPortada=new Array();
	PIMGPortada=new Array;
	var intNumImatgesCarregades=0;
	var strPhoto='';
	
	if(strNumRevista=='10'){
		if(strIdioma=='en'){
			for(var i=1;i<=10;i++){
				strSrcImatgesPortada.push('numeros/10/portada/en/' + i + '.jpg');
			}			
		}else{
			for(var i=1;i<=10;i++){
				strSrcImatgesPortada.push('numeros/10/portada/es/' + i + '.jpg');
			}
		}
	}
	for(var i=0;i<strSrcImatgesPortada.length;i++){
		PIMGPortada[i]=new Image();
		PIMGPortada[i].onload = function(){
			intNumImatgesCarregades++;
			
			//alert(intNumImatgesCarregades + ' de ' + strSrcImatges.length + ' : ' + this.src);
			if(intNumImatgesCarregades==strSrcImatgesPortada.length){				
				photoWidth=$(PIMGPortada[0]).attr('width');
				photoHeight=$(PIMGPortada[0]).attr('height');
				var fonsWidth=$('#fons').width();
				var fonsHeight=$('#fons').height();
				var newPhotoWidth=photoWidth;
				var newPhotoHeight=photoHeight;
				//if(photoWidth>=fonsWidth){
					newPhotoWidth=fonsWidth;
					newPhotoHeight=(photoHeight*newPhotoWidth)/photoWidth;
				//}
				if(newPhotoHeight>=fonsHeight){
					newPhotoHeight=fonsHeight;
					newPhotoWidth=(photoWidth*newPhotoHeight)/photoHeight;
				}
				$('#fons').fadeOut(666, function(){
					var left=(fonsWidth/2) - newPhotoWidth/2;
					var top=(fonsHeight/2 - newPhotoHeight/2);
					
					strPhoto+='<img id="bg_img" alt="photo" src="'+strSrcImatgesPortada[0]+'"/>';
					$('#fons').html(strPhoto);
					$('#bg_img').width(newPhotoWidth).height(newPhotoHeight).css({'top':top, 'left':left});
					$('#fons').fadeIn(666);
					interval=window.setInterval(imgCoverChange, 1800);
				});
			}
		}
		PIMGPortada[i].src=strSrcImatgesPortada[i];
	}
}

function coverSimple(){
	var strSrcImatges=new Array();
	var PIMG=new Array;
	var strPhoto='';
	
	if(strNumRevista=='09'){
		if(strIdioma=='en'){
			strSrcImatges.push('numeros/' + strNumRevista + '/portada_en.gif');
		}else{
			strSrcImatges.push('numeros/' + strNumRevista + '/portada.gif');
		}
	}else{
		strSrcImatges.push('numeros/' + strNumRevista + '/portada.jpg');
	}
	PIMG[0]=new Image();
	PIMG[0].onload = function(){
		photoWidth=$(this).attr('width');
		photoHeight=$(this).attr('height');
		var fonsWidth=$('#fons').width();
		var fonsHeight=$('#fons').height();
		var newPhotoWidth=photoWidth;
		var newPhotoHeight=photoHeight;
		//if(photoWidth>=fonsWidth){
			newPhotoWidth=fonsWidth;
			newPhotoHeight=(photoHeight*newPhotoWidth)/photoWidth;
		//}
		if(newPhotoHeight>=fonsHeight){
			newPhotoHeight=fonsHeight;
			newPhotoWidth=(photoWidth*newPhotoHeight)/photoHeight;
		}
		$('#fons').fadeOut('slow', function(){
			var left=(fonsWidth/2) - newPhotoWidth/2;
			var top=(fonsHeight/2 - newPhotoHeight/2);
			
			strPhoto+='<img id="bg_img" alt="photo" src="'+strSrcImatges[0]+'"/>';
			$('#fons').html(strPhoto);
			$('#bg_img').width(newPhotoWidth).height(newPhotoHeight).css({'top':top, 'left':left});
			$('#fons').fadeIn('slow');
		});		
	}
	PIMG[0].src=strSrcImatges[0];
}

function resizeImage(){	
	var fonsWidth=$('#fons').width();
	var fonsHeight=$('#fons').height();
	var newPhotoWidth=photoWidth;
	var newPhotoHeight=photoHeight;
	
	//if(photoWidth>=fonsWidth){
		newPhotoWidth=fonsWidth;
		newPhotoHeight=(photoHeight*newPhotoWidth)/photoWidth;
	//}
	if(newPhotoHeight>=fonsHeight){
		newPhotoHeight=fonsHeight;
		newPhotoWidth=(photoWidth*newPhotoHeight)/photoHeight;
	}
	
	var left=(fonsWidth/2) - newPhotoWidth/2;
	var top=(fonsHeight/2 - newPhotoHeight/2);
	$('#bg_img').width(newPhotoWidth).height(newPhotoHeight).css({'top':top, 'left':left});
}

function loadCover(){
	if(strNumRevista=='10'){
		coverMultiple();
	}else{
		coverSimple();
	}
}

window.onresize=function(){
	resizeImage();
}

window.onload=function(){
	
}

$(document).ready(function(){	
	var intNumImatgesCarregades=0;
	var strSrcImatges= new Array();
	var PIMG=new Array;
	
	/*if(strNumRevista=='09'){
		if(strIdioma=='en'){
			strSrcImatges.push('numeros/' + strNumRevista + '/portada_en.gif');
		}else{
			strSrcImatges.push('numeros/' + strNumRevista + '/portada.gif');
		}
	}else if(strNumRevista=='10'){
		if(strIdioma=='en'){
			for(i=1;i<=10;i++){
				strSrcImatges.push('numeros/10/portada/en/' + i + '.jpg');
			}
		}else{
			for(i=1;i<=10;i++){
				strSrcImatges.push('numeros/10/portada/es/' + i + '.jpg');
			}
		}
	}else{
		strSrcImatges.push('numeros/' + strNumRevista + '/portada.jpg');
	}*/
	
	
	strSrcImatges.push('img/menu_fons_dalt.gif');
	strSrcImatges.push('img/menu_fons_baix.gif');
	strSrcImatges.push('img/idioma_fons.gif');
	strSrcImatges.push('img/peu_foto_fons.gif');	
	strSrcImatges.push('img/menu_portada_titol_ca.gif');
	strSrcImatges.push('img/menu_portada_titol_en.gif');
	strSrcImatges.push('img/menu_portada_titol_es.gif');
	strSrcImatges.push('img/menu_report.gif');
	strSrcImatges.push('img/control_esquerra.gif');
	strSrcImatges.push('img/control_esquerra_on.gif');
	strSrcImatges.push('img/control_dret.gif');
	strSrcImatges.push('img/control_dret_on.gif');
	strSrcImatges.push('img/generalitat.gif');
	strSrcImatges.push('img/logo_facebook.png');
	strSrcImatges.push('img/logo_twitter.png');
		
	for(var i=0;i<strSrcImatges.length;i++){
		PIMG[i]=new Image();
		PIMG[i].onload = function(){
			intNumImatgesCarregades++;
			//alert(intNumImatgesCarregades + ' de ' + strSrcImatges.length + ' : ' + this.src);
			if(intNumImatgesCarregades==strSrcImatges.length){
				//$('#fons').html('<img id="bg_img" alt="photo"/>');
				//$('#fons > img').attr('src',strSrcImatges[0]);				
				//$('#fons').fadeIn('slow');
				$('#sub_menu_subscripcio').fadeIn('slow');
				$('#sub_menu_barra_superior').fadeIn('slow', function(){
					delete strSrcImatges;
					delete PIMG;
					/*if(strNumRevista=='10'){
						interval=window.setInterval(imgCoverChange, 3000);
					}*/
				});
				loadCover();
				iniciarEvents();
			}
		}
		PIMG[i].src=strSrcImatges[i];
	}
});
