var doAll = (document.all!=null);
var doLayers = (document.layers!=null);
var isHell = navigator.userAgent.indexOf('MSIE') >= 0;
var isHellMac = isHell && navigator.platform.indexOf('Mac') >= 0;

function genGetElementById(id) {
    if (doAll) 
        return document.all[id];
    else if (doLayers)
        return document.layers[id];
    else
        return document.getElementById(id);
}

var imgMarginBt = 10;
var imgWidth = 0;
var mainDiv = null;
var photoDiv = null;
var titleDiv = null;
var footerDiv = null;
var sideAnchors = [];
var sidePics = [];
var watchSpan;
var watchHeight = -1;


function absTop(o) {
    if (o.offsetParent == null)
        return o.offsetTop;
    else
        return o.offsetTop + absTop(o.offsetParent);
}

function winHeigth() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    return window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight) {
    //IE 4 compatible
    return document.body.clientHeight;
  }
  return 0;
}


function subHeight(o) {
    var fh = 0;
    var kids = o.childNodes;
    for (var i = 0; i < kids.length; i++) {
        var kid = kids[i];
        var h = kid.offsetTop + kid.offsetHeight;
        if (h > 0)
            fh = Math.max(fh, h);
    }
    return fh;
}

function setup() {
    var theDivs = document.getElementsByTagName("div");
    imgWidth = 160;
    var j = 0;
    var k = 0;
    for (var i = 0; i < theDivs.length; i++) {
        s = theDivs[i];
        if (s.className == "sidePic") {
            sidePics[j++] = s;
            var top = s.offsetTop;
            var link = s.getElementsByTagName("a")[0];
            var img = link.getElementsByTagName("img")[0];
            imgWidth = Math.max(imgWidth, img.width);
        }
    }
    var theSpans = document.getElementsByTagName("span");
    for (var i = 0; i < theSpans.length; i++) {
        s = theSpans[i];
        if (s.className == "sideAnchor")
            sideAnchors[k++] = s;
    }
    if (j != k) {
        alert("#sideAnchor ("+k+") != #sidePic ("+j+")");
    }
    photoDiv =  genGetElementById("photoList");
    photoDiv.style.width = (imgWidth + 40)+"px";

    mainDiv =  genGetElementById("main");
    imgWidth = photoDiv.offsetWidth; // should be the same... right!
    
    if (isHellMac) {
        mainDiv.style.marginRight = (imgWidth + 15)+"px";
        mainDiv.style.right = "0px";
    } else {
        mainDiv.style.right = (imgWidth + 15)+"px";
    }
    
    titleDiv =  genGetElementById("mainTitle");
    watchSpan =  genGetElementById("sizeWatch");
    watchHeight = watchSpan.offsetHeight;
    setTimeout("textSizeWatch();",1000);
    footerDiv = genGetElementById("footer");
    if (isHell) {
        // It can be done, but NOT worth the bother.
        // Get a real browser.
        footerDiv.style.position = "absolute";
    } else {
        footerDiv.style.position = "fixed";
    }
    resize();
}

function resize() {
    var titleBot = Math.max(titleDiv.offsetHeight, subHeight(titleDiv)) + 10;
    mainDiv.style.paddingTop = titleBot+"px";
    photoDiv.style.paddingTop = titleBot+"px";

    var minPos = titleBot;
    for (var i = 0; i < sideAnchors.length; i++) {
        var anchor = sideAnchors[i];
        var imageSpan = sidePics[i];
        var top = absTop(anchor);
        var link = imageSpan.getElementsByTagName("a")[0];
        var img = link.getElementsByTagName("img")[0];
        var newPos = Math.max(minPos, top - Math.round(img.height * 0.2 ));
        imageSpan.style.marginTop = (newPos-minPos)+"px";
        if (isHellMac) {
            minPos = newPos + img.height + imgMarginBt;
        } else {
            minPos = imageSpan.offsetTop + imageSpan.offsetHeight + imgMarginBt;
        }
    }
    photoDiv.style.height = 10+Math.max(subHeight(photoDiv), mainDiv.offsetHeight)+"px";
    if (isHell) {
        footerDiv.style.top = (Math.max(photoDiv.offsetHeight, mainDiv.offsetHeight)-footerDiv.offsetHeight)+"px";
    }
}

function textSizeWatch() {
    if (watchHeight != watchSpan.offsetHeight) {
        watchHeight = watchSpan.offsetHeight;
        resize();
    }
    setTimeout("textSizeWatch();",300);
}

//alert("this.dataBrowser:"+this.dataBrowser+", navigator.userAgent:"+navigator.userAgent+", navigator.vendor:"+navigator.vendor+", navigator.platform:"+navigator.platform);

