var swGavines = 0;
var swProductes = 0;
var rot = 2;
var rot2 = 1;

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';
	if(idp!=4){
		document.getElementById("menu_"+idp).className = 'menuselected';
	}
	}
	
function finestra (url,nomfinestra,parametres) 
	{
	w=window.open(url,nomfinestra,parametres);
	}

function transferMe(from, to){
	var options = { to: "#"+to, className: 'ui-effects-transfer' };
	$("#"+from).effect('transfer', options, 500, changeBigImg(from, to));
}

function changeBigImg(from, to){
	setTimeout(function(){
		$("#"+to).attr("src", $("#"+from).attr("src").replace("m.jpg", ".jpg"));
	}, 500);

};

function rotator()
	{
	if(rot<6){
		rot++;
	} else {
		rot = 1;
	}
	if(rot%2 == 0){
		$("#entorn1").ImageSwitch({NewImage:"images/entorn/"+rot+".jpg"});
	} else {
		$("#entorn2").ImageSwitch({NewImage:"images/entorn/"+rot+".jpg"});
	}

	rot2 = 1-rot2;
	$("#imgEmpresa").ImageSwitch({Type: "ScrollOut", NewImage:"images/empresa"+rot2+".jpg"});
	

	setTimeout("rotator()", 5000);
	}



/* GESTIÓ DE PRODUCTES */

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

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

function endLoading(data)
	{
	var conexions = 0;
	promocions = data;
	//console.log(promocions);
	if(conexions == 0)
		{
		setCategories();
		}
	}

function setCategories()
	{
	var vCAT = promocions.vCategories;
	document.getElementById('prdCat').innerHTML = '';
	for(var i=0;i<vCAT.length; i++)
		{
		var ul = document.createElement("ul");
		ul.className = 'first';

		var li = document.createElement("li");
		li.innerHTML = vCAT[i].nom;
		var ul2 = document.createElement("ul");
		ul2.className = "second";
		
		setProductes(ul2, vCAT[i].vProductes);
		
		li.appendChild(ul2);
		ul.appendChild(li);
		document.getElementById('prdCat').appendChild(ul);
		}
	
	var ul = document.createElement("ul");
	ul.className = 'first';

	var li = document.createElement("li");
	li.innerHTML = vLang[0];
	li.style.cursor = 'pointer';
	li.onclick = function(){showPage(4)};
	ul.appendChild(li);
	document.getElementById('prdCat').appendChild(ul);
		
	}
	
	
function setProductes(ul, vPRD)
	{
	for(var j=0;j<vPRD.length;j++)
		{
		var li = document.createElement("li");
		li.innerHTML = vPRD[j].nom;
		li.node = vPRD[j];
		li.onclick = function()
			{
			setPrdFinal(this.node);
			setSelected(this)
			}
		if(swProductes == 0 && j == 0)
			{
			setPrdFinal(vPRD[j]);
			setSelected(li)
			swProductes = 1;
			}
		ul.appendChild(li);
		}
	if(!swGavines)
		{
		var li = document.createElement("li");
		li.innerHTML = "RESIDENCIAL LES GAVINES";
		li.onclick = function()
			{
			window.open('http://www.gavinesalfacs.com');
			}
		ul.appendChild(li);
		
		swGavines = 1;
		}
	}

function setPrdFinal(node)
	{
	var url = "";
	if(node.vIMG.length>0){
		 url = node.vIMG[0].url;
	}
	document.getElementById('prdTitle').innerHTML = node.nom;
	document.getElementById('prdDescription').innerHTML = node.descripcio;
	document.getElementById("prdImg").innerHTML = "";
	
	if(node.vIMG.length>0){
		document.getElementById('prdImg').style.background = "#3B2F8D url('http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+url+"&w=380&h=262') no-repeat center center";
		document.getElementById("prdImg").innerHTML = "<div class='pointered'></div>";
		
		document.getElementById('prdImg').onclick  = function(){
			alertIMG(this.urlIMG, "");
		}
	} else {
		document.getElementById('prdImg').style.background = "#3B2F8D url('images/noIMG.jpg') no-repeat center center";
		document.getElementById('prdImg').onclick  = function(){}
	}
	document.getElementById('prdImg').urlIMG = 'http://extranet.smawebsite.net/'+url;
	
	
	//[ BOTONS ]//
	document.getElementById('bot1').node = node;
	document.getElementById('bot1').onclick = function()
		{
		var url = "";
		if(this.node.vIMG.length>1){
			 url = this.node.vIMG[1].url;
		}
		document.getElementById("prdImg").innerHTML = "";
		if(node.vIMG.length>0){
			document.getElementById("prdImg").innerHTML = "<div class='pointered'></div>";
			document.getElementById('prdImg').style.background = "#3B2F8D url('http://shared.smawebsite.net/aspectratio.php?p=gestioclients/"+url+"&w=380&h=262') no-repeat center center";
		} else {
			document.getElementById('prdImg').style.background = "#3B2F8D url('images/noIMG.jpg') no-repeat center center";
		}
	
		
		document.getElementById('prdImg').urlIMG = 'http://extranet.smawebsite.net/'+url;
		document.getElementById('prdImg').onclick  = function()
			{
			alertIMG(this.urlIMG, "");
			}
		}

	document.getElementById('bot2').node = node;
	document.getElementById('bot2').onclick = function()
		{
		setDocuments(this.node);
		document.getElementById('prdImg').onclick  = function(){}
		}


/*
	document.getElementById('bot3').node = node;
	document.getElementById('bot3').onclick = function()
		{
		document.getElementById('prdImg').style.background = "url('') no-repeat center center";
		$("#prdImg").html("");

		var html = '<div class="no_disponibles">'+vLang[3]+'</div>';
		if(this.node.descripcio_ampli)
			{
			html += this.node.descripcio_ampli;
			}
		html += "<br /><br /><div id='btn_acabats' onClick='showPage(4);'>"+vLang[2]+"</div>";
		$("#prdImg").html(html);
		document.getElementById('prdImg').onclick  = function(){}
		}
*/

	}

function setDocuments(node)
	{
	var vDOC = node.vDOC;
	
	document.getElementById('prdImg').style.background = "url('') no-repeat center center";

	document.getElementById("prdImg").innerHTML = "";

	if(vDOC.length > 0)
		{
		for(var i=0;i<vDOC.length;i++)
			{
			var obj = document.createElement("div");
			obj.className = 'document';
			obj.innerHTML = vDOC[i].nom;
			obj.url = "http://extranet.smawebsite.net/"+vDOC[i].url;
			obj.onclick = function()
				{
				finestra(this.url, "_blank", "");
				}
			
			document.getElementById("prdImg").appendChild(obj);
			}
		//html += "<br /><br /><div id='btn_acabats' onClick='showPage(4);'>"+vLang[2]+"</div>";
		}
	else
		{
		document.getElementById("prdImg").innerHTML = "<div class='no_disponibles'>"+vLang[1]+"</div>";
		}

	var obj = document.createElement("div");
	obj.id = 'btn_acabats'
	obj.innerHTML = vLang[2];
	obj.onclick = function(){showPage(4);}
	document.getElementById("prdImg").appendChild(obj);
	}

function setSelected(obj)
	{
	var vAux = document.getElementById('prdCat').getElementsByTagName("li");
	for(var i=0; i<vAux.length; i++)
		{
		if(vAux[i].className == 'selected')
			{
			vAux[i].className = '';
			}
		}
	obj.className = 'selected';
	}
