/* Ruben posa aixó a 1 per a que funcione el banner */
var funciona_el_banner = 0;

var idioma = 'ca';
var domini = "nicanor.cat";
var catLoaded = 0;
var cat2Loaded = 0;
miFecha = new Date()
diaSemana = miFecha.getDay()-1;
var vIMGsCarrusel = Array();
var imatgeActiva = 4;
var vMonth = Array("Gener", "Febrer", "Març", "Abril", "Maig", "Juny", "Juliol", "Agost", "Setembre", "Octubre", "Novembre", "Desembre");
var globNod = Array();
var proxy = "proxy.php";


window.onresize = function()
	{
	resizeContenidor();
	}
function resizeContenidor()
	{
	var wW = document.documentElement.clientWidth;
	var hW = document.documentElement.clientHeight;
	var wC = $("contenidor").offsetWidth;
	var hC = $("contenidor").offsetHeight;
	
	if(wW < wC)
		{
		$("contenidor").style.left = "0px";
		$("contenidor").style.marginLeft = "0px";
		$("contenidor").style.top = "0px";
		$("contenidor").style.marginTop = "0px";
		}
	else
		{
		$("contenidor").style.left = "50%";
		$("contenidor").style.marginLeft = "-492.5px";
		$("contenidor").style.top = "50%";
		$("contenidor").style.marginTop = "-295px";
		}
	}



//[ Precarrega d'imatges]//

var img = new Image();
img.src = "images/spinner.gif";

function showPage(idp)
	{
	var capas = Array("0", "1", "2", "3", "4", "5");

	for(var i=0; i<capas.length; i++)
		{
		if(capas[i]!="5")
			{
			document.getElementById("menu_"+capas[i]).className = 'menu';
			}
		else
			{
			document.getElementById("menu_"+capas[i]).className = 'noticies';
			}
		document.getElementById("ctl_"+capas[i]).style.display = 'none';
		}
	document.getElementById("ctl_"+idp).style.display='block';
	if(idp!=5)
		{
		document.getElementById("menu_"+idp).className = 'menuSelected';
		}
	else
		{
		document.getElementById("menu_"+idp).className = 'noticies';
		}
	if($('mapa').style.display!='none')
		{
		hideMapa();
		}
	}
	
function finestra (url,nomfinestra,parametres) 
	{
	w=window.open(url,nomfinestra,parametres);
	}

function carrusel()
	{
	var activa = 'a';
	var inactiva = 'b';
	if(document.getElementById("img1b").style.display != 'none')
		{
		activa = 'b';
		inactiva = 'a';
		}

	var img = document.getElementById("img1"+activa).src;
	document.getElementById("img1"+inactiva).src = document.getElementById("img2"+activa).src;
	document.getElementById("img2"+inactiva).src = document.getElementById("img3"+activa).src;
	document.getElementById("img3"+inactiva).src = document.getElementById("img4"+activa).src;
	document.getElementById("img4"+inactiva).src = vIMGsCarrusel[imatgeActiva];
	
	imatgeActiva++;
	if((imatgeActiva+1)>vIMGsCarrusel.length)
		{
		imatgeActiva = 0;
		}
	
	showHide("img1a");
	showHide("img1b");
	showHide("img2a");
	showHide("img2b");
	showHide("img3a");
	showHide("img3b");
	showHide("img4a");
	showHide("img4b");
	
	setTimeout("carrusel()", 2000);
	}
function iniciaCarrusel()
	{
	if(funciona_el_banner == 1) {
		showPubli();
	}
	setTimeout("carrusel()", 3000);
	}


function showHide(id)
	{
	OpenClose(id, "Appear");
	}



function OpenClose(id,effect)
{

	if(effect == "") effect = "Appear";
	switch(effect)
	{
		case "Blind":
			if($(id).style.display == "none")
				new Effect.BlindDown(id);
			else	
				new Effect.BlindUp(id);
		break;
		case "Appear":
			if($(id).style.display == "none")
				new Effect.Appear(id);
			else	
				new Effect.Fade(id);
		
		break;
	}
}




function myObj(){}
myObj.prototype = new sma_oCategoria();
var myProducts = Array()
var myProducts2 = Array()
var myProducts3 = Array()

function iniciaRotator()
	{
	if(!myProducts2[idioma])
		{
		myProducts2[idioma] = new myObj();
		myProducts2[idioma].vCategories.length = 0;
		myProducts2[idioma].setCategoria(137);
		myProducts2[idioma].setDomini(domini);
		myProducts2[idioma].setProxy(proxy);
		myProducts2[idioma].setIdioma(idioma);
		myProducts2[idioma].setProcessador("endLoading2()");
		myProducts2[idioma].init();
		}
	}

function endLoading2(conexions2)
	{
	if(conexions2 == 0)
		{
		var vIMG = myProducts2[idioma].vProductes[0].vIMG;

		for(var i=0; i<vIMG.length; i++)
			{
			vIMGsCarrusel[i] = "http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+vIMG[i].url+"&w=150&h=92";
			}
		var urlTMP = "http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+vIMG[3].url+"&w=150&h=92";

		document.getElementById("img1a").src = vIMGsCarrusel[0];
		document.getElementById("img1b").src = vIMGsCarrusel[1];
		document.getElementById("img2a").src = vIMGsCarrusel[1];
		document.getElementById("img2b").src = vIMGsCarrusel[2];
		document.getElementById("img3a").src = vIMGsCarrusel[2];
		document.getElementById("img3b").src = vIMGsCarrusel[3];
		document.getElementById("img4a").src = vIMGsCarrusel[3];
		document.getElementById("img4b").src = vIMGsCarrusel[4];

		$("slogan").innerHTML = myProducts2[idioma].vProductes[0].descripcio;
	
		getNoticies();
		}
	}

function getNoticies()
	{
	if(!myProducts3[idioma])
		{
		myProducts3[idioma] = new myObj();
		myProducts3[idioma].vCategories.length = 0;
		myProducts3[idioma].setCategoria(366);
		myProducts3[idioma].setDomini(domini);
		myProducts3[idioma].setProxy(proxy);
		myProducts3[idioma].setIdioma(idioma);
		myProducts3[idioma].setProcessador("endLoading3()");
		myProducts3[idioma].init();
		}
	}

function endLoading3(conexions3)
	{
	var sw = 0;
	if(conexions3 == 0)
		{
		$('botoneraNoticies').innerHTML = '';
		var vPRD = myProducts3[idioma].vProductes;
		for(var i=0; i<vPRD.length; i++)
			{
			var obj = document.createElement("div");
			//obj.type = 'button';
			obj.className = 'button2';
			if(sw == 0)
				{ 
				obj.className = 'button1'; 
				sw = 1; 
				setNoticia(vPRD[i], obj);
				}
			obj.node = vPRD[i];
			obj.onclick = function()
				{
				setNoticia(this.node, this);
				}
			obj.innerHTML = vPRD[i].nom;
			$('botoneraNoticies').appendChild(obj);
			}
		downloadData();
		}
	}

function downloadData()
	{
	if(!myProducts[idioma])
		{
		myProducts[idioma] = new myObj();
		myProducts[idioma].vCategories.length = 0;
		myProducts[idioma].setCategoria(113);
		myProducts[idioma].setDomini(domini);
		myProducts[idioma].setProxy(proxy);
		myProducts[idioma].setIdioma(idioma);
		myProducts[idioma].setProcessador("endLoading()");
		myProducts[idioma].setAutoLoad(1);
		myProducts[idioma].init();
		}
	}

//[ PROCESSA EL FINAL DEL PROCESSAMENT DE DADES ]//
function endLoading(conexions)
	{
	//$("debug").innerHTML += myProducts[idioma].vCategories.length;
	if(myProducts[idioma].vCategories.length>0 && catLoaded == 0)
		{//[ Si ya se han cargado las categorias ]//
		catLoaded = 1;
		//[ Carreguem les categories principals ]//
		loadCategoriesPrincipals(myProducts[idioma].vCategories);
		}
/*
	if(conexions == 0)
		{
		new Effect.Appear("menuDiari");
		new Effect.Fade("loadingMenu");

		//$('loadingMenu').style.display = 'none';
		//$('menuDiari').style.display = 'block';

		if(cat2Loaded == 0)
			{
			cat2Loaded = 1;
			loadSubCats(myProducts[idioma].vCategories[diaSemana]);
			}
		}
*/
	
	}

function loadCategoriesPrincipals(vCAT)
	{
	$('diesSetmana').innerHTML = '';
	for(var i=0;i<vCAT.length;i++)
		{
		var aux = document.createElement("input");
		aux.type = "button";
		aux.titlePrd = vCAT[i].nom;
		aux.descriptionPrd = vCAT[i].descripcio;
		if(vCAT[i].url != '')
			{
			aux.prdImg = "http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+vCAT[i].url+"&w=350&h=185";
			}
		else
			{
			aux.prdImg = "images/plat.jpg";
			}

		if(i==diaSemana)
			{
			aux.className = "button1";
			if(idioma == 'ca')
				{
				aux.value = "Avui "+vCAT[i].nom;
				loadSubCats(vCAT[i]);
				}
			else
				{
				aux.value = "Hoy "+vCAT[i].nom;
				}

			var aquel_dia = new Date ();
			aux.data = aquel_dia.getDate()+" "+vMonth[aquel_dia.getMonth()]+" "+aquel_dia.getFullYear();
			document.getElementById("data").innerHTML = aux.data;
			
			$('titlePrd').innerHTML = aux.titlePrd;
			$('descriptionPrd').innerHTML = aux.descriptionPrd;
			$('prdImg').src = aux.prdImg;
			}
		else
			{
			aux.className = "button2";
			aux.value = vCAT[i].nom;
			
			var aquel_dia = new Date (miFecha.getFullYear(), miFecha.getMonth(), (miFecha.getDate()-(diaSemana-i)));
			aux.data = aquel_dia.getDate()+" "+vMonth[aquel_dia.getMonth()]+" "+aquel_dia.getFullYear();
			}
		aux.id = 'cPri_'+vCAT[i].idCategoria;
		aux.node = vCAT[i];
		aux.weekDay = i;
		
		aux.onclick = function()
			{
			if(this.weekDay == 5 || this.weekDay == 6){$('reallyPreu').innerHTML = "18";}
			else {$('reallyPreu').innerHTML = "10";}
			loadSubCats(this.node)
			$('data').innerHTML = this.data;
			$('titlePrd').innerHTML = this.titlePrd;
			$('descriptionPrd').innerHTML = this.descriptionPrd;
			$('prdImg').src = this.prdImg;
			};
		
		$('diesSetmana').appendChild(aux);
		}
	}
function loadSubCats(node)
	{
	if(!globNod[node.idCategoria])
		{
		$('loadingMenu').style.display = 'block';
		$('menuDiari').style.display = 'none';

		globNod[node.idCategoria] = node;
		node.setProcessador("processaSubCats("+node.idCategoria+")");
		node.setAutoLoad(-1);
		node.init();
		}
	else
		{
		processaSubCats(node.idCategoria);
		}
	}
function processaSubCats(nodeId, conexions)
	{
	if(conexions == 0)
		{
		$('loadingMenu').style.display = 'none';
		$('menuDiari').style.display = 'block';
		}

	var node = globNod[nodeId];
	var vCAT = node.vCategories;
	if(vCAT.length > 0)
		{
		var cnt = $('menuDiari');
		cnt.innerHTML = '';
		
		for(var i=0;i<vCAT.length;i++)
			{
			var aux = document.createElement("li");
			aux.innerHTML = vCAT[i].nom;
			aux.className = "title";
			cnt.appendChild(aux);
	
			var vPRD = vCAT[i].vProductes;
			for(var j=0;j<vPRD.length;j++)
				{
				var aux = document.createElement("li");
				aux.id = "li_"+vPRD[j].idProducte;
				if(j==0 && i==0){aux.className = "contentSelected";}
				else{aux.className = "content";}
				aux.innerHTML = vPRD[j].nom;
				aux.node = vPRD[j];
				aux.onclick = function(){loadProducte(this.node)};
				
				cnt.appendChild(aux);
				}
			
			var aux = document.createElement("li");
			aux.innerHTML = "&nbsp;";
			cnt.appendChild(aux);
			}
		cnt.appendChild(aux);
		}


	selectMenu('cPri_'+node.idCategoria);
	}


function loadProducte(node)
	{
	if(node)
		{
		selectSubMenu("li_"+node.idProducte);
		$('titlePrd').innerHTML = node.nom;
		$('descriptionPrd').innerHTML = node.descripcio;
		if(node.vIMG.length>0)
			{
			$('prdImg').src = "http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+node.vIMG[0].url+"&w=350&h=185";
			}
		else
			{
			$('prdImg').src = "images/plat.jpg";
			}
		}
	}

function selectMenu(id)
	{
	var vInp = $('diesSetmana').getElementsByTagName("input");
	for(var i=0;i<vInp.length;i++)
		{
		if(vInp[i].className == 'button1'){vInp[i].className = 'button2';}
		}
	$(id).className = 'button1';
	}

function selectSubMenu(id)
	{
	var vInp = $('menuDiari').getElementsByTagName("li");
	for(var i=0;i<vInp.length;i++)
		{
		if(vInp[i].className == 'contentSelected'){vInp[i].className = 'content';}
		}
	$(id).className = 'contentSelected';
	}

function nicanor(id)
	{
	var vInp = $('botNic').getElementsByTagName("input");
	for(var i=0;i<vInp.length;i++)
		{
		if(vInp[i].className == 'button1'){vInp[i].className = 'button2';}
		}
	$('nicanor_'+id).className = 'button1';
	
	var vInp = $('ctl_0').getElementsByTagName("div");
	for(var i=0;i<vInp.length;i++)
		{
		if(vInp[i].className == 'description' || vInp[i].className == 'carruselEmp' || vInp[i].className == 'bigImg')
			{
			vInp[i].style.display = 'none';
			}
		}
	$('cNicanor_'+id).style.display = 'block';
	$('carNicanor_'+id).style.display = 'block';
	$('bigNicanor_'+id).style.display = 'block';
	}

function messerveis(id)
	{
	var vInp = $('botMes').getElementsByTagName("input");
	for(var i=0;i<vInp.length;i++)
		{
		if(vInp[i].className == 'button1'){vInp[i].className = 'button2';}
		}
	$('messerveis_'+id).className = 'button1';
	
	var vInp = $('ctl_3').getElementsByTagName("div");
	for(var i=0;i<vInp.length;i++)
		{
		if(vInp[i].className == 'description'){vInp[i].style.display = 'none';}
		if(vInp[i].className == 'ilinksEmp'){vInp[i].style.display = 'none';}
		if(vInp[i].className == 'horari'){vInp[i].style.display = 'none';}
		if(vInp[i].className == 'logovencam'){vInp[i].style.display = 'none';}
		if(vInp[i].className == 'logonicanor'){vInp[i].style.display = 'none';}
		if(vInp[i].className == 'carruselEmp'){vInp[i].style.display = 'none';}
		if(vInp[i].className == 'bigImg'){vInp[i].style.display = 'none';}
		}
	$('cMesserveis_'+id).style.display = 'block';
	$('logoMesserveis_'+id).style.display = 'block';
	$('horariMesserveis_'+id).style.display = 'block';
	$('linksMesserveis_'+id).style.display = 'block';
	$('carruselMesserveis_'+id).style.display = 'block';
	$('bigMesserveis_'+id).style.display = 'block';
	}

function imprimir(que) 
	{
	ventana = window.open("./print.php?data="+escape(document.getElementById('data').innerHTML)+"&menu="+escape(document.getElementById(que).innerHTML), "carta");
	return;
	getElementById('menu').innerHTML = document.getElementById(que).innerHTML;
	//ventana.document.open();
	//ventana.document.write(contenido);
	return;
	ventana.document.close();
	}

function showMapa()
	{
	document.getElementById("mapa").style.display = "block";
	if(document.getElementById("ctl_3").style.display == "block")
		{
		if(document.getElementById("linksMesserveis_0").style.display == "block")
			{
			sma_maps_showMAP(idioma, true, '40.719700, 0.731042', "Avgda. Esportiva, 118 43580 DELTEBRE", "977 481 563");
			}
		else
			{//VENCAM
			sma_maps_showMAP(idioma, true, '40.720066, 0.730829', "Av Esportiva, 118, baixos 43580 DELTEBRE", "977 481 563");
			}
		}
	else
		{
		sma_maps_showMAP(idioma, true);
		}
	}
	
function hideMapa()
	{
	document.getElementById("mapa").style.display = "none";
	}
function setDia()
	{
	if(diaSemana==5 || diaSemana==6)
		{
		showPage(0);
		}
	}
function cambiaBigImg(link, obj)
	{
	document.getElementById(obj).style.backgroundImage = "url('"+link+"')";
	document.getElementById(obj).onclick = function()
		{
		var final = link;
		final = final.replace("petit/", "petit/big");
		final = final.replace("restaurant/", "restaurant/big");
		final = final.replace("cafeteria/", "cafeteria/big");
		final = final.replace("vencam/", "vencam/big");
		
		alertIMG(final, "");
		}
	}

function changeLanguage(language)
	{
	idioma = language;

	new Effect.Appear("loadingMenu");
	new Effect.Fade("menuDiari");
	
	cat2Loaded = 0
	catLoaded = 0;

	iniciaRotator()
	actualitzaIdioma();
	}

function showPubli()
	{
	$('overpublicitat').style.display = 'block';
	setTimeout("Effect.Grow('publicitat')", 5000);
	setTimeout("showClosePubli()", 6000);
	
	}

function closePubli()
	{
	$('closePubli').style.display = 'none';
	Effect.Shrink("publicitat");
	setTimeout("closeOver()", 1000);
	}


function closeOver()
	{
	$('overpublicitat').style.display = 'none';
	}
function showClosePubli()
	{
	$('closePubli').style.display = 'block';
	}

function setNoticia(node, obj)
	{
	var v = $("botoneraNoticies").getElementsByTagName("div");
	for(var i=0; i<v.length; i++)
		{
		v[i].className = 'button2';
		}
	obj.className = 'button1';
	
	$('titol_noticia').innerHTML = node.nom;
	$('descripcio_noticia').innerHTML = node.descripcio;
	$('imatges_noticia').innerHTML = '';
	
	for(var i=0; i<node.vIMG.length; i++)
		{
		var obj = document.createElement("div");
		obj.className = 'cataleg';
		var img = document.createElement("img");
		img.src = "http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+node.vIMG[i].url+"&w=93&h=110";
		img.className = 'cataleg';
		img.url = node.vIMG[i].url;
		img.onclick = function()
			{
			finestra ("http://extranet.smawebsite.net/"+this.url, "_img", "");
			}
		obj.appendChild(img);
		
		var span = document.createElement("span");
		span.className = 'cataleg';
		span.innerHTML = node.vIMG[i].nom;
		obj.appendChild(span);
		
		$('imatges_noticia').appendChild(obj);
		}

	for(var i=0; i<node.vDOC.length; i++)
		{
		var obj = document.createElement("div");
		obj.className = 'cataleg';
		var img = document.createElement("img");
		img.className = 'cataleg';
		img.src = "images/pdf.jpg";
		img.urlDoc = node.vDOC[i].url;
		img.onclick = function()
			{
			finestra ("http://extranet.smawebsite.net/"+this.urlDoc, "_pdf", "");
			}
		obj.appendChild(img);

		var span = document.createElement("span");
		span.innerHTML = node.vDOC[i].nom;
		span.className = 'cataleg';
		obj.appendChild(span);

		$('imatges_noticia').appendChild(obj);
		}
	}
