var proxy = "proxy.php";


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

function showPage(idp, idm)
	{
	var vApartats = document.getElementById("contenidor").getElementsByTagName("div");

	$(".menuSelected").removeClass('menuSelected');

	if($(".central:visible").length){
		$(".central:visible").hide("drop", {direction: "right"}, "", function(){
			$("#ctl_"+idp).show("drop", {direction: "right"});
		});
	} else {
		$("#ctl_1").hide("drop", {direction: "right"}, "", function(){
			$("#ctl_"+idp).show("drop", {direction: "right"});
		});
	}

	

	if(idm || idm==0){
		$("#botoContactar").removeClass("ctoSelected");
		$("#menu_"+idm).addClass('menuSelected');
	}
	if(!idm && idm!=0){
		$("#botoContactar").addClass("ctoSelected");
	}
	}
	
function finestra (url,nomfinestra,parametres) 
	{
	w=window.open(url,nomfinestra,parametres);
	}

function setSelectedIMG(obj, src, nom, descripcio)
	{
	src = src.replace("&w=95&h=62", "&w=340&h=228");
	var vAux = document.getElementById('modulX').getElementsByTagName("div");
	
	for(var i=0; i<vAux.length; i++)
		{
		if(vAux[i].className == 'miniIMGselected')
			{
			vAux[i].className = 'miniIMG';
			}
		}
	obj.className = 'miniIMGselected';
	document.getElementById('bigIMG').style.backgroundImage = 'url("'+src+'")';
	document.getElementById('prdImgNom').innerHTML = nom;
	document.getElementById('prdImgDes').innerHTML = descripcio;
	}

function zoomer()
	{
	var urlIMG = document.getElementById('bigIMG').style.backgroundImage.replace("url(", "").replace(")", "").replace("aspectratio.php?p=", "").replace("&w=340&h=228", "");
	alertIMG(urlIMG, document.getElementById('prdImgNom').innerHTML);
	}


/* GESTIÓ DE PRODUCTES */

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

function inicia()
	{
	centre.setCategoria(catInicial);
	centre.setDomini(domini);
	centre.setProxy(proxy);
	centre.setIdioma(idioma);
	centre.setProcessador("endLoading");
	centre.init();
	}

function endLoading(data)
	{
	var conexions = 0;
	centre = data;
	if(conexions == 0)
		{
		setNode(centre);
		rotateBigIMG();
		}
	}

function goFirst()
	{
	document.getElementById("path").innerHTML = '';
	setNode(centre)
	}
	
function setNode(node)
	{
	document.getElementById("botonera1").innerHTML = '';
	setPath(node);

	setCategories(node);
	setProductes(node);
	}

function setCategories(node)
	{
	var vBot = document.getElementById("contenidor").getElementsByTagName("div");
	var sw = 0;
	for(var i=0;i<vBot.length; i++)
		{
		if(vBot[i].className == 'menu' || vBot[i].className == 'menuSelected' && vBot[i].innerHTML == '')
			{
			sw++;
			}
		}
	sw++;

	var vCAT = node.vCategories;
	var container = document.getElementById("botonera1");
	
	for(var i=0;i<vCAT.length; i++)
		{
		var obj = document.createElement("div");
		obj.className = 'categoria'
		obj.id = 'cat_'+sw;
		obj.innerHTML = vCAT[i].nom;
		obj.node = vCAT[i];
		obj.onclick = function()
			{
			setNode(this.node);
			}
		container.appendChild(obj);
		}
	}



function setProductes(node)
	{
	var vBot = document.getElementById("contenidor").getElementsByTagName("div");
	var sw = 0;
	for(var i=0;i<vBot.length; i++)
		{
		if(vBot[i].className == 'menu' || vBot[i].className == 'menuSelected' && vBot[i].innerHTML == '')
			{
			sw++;
			}
		}
	sw++;

	var vAux = Array();
	var container = document.getElementById("botonera1");
	var container2 = document.getElementById("miniProductes");
	container2.innerHTML = '';
	var vPRD = node.vProductes;

	for(var i=0;i<vPRD.length; i++)
		{
		var obj = document.createElement("div");
		obj.className = 'feina';
		obj.id = 'feina_'+sw;
		obj.innerHTML = "<div class='imagen'><img src='http://shared.dertochip.com/aspectratio.php?p=gestioclients/"+vPRD[i].url+"&w=180&h=180' /></div><div class='name'>"+vPRD[i].nom+"</div>";
		
		obj.node = vPRD[i];
		obj.onclick = function()
			{
			loadProducte(this.node);
			}
		container.appendChild(obj);
		sw++;



		var obj = document.createElement("div");
		obj.className = 'minifeina';
		obj.id = 'minifeina_'+vPRD[i].idProducte;
		obj.innerHTML = vPRD[i].nom;
		
		obj.node = vPRD[i];
		obj.onclick = function()
			{
			loadProducte(this.node);
			}
		container2.appendChild(obj);
		}
	
	if(vPRD.length>0)
		{
		loadProducte(vPRD[0]);
		}
	}

function loadProducte(producte)
	{
	$(".minifeina").removeClass('minifeinaSelected');
	$("#minifeina_"+producte.idProducte).addClass('minifeinaSelected');

	document.getElementById("botonera1").style.display = 'none';
	document.getElementById("prdFinal").style.display = 'block';
	document.getElementById("prdDes").innerHTML = producte.descripcio;
	document.getElementById("prdNom").innerHTML = producte.nom;
	document.getElementById('bigIMG').style.backgroundImage = 'url("http://shared.dertochip.com/aspectratio.php?p=gestioclients/'+producte.url+'&w=340&h=231")';
	document.getElementById('bigIMG').vIMG = producte.vIMG;
	document.getElementById('bigIMG').counter = 0;
	document.getElementById("prdImgNom").innerHTML = "";
	document.getElementById("prdImgDes").innerHTML = "";
	loadImages(producte.vIMG, 0);
	}

function rotateBigIMG(){
	if(document.getElementById('bigIMG').vIMG.length >= 1) {
		$("#bigIMG").css({"background" : 'url("http://shared.dertochip.com/aspectratio.php?p=gestioclients/'+document.getElementById('bigIMG').vIMG[document.getElementById('bigIMG').counter].url+'&w=340&h=231")'}, "slow");
		document.getElementById('bigIMG').counter = document.getElementById('bigIMG').counter + 1;
		if(document.getElementById('bigIMG').counter >= document.getElementById('bigIMG').vIMG.length){
			document.getElementById('bigIMG').counter = 0;
		}
	}
	setTimeout("rotateBigIMG()", 3000);

}

function loadImages(vIMG, from)
	{
	var contenidor = document.getElementById('modulX');
	contenidor.innerHTML = '';

	document.getElementById('plusButton').style.display = 'none';

	for(var i=from; i<vIMG.length && i<(from + 5); i++)
		{
		var div = document.createElement("div");
		div.className = "miniIMG"
		
		var img = document.createElement("img");
		img.src = "http://shared.dertochip.com/aspectratio.php?p=gestioclients/"+vIMG[i].url+"&w=95&h=62";
		img.node = vIMG[i];
		img.onclick = function()
			{
			setSelectedIMG(this.parentNode, this.src, this.node.nom, this.node.descripcio);
			}
		if(i == 0)
			{
			setSelectedIMG(div, img.src, vIMG[i].nom, vIMG[i].descripcio);
			}
		if(vIMG.length > 5)
			{
			document.getElementById('plusButton').style.display = 'block';
			document.getElementById('plusButton').node = vIMG;
			if(vIMG.length > (from + 5))
				{
				document.getElementById('plusButton').next = (from + 5);
				}
			else
				{
				document.getElementById('plusButton').next = 0;
				}
			document.getElementById('plusButton').onclick = function()
				{
				loadImages(this.node, this.next);
				}
			}
		div.appendChild(img);
		contenidor.appendChild(div);
		}
	}

function setPath(node)
	{
	var container = document.getElementById("path");
	container.innerHTML = '';
	var vObj = Array();
	var i=0;
	
	while(node.parentNode)
		{
		vObj[i] = document.createElement("div");
		vObj[i].className = 'path';
		vObj[i].innerHTML = node.nom+" >";
		vObj[i].node = node;
		vObj[i].onclick = function()
			{
			setNode(this.node);
			}
		node = node.parentNode;
		i++;
		}

	for(var j=(i-1); j>=0; j--)
		{
		container.appendChild(vObj[j]);
		}
	}
