var movent=false;
var act=3;
var actft=3;
var actart=3;
var numf='';
var numft='';
var numart='';

window.onload=function() {
	var dMap=$('map_canvas');
	if (dMap) {
		mapa();
	}
	var dDisplay=$('display');
	var dNavegador=$('navegador');
	if ((dDisplay) && (dNavegador)) {
		var protoDisplay=new ProtoDisplay(dDisplay, dNavegador);
	}
	var dImg=$('visorImg');
	if (dImg) {
		var protoViewer=new ProtoViewer(dImg, {
			useLightBox: true,
			lightBoxFolder: "../../admin/uploads/fotos/",
			buttonLeftWidth: 30,
			buttonRightWidth: 30,
			largeImgWidth: 273,
			largeImgHeight: 186,
			smallImgWidth: 80,
			smallImgHeight: 60,
			buttonLeft: '../img/btn_izq.png',
			buttonRight: '../img/btn_dcha.png',
			titlePosition: 'bottom',
			navigationPosition: 'top'
		});
	}
	var dImg2=$('artRel');
	if (dImg2) {
		var protoCarousel=new ProtoCarousel(dImg2, {
			buttonLeftWidth: 26,
			buttonRightWidth: 26,
			buttonLeft: '../img/btn_izq.png',
			buttonRight: '../img/btn_dcha.png'
		});
	}
	var dOfertas=$('ofertasLateral');
	if (dOfertas) {
		canviBanner(dOfertas.firstChild);
	}
}
function canviBanner(d) {
	new Effect.Appear(d, { duration: 2, 
		afterFinish: function() {
			new PeriodicalExecuter(function(pe) {
				pe.stop();
				new Effect.Fade(d, { duration: 2 });
				
				if (d.nextSibling) {
					d=d.nextSibling;
				} else {
					d=d.parentNode.firstChild;
				}
				
				canviBanner(d);
			},3);
		}
	});
}

function desplega(img) {
	if (img.getAttribute("rel")=="abierto") {
		new Effect.SlideUp(img.parentNode.parentNode.nextSibling, { duration: 0.5 });
		img.setAttribute("src","../img/desplegable_gris.png");
		img.setAttribute("rel","cerrado");
	} else {
		new Effect.SlideDown(img.parentNode.parentNode.nextSibling, { duration: 0.5 });
		img.setAttribute("src","../img/desplegable.png");
		img.setAttribute("rel","abierto");
	}
}

function esborraTexte() {
	if ($('txtBuscar').value==$('hBuscar').value) {
		$('txtBuscar').value="";
	}
}

function posaTexte() {
	if ($('txtBuscar').value=="") {
		$('txtBuscar').value=$('hBuscar').value;
	}
}

function login() {
	new Effect.Fade('l1', { duration: 1, afterFinish: function() {
			new Effect.Appear('l2', { duration: 1,
				afterFinish: function() {
					var url="../xml/login.php";
					var params="usu="+$('txtUsu').value+"&pass="+hex_md5($('txtPass').value);
					var ajax=new Ajax.Request( url, {
						method: 'post',
						postBody: params,
						onComplete: function(reply) {
							if (reply.responseText=="ok") {
								mostrarNovedades();
								//window.location="shop.php";
							} else {
								$('txtUsu').value="";
								$('txtPass').value="";
								new Effect.Fade('l2', { duration: 1, 
									afterFinish: function() {
										new Effect.Appear('l3', { duration: 1 });
									}
								});
							}
						}
					});
				}
			});
		}
	});
}
function mostrarNovedades() {
	
	var url="../xml/info.php";
	var params="conscod=1";
	var ajax=new Ajax.Request(url, {
		method: 'get',
		parameters: params,
		onComplete: mostrarNovedades2
	});
}
function mostrarNovedades2(reply) {
	
	/////noticias
	var novedades=false;
	
	
	var info=reply.responseXML.selectNodes("info");
	
	var info=info[0];
	
	var cont=$('contInfo');
	
	var divlogo=new Element("div", {'style':' color: #91B834; float:left; margin-bottom:15px; text-aling:right;' }).update("<img src='../img/iconoNoticias.png'>");
	var divx=new Element("div", {'style':' color: #91B834; float:right; margin-bottom:15px; text-aling:right;' }).update("<div onclick='amagaOverlay2();'><img src='../img/x.jpg'>");
	
	
	var men1=new Element("div", {'style':' color: #91B834; float:left; margin-bottom:15px; text-aling:right;' }).update("La ultima vez que nos visito fué "+info.childNodes[0].childNodes[2].firstChild.nodeValue);
	
	var men2=new Element("div", {'style':' color: #91B834; float:left; margin-bottom:30px;' }).update(" Bienvenido <b>"+info.childNodes[0].childNodes[0].firstChild.nodeValue+" "+info.childNodes[0].childNodes[1].firstChild.nodeValue+"</b>");
	cont.appendChild(divlogo);
	cont.appendChild(divx);
	cont.appendChild(men1);
	cont.appendChild(men2);
	
	
	
	var divseccion=new Element("div", {'style':' float: left; margin-bottom: 30px; height: 98px; width: 436px; margin-left:68px;' });
	var divflizq=new Element("div", {'style':' width:40px; float:left;' }).update("<div onclick='izq();' style='margin-top:29px;'><img style='width:40px;' src='../img/flizq.jpg'></div>");
	divseccion.appendChild(divflizq);
	var divmovil=new Element("div", {'style':' width:'+lenghtlargo+'px; float:left;' , 'id':'movil'});
	var not=info.childNodes[1];
	var notcont=info.childNodes[1].childNodes.length
	var x=0;
	
	
	while(x<notcont){
		var notcod=info.childNodes[1].childNodes[x].firstChild.firstChild.nodeValue;
		var nottit=info.childNodes[1].childNodes[x].childNodes[1].firstChild.nodeValue;
		var image=info.childNodes[1].childNodes[x].childNodes[2].firstChild.nodeValue;
		if(image!='NO'){
			var not1=new Element("div", {'style':'width:118px; text-aling:left; color: #91B834; float:left; margin-bottom:15px; margin-top:10px; text-aling:right;' }).update("<div><img src=\"../../admin/"+image+"\" style='width: 65px; height: 57px; margin-top: 6px;'></div><div><a style='color:#668325' target='blank' href='noticias.php?not="+notcod+"'>"+nottit+"</a></div>");
		}else{
			var not1=new Element("div", {'style':'width:118px; text-aling:left; color: #91B834; float:left; margin-bottom:15px; margin-top:10px; text-aling:right;' }).update("<div><img src='../img/new.jpg' style='margin-rigth:10px; width:65px;'></div><div><a style='color:#668325' target='blank' href='noticias.php?not="+notcod+"'>"+nottit+"</a></div>");
		}
		
		divmovil.appendChild(not1);
		x++;
	}
	
	numf=x;
	var lenghtlargo=x*118;
	
	divmovil.style.width=lenghtlargo+"px";
	
	var divfijo=new Element("div", {'style':'overflow:hidden; float: left; position: relative; width: 354px;', 'id':'fijo' });
	
	divfijo.appendChild(divmovil);
	divseccion.appendChild(divfijo);
	var divflder=new Element("div", {'style':' width:40px; float:left;' }).update("<div onclick='der();' style='margin-top:29px;'><img style='width:40px;' src='../img/flder.jpg'></div>");
	divseccion.appendChild(divflder);
	
	var men3=new Element("div", {'style':'color: rgb(145, 184, 52); float: left; font-weight: bold; width: 301px; margin-left: 137px; font-size:13px; text-decoration:underline; color:#82878B;'}).update("Últimas Notícias");
	
	if(numf!=0){
		cont.appendChild(men3);
		cont.appendChild(divseccion);
		novedades=true;
	}
	
	
	///////torneos
	
	

	var divseccion=new Element("div", {'style':' float: left; margin-bottom: 30px; height: 98px; width: 436px; margin-left:68px;' });
	var divflizq=new Element("div", {'style':' width:40px; float:left;' }).update("<div onclick='izqft();' style='margin-top:29px;'><img style='width:40px;' src='../img/flizq.jpg'></div>");
	divseccion.appendChild(divflizq);
	var divmovil=new Element("div", {'style':' width:'+lenghtlargo+'px; float:left;' , 'id':'movilft'});
	var not=info.childNodes[2];
	var notcont=info.childNodes[2].childNodes.length
	var x=0;
	
	
	while(x<notcont){
		var notcod=info.childNodes[2].childNodes[x].firstChild.firstChild.nodeValue;
		var nottit=info.childNodes[2].childNodes[x].childNodes[1].firstChild.nodeValue;
		var image=info.childNodes[2].childNodes[x].childNodes[2].firstChild.nodeValue;
		if(image!='NO'){
			var not1=new Element("div", {'style':'width:118px; text-aling:left; color: #91B834; float:left; margin-bottom:15px; margin-top:10px; text-aling:right;' }).update("<div><img src=\"../../admin/"+image+"\" style='width: 65px; height: 57px; margin-top: 6px;'></div><div><a style='color:#668325' target='blank' href='torneos.php?not="+notcod+"'>"+nottit+"</a></div>");
		}else{
			var not1=new Element("div", {'style':'width:118px; text-aling:left; color: #91B834; float:left; margin-bottom:15px; margin-top:10px; text-aling:right;' }).update("<div><img src='../img/golf.jpg' style='margin-rigth:10px; width:65px;'></div><div><a style='color:#668325' target='blank' href='torneos.php?not="+notcod+"'>"+nottit+"</a></div>");
		}
		
		
		
		divmovil.appendChild(not1);
		x++;
	}
	
	numft=x;
	var lenghtlargo=x*118;
	
	divmovil.style.width=lenghtlargo+"px";
	
	var divfijo=new Element("div", {'style':'overflow:hidden; float: left; position: relative; width: 354px;', 'id':'fijo' });
	
	divfijo.appendChild(divmovil);
	divseccion.appendChild(divfijo);
	var divflder=new Element("div", {'style':' width:40px; float:left;' }).update("<div onclick='derft();' style='margin-top:29px;'><img style='width:40px;' src='../img/flder.jpg'></div>");
	divseccion.appendChild(divflder);
	
	var men3=new Element("div", {'style':'color: rgb(145, 184, 52); float: left; font-weight: bold; width: 301px; margin-left: 137px;  font-size:13px; text-decoration:underline; color:#82878B;' }).update(" ÚltimosTorneos");
	if(numft!=0){
		cont.appendChild(men3);
		cont.appendChild(divseccion);
		novedades=true;
	}
	
	
	
	
///////articulos
	
	

	var divseccion=new Element("div", {'style':' float: left; margin-bottom: 30px; height: 98px; width: 436px; margin-left:68px;' });
	var divflizq=new Element("div", {'style':' width:40px; float:left;' }).update("<div onclick='izqart();' style='margin-top:29px;'><img style='width:40px;' src='../img/flizq.jpg'></div>");
	divseccion.appendChild(divflizq);
	var divmovil=new Element("div", {'style':' width:'+lenghtlargo+'px; float:left;' , 'id':'movilart'});
	var not=info.childNodes[3];
	var notcont=info.childNodes[3].childNodes.length
	var x=0;
	
	
	while(x<notcont){
		var notcod=info.childNodes[3].childNodes[x].firstChild.firstChild.nodeValue;
		var nottit=info.childNodes[3].childNodes[x].childNodes[1].firstChild.nodeValue;
		var image=info.childNodes[3].childNodes[x].childNodes[2].firstChild.nodeValue;
	
		var not1=new Element("div", {'style':'width:118px; text-aling:left; color: #91B834; float:left; margin-bottom:15px; margin-top:10px; text-aling:right;' }).update("<div><img src=\"../../admin/uploads/medthumbs/"+image+"_1.jpg\" style='width: 65px; height: 57px; margin-top: 6px;'></div><div><a style='color:#668325' target='blank' href='articulo.php?artcod="+image+"'>"+nottit+"</a></div>");
		
		
		
		
		divmovil.appendChild(not1);
		x++;
	}
	
	numart=x;
	var lenghtlargo=x*118;
	
	divmovil.style.width=lenghtlargo+"px";
	
	var divfijo=new Element("div", {'style':'overflow:hidden; float: left; position: relative; width: 354px;', 'id':'fijo' });
	
	divfijo.appendChild(divmovil);
	divseccion.appendChild(divfijo);
	var divflder=new Element("div", {'style':' width:40px; float:left;' }).update("<div onclick='derart();' style='margin-top:29px;'><img style='width:40px;' src='../img/flder.jpg'></div>");
	divseccion.appendChild(divflder);
	
	var men3=new Element("div", {'style':'color: rgb(145, 184, 52); float: left; font-weight: bold; width: 301px; margin-left: 137px; font-size:13px; text-decoration:underline; color:#82878B;' }).update("Últimos Articulos");
	if(numart!=0){
		cont.appendChild(men3);
		cont.appendChild(divseccion);
		novedades=true;
	}
	
	if(novedades==true){
		posaOverlay($('dLogin'));
	}else{
		window.location="shop.php";
	}
}

function der() {
	if(movent==false){
		if(numf>act){
			movent=true;
			new Effect.Move("movil", { mode: 'relative', x:-118, afterFinish: function(){
				movent=false;
			} });
			act++;
		}
	}
}
function izq() {
	if(movent==false){
		if(act>=4){
			movent=true;
			new Effect.Move("movil", { mode: 'relative', x:118, afterFinish: function(){
				movent=false;
			} });
			act--;
		}
	}
}

function derft() {
	if(movent==false){
		if(numft>actft){
			movent=true;
			new Effect.Move("movilft", { mode: 'relative', x:-118, afterFinish: function(){
				movent=false;
			} });
			actft++;
		}
	}
}
function izqft() {
	if(movent==false){
		if(actft>=4){
			movent=true;
			new Effect.Move("movilft", { mode: 'relative', x:118, afterFinish: function(){
				movent=false;
			} });
			actft--;
		}
	}
}

function derart() {
	if(movent==false){
		if(numart>actart){
			movent=true;
			new Effect.Move("movilart", { mode: 'relative', x:-118, afterFinish: function(){
				movent=false;
			} });
			actart++;
		}
	}
}
function izqart() {
	if(movent==false){
		if(actart>=4){
			movent=true;
			new Effect.Move("movilart", { mode: 'relative', x:118, afterFinish: function(){
				movent=false;
			} });
			actart--;
		}
	}
}


function login2() {
	$('mens1').hide();
	$('mens2').hide();
	$('mens1').show();
	var url="../xml/login.php";
	var params="usu="+$('logUsu').value+"&pass="+hex_md5($('logPass').value);
	var ajax=new Ajax.Request( url, {
		method: 'post',
		postBody: params,
		onComplete: function(reply) {
			$('mens1').hide();
		
			if (reply.responseText=="ok") {
				var str=gup("fwd").replace(/#/gi,"&");
				str=str.replace(/php&/gi,"php?");
				window.location=str;
			} else {
				$('mens2').show();
				$('logUsu').value="";
				$('logPass').value="";
			}	
		}
	});
}

function contLogin() {
	new Effect.Fade('l3', { duration: 1,
		afterFinish: function() {
			new Effect.Appear('l1', { duration: 1 });
		}
	});
}

function mapa() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(39.44806, 2.76298), 16);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
		map.openInfoWindow(map.getCenter(),
			"<div id='menmapa' style='width: 270px;'><div id='iconomapa' style='float: left; margin-right: 23px;'><img src=../img/ico.png /></div><div style='float: right;'><div id='titulomapa' style='font-weight: bold; font-size: 15px;'>Golfm&aacute;tica</div><div id='dirmapa' style='font-size:12px;'>Crta. Cabo Blanco, km 6,3<br />Maioris<br/>07609 Llucmajor</div><div id='telmapa' style='font-size:12px;'>971 128 330</div></div><div style='clear: both'></div>");
		
		/*
		GEvent.addListener(map,"click", function(overlay,latlng) {
			if (latlng) {
				//var myHtml = "The GLatLng value is: " + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom();
				var myHtml = "The GLatLng value is: " + latlng + " at zoom level " + map.getZoom();
				map.openInfoWindow(latlng, myHtml);
			}
		});
		*/
	}
}

function registrarUsu() {
	var mens1=$('mens1');
	var mens2=$('mens2');
	var mens3=$('mens3');
	var mens4=$('mens4');
	var mens5=$('mens5');
	var mens6=$('mens6');
	var mens7=$('mens7');
	
	mens1.hide();
	mens2.hide();
	mens3.hide();
	mens4.hide();
	mens5.hide();
	mens6.hide();
	mens7.hide();
	
	var usu=$('txtUsuario').value;
	var pass1=$('txtPass1').value;
	var pass2=$('txtPass2').value;
	var nombre=$('txtNombre').value;
	var apellidos=$('txtApellidos').value;
	var dir=$('txtDir').value;
	var cp=$('txtCP').value;
	var pob=$('txtPob').value;
	var prov1=$('cboProv').value;
	var tel=$('txtTel').value;
	var mail=$('txtMail').value;
	var chkPol=$('chkPriv');
	
	
	if ((usu=="") || (pass1=="") || (pass2=="") || (nombre=="") || (apellidos=="") || (dir=="") || (cp=="") || (pob=="") || (tel=="") || (mail=="")) {
		mens3.show();
		return;
	}
	
	if (pass1!=pass2) {
		mens4.show();
		return;
	}
	
	if (chkPol.checked==false) {
		mens5.show();
		return;
	}
	
	if ($('chkTarjeta').checked) {
		var tar=2;
	} else {
		var tar=1;
	}	
	
	mens1.show();
	
	var url="../xml/guardaUsu.php";
	var params="usu="+usu+"&pass="+hex_md5(pass1)+"&nombre="+nombre+"&apellidos="+apellidos+"&dir="+dir+"&cp="+cp+"&pob="+pob+"&provcod="+prov1+"&tel="+tel+"&mail="+mail+"&tarjeta="+tar;
	var ajax=new Ajax.Request( url, {
		method: 'POST',
		postBody: params,
		onComplete: function(reply) {
			mens1.hide();
			
			if (reply.responseText=="in use") {
				mens7.show();
			} else {
				if (reply.responseText=="ok") {
					mens2.show();
					$('txtUsuario').value="";
					$('txtPass1').value="";
					$('txtPass2').value="";
					$('txtNombre').value="";
					$('txtApellidos').value="";
					$('txtDir').value="";
					$('txtCP').value="";
					$('txtPob').value="";
					$('txtTel').value="";
					$('txtMail').value="";
					$('chkPriv').checked=false;
				} else {
					mens6.show();
				}
			}
		}
	});
	
}

function solicitarTar() {
	$('tarmens2').hide();
	$('tarmens3').hide();
	$('tarmens1').show();
	
	var url="../xml/solicitudTarjeta.php";
	var ajax=new Ajax.Request(url, {
		onComplete: function(reply) {
			$('tarmens1').hide();
			
			if (reply.responseText=="ok") {
				$('tarmens3').show();
				$('tarPaso1').hide();
				$('tarPaso2').show();
			} else {
				$('tarmens2').show();
			}
		}
	});
}

function modificaUsu() {
	var mens1=$('mens1');
	var mens2=$('mens2');
	var mens3=$('mens3');
	var mens4=$('mens4');
	var mens5=$('mens5');
	
	mens1.hide();
	mens2.hide();
	mens3.hide();
	mens4.hide();
	mens5.hide();
	
	var usu=$('txtUsuario').value;
	var pass1=$('txtPass1').value;
	var pass2=$('txtPass2').value;
	var nombre=$('txtNombre').value;
	var apellidos=$('txtApellidos').value;
	var dir=$('txtDir').value;
	var cp=$('txtCP').value;
	var pob=$('txtPob').value;
	var prov1=$('cboProv').value;
	var tel=$('txtTel').value;
	var mail=$('txtMail').value;
	
	
	if ((usu=="") || (nombre=="") || (apellidos=="") || (dir=="") || (cp=="") || (pob=="") || (tel=="") || (mail=="")) {
		mens3.show();
		return;
	}
	
	if (pass1!=pass2) {
		mens4.show();
		return;
	}
	
	mens1.show();
	
	var url="../xml/modificaUsu.php";
	var params="usu="+usu+"&nombre="+nombre+"&apellidos="+apellidos+"&dir="+dir+"&cp="+cp+"&pob="+pob+"&provcod="+prov1+"&tel="+tel+"&mail="+mail;
	
	if (pass1!="") {
		params=params+"&pass="+hex_md5(pass1);
	}
	
	var ajax=new Ajax.Request( url, {
		method: 'POST',
		postBody: params,
		onComplete: function(reply) {
			mens1.hide();
			
			if (reply.responseText=="ok") {
				mens2.show();
			} else {
				mens5.show();
			}
		}
	});
	
}

function afegeixCarro(img) {
	var cant=img.parentNode.previousSibling.firstChild.value;
	
	if ((isNaN(cant)) || (cant=="")) {
		alert($('hError').value);
		img.parentNode.previousSibling.firstChild.value="";
		return;
	}

	var arrCodigos=new Array();
	var arrCant=new Array();
	
	if (getCookie("CODIGOS")) {
		arrCodigos=getCookie("CODIGOS").split(",");
		arrCant=getCookie("CANTIDADES").split(",");
	}
	
	arrCodigos.push(img.nextSibling.value);
	arrCant.push(cant);
	
	var arrCodigos2=new Array();
	var arrCant2=new Array();
	
	for (var k=0;k<arrCodigos.length;k++) {
		var trobat=false;
		var jTrobat;
		var cantTrobat=0;
		
		for (var j=0;j<arrCodigos2.length;j++) {
			if (arrCodigos[k]==arrCodigos2[j]) {
				trobat=true;
				jTrobat=j
				cantTrobat=arrCant2[j];
			}
		}
		
		if (trobat) {
			arrCant2[jTrobat]=parseInt(arrCant2[jTrobat])+parseInt(arrCant[k]);
		} else {
			//el codi no ha estat trobat, per tant l'afegim al segón array
			arrCodigos2.push(arrCodigos[k]);
			arrCant2.push(arrCant[k]);
		}
	}
	
	//guardam la cookie
	setCookieLT("CODIGOS",arrCodigos2.toString(),43200);
	setCookieLT("CANTIDADES",arrCant2.toString(),43200);
	
	window.location="carro.php";
}

function actualitzaCantidad(inp) {
	if (isNaN(parseInt(inp.value))) {
		alert($('hError').value);
	} else {
		var codigo=inp.nextSibling.value;
		
		if (getCookie("CODIGOS")) {
			arrCodigos=getCookie("CODIGOS").split(",");
			arrCant=getCookie("CANTIDADES").split(",");
			
			for (var k=0;k<arrCodigos.length;k++) {
				if (arrCodigos[k]==codigo) {
					arrCant[k]=inp.value;
				}
			}
			
			setCookieLT("CODIGOS", arrCodigos.toString(), 43200);
			setCookieLT("CANTIDADES", arrCant.toString(), 43200);
		}
	}
	
	window.location.reload();
}

function elimina(inp) {
	if (confirm($('hElimina').value)) {
		var codigo=inp.parentNode.previousSibling.previousSibling.lastChild.value;
		
		if (getCookie("CODIGOS")) {
			arrCodigos=getCookie("CODIGOS").split(",");
			arrCant=getCookie("CANTIDADES").split(",");
			
			for (var k=0;k<arrCodigos.length;k++) {
				if (arrCodigos[k]==codigo) {
					arrCodigos.splice(k,1);
					arrCant.splice(k,1);	
				}
			}
			
			setCookieLT("CODIGOS", arrCodigos.toString(), 43200);
			setCookieLT("CANTIDADES", arrCant.toString(), 43200);
			
			window.location.reload();
		}
	}
}

function compra() {
	window.location="reserva.php";
}

function gup( name ){
	var regexS = "[\\?&]"+name+"=([^&]*)";
	var regex = new RegExp ( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return"";
	else
		return results[1];
}
