//Variables Usuari
var AmpleImatge=896; var AltImatge=481; var marge=20;

//Variables no tocar
var Centrat=0; var AmpleMenu=0; var AmpleTitol=0; var Vertical=0; 
var PosMenu=0; var tempX=0; var tempY=0; var timou=''; var x=0; semafor=false; var aleatori; var guardadaSencera;
var EstaFentCursorScroll=false; var OffsetScrollY=0; var TendenciaScroll=0; var AutoLoop=true; var kk='';

//Estandars
var IE = document.all?true:false;
var ns4=false; var ie4=false; var standard=false;
if (document.getElementById) {
  standard=true
} else if (document.all) {
  ie4=true
} else if (document.layers) {
  ns4=true
} else {
}

function mouCapaX(nomCapa,x) {
  if (ns4) document.layers[nomCapa].left=x;
  if (ie4) document.all[nomCapa].style.left=x;
  if (standard) document.getElementById(nomCapa).style.left=x;  
}

function mouCapa(nomCapa,x,y) {
  if (ns4) { document.layers[nomCapa].left=x; document.layers[nomCapa].top=y; }
  if (ie4) { document.all[nomCapa].style.left=x; document.all[nomCapa].style.top=y; }
  if (standard) { document.getElementById(nomCapa).style.left=x; document.getElementById(nomCapa).style.top=y; }
}

function vistaCapa(nomCapa,accio) {
  if (ns4) document.layers[nomCapa].visibility = accio;
  if (ie4) document.all[nomCapa].style.visibility = accio;
  if (standard) document.getElementById(nomCapa).style.visibility = accio;
}

//Imatge random
function imgRandom(nom,tope) {
  var src = 'img/' + url + '_';
  guardadaSencera = getCookie('imgrnd');
//alert("Cookie guardada: " + guardadaSencera)
  if (!guardadaSencera) {guardadaSencera='0'}

  while (guardadaSencera.length < posCookie+1) { guardadaSencera=guardadaSencera + '0'; }
//alert("Cookie allargada: " + guardadaSencera)
  var guardada = guardadaSencera.charAt(posCookie);

  aleatori = guardada;
  if (tope) {
	while (guardada == aleatori && (tope > 1 || aleatori == 0)) { aleatori = Math.floor(Math.random() * tope) + 1; }
  } else {
	while (guardada == aleatori && (quants > 1 || aleatori == 0)) { aleatori = Math.floor(Math.random() * quants) + 1; }
  }
//alert("Aleatori: "+aleatori)
  guardadaSencera = guardadaSencera.substring(0,posCookie) + aleatori + guardadaSencera.substring(posCookie+1,100);
//alert("Cookie que guardarem: " + guardadaSencera)
  setCookieLT('imgrnd', guardadaSencera, 5)

  var nom_src = '<img src="' + src + aleatori + '.jpg" id="' + nom + '" width="'+ AmpleImatge +'" height="'+ AltImatge +'" />';
  
  return nom_src;
}

//Cookies
function setCookieLT(name, value, dies, path, domain, secure)
{
  var hoy=new Date();
  var lifetime=new Date(hoy.getFullYear(),hoy.getMonth(),(hoy.getDate()+dies));
  setCookie(name, value, lifetime, path, domain, secure);
}
function setCookie(name, value, expires, path, domain, secure)
{
  document.cookie =
    name+"="+escape(value)+
    (expires ? "; expires="+expires.toGMTString() : "")+
    (path    ? "; path="   +path   : "")+
    (domain  ? "; domain=" +domain : "")+
    (secure  ? "; secure" : "");
}
function getCookie(name) {
  cookie = " "+document.cookie; offset = cookie.indexOf(" "+name+"=");
  if (offset == -1) return undefined;
  offset += name.length+2;
  end     = cookie.indexOf(";", offset)
  if (end == -1) end = cookie.length;
  return unescape(cookie.substring(offset, end));
}

// Mouse
function getMouseXY(e) {
if (!semafor) {
  semafor=true;
  if (timou!='') {
	clearTimeout(timou);
  }
  if (IE) { //para IE
	tempX = event.clientX + document.body.scrollLeft;
	tempY = event.clientY + document.body.scrollTop;
  } else { //para netscape
	tempX = e.pageX;
	tempY = e.pageY;
  }
  if (tempX < 0){tempX = 0;}
  if (tempY < 0){tempY = 0;}

  if ((tempY > Vertical && tempY < Vertical + AltImatge) && (tempX > Centrat && tempX < Centrat + AmpleImatge)) {
  	x = (Centrat + AmpleTitol) - Math.floor((tempX-(Centrat + AmpleTitol)) * ((AmpleMenu-(AmpleImatge-AmpleTitol)+30) / (AmpleImatge-AmpleTitol))) + 15 ;
  	if (x>Centrat + AmpleTitol) x=Centrat + AmpleTitol;
  	var tope = (Centrat) - AmpleMenu + AmpleImatge
  	if (x<tope) x=tope;

	desplaMenu(true);
  }
  semafor=false;
}
if (EstaFentCursorScroll) {
	mouScroll(tempY-OffsetScrollY);
	return false;
}
}

function desplaMenu(especial) {
  timou='';
  if (!semafor || especial) {
	semafor=true;
	var tou=true;
	if (x + 20 < posMenu) {
		xReal = posMenu - 5;
	} else if (x - 20 > posMenu) {
		xReal = posMenu + 5;
	} else if (x + 10 < posMenu) {
		xReal = posMenu - 3;
	} else if (x - 10 > posMenu) {
		xReal = posMenu + 3;
	} else if (x + 2 < posMenu) {
		xReal = posMenu - 1;
	} else if (x - 2 > posMenu) {
		xReal = posMenu + 1;
	} else {
		xReal=x; tou=false;
	}

	if (tou && timou=='') {
	  timou=setTimeout('desplaMenu(false)',10)
	}

	mouCapaX('MenuCapa',xReal);
  	document.getElementById('MenuCapa').style.clip = "rect(0px "+(Centrat-xReal+AmpleImatge)+"px 35px "+(Centrat-xReal)+"px)"
	posMenu=xReal;
	semafor=false;
  }
}

function redimensiona() {
  if (ns4) {location.reload(); } else {inic(); }
}
function inic(com) {
  AmpleMenu=parseInt(document.getElementById('MenuCapa').offsetWidth);
  AmpleTitol=0; //AmpleTitol=parseInt(document.getElementById('Titol').offsetWidth);
  document.getElementById('MenuCapa').style.clip = "rect(0px " + (AmpleImatge-AmpleTitol) + "px 35px 0px)"
  document.body.style.overflow='hidden';

  if (ns4) {
	var finestrax = window.innerWidth; 
	var finestray = window.innerHeight; 
  } else {
	var finestrax = document.body.clientWidth; 
	var finestray = document.body.clientHeight;
  }
  Centrat=Math.floor((finestrax - AmpleImatge) /2); if (Centrat<0) Centrat=0;
  Vertical=((finestray - AltImatge) /2)+10; if (Vertical<0) Vertical=0;
  mouCapa('MenuCapa', Centrat+AmpleTitol, 400+Vertical); mouCapa('MenuFons', Centrat, 400+Vertical); mouCapa('foto', Centrat, Vertical);
  mouCapa('adreca', Centrat, finestray-20); mouCapa('idiomes', Centrat, Vertical-55); mouCapa('Titol', Centrat, 364+Vertical);

  ample=parseInt(document.getElementById('texte').offsetWidth)+marge*2;
  alt=parseInt(document.getElementById('texte').offsetHeight)+marge*2;
  posX=parseInt(document.getElementById('texte').offsetLeft)-marge;
  posY=parseInt(document.getElementById('texte').offsetTop)-marge;
  document.getElementById('Contingut').style.width=ample;
  document.getElementById('Contingut').style.height=alt;
  mouCapa('Contingut', posX, posY);

  vistaCapa('MenuCapa','visible'); vistaCapa('MenuFons','visible'); vistaCapa ('foto','visible'); 
  vistaCapa ('adreca','visible'); vistaCapa ('idiomes','visible'); //vistaCapa ('Titol','visible');

  posMenu = Centrat + AmpleTitol;
  
  if (quants > 1) {
	mouCapa('BotonsHi',aleatori*25+1,1);
	mouCapa('Botons',AmpleImatge-10+Centrat-parseInt(document.getElementById('BotonsTop').offsetWidth),435+Vertical);
	vistaCapa('Botons','visible');
  }
  //alert (event.clientX)
  //alert (document.onload.pageX)

  if (!IE) document.captureEvents(Event.MOUSEMOVE)
  document.onmousemove = getMouseXY;
  
  if (com != 'fix') {
	  setTimeout('saltaImatgeAuto(1)',4000);
  }
}

function canviIdioma (n) {
	if (n==1) {alert("En construcció")}
	if (n==3 || n==4) {alert("Under construction")}
}

function saltaImatge(n) {
	AutoLoop=false;
  if (kk != "") {
	clearTimeout(kk);
	kk=""
  }
	saltaImatgeAuto(n)
}

function saltaImatgeAuto(n) {
	n=aleatori+n;
	if (n > quants) {n=1}
	if (n < 1) {n=quants}
	posaImatgeAuto(n);
}

function posaImatge(n) {
  if (kk != "") {
	clearTimeout(kk);
	kk=""
  }
  AutoLoop=false;
  posaImatgeAuto(n);
}

function posaImatgeAuto(n) {
  var src = 'img/' + url + '_' + n + '.jpg';
  document.getElementById('imatge').src=src;
  mouCapa('BotonsHi',n*25+1,1)
  aleatori=n;
  guardadaSencera = guardadaSencera.substring(0,posCookie) + aleatori + guardadaSencera.substring(posCookie+1,100);
  setCookieLT('imgrnd', guardadaSencera, 5)
  if (AutoLoop) {
	kk=setTimeout('saltaImatgeAuto(1)',4000);
  }
}

function botons() {
  var html_txt='';
  for (var a=1; a<=quants; a++) {
	html_txt=html_txt + '<td><a href="#" onClick="posaImatge(' + a + ')" class="boto"><img src="img/boto_' + a + '.gif" width="24" height="24" border="0"></a></td>';
  }
  return html_txt;
}

function botonsFons() {
  var html_txt='';
  for (var a=1; a<=quants; a++) {
	html_txt=html_txt + '<td><img src="img/pgris.gif" width="24" height="24" border="0"></td>';
  }
  return html_txt;
}

//SCROLL

function inicScroll() {
	document.getElementById('cursor').onmousedown = CursorScrollOn;
	document.onmouseup  = CursorScrollOff;
}

function CursorScrollOn(e) {
	OffsetScrollY = tempY - parseInt(document.getElementById('cursor').offsetTop);
	EstaFentCursorScroll=true;
	return false;
}
function CursorScrollOff(e) {
	EstaFentCursorScroll=false;
	return;
}
function a(e) {
	return false;
}

function pujaScroll() {
	TendenciaScroll=-4;
	setTimeout('autoScroll()',20)
}
function baixaScroll() {
	TendenciaScroll=4;
	setTimeout('autoScroll()',20)
}
function paraScroll() {
	TendenciaScroll=0;
}
function autoScroll() {
	mouScroll(parseInt(document.getElementById('cursor').offsetTop)+TendenciaScroll);
	if (TendenciaScroll != 0) {
		setTimeout('autoScroll()',20)
	}
}
function mouScroll(yyy) {
	if (yyy<CursorScrollY) yyy=CursorScrollY
	if (yyy>CursorScrollY+CursorScrollMaxDesp) yyy=CursorScrollY+CursorScrollMaxDesp
	mouCapa ('cursor',CursorScrollX,yyy);
	yyyy=(yyy-CursorScrollY-5) * ((parseInt(document.getElementById('scroll').offsetHeight)+20-ScrollEsVeuen) / CursorScrollMaxDesp);
	mouCapa ('scroll',ScrollposX,ScrollposY	- yyyy)
  	document.getElementById('scroll').style.clip = "rect("+(yyyy+10)+"px 220px "+(yyyy+ScrollEsVeuen-10)+"px 0px)"
}
