var noIniciada=true;
var sonant=0;

function disableselect(e)
{
  return false
}
function reEnable()
{
  return true
}
//if IE4+

	document.onselectstart=new Function ("return false")
	//if NS6
	if (window.sidebar)
	{
	  document.onmousedown=disableselect
	  document.onclick=reEnable
	}


window.onload=function() {
	
	var so = new SWFObject('src/mediaplayer.swf','jstest','1','1','8');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addVariable('width','400');
		so.addVariable('height','220');
		so.addVariable('file','src/zen.mp3');
		so.addVariable('javascriptid','jstest');
		so.addVariable('enablejs','true');
		so.addVariable('frontcolor','0xFFFFFF');
		so.addVariable('lightcolor','0xFFFFFF');
		so.addVariable('screencolor','0xFFFFFF');
		so.write('flashbanner');
	
	var cod=gup('cod');
	var viajes=gup('viajes');
	var not=gup('not');
	
	if (cod!="") {
		obreNode(cod);
	}
	
	if (viajes=="1") {
		var so2=new SWFObject('src/topviajes.swf','jstest2','899','228','8');
		so2.write('flash');
	} else {
		var so2=new SWFObject('src/topcomparte.swf','jstest2','899','228','8');
		so2.write('flash');
	}
	
	var img=$('imgMus');
	
	if (img) {
		img.parentNode.removeChild(img);
	}
	
	var img2=$('imgPart');
	
	if (img2) {
		img2.parentNode.removeChild(img2);
	}
	
	//$('imgPart').src="img/partoOff.jpg";
	
	var dMenuItem=$('dtree0').childNodes;
	
	for (var k=0;k<dMenuItem.length;k++) {
		if (dMenuItem[k].className=="dTreeNode") {
			dMenuItem[k].removeChild(dMenuItem[k].firstChild);
			dMenuItem[k].removeChild(dMenuItem[k].firstChild);
			dMenuItem[k].style.backgroundImage="url('img/fondomenu.jpg')";
			dMenuItem[k].style.height="17px";
			dMenuItem[k].style.padding="4px 5px";
		}
	}
	
	var dMenuItem2=$('menuViajes').firstChild.firstChild.childNodes;
	
	for (var k=0;k<dMenuItem2.length;k++) {
		if (dMenuItem2[k].className=="dTreeNode") {
			dMenuItem2[k].removeChild(dMenuItem2[k].firstChild);
			dMenuItem2[k].removeChild(dMenuItem2[k].firstChild);
			dMenuItem2[k].style.backgroundImage="url('img/fondomenu.jpg')";
			dMenuItem2[k].style.height="17px";
			dMenuItem2[k].style.padding="4px 5px";
		}
	}
}

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];
}

function obreNode(cod) {
	
	var d2=$('stree'+cod);
	
	if (d2) {
		d2.style.color='#DB38A3';
	}
	
	var d=$('dtree'+cod);
	
	if (d) {
	
		if (d.childNodes.length>0) {
			d.style.display='block';
		}
		
		while (d.parentNode.className=="clip") {
			var cod=d.parentNode.id.substring(5);
			var d=$('dtree'+cod);
			d.style.display='block';
		}
		
	}
	
	
}

function enviar() {
	
	if (($('nombre').value=="") || ($('apellidos').value=="") || ($('telefono').value=="") || ($('mail').value=="") || ($('mensaje').value=="")) {
		alert("Debe completar todos los datos.");
		return;
	}
	
	var url="mail.php";
	var params="nombre="+$('nombre').value+"&apellidos="+$('apellidos').value+"&tel="+$('telefono').value+"&mail="+$('mail').value+"&mensaje="+$('mensaje').value;
	var ajax=new Ajax.Request ( url, {
		method: 'post',
		postBody: params,
		onLoading: function() {
					buida($('msg'));
					var t=document.createTextNode('Enviando mensaje...');
					$('msg').style.visibility='visible';
					$('msg').appendChild(t);
				},
		onComplete:function(reply) {
					if (reply.responseText=="Err") {
						var txt="Ha ocurrido un error al enviar el mensaje.";
					} else {
						var txt="El mensaje se ha enviado satisfactoriamente.";
					}
					buida($('msg'));
					var t=document.createTextNode(txt);
					$('msg').appendChild(t);
					
					if (reply.responseText=="Ok") {
						$('nombre').value="";
						$('apellidos').value="";
						$('telefono').value="";
						$('mail').value="";
						$('mensaje').value="";
					}
				}
		});	
}

function arranca() {
	
	if (noIniciada) {
		noIniciada=false;
		
		sonant=0;
		setTimeout("sendEvent('jstest','playpause')",2000);
	}
}

function sendEvent(swf,typ,prm) {
	
  thisMovie(swf).sendEvent(typ,prm);
  
};

function getUpdate(typ,pr1,pr2,swf) {};

function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
	  return window[swf];
  } else {
    return document[swf];
  }
};

function loadFile(swf,obj) { 
  thisMovie(swf).loadFile(obj); 
};

function arrancaMusica(img) {
	if (img.src.indexOf("musicaOn.jpg")!=-1) {
		img.src="img/musicaOff.jpg";
	} else {
		img.src="img/musicaOn.jpg";	
	}
	
	if (sonant==0) {
		sendEvent('jstest','playpause');
	} else {
		$('imgPart').src="img/partoOff.jpg";
		sonant=0;
		sendEvent('jstest','playpause');
		loadFile('jstest',{file:'src/zen.mp3'});
		sendEvent('jstest','playpause');
	}
}

function arrancaPart(img) {
	if (img.src.indexOf("partoOn.jpg")!=-1) {
		img.src="img/partoOff.jpg";
	} else {
		img.src="img/partoOn.jpg";	
	}
	
	if (sonant==1) {
		sendEvent('jstest','playpause');
	} else {
		//$('imgMus').src="img/musicaOff.jpg";
		sonant=1;
		sendEvent('jstest','playpause');
		loadFile('jstest',{file:'src/part.mp3'});
		sendEvent('jstest','playpause');
	}
}

function buida(el){//clearText(el) elimina tots els "child nodes" de l'element que li passam per parámetre
    if(el!=null){
        if(el.childNodes){
            for(var i=el.childNodes.length-1;i>=0;i--){
                var childNode=el.childNodes[i];
                el.removeChild(childNode);
            }
        }
    }
}