/* 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;

//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 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);	
	}else{
		strSrc='numeros/' + strNumRevista + '/' + strReportatge + '/portada.jpg';
	}
	objImg.onload = function(){
		$('#fons > img').fadeOut('normal',function(){
			$('#fons > img').removeAttr('src').attr('src',strSrc);
			$('#fons >img').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 '99':
			switch(strReportatge){
				case 'report1':
					intFotos=99;
					intTipus=FOTO;
				break;
				case 'report2':
					intFotos=99;
					intTipus=FOTO;
				break;
				case 'report3':
					intFotos=99;
					intTipus=FOTO;
				break;
				case 'report4':
					intFotos=99;
					intTipus=FOTO;
				break;
				case 'report5':
					//pagines de la entrevista
					intFotos=99;
					intTipus=ENTREVISTA;
				break;
				case 'report6':
					intFotos=1;
					intTipus=VIDEO;
				break;
			}		
		break;
	}
	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;
	}	
}

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');
	});
}

window.onload=function(){

}

$(document).ready(function(){	
	var intNumImatgesCarregades=0;
	var strSrcImatges= new Array();
	var PIMG=new Array;
	
	strSrcImatges[0]='numeros/' + strNumRevista + '/portada.jpg';
	strSrcImatges[1]='img/menu_fons_dalt.gif';
	strSrcImatges[2]='img/menu_fons_baix.gif';
	strSrcImatges[3]='img/idioma_fons.gif';
	strSrcImatges[4]='img/peu_foto_fons.gif';	
	strSrcImatges[5]='img/menu_portada_titol_ca.gif';
	strSrcImatges[6]='img/menu_portada_titol_en.gif';
	strSrcImatges[7]='img/menu_portada_titol_es.gif';
	strSrcImatges[8]='img/menu_report.gif';
	strSrcImatges[9]='img/control_esquerra.gif';
	strSrcImatges[10]='img/control_esquerra_on.gif';
	strSrcImatges[11]='img/control_dret.gif';
	strSrcImatges[12]='img/control_dret_on.gif';
	strSrcImatges[13]='img/generalitat.gif';
		
	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 > img').attr('src',strSrcImatges[0]);				
				$('#fons').fadeIn('slow');
				$('#sub_menu_subscripcio').fadeIn('slow');
				$('#sub_menu_barra_superior').fadeIn('slow', function(){
					delete strSrcImatges;
					delete PIMG;		
				});
				iniciarEvents();
			}
		}
		PIMG[i].src=strSrcImatges[i];
	}
});