// € JavaScript Document

var buttonsOn = new Array( 'contacta_2.png', 'disseny_2.png', 'encarrec_2.png', 'inici_2.png', 'propies_2.png', 'publicacions_2.png', 'queFem_2.png' );
var buttonsOff = new Array( 'contacta_1.png', 'disseny_1.png', 'encarrec_1.png', 'inici_1.png', 'propies_1.png', 'publicacions_1.png', 'queFem_1.png' );
var logos = new Array( 'inici/logo.jpg', 'quefem/logo.jpg', 'publicacions/logo.jpg', 'disseny/logo.jpg', 'contacta/logo.jpg' );

var inSectionIndex = 0;

var pPropies = new Array( 'ppropies/tempsDeViure', 'ppropies/catalunyaSport', 'ppropies/gentDelBarri' );
var pPropiesLink = new Array( 'http://www.tempsdeviure.lesrevistes.cat', '', '' );

var pEncarrec = new Array( 'pencarrec/vivimJunts', 'pencarrec/codroper', 'pencarrec/viaFora', 'pencarrec/laMunicipal', 'pencarrec/newsMarketSport', 'pencarrec/seleccionat' );
var pEncarrecLink = new Array( '', '', '', '', '', '' );

var disseny = new Array( 'disseny/D-ClinicaDental', 'disseny/D-CongostAutisme', 'disseny/D-FC-Barcelona-DREAMTEAM', 'disseny/D-FormularisJuridics', 'disseny/D-INTERSINDICAL', 'disseny/D-MARKET-SPORT', 'disseny/D-PlanetFutbol', 'disseny/D-PLATAFORMA-PROSELECC', 'disseny/D-Secretaria-General', 'disseny/D-Tibidabo', 'disseny/D-VIA-FORA', 'disseny/D-El-Setial' );
var dissenyLink = new Array( '', '', '', '', '', '', '', '', '', '', '', '' );

function init(){
	// Inicialitzar imatges

	for( var i = 0 ; i < buttonsOn.length ; i++ ){
		var im = new Image();
		im.src = '/imatges/' + buttonsOn[i];
	}
	for( var i = 0 ; i < buttonsOff.length ; i++ ){
		var im = new Image();
		im.src = '/imatges/' + buttonsOff[i];
	}
	for( var i = 0 ; i < logos.length ; i++ ){
		var im = new Image();
		im.src = '/seccions/' + logos[i];
	}
	for( var i = 1 ; i <= 12 ; i++ ){
		var im = new Image();
		im.src = '/seccions/disseny/' + i + '-ROJO.png';
	}
	
	document.getElementById( 'navbuts1' ).style.backgroundImage = 'url( /imagenes/inici_2.png )';
	
	getSection( 'inici' );
	setLink( pPropiesLink, 0 );
}

function getSection( section, subsection ){
	//alert( section );
	
	inSectionIndex = 0;
	
	// Reiniciem tots els botons
	document.getElementById( 'navbuts1' ).style.backgroundImage = '';
	document.getElementById( 'navbuts2' ).style.backgroundImage = '';
	document.getElementById( 'navbuts3' ).style.backgroundImage = '';
	document.getElementById( 'navbuts4' ).style.backgroundImage = '';
	document.getElementById( 'navbuts5' ).style.backgroundImage = '';
	document.getElementById( 'navbuts6' ).style.backgroundImage = '';
	document.getElementById( 'navbuts7' ).style.backgroundImage = '';
	
	var temp = document.getElementById( 'layer2Middle' );
	
	// Canviem el logotip
	switch( section ){
		case 'inici':
			temp.style.backgroundImage = 'url( /seccions/inici/logo.jpg )';
			document.getElementById( 'navbuts1' ).style.backgroundImage = 'url( /imatges/inici_2.png )';
			break;
			
		case 'quefem':
			temp.style.backgroundImage = 'url( /seccions/quefem/logo.jpg )';
			document.getElementById( 'navbuts2' ).style.backgroundImage = 'url( /imatges/queFem_2.png )';
			break;
		
		case 'publicacions':
			temp.style.backgroundImage = 'url( /seccions/publicacions/logo.jpg )';
			document.getElementById( 'navbuts3' ).style.backgroundImage = 'url( /imatges/publicacions_2.png )';
			break;
		case 'ppropies':
			temp.style.backgroundImage = 'url( /seccions/publicacions/logo.jpg )';
			document.getElementById( 'navbuts4' ).style.backgroundImage = 'url( /imatges/propies_2.png )';
			if( !subsection ) var subsection = 'tempsDeViure';
			break;
		case 'pencarrec':
			temp.style.backgroundImage = 'url( /seccions/publicacions/logo.jpg )';
			document.getElementById( 'navbuts5' ).style.backgroundImage = 'url( /imatges/encarrec_2.png )';
			if( !subsection ) var subsection = 'vivimJunts';
			break;
		case 'disseny':
			temp.style.backgroundImage = 'url( /seccions/disseny/logo.jpg )';
			document.getElementById( 'navbuts6' ).style.backgroundImage = 'url( /imatges/disseny_2.png )';
			break;
		case 'contacta':
			temp.style.backgroundImage = 'url( /seccions/contacta/logo.jpg )';
			document.getElementById( 'navbuts7' ).style.backgroundImage = 'url( /imatges/contacta_2.png )';
			break;
	}
	

	
	// Carreguem la secció
	
	
	var lw = new loadWrapper();
	lw.setCallback( catchResponse );
	lw.setURL( '/seccions/' + section + '/base.html' );
	//lw.setMode( 'xml' );
	lw.gogogo();

	function catchResponse( data, url, status ){
		//alert( data + ' ' + url + ' ' + status );
		if( !data ) return;
		
		var obj = document.getElementById( 'contentBox' );
		obj.innerHTML = data;
		//alert( section + ' ' + subsection );
		
		switch( section ){
			case 'ppropies':
				if( subsection ){
					for( var i = 0 ; i < pPropies.length ; i++ ){
						if( 'ppropies/' + subsection == pPropies[i] ){
							inSectionIndex = i;
							setSec( pPropies );
						}
					}
				}
				break;
			case 'pencarrec':
				if( subsection ){
					for( var i = 0 ; i < pEncarrec.length ; i++ ){
						if( 'pencarrec/' + subsection == pEncarrec[i] ){
							inSectionIndex = i;
							setSec( pEncarrec );
						}
					}
				}
				break;
			case 'disseny':
				dissenyChangeSection( subsection );
				
				
				//alert( document.getElementById( 'dissbuts1' ) );

			}
	}
}

function pPropiesNext(){
	nexSec( pPropies );
	setLink( pPropiesLink );
}
function pPropiesPrev(){
	prevSec( pPropies );
	setLink( pPropiesLink );
}

function pEncarrecNext(){
	nexSec( pEncarrec );
	setLink( pEncarrecLink );
}
function pEncarrecPrev(){
	prevSec( pEncarrec );
	setLink( pEncarrecLink );
}

function dissenyNext(){
	nexSecDisseny( disseny );
	setLink(dissenyLink );
}
function dissenyPrev(){
	prevSecDisseny( disseny );
	setLink( dissenyLink );
}
function dissenySec( index ){
	inSectionIndex = index;
	setSecDisseny( disseny );
}

function setSec( nomSecs ){
	
	//inSectionIndex++;
	//inSectionIndex = inSectionIndex % nomSecs.length;

	var nextSec = nomSecs[inSectionIndex];

	var temp = document.getElementById( 'lZone' );
	temp.src = '/seccions/' + nextSec + '-l.jpg';

	var temp = document.getElementById( 'rZone' );
	temp.src = '/seccions/' + nextSec + '-r.png';
}

function setSecDisseny( nomSecs ){
	var nextSec = nomSecs[inSectionIndex];
	var temp = document.getElementById( 'lZone' );
	temp.src = '/seccions/' + nextSec + '.jpg';

	//var temp = document.getElementById( 'rZone' );
	//temp.src = '/seccions/' + nextSec + '.png';
	document.getElementById( 'dissbuts1' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts2' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts3' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts4' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts5' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts6' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts7' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts8' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts9' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts10' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts11' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts12' ).style.backgroundImage = '';
	//alert( inSectionIndex );
	var aux = parseInt( inSectionIndex ) + 1;
	//alert( aux );
	if( document.getElementById( 'dissbuts' + aux ) ){
		document.getElementById( 'dissbuts' + aux ).style.backgroundImage = 'url( /seccions/disseny/' + aux + '-ROJO.png )';
	} else {
		document.getElementById( 'dissbuts1' ).style.backgroundImage = 'url( /seccions/disseny/1-ROJO.png )';
	}
}

function nexSec( nomSecs ){
	inSectionIndex++;
	inSectionIndex = inSectionIndex % nomSecs.length;
	setSec( nomSecs );
}

function prevSec( nomSecs ){
	inSectionIndex--;
	if( inSectionIndex < 0 ) inSectionIndex = nomSecs.length - 1;
	setSec( nomSecs );
}

function nexSecDisseny( nomSecs ){
	inSectionIndex++;
	inSectionIndex = inSectionIndex % nomSecs.length;
	setSecDisseny( nomSecs );
	dissenyChangeSection( inSectionIndex + 1 );
}
function prevSecDisseny( nomSecs ){
	inSectionIndex--;
	if( inSectionIndex < 0 ) inSectionIndex = nomSecs.length - 1;
	setSecDisseny( nomSecs );
	dissenyChangeSection( inSectionIndex + 1 );
}

function setLink( linksArray ){
	var temp = document.getElementById( 'linkImatge' );
	if( !temp ) return;
	if( linksArray[inSectionIndex] && linksArray[inSectionIndex] != '' ){
		temp.style.display = 'block';
		temp.onclick = function(){ window.open( linksArray[inSectionIndex] ); }
	} else {
		temp.style.display = 'none';
		temp.onclick = '';
	}
}

function enviarFormulari(){
	var nom = document.getElementById( 'nom' );
	var empresa = document.getElementById( 'empresa' );
	var missatge = document.getElementById( 'missatge' );
	var eMailCos = 'Nom: ' + nom.value + '<br/>\nEmpresa: ' + empresa.value + '<br/>\nMissatge:<br/>\n' + missatge.value;
	window.open( 'mailto:monmedia@monmedia.net?subject=Formulari de concate de la web.&body=' + eMailCos );
}


function dissenyChangeSection( subsection ){
	/*
	for( var i = 0 ; i < disseny.length ; i++ ){
		if( 'disseny/' + subsection == disseny[i] ){
			inSectionIndex = i;
			setSecDisseny( disseny );
		}
	}
	*/
	
	// Reiniciem tots els botons
	document.getElementById( 'dissbuts1' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts2' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts3' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts4' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts5' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts6' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts7' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts8' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts9' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts10' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts11' ).style.backgroundImage = '';
	document.getElementById( 'dissbuts12' ).style.backgroundImage = '';
	
	if( subsection ){
		document.getElementById( 'dissbuts' + subsection ).style.backgroundImage = 'url( /seccions/disseny/' + subsection + '-ROJO.png )';
		/*
		if( disseny[inSectionIndex-1] ){
			alert( inSectionIndex + ' ' + disseny[inSectionIndex-1] );
			setSecDisseny( disseny );
		}
		*/
	} else {
		document.getElementById( 'dissbuts1' ).style.backgroundImage = 'url( /seccions/disseny/1-ROJO.png )';
	}
}

