/*
--  els primers dies
--  http://acte.pangea.org
--  funcions globals
*/


// ----------------------------------------------------------------------------
// adaptacio funcions MM

function buscaObjecte (n,d) {
  var p,i,x;
  if ( !d ) {
    d = document;
  }
  if ( ( p = n.indexOf("?") ) > 0 && parent.frames.length) {
    d = parent.frames[n.substring(p+1)].document;
    n = n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) {
    x=d.all[n];
  }
  for (i=0;!x&&i<d.forms.length;i++) {
    x = d.forms[i][n];
  }
  for (i=0;!x&&d.layers&&i<d.layers.length;i++) {
    x=buscaObjecte(n,d.layers[i].document);
  }
  if(!x && d.getElementById) {
    x=d.getElementById(n);
  }
  return x;
}


// ----------------------------------------------------------------------------
// adaptacio funcions MM

function canviaPropietat (objName, x, theProp, theValue) {
  var obj = buscaObjecte (objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}


// ----------------------------------------------------------------------------
// clic en una imatge per anar a una altra pagina

function go (url) {
    window.location = url;
}

// ----------------------------------------------------------------------------
// mostra la capa del text

function fnClic (t) {
    canviaPropietat ('txt-' + t,'','style.visibility','visible','layer')
    fnToca (t);
}

// ----------------------------------------------------------------------------
// amaga la capa del text

function fnOut (t) {
    canviaPropietat ('txt-' + t,'','style.visibility','hidden','layer')
}


// ----------------------------------------------------------------------------
// toca un so en l'internet explorer

function fnToca(so) {
  var thissound=eval("document." + so);
  thissound.Play();
}


// ----------------------------------------------------------------------------
// crea els objectes de so en una pagina

function imprimeixEmbed () {
    for (i=0; i < coses.length; i++) {
        document.write('<embed name="' + coses[i][0] + '" src="' + dir_audio + coses[i][0] + ext_audio + '" autostart="false" hidden="true" enablejavascript="true"></embed>\n');
    }
}


// ----------------------------------------------------------------------------
// quocient enter d'una divisio

function divEnt (a,b) {
    return (Math.floor(a/b));
}

// ----------------------------------------------------------------------------
// crea les capes amb els textos dels objectes

function imprimeixTextos () {
    for (i=0; i < coses.length; i++) {
        x = ((i%nc) * imgX) + ((i%nc) * sepX);
        y = oriY + sepY + hTitol + (divEnt(i,nc)*imgY) + (divEnt(i,nc)*sepY) + imgY - txtY;
        document.write('<div id="txt-' + coses[i][0] + '" class="textos" style="left: ' + x + 'px; top: ' + y + 'px;">' + coses[i][1] + '</div>');
    }
}

// ----------------------------------------------------------------------------
// crea les capes amb les imatges dels objectes

function imprimeixImatges () {
    for (i=0; i < coses.length; i++) {
        //x = oriX + ((i%nc) * imgX) + ((i%nc) * sepX);
        //y = oriY + (divEnt(i,nc)*imgY) + (divEnt(i,nc)*sepY);
        x = ((i%nc) * imgX) + ((i%nc) * sepX);
        y = sepY + hTitol + (divEnt(i,nc)*imgY) + (divEnt(i,nc)*sepY);
        document.write('<div class="imatge" style="left: ' + x + 'px; top: ' + y + 'px;">');
        document.write('<img src="' + dir_img + coses[i][0] + ext_img +
                       '" alt="' + coses[i][1] +
                       '" width="' + imgX +'" height="' + imgY + '" ' +
                       ' onClick="fnClic(\'' + coses[i][0] + '\');" ' +
                       ' onMouseOut="fnOut(\'' + coses[i][0] +'\');" >');
        document.write('</div>\n');
    }
}


// ----------------------------------------------------------------------------
// imprimeix el titol de la pagina

function imprimeixTitol (t) {

}

// ----------------------------------------------------------------------------
// imprimeix la pagina

function imprimeixPagina (tp,ps) {

    // imprimeix el contenidor
    document.writeln('<div class="contenidor" style="left:'+oriX+'px; top:'+(oriY)+'px; width:'+wContenidor+'px;">');

    // imprimeix el titol
    document.writeln('<div class="titolpagina" style="height:'+hTitol+'px;">');
    document.writeln('<div class="texttitol">'+tp+'</div>');
    document.writeln('</div>'); // final titol

    imprimeixImatges ();
    imprimeixTextos ();

    // imprimeix el peu
    nf = Math.ceil (coses.length / nc);
    y = hTitol + sepY + (nf*imgY) + ((nf-1)*sepY) + sepY;
    document.writeln('<div class="peupagina" style="top: '+y+'px; height:'+hPeu+'px;">');
    document.writeln('<div class="enrera"><a href="#" onclick="history.back();return false;"><img src="img/enrera.png" width="20" height="20" border="0" alt="ENRERA" title="ENRERA"></a></div>');
    document.writeln('<div class="textenrera"><a href="#" onclick="history.back();return false;">ENRERA</a></div>');

    if (ps != null) {
        document.writeln('<div class="continua"><a href="'+ps+'"><img src="img/continua.png" width="20" height="20" border="0" alt="CONTINUA" title="CONTINUA"></a></div>');
        document.writeln('<div class="textcontinua"><a href="'+ps+'">CONTINUA</a></div>');
    }

    document.writeln('</div>'); // final del peu


    document.writeln('</div>');  // contenidor
    imprimeixEmbed ();
}

// ----------------------------------------------------------------------------
// maximitza la finestra

function maximitzaFinestra () {
    self.moveTo(0,0);
    self.resizeTo(screen.availWidth,screen.availHeight);
}


