// copyright Roberto Bourgonjen 2008
var body,width,height,logo,logodiv,name,subtitle,iw,ih,lw,lh,tw,th,imagea,imageb;
var images = [['DSC02252-bwsmall.jpg',1012,1280],
              ['DSC02504-bwsmall.jpg',960,1280],
              ['DSC03101-bw.jpg',960,1179],
              ['DSC02518-bwsmall.jpg',960,1280],
              ['DSC02513-bwsmall.jpg',973,1280],
              ['DSC03125-bwsmall.jpg',960,1280]];
var image = 0;//Math.round(Math.random() * (images.length - 1));
function scale (){
  if(!imagea){
     body = document.body;
     imagea = document.getElementById('main-image');
     imagea.src = images[image][0];
     imageb = document.createElement("img");
     imageb.className = 'preload';
     imageb.src = images[(image + 1) % images.length][0];
     document.body.appendChild(imageb);
     logo = document.createElement("img");
     logodiv = document.getElementById("logodiv");
     logodiv.appendChild(logo);
     name = document.getElementById('name');
     subtitle = document.getElementById('subtitle');
     txt = document.getElementById('content');
  }
  width = images[image][1];
  height = images[image][2];
  tw  = document.getElementById('page-left').offsetWidth;
  th = document.getElementById('page-left').offsetHeight;
  if((width / height) > (tw / th)){
     ih = th;
     iw = (th / height) * width;
     imagea.height = ih;
     imagea.width = iw;
     imagea.style.marginTop = '';
     imagea.style.marginLeft = ((tw - iw) / 2) + 'px';
  }
  else{
    iw = tw;
    ih = (tw / width) * height;
    imagea.width = iw;
    imagea.height = ih;
    imagea.style.marginLeft = '';
    imagea.style.marginTop = ((th - ih) / 2) + 'px';
  }
  lw = logodiv.offsetWidth;
  lh = logodiv.offsetHeight;
  if(lw / lh > 280 / 212){
     lw = (280 / 212) * lh;
  }
  lw = lw - (lw % 10);
  if(lw > 400){
    lw = 400;
  }
  else if(lw < 50){
    lw = 50;
  }
  lh = (lw * (212 / 280));
  logo.style.marginLeft = ((logodiv.offsetWidth - lw) / 2) + 'px';
  logo.src = "logo-" + lw + '.gif';
  tw = txt.offsetWidth;
  th = txt.offsetHeight;
  justify(subtitle,tw);
  justify(name,tw);
  name.style.top = (logodiv.offsetTop + (lh * 0.48) - name.offsetHeight) + 'px';
  subtitle.style.top = (logodiv.offsetTop + (lh * 0.52)) + 'px';
}

function toggleimg(){
   image = (image + 1) % images.length;
   imagea.src = imageb.src;
   imageb.src = images[(image + 1) % images.length][0];
   scale();
}

function justify(el,w){
  var j = 2;
  var direction = 0;
  var steps = 0;
  var step = 0.2;
  var s = el.offsetHeight;
  while(w != el.offsetWidth && steps < 300){
    if(w > el.offsetWidth){
       if(direction < 0){
         if(step < 0.01){
           direction = 0;
           break;
         }
         step = step / 2;
       }
       direction = 1;
      j += step;
    }
    else{
       if(direction > 0){
         step = step / 2;
       }
       direction = -1;
       j -= step;
    }
    if(j > 0){
      el.style.fontSize = j + 'pt';
    }
    //el.style.letterSpacing = (j * 0.3) + 'em';
    steps++;
  }
  return j;
}

function docsize() {
  if (window.innerHeight) {
    return [window.innerWidth, window.innerHeight];
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    return [document.documentElement.clientWidth, document.documentElement.clientHeight];
  }
  else if  (document.body) {
    return [document.body.clientWidth, document.body.clientHeight];
  }
}

