/**
 * @author r
 */

//funcions de càrrega a l'inici
	$(document).ready(function(){		
			seccions('banner', 'pags/banner_cat.php');
			seccions('menu', 'pags/menu_cat.php');
			seccions('menu_prod', 'pags/menu_prod.php');
			seccions('stage', 'pags/produccions.php');
			unFocus.History.addHistory('produccions');		 
	});


//funcio de navegacio principal
	function seccions(div, pag)
	{
		divdesti ="#"+div;
		$(divdesti).load(pag);
	}

//funció per a obrir produccions directament del menu lateral dret
function getProd(id)
{
	idProd = id;
	switch (idiomaWeb)
	{
		case "cat":
		seccions('stage', 'pags/produccions.php');
		$.scrollTo( {top:'+=400px'}, 200);
		unFocus.History.addHistory('produccions');
		break;
		case 'cast':
		seccions ('stage', 'pags/producciones.php');	
		$.scrollTo( {top:'+=400px'}, 200);
		unFocus.History.addHistory('producciones');
		break;
		case 'eng':
		seccions ('stage', 'pags/productions.php');	
		$.scrollTo( {top:'+=400px'}, 200);
		unFocus.History.addHistory('productions');
		break;
		default:
		seccions('stage', 'pags/produccions.php');
		$.scrollTo( {top:'+=400px'}, 200);
		unFocus.History.addHistory('produccions');
		break;
	}
}

//funció per a carregar producció individual de l'històric directament del menu lateral dret
function getProdInd(id)
{
	idProd = id;
	switch (idiomaWeb)
	{
		case "cat":
		seccions('stage', 'pags/produccio_ind.php?id='+idProd);
		unFocus.History.addHistory('produccio');
		break;
		case 'cast':
		seccions ('stage', 'pags/produccion_ind.php?id='+idProd);	
		unFocus.History.addHistory('produccion');
		break;
		case 'eng':
		seccions ('stage', 'pags/production_ind.php?id='+idProd);	
		unFocus.History.addHistory('production');
		break;
		default:
		seccions('stage', 'pags/produccio_ind.php?id='+idProd);
		unFocus.History.addHistory('produccio');
		break;
	}
}

/* 
   ***********************************
   ******* funcions d'idiomes ********
   ***********************************
*/

//efectes over i onFocus del menu d'idioma
function setMenuIdioma(lang)
{
	$('#idioma a#' + lang).css({
		'color': '#d1d0ca'
	});
	$('#idioma a:not(#' + lang + ')').css({
		'color': '#8b897a',
	});
	$('#idioma a#' + lang).mouseover(function(){
		$(this).css({
			'color': '#d1d0ca',
		});
	});
	$('#idioma a#' + lang).mouseout(function(){
		$(this).css({
			'color': '#d1d0ca',
			});
	});
	$('#idioma a:not(#' + lang + ')').mouseover(function(){
		$(this).css({
			'color': '#d1d0ca',
		});
	});
	$('#idioma a:not(#' + lang + ')').mouseout(function(){
		$(this).css({
			'color': '#8b897a',
			});
	});
}

//funció per a gestionar el canvi d'idioma
function idioma(lang)
{
	idiomaWeb=lang;
	seccions('banner','pags/banner_' + idiomaWeb + '.php');
	//seccions('dreta','pags/menu2.php?lang='+idiomaWeb);
	setMenuIdioma(lang);
	seccions('menu','pags/menu_'+ idiomaWeb +'.php');
	var sec= unFocus.History.getCurrent();
	switch (sec)
	{
		case 'bcn216_cat':
		if(lang=="cast")
		{
			seccions('stage','pags/bcn216_cast.php');
			unFocus.History.addHistory('bcn216_cast');
		}
		
		else if(lang=="eng")
		{
			seccions('stage','pags/bcn216_eng.php');
			unFocus.History.addHistory('bcn216_eng');
		}
		break;
		
		case 'bcn216_cast':
		if(lang=="cat")
		{
			seccions('stage','pags/bcn216_cat.php');
			unFocus.History.addHistory('bcn216_cat');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/bcn216_eng.php');
			unFocus.History.addHistory('bcn216_eng');
		}
		break;
		
		case 'bcn216_eng':
		if(lang=="cat")
		{
			seccions('stage','pags/bcn216_cat.php');
			unFocus.History.addHistory('bcn216_cat');
		}
		else if(lang=="cast")
		{
			seccions('stage','pags/bcn216_cast.php');
			unFocus.History.addHistory('bcn216_cast');
		}
		break;
		
		case 'martinez-izquierdo_cat':
		if(lang=="cast")
		{
			seccions('stage','pags/martinez-izquierdo_cast.php');
			unFocus.History.addHistory('martinez-izquierdo_cast');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/bcn216_eng.php');
			unFocus.History.addHistory('bcn216_eng');
		}
		break;
		
		case 'martinez-izquierdo_cast':
		if(lang=="cat")
		{
			seccions('stage','pags/martinez-izquierdo_cat.php');
			unFocus.History.addHistory('martinez-izquierdo_cat');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/bcn216_eng.php');
			unFocus.History.addHistory('bcn216_eng');
		}
		break;
		
		case 'davidalbet_cat':
		if(lang=="cast")
		{
			seccions('stage','pags/davidalbet_cast.php');
			unFocus.History.addHistory('davidalbet_cast');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/bcn216_eng.php');
			unFocus.History.addHistory('bcn216_eng');
		}
		break;
		
		case 'davidalbet_cast':
		if(lang=="cat")
		{
			seccions('stage','pags/davidalbet_cat.php');
			unFocus.History.addHistory('davidalbet_cat');
		}		
		else if(lang=="eng")
		{
			seccions('stage','pags/bcn216_eng.php');
			unFocus.History.addHistory('bcn216_eng');
		}
		break;
		
		case 'produccions':
		if(lang=="cast")
		{
			seccions('stage','pags/producciones.php');
			unFocus.History.addHistory('producciones');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/productions.php');
			unFocus.History.addHistory('productions');
		}
		break;
		
		case 'producciones':
		if(lang=="cat")
		{
			seccions('stage','pags/produccions.php');
			unFocus.History.addHistory('produccions');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/productions.php');
			unFocus.History.addHistory('productions');
		}
		break;
		
		case 'productions':
		if(lang=="cat")
		{
			seccions('stage','pags/produccions.php');
			unFocus.History.addHistory('produccions');
		}
		else if(lang=="cast")
		{
			seccions('stage','pags/producciones.php');
			unFocus.History.addHistory('producciones');
		}
		break;		
		
		case 'produccio':
		if(lang=="cast")
		{
			seccions('stage','pags/producciones.php');
			unFocus.History.addHistory('producciones');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/productions.php');
			unFocus.History.addHistory('productions');
		}
		break;
		
		case 'produccion':
		if(lang=="cat")
		{
			seccions('stage','pags/produccions.php');
			unFocus.History.addHistory('produccions');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/productions.php');
			unFocus.History.addHistory('productions');
		}
		break;
		
		case 'production':
		if(lang=="cat")
		{
			seccions('stage','pags/produccions.php');
			unFocus.History.addHistory('produccions');
		}
		else if(lang=="cast")
		{
			seccions('stage','pags/producciones.php');
			unFocus.History.addHistory('producciones');
		}
		break;
		
		case 'contacte':
		if(lang=="cast")
		{
			seccions('stage','pags/contacto.php');
			unFocus.History.addHistory('contacto');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/contact.php');
			unFocus.History.addHistory('contact');
		}
		break;
		
		case 'contacto':
		if(lang=="cat")
		{
			seccions('stage','pags/contacte.php');
			unFocus.History.addHistory('contacte');
		}
		else if(lang=="eng")
		{
			seccions('stage','pags/contact.php');
			unFocus.History.addHistory('contact');
		}
		break;
		
		case 'contact':
		if(lang=="cat")
		{
			seccions('stage','pags/contacte.php');
			unFocus.History.addHistory('contacte');
		}
		else if(lang=="cast")
		{
			seccions('stage','pags/contacto.php');
			unFocus.History.addHistory('contacto');
		}
		break;
		
		default:
		seccions('stage','pags/produccions.php');
		unFocus.History.addHistory('produccions');
		break;
	}	 
}


//enviament info formulari per mail
function enviamail()
  {
  	//recollim dades del formulari
  	valors="";
	for(var i=0; i< document.form.elements.length; i++)
	{
		valors +=document.form.elements[i].name + "=" + escape(document.form.elements[i].value) + "&";
	}

	$.post("pags/process_form.php", valors, function(datos){
		$("#form").reset();
		$("#contacte").html(datos);
		})	
		//return false;
  } 
 
//funcio per a resetejar formularis [$('#formulari').reset();]
	jQuery.fn.reset = function () {
  	$(this).each (function() { this.reset(); });
}
 
//Unfocus History (plugin per a historial navegador)
function PromptMe() {
	// some application vars
	var stateVar = "nothin'", displayDiv = document.getElementById("stage");
	
	// the sole public method to manipulate this application
	this.promtForNew = function() {
		// do the stuff that changes the state
		var newVal = window.prompt("Please enter some value to store in the history and url");
		// Set the new history hash. This value must be a string, so serializtion is up to you.
		// JSON works nicely, if you need something quick: http://www.json.org
		// When a new entry is made, unFocus.History will notify the historyListener 
		// method of the change, even on the first call. For this demo app we can 
		// rely on the historyListener method to update the state, and view.
		unFocus.History.addHistory(newVal);
	};
	
	// This is the method that will recieve notifications from the History Keeper,
	// which will then update the state of the app.
	// :NOTE: This will be called when a new entry is added.
	// :NOTE: This will be called if another app on the page sets a history,
	//        so you will need to watch out for this, if you have more than
	//        one script using the history keeper, by doing some kind of 
	//        check to make sure that the new hash belongs to this app.
	//        I wouldn't expect this to be a problem all that frequently.
	this.historyListener = function(historyHash) {
		// update the stateVar
		stateVar = historyHash;
		// update display content
		if(historyHash){			
			seccions('stage',"pags/"+ historyHash+".php");
		} else {
			seccions('stage',"pags/produccions.php");			
		}
	};
	// subscribe to unFocus.History
	unFocus.History.addEventListener('historyChange', this.historyListener);
	
	// Check for an initial value (deep link).
	// In this demo app, the historyListener can handle the task.
	this.historyListener(unFocus.History.getCurrent());
};
// instantiate and inialize the app. DOM has to be ready so we can get a ref to
// the HistoryState DOM element (aka the view), so we use QuickLoader to make
// sure it's ready.
var BCN216Nav;