//
// Author Manuel Mollar
// Use this script for free, but never remove the author notice, even if you modify it
// For more info mail to mm @at@ mobelt.com
//

var iden, ancho, alto, minsep, minleft, pov, poh, topleft, bottom, firstcent, center, rand,
    ftop= new Array, fleft= new Array, fwidth= new Array, fheight= new Array, curonscr, activo=false,
    zonas= new Array("t","l","c","b");

function ocultos() {
  ocul=iden+'oculto=,';
  for (z in zonas)
    for (i= (zonas[z] == 'c') ? firstcent : 0; ; i++) {
      try { l=zonas[z]+i;
        if (document.getElementById(l).style.visibility != 'visible')
	  ocul+=l+',';
       }
      catch (e) { break; }
     }
  document.cookie=ocul+"; expires=Fri, 31-Dec-2010 00:00:00";
 }

function maximizap(este,prof,tout) {
  for (i= 0; i<prof; i++)
    este=este.parentNode;
  clearTimeout(sti);
  with (este) {
    with (style) {
      left=5;
      top=5;
      x=document.body.scrollWidth;
      width=((x > ancho) ? x : ancho)-10;
      x=document.body.scrollHeight;
      height=((x > alto) ? x : alto)-10;
      zIndex=9999;
      visibility='visible';
     }
    if (tout)
      setTimeout('restaurar("'+id+'")',tout*1000);
   }
 }

function maximiza(aquien,tout) {
  maximizap(document.getElementById(aquien),0,tout);
}

function restaurap(este,prof,tout) {
  for (i= 0; i<prof; i++)
    este=este.parentNode;
  with (este) {
    with (style)
      if (width == fwidth[id]) {
        if (! tout)
          ocultarp(este,0);
       }
      else {
	left=fleft[id];
	top=ftop[id];
	width=fwidth[id];
	height=fheight[id];
	zIndex=0;
        visibility='visible';
       }
    if (tout)
      setTimeout('ocultar("'+id+'")',tout*1000);
   }
 }

function restaura(aquien,tout) {
  restaurap(document.getElementById(aquien),0,tout);
}

function restaurar(aquien,tout) {
  restaura(aquien,tout);
  visible(aquien);
  recoloca();
}

function conmutap(este,prof,tout) {
  for (i= 0; i<prof; i++)
    este=este.parentNode;
  with (este)
    with (style)
      if (width == fwidth[id])
        maximizap(este,0,tout);
      else
        restaurap(este,0);
 }

function conmuta(aquien,tout) {
  conmutap(document.getElementById(aquien),0,tout);
 }

function ocultap(este,prof,tout) {
  for (i= 0; i<prof; i++)
    este=este.parentNode;
  with (este) {
    with (style) {
      visibility='hidden'; top=0; left=0;
      zIndex= 0;
      try { // solo para las clases estandard
        width=fwidth[id];
        height=fheight[id];
       }
      catch(e) { }
     }
    if (tout)
      setTimeout('visible("'+id+'"); ocultos(); recoloca()',tout*1000);
    else
      ocultos();
   }
 }

function oculta(aquien,tout) {
  ocultap(document.getElementById(aquien),0,tout);
 }

function ocultarp(este,prof,tout) {
  ocultap(este,prof,tout);
  recoloca();
}

function ocultar(aquien,tout) {
  ocultarp(document.getElementById(aquien),0,tout);
 }


function reubicaz(nz,izq,arr,anc) {
  var t=izq+minsep; p=arr; m=0; mm=anc;
      ib=(nz == 'c') ? firstcent : 0;
      c=0; cortar=false;
  for (i= ib; ; i++)
    try {
      with (document.getElementById(nz+i)) {
        cortard= (lang == 'break');
	if (style.visibility=="visible") {
	  tf=t+offsetWidth+minsep;
	  if ((tf > anc) || cortar) {
	    d=(anc-t)/(c+1); dd=d;
	    if ((d > 0) && center)
	      while (ib < i) {
		if (document.getElementById(nz+ib).style.visibility=='visible') {
		  fleft[nz+ib]+=dd;
		  dd+=d;
		 }
		ib++;
	       }
	    if (i > 0)
	      p+=m+minsep;
	    t=izq+minsep;
	    tf=t+offsetWidth+minsep;
	    if (tf > mm)
	      mm=tf;
	    m=0; ib=i; c=0;
	    cortar=false;
	   }
	  ftop[nz+i]=p;
	  fleft[nz+i]=t;
	  t=tf;
	  c++;
	  if (offsetHeight >m)
	    m=offsetHeight;
	 }
	else {
	  ftop[nz+i]=0;
	  fleft[nz+i]=0;
	 }
	if (cortard)
	  cortar= true;
       }
     }
    catch (e) { break; }
  d=(anc-t)/(c+1); dd=d;
  if ((d > 0) && center)
    while (ib < i)  {
      if (document.getElementById(nz+ib).style.visibility=='visible') {
	fleft[nz+ib]+=dd;
	dd+=d;
       }
      ib++;
     }
  p+=m+minsep;
  pov=p;
  poh=mm;
 }

function reubica() {
  ancho=document.body.clientWidth;
  alto=document.body.clientHeight;
  if (topleft) {
    reubicaz("t",0,0,ancho);
    povt=pov;
    reubicaz("l",0,pov,minleft);
    pohl=poh;
    povl=pov;
   }
  else {
    reubicaz("l",0,0,minleft);
    pohl=poh;
    povl=pov;
    reubicaz("t",pohl,0,ancho);
    povt=pov;
   }
  reubicaz("c",pohl,povt,ancho);
  povc=pov;
  reubicaz("b",pohl,povc,ancho);
  if ((pov < povl) && bottom)
    reubicaz("b",pohl,povl-pov+povc,ancho);
  document.cookie=iden+"opciones='minsep="+minsep+
    "!minleft="+minleft+
    "!topleft="+topleft+
    "!bottom="+bottom+
    "!firstcent="+firstcent+
    "!center="+center+
    "!rand="+rand+
    "'; expires=Fri, 31-Dec-2010 00:00:00";
 }

var sti=0;
function recoloca() {
  reubica();
  clearTimeout(sti);
  flotantes();
  movv();
 }

function movv() {
  var cont=false;
  for (z in zonas)
    for (i= (zonas[z] == 'c') ? firstcent : 0; ; i++) {
      try { l=zonas[z]+i;
	with (document.getElementById(l)) {
	  v=ftop[l]-offsetTop;
	  if (Math.abs(v) > 80)
	    desp= 80;
	  else
	    desp= 20;
	  if (Math.abs(v) > 20) {
	    v=(v > 0) ? desp : -desp;
	    style.top= offsetTop+v;
	    cont=true;
	   }
	  else style.top=ftop[l];
	  v=fleft[l]-offsetLeft;
	  if (Math.abs(v) > 80)
	    desp= 80;
	  else
	    desp= 20;
	  if (Math.abs(v) > 20) {
	    v=(v > 0) ? desp : -desp;
	    style.left= offsetLeft+v;
	    cont=true;
	   }
	  else style.left=fleft[l];
	 }
       }
      catch (e) { break; }
     }
  if (cont)
    sti=setTimeout("movv()",100);
 }

function fotovisible(cual) {
  for (j=0; ; j++) {
    try { with (document.getElementById("foto_"+cual+"_"+j)) src=lang; }
    catch(e) { break; }
   }
}

function visible(cual) {
  document.getElementById(cual).style.visibility="visible";
  fotovisible(cual);
}

function instala(viden,vcenter,vtopleft,vbottom,vminsep,vminleft,vfirstcent,vrand,fuerza) {

  //window.top.moveTo(0,0);
  //window.top.resizeTo(window.screen.availWidth,window.screen.availHeight);
  //window.top.resizeTo(800,600);
  ancho=document.body.clientWidth;
  alto=document.body.clientHeight;
  iden=viden || alert('se necesita un identificador');
  minsep=vminsep || 0;
  minleft=vminleft || 0;
  topleft=vtopleft || false;
  bottom=vbottom || false;
  firstcent=vfirstcent || 0;
  center=vcenter || false;
  rand=vrand || 0; // 1 la prime ver, 0 nunca 2 siempre
  if (! fuerza)
    try { eval(document.cookie.replace(new RegExp(".*"+iden+"opciones='([^']*)'.*"),"$1;").replace(/!/g,';')); } catch(e) {};
  if ((rand > 0) && ((rand == 2) || (document.cookie.indexOf(iden+'oculto=') < 0)))
    for (z in zonas)
      for (i= (zonas[z] == 'c') ? firstcent : 0; ; i++) {
	try { l=zonas[z]+i;
	  with (document.getElementById(l)) {
	    style.top=Math.floor(Math.random()*(alto-offsetHeight));
	    style.left=Math.floor(Math.random()*(ancho-offsetWidth));
	    fotovisible(l);
	   }
	 }
	catch (e) { break; }
       }
  else
    for (z in zonas)
      for (i= (zonas[z] == 'c') ? firstcent : 0; ; i++) {
	try { l=zonas[z]+i;
	  if ((document.cookie.search(iden+'oculto=[^;]*,'+l+',') < 0) && document.getElementById(l).style)
	    fotovisible(l);
	 }
	catch (e) { break; }
       }
  for (z in zonas)
    for (i= 0; ; i++) {
      try { l=zonas[z]+i;
	with (document.getElementById(l).style) {
	  fwidth[l]=width;
	  fheight[l]=height;
	 }
       }
      catch (e) { break; }
     }
}

function flotantes() {
  ancho=document.body.clientWidth;
  alto=document.body.clientHeight;
  for (i=0; ;i++) {
    try { l='f'+i;
      with (document.getElementById(l)) {
	p=ftop[l];
	p=(p >0 ) ? p : alto+p-offsetHeight;
        style.top=p+document.body.scrollTop;
	p=fleft[i];
	p=(p >0 ) ? p : ancho+p-offsetWidth;
        style.left=p+document.body.scrollLeft;
       }
     }
    catch (e) { break }
   }
  try { curonscr(); } catch (e) {}
}

function inicia() {
  for (z in zonas)
    for (i= (zonas[z] == 'c') ? firstcent : 0; ; i++) {
      try { l=zonas[z]+i;
	if (document.cookie.search(iden+'oculto=[^;]*,'+l+',') < 0)
	  document.getElementById(l).style.visibility="visible";
       }
      catch (e) { break; }
     }
  reubica();
  segvez=(document.cookie.indexOf(iden+'oculto=') >= 0);
  if ((rand == 0) || (segvez && (rand < 2)) ) {
    for (z in zonas)
      for (i= (zonas[z] == 'c') ? firstcent : 0; ; i++) {
	try { l=zonas[z]+i;
	  with (document.getElementById(l).style) {
	    top=ftop[l];
	    left=fleft[l];
	   }
	 }
	catch (e) { break; }
       }
   }
  setTimeout("movv()",100);
  ocultos();
  for (i=0; ;i++) {
    try { l='f'+i;
      with (document.getElementById(l)) {
        ftop[l]=offsetTop;
        fleft[i]=offsetLeft;
       }
     }
    catch (e) { break; }
   }
  try { document.getElementById('w').style.visibility='hidden' ; } catch (e) {}
  if (i > 0) {
    curonscr=window.onscroll;
    window.onscroll=flotantes;
    flotantes();
    for (i=0; ;i++) {
      try { document.getElementById('f'+i).style.visibility='visible' ; }
      catch (e) { break; }
     }
   }
  window.onresize=recoloca;
  activo=true;
  return ! segvez;
}

function resetea() {
  for (z in zonas)
    for (i= (zonas[z] == 'c') ? firstcent : 0; ; i++) {
      try { visible(zonas[z]+i); }
      catch (e) { break; }
     }
  recoloca();
  ocultos();
 }

function activap(este,prof,tout) {
  for (i= 0; i<prof; i++)
    este=este.parentNode;
  clearTimeout(sti);
  with (este) {
    fotovisible(id);
    with (style) {
      zIndex=9999;
      x=(ancho-offsetWidth)/2+document.body.scrollLeft;
      style.left=(x > 0) ? x : 0 ;
      x=(alto-offsetHeight)/2+document.body.scrollTop;
      style.top=(x > 0) ? x : 0 ;
      style.visibility='visible';
     }
    ocultos();
    if (tout)
      setTimeout('oculta("'+id+'")',tout*1000);
    return style;
   }
 }

function activa (aquien,tout) {
  activap(document.getElementById(aquien),0,tout);
 }

var tipti = 0;
function tip(lax,lay,tip,txt,ret,tout) {
  if ((ret) && (ret > 0)) {
    clearTimeout(tipti);
    tipti=setTimeout('tip('+lax+','+lay+',"'+tip+'","'+txt+'",0,'+tout+')',ret*1000);
    return
   }
  tipo=document.getElementById(tip);
  with (tipo)
    with (style) {
      if (txt.length > 0 )
	innerHTML=txt;
      lax=lax+X; z=document.body.clientWidth;
      if (lax+offsetWidth > z)
        lax=z-offsetWidth;
      left=lax;
      lay=lay+Y; z=document.body.clientHeight;
      if (lay+offsetHeight > z)
        lay=z-offsetHeight;
      top=lay;
      zIndex=99999;
      visibility='visible';
     }
  if (tout)
    setTimeout("oculta('"+tip+"',0)",tout*1000);
 }
    
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

var X = 0
var Y = 0

function getMouseXY(e) {
  if (IE) {
    X = event.clientX + document.body.scrollLeft;
    Y = event.clientY + document.body.scrollTop;
   }
  else {
    X = e.pageX;
    Y = e.pageY;
   }
}



