var idioma = 'ca';
var domini = "grainart.org";
var prdLoaded = 0;
var catLoaded = 0;
var swCat = 0;


function showPage(idp)
	{
	var vApartats = document.getElementById("contenidor").getElementsByTagName("div");
	
	for(var i=0; i<vApartats.length; i++)
		{
		if(vApartats[i].className == 'menuselected')
			{
			vApartats[i].className = 'menu';
			}
		if(vApartats[i].className == 'central')
			{
			vApartats[i].style.display = 'none';
			}
		}
	
	document.getElementById("ctl_"+idp).style.display='block';
	document.getElementById("menu_"+idp).className = 'menuselected';

	if(idp == 5)
		{
		sma_maps_showMAP(idioma);
		}
	}
	
function finestra (url,nomfinestra,parametres) 
	{
	w=window.open(url,nomfinestra,parametres);
	}

function myObj(){}
myObj.prototype = new sma_oCategoria();
myProducts = new myObj();

function inicia()
	{
	myProducts.setCategoria(0);
	myProducts.setDomini(domini);
	myProducts.setProxy("proxy.php");
	myProducts.setIdioma(idioma);
	myProducts.setProcessador("endLoading");
	myProducts.init();
	}

function endLoading(data)
	{
	var conexions = 0;
	myProducts = data;
	if(myProducts.vCategories.length>0 && catLoaded == 0)
		{
		catLoaded = 1;
		loadCategoriesPrincipals(myProducts.vCategories);
		}	
	if(catLoaded == 1)
		{
		if(myProducts.vCategories[0].vCategories.length>0)
			{
			loadCatList(myProducts.vCategories[0]);
			catLoaded = 2;
			}
		}
	if(catLoaded == 2)
		{
		if(myProducts.vCategories[0].vCategories[0].vProductes.length>0)
			{
			loadPrdList(myProducts.vCategories[0].vCategories[0]);
			catLoaded = 3;
			}
		}
	if(catLoaded == 3)
		{
		if(myProducts.vCategories[0].vCategories[0].vProductes[0])
			{
			loadPrdFinal(myProducts.vCategories[0].vCategories[0].vProductes[0]);
			catLoaded = 4;
			}
		}
	}

function setPrincipalSelected(obj)
	{
	var vCat = document.getElementById("catPrincipals").getElementsByTagName("div");
	for(var i=0;i<vCat.length;i++)
		{
		vCat[i].className = vCat[i].className.replace("Selected", "");
		}
	obj.className += 'Selected';
	}

function loadCategoriesPrincipals(vCat)
	{
	document.getElementById("catPrincipals").innerHTML = "";
	for(var i=0;i<vCat.length;i++)
		{
		var oMenu = document.createElement("div");
		oMenu.className = "catPrincipal";
		if(i==0){oMenu.className += "Selected";}
		oMenu.innerHTML = vCat[i].nom;
		oMenu.node = vCat[i];
		oMenu.onclick=function()
			{
			setPrincipalSelected(this);
			loadCatList(this.node);
			}
		document.getElementById("catPrincipals").appendChild(oMenu);
		}
	}

function loadCatList(oCat)
	{
	var vCat = oCat.vCategories;
	document.getElementById("catSecundaries").innerHTML = "";

	for(var i=0;i<vCat.length;i++)
		{
		var oMenu = document.createElement("div");

		if(i!=0)
			{
			oMenu.className = "catSecundaria";
			}
		else
			{
			oMenu.className = "catSecundariaSelected";
			}
		oMenu.innerHTML = vCat[i].nom;
		oMenu.node = vCat[i];
		oMenu.onclick=function()
			{
			desmarcaCategories();
			this.className = "catSecundariaSelected";
			loadPrdList(this.node);
			}
		document.getElementById("catSecundaries").appendChild(oMenu);
		}
	}

function loadPrdList(oCat)
	{
	var vPrd = oCat.vProductes;
	document.getElementById("prdList").innerHTML = "";

	for(var i=0;i<vPrd.length;i++)
		{
		var oMenu = document.createElement("div");
		oMenu.className = "photo";

/*
		var oTitle = document.createElement("div");
		oTitle.className = "title";
		oTitle.innerHTML = vPrd[i].nom;
		oMenu.appendChild(oTitle);
*/

		var oImg = document.createElement("img");
		oImg.className = "photo";
		if(vPrd[i].vIMG.length > 0)
			{
			oImg.src = "http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+vPrd[i].vIMG[0].url+"&w=165&h=112";
			}
		else
			{
			oImg.src = "http://www.grainart.org/beta/images/noIMG165.jpg";
			}
		
		oMenu.appendChild(oImg);

		oMenu.node = vPrd[i];
		oMenu.onclick=function()
			{
			loadPrdFinal(this.node);
			}

		document.getElementById("prdList").appendChild(oMenu);
		}
	
	}

function loadPrdFinal(node)
	{
	document.getElementById("prdTitle").innerHTML = node.nom;
	if(node.vIMG.length > 0)
		{
		document.getElementById("prdBigPhoto").src = "http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+node.vIMG[0].url+"&w=355&h=244";
		}
	else
		{
		document.getElementById("prdBigPhoto").src = "http://www.grainart.org/beta/images/noIMG355.jpg";
		}
	document.getElementById("prdDescripcio").innerHTML = node.descripcio;
	document.getElementById("prdDescripcioAmpli").innerHTML = "<img src='http://extranet.smawebsite.net/"+node.vIMG[0].url+"' class='prdImg2'/>"+node.descripcio+"<br /><br />"+node.descripcioAmpliada;
	if(node.descripcioAmpliada!='')
		{
		document.getElementById("masInfo").style.display = 'block';
		}
	else
		{
		document.getElementById("masInfo").style.display = 'none';
		}
	document.getElementById("prdPreu").innerHTML = "P.V.P. "+node.preu+" &euro;";
	}

function desmarcaCategories()
	{
	var vAux = document.getElementById("catSecundaries").getElementsByTagName("div");
	for(var i=0;i<vAux.length;i++)
		{
		vAux[i].className = "catSecundaria";
		}
	}

function OpenClose(obj)
	{
	if(document.getElementById(obj).style.display == 'block')
		{
		document.getElementById("imgMesInfo").src = 'images/mesInfo.gif';
		document.getElementById(obj).style.display = 'none';
		}
	else
		{
		document.getElementById("imgMesInfo").src = 'images/mesInfo2.gif';
		document.getElementById(obj).style.display = 'block';
		}
	}