/******************************************
  Browser independant page-specific code
  for http://www.baggesen.org/
  (c) Copyright 2001 J.C.Ross
******************************************/
// N.B. This DOES work in Gecko (NS6), but I'm
// dissapointed by the performance.
// (I had to do some enormous work-arounds
// to get the scrolling working at all quickly)
// Bugs still appear, in the form of buttons
// dissapearing.  This is a Netscape 6 bug!

var shut;
var loadedOnce;
var curCommand = "";
var oldCommand = "";
var textLayer;
var scrollOn = false;

var titim;
var bottomdivImage;

var pictureImage;
var mailImage;
var linksImage;
var cvImage;
var copyImage;
var upImage;
var downImage;

function layoutPage()
{
    beginCritical('layoutPage')
    imagesToLoad = 3;
    imagesLoaded = 0;
    callback = 'onTitleLoaded()';

    titleLayer = new Layer("title");
    infoLayer = new Layer("info");
    navigationLayer = new Layer("navigation");
    bottomLayer = new Layer("bottomdiv");
    topmaskLayer = new Layer("toplayer");
    bottommaskLayer = new Layer("bottomlayer");
    scrollUpLayer = new Layer("scrollup");
    scrollDownLayer = new Layer("scrolldown");
    textLayer = new Layer("picturetext");
    loaderLayer = new Layer("loader");

    titleLayer.style.visibility = 'hidden';
    navigationLayer.style.visibility = 'hidden';
    bottomLayer.style.visibility = 'hidden';

    titim = createImage("images/title.gif");
    bottomdivImage = createImage("images/bottomdiv.gif");
    dummyImage = createImage("images/blank.gif");

}

function setInfo(text) {
    infoLayer.style.visibility = 'hidden';
    writeContent(infoLayer.content, "<p class='info'>"+text+"</p>");
    infoLayer.style.visibility = 'visible';
}

var theTimer;

var topmaskHeight;

function onTitleLoaded()
{
    beginCritical('onTitleLoaded0');
    setInfo("");
    infoLayer.style.visibility = 'hidden';
    loaderLayer.style.visibility = 'hidden';

    imagesLoaded = 0;
    imagesToLoad = 24;

    callback = 'onImagesLoaded()';

    titleLayer.getImage("tit").src = titim.src;
    bottomLayer.getImage("bottom").src = bottomdivImage.src;
    titleLayer.style.left = pixelLength(
 (available_width - titleLayer.getWidth())/2);
    titleLayer.style.top = pixelLength(
 (available_height - titleLayer.getHeight())/2);

    bottommaskLayer.style.width = pixelLength(available_width);
    topmaskLayer.style.width = pixelLength(available_width);

    bottommaskLayer.style.height = pixelLength(
          (available_height + titleLayer.getHeight())/2);
    topmaskHeight = (available_height + titleLayer.getHeight())/2;
 //    alert(topmaskHeight);
    topmaskLayer.style.height = pixelLength(topmaskHeight);
    if (is.ns4) {
        writeContent(bottommaskLayer.content,
            "<table class='mask' width=100% height=100%><tr>" +
            "<td class='mask' bgcolor=white>&nbsp</td></tr></table>");
        writeContent(topmaskLayer.content,
            "<table class='mask' width=100% height='" +
  topmaskHeight + "'><tr>" +
            "<td class='mask' bgcolor=white>&nbsp</td></tr></table>");
    }

    titleLayer.style.visibility = 'visible';

    infoLayer.style.top = pixelLength(parseInt(titleLayer.style.top) + 75);

    if (!sizeCheck()) return;

    beginCritical('onTitleLoaded1');

    theTimer = setTimeout('loadProgress()',2500);

    // define menu images:
    pictureImage = navigationLayer.getImage("picture");
    mailImage = navigationLayer.getImage("mail");
    //ie5+mac bug: 'links' is predefined object!
    linksImage = navigationLayer.getImage("lnks");
    cvImage = navigationLayer.getImage("cv");
    copyImage = navigationLayer.getImage("copy");
    upImage = scrollUpLayer.getImage("up");
    downImage = scrollDownLayer.getImage("down");

    // load the rest of the images
    picture1Image = createImage("images/picture.gif");
    picture2Image = createImage("images/picture2.gif");
    picture3Image = createImage("images/picture3.gif");
    picture4Image = createImage("images/picture4.gif");

    mail1Image = createImage("images/mail.gif");
    mail2Image = createImage("images/mail2.gif");
    mail3Image = createImage("images/mail3.gif");
    mail4Image = createImage("images/mail4.gif");

    cv1Image = createImage("images/cv.gif");
    cv2Image = createImage("images/cv2.gif");
    cv3Image = createImage("images/cv3.gif");
    cv4Image = createImage("images/cv4.gif");

    links1Image = createImage("images/links.gif");
    links2Image = createImage("images/links2.gif");
    links3Image = createImage("images/links3.gif");
    links4Image = createImage("images/links4.gif");

    copy1Image = createImage("images/info.gif");
    copy2Image = createImage("images/info2.gif");
    copy3Image = createImage("images/info3.gif");
    copy4Image = createImage("images/info4.gif");

    up1Image = createImage("images/up.gif");
    up2Image = createImage("images/up2.gif");
    down1Image = createImage("images/down.gif");
    down2Image = createImage("images/down2.gif");

    endCritical();
}

function sizeCheck()
{
    if (available_width < 500 || available_height < 400) {
        openWindow("index.html", "_blank", 640, 480, "resizable=yes");
        setInfo("Window should be at least 500x400");
 endCritical();
        return false;
    }
    return true;
}

function openGallery(name, width, height)
{
    setInfo("Loading gallery in new window...");
    openWindow(name + "/" + name + ".html", name, width, height);
    setTimeout("setInfo('');", 1000);
}

function onImagesLoaded()
{
    clearTimeout(theTimer);
    setInfo("");

    layoutNavigation();
    setTimeout("layoutClose()", 1000);
}

function loadProgress()
{
    if (imagesLoaded<imagesToLoad) {
 setInfo("Loading... [ "+imagesLoaded+" / "+imagesToLoad+" ]");
 setTimeout("loadProgress()",2500);
    } else setInfo("");
}

function layoutClose()
{
    scrollUpLayer.style.visibility = 'hidden';
    scrollDownLayer.style.visibility = 'hidden';

    titleCoord = (available_height-titleLayer.getHeight())/2-50;
    bottomCoord = titleCoord+86;

 // slide em.  Set state 'shut'.
    slideMenus(titleCoord, bottomCoord, "onLayoutClose();");
}

function onLayoutClose() {
    shut = true;
 //alert("oldCommand: "+oldCommand+", curCommand: "+curCommand);
    if (oldCommand != "") {
        highlight(oldCommand, false);
    }
    if (curCommand != "") {
        select(curCommand);
    } else {
        selecting = false;
    }
}

function layoutOpen(margin, command) {
 //alert("Opening");
    shut = false;
    titleCoord = margin;
    bottomCoord = available_height - titleLayer.getHeight() - margin;
    slideMenus(titleCoord, bottomCoord, command);
}

function cleanupPage() {
    scrollUpLayer.style.visibility = 'hidden';
    scrollDownLayer.style.visibility = 'hidden';
    textLayer.style.visibility = 'hidden';
    textLayer.style.display = 'none';
    titleLayer.style.visibility = 'hidden';
    bottomLayer.style.visibility = 'hidden';
    navigationLayer.style.visibility = 'hidden';
}

function slideMenus(titCoord, bottomCoord, notify) {
    layers = new Array(6);
    layers[0] = titleLayer.style;
    layers[1] = infoLayer.style;
    layers[2] = navigationLayer.style;
    layers[3] = bottomLayer.style;
    layers[4] = topmaskLayer.style;
    layers[5] = bottommaskLayer.style;

    coords = new Array(6);
    coords[0] = titCoord;
    coords[1] = titCoord+75;
    coords[2] = bottomCoord+20;
    coords[3] = bottomCoord;
    coords[4] = titCoord + 100 - topmaskHeight;
    coords[5] = bottomCoord + 4;
    slideLayers(layers, coords, 8, notify);
}

function layoutNavigation() {
    beginCritical('layoutNavigation');

    navigationLayer.getImage("picture").src = picture1Image.src;
    navigationLayer.getImage("mail").src = mail1Image.src;
    navigationLayer.getImage("lnks").src = links1Image.src;
    navigationLayer.getImage("cv").src = cv1Image.src;
    navigationLayer.getImage("copy").src = copy1Image.src;

    scrollUpLayer.getImage("up").src = up1Image.src;
    scrollDownLayer.getImage("down").src = down1Image.src;

    navigationLayer.style.left = pixelLength(
 (available_width-navigationLayer.getWidth())/2);
    navigationLayer.style.top = pixelLength(
 parseInt(titleLayer.style.top)+20);
    bottomLayer.style.left = titleLayer.style.left;
    bottomLayer.style.top = titleLayer.style.top;
    scrollDownLayer.style.left = pixelLength(
 (available_width+titleLayer.getWidth())/2 - 48);
    scrollUpLayer.style.left = pixelLength(
 (available_width+titleLayer.getWidth())/2 - 48);

    topmaskLayer.style.left = pixelLength(0);

    topmaskLayer.style.top = pixelLength(
        parseInt(titleLayer.style.top) + 100 - topmaskHeight);

    bottommaskLayer.style.left = pixelLength(0);
    bottommaskLayer.style.top = pixelLength(parseInt(bottomLayer.style.top));

    topmaskLayer.style.visibility = 'visible';
    bottommaskLayer.style.visibility = 'visible';
    bottomLayer.style.visibility = 'visible';
    navigationLayer.style.visibility = 'visible';
    scrollUpLayer.style.visibility = 'hidden';
    scrollDownLayer.style.visibility = 'hidden';

    endCritical();
}

function highlight(imageName,highlt,info) {
    if (curCommand == imageName) {
 if (highlt) { setInfo("Close"); }
 else { setInfo(""); }
 return;
    }
    if (highlt) {
 eval(imageName+"Image").src = eval(imageName+"2Image").src;
 setInfo(info);
 window.status = "";
    } else {
 eval(imageName+"Image").src = eval(imageName+"1Image").src;
 setInfo("");
 window.status = "";
    }
}

var selecting = false;

function select(cmd) {
    eval(cmd+"Image").src = eval(cmd+"3Image").src;
    if (shut == false) {
 if (selecting) return;
 selecting = true;
 oldCommand = curCommand;
 if (curCommand == cmd) {
     curCommand = "";
 } else {
     curCommand = cmd;
 }
 layoutClose();
    } else {
 selecting = true;
 if (oldCommand != "") {
     var myLayer = new Layer(oldCommand+'text');
     myLayer.style.visibility = 'hidden';
     myLayer.style.display = 'none';
 }
 curCommand = cmd;
 showText();
    }
    return false;
}


function showText(){
    if (curCommand == "") return;

    textLayer = new Layer(curCommand+'text');
    textLayer.style.display = 'block';
    textLayer.style.visibility = 'visible';



    textLayer.style.width = pixelLength(400);
    var y = (available_height - textLayer.getHeight())/2;

    if (y<118) {
 y=138;
 scrollOn = true;
    } else {
 scrollOn = false;
    }

    textLayer.style.top = pixelLength(y);
    textLayer.style.left = pixelLength((available_width - 400)/2);
 //    textLayer.style.visibility = 'visible';

    layoutOpen(y-118, 'onTextOpen(); highlight("'+curCommand+'",false)');
    setInfo("");
}

function onTextOpen()
{
    eval(curCommand+"Image").src = eval(curCommand+"4Image").src;
    if (scrollOn) {
 scrollUpLayer.style.top = pixelLength(
     parseInt(titleLayer.style.top) + 84);
 scrollDownLayer.style.top = pixelLength(
     parseInt(bottomLayer.style.top) - 8);
 scrollDownLayer.style.visibility = 'visible';
    }
    selecting = false;
}

function onScrollUp()
{
    var amount = available_height/2 - parseInt(titleLayer.style.top)
 - 100;
    var textHeight = textLayer.getHeight();
    var topOfText = parseInt(textLayer.style.top);
    if (amount >= (parseInt(titleLayer.style.top)+118)-topOfText) {
 scrollUpLayer.style.visibility = 'hidden';
 amount = (parseInt(titleLayer.style.top)+118)-topOfText;
    }
    doScroll(parseInt(textLayer.style.top) + amount, "true");
    scrollDownLayer.style.visibility = 'visible';
}

function onScrollDown()
{
    var amount = available_height/2 - parseInt(titleLayer.style.top)
 - 100;
    var textHeight = textLayer.getHeight();
    var topOfText = parseInt(textLayer.style.top);

    if (amount >=
 (topOfText + textHeight - parseInt(bottomLayer.style.top) - 1))
    {
 scrollDownLayer.style.visibility = 'hidden';
 amount = (topOfText + textHeight - parseInt(bottomLayer.style.top));
    }
    doScroll(parseInt(textLayer.style.top) - amount, "true");
    scrollUpLayer.style.visibility = 'visible';
}

function doScroll(to, notify)
{
    layers = new Array(1);
    layers[0] = textLayer.style;

    coords = new Array(1);
    coords[0] = to;

    slideLayers(layers, coords, 8, notify, (sliding?'decel':'normal'));
}

function closePage()
{
    cleanupPage();
}


/****** logging ******/
function Logger(name)
{
    this.name = name;
    this.actionsHash = new Object();
    this.actionsArray = new Array();
    this.log = loggerLog;
    this.close = loggerClose;

    var myImage = new Image();
    myImage.src = 'images/logstart.gif?' + name;

    return this;
}

function loggerLog(anAction)
{
    if (this.actionsHash[anAction]) {
 this.actionsHash[anAction]++;
    } else {
 this.actionsHash[anAction] = 1;
 this.actionsArray[this.actionsArray.length] = anAction;
    }
}

function loggerClose()
{
    var myRequest = this.name;
    for (var i=0; i<this.actionsArray.length; i++) {
        myRequest += '&';
 myRequest += this.actionsArray[i] + '=' +
     this.actionsHash[this.actionsArray[i]];
    }
    var myImage = new Image();
    myImage.src = 'images/logstop.gif?' + myRequest;
    for (var i=0; i<600000; i++) { var x=i; }
}

