/******************************************
 Lightweight cross-browser Javascript code
  (c) Copyright 2001 J.C.Ross
******************************************/

/**** Browser detection ********/
// 'borrowed' from the guru ... www.htmlguru.com
function Is() {
    agent  = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.ns    = ((agent.indexOf('mozilla')   !=   -1) &&
                 ((agent.indexOf('spoofer')   ==   -1) &&
                 (agent.indexOf('compatible') ==   -1)));
    this.ns2   = (this.ns && (this.major      ==    3));
    this.ns3   = (this.ns && (this.major      ==    3));
    this.ns4b  = (this.ns && (this.major      ==    4) &&
                 (this.minor                  <= 4.03));
    this.ns4   = (this.ns && (this.major      ==    4));
    this.ns6    = (this.ns && (this.major     >=    5));
    this.ie    = (agent.indexOf("msie")       !=   -1);
    this.ie3   = (this.ie && (this.major      <     4));
    this.ie4   = (this.ie && (this.major      ==    4) &&
                 (agent.indexOf("msie 4")   !=   -1));
    this.ie5   = (this.ie && (this.major      ==    4) &&
                 (agent.indexOf("msie 5")   !=   -1));
    this.ie55  = (this.ie && (this.major      ==    4) &&
                 (agent.indexOf("msie 5.5")   !=   -1));
    this.ie6   = (this.ie && agent.indexOf("msie 6") != -1);
    this.ie7   = (this.ie && agent.indexOf("msie 7") != -1);
    this.dom   = (document.getElementById && ! this.ie5);
    this.mac   = (agent.indexOf('mac') !=  -1);
    this.macie = this.mac && this.ie5;

    return this.standard;
}

var is = new Is();

var available_width;
var available_height;

function getDimensions() 
{
    if(is.ns4 || is.ns6) {
        available_width=innerWidth;
        available_height=innerHeight;
    } else if(is.ie4 || is.ie5 ) {
        available_width=document.body.clientWidth;
        available_height=document.body.clientHeight;
    } else if(is.ie ) {
        available_width=document.documentElement.clientWidth;
        available_height=document.documentElement.clientHeight;
    } else {
        alert("Your browser is not (yet) supported... sorry!\n" +
	   "These pages might not work properly.\n" +
	   "It would be appreciated if you contact the webmaster" +
	   "to tell him how you get on!\n" +
	   "(Netscape, Mozilla and IE all work.)" );
        available_width=640;
        available_height=480;
    }
}

getDimensions();

/**** Layer: a light-weight cross-browser object ****/
function Layer(name)
{
    if (is.dom) { // gecko/dom1
        if (! document.getElementById(name)) panic("Layer_" + name);
        this.style = document.getElementById(name).style;
        this.document = document.getElementById(name);
        this.content = document.getElementById(name);
    } else if (is.ns4) {
        if (! document[name]) panic("Layer_" + name);
        this.style = document[name];
        this.document = document[name].document;
        this.content = document[name].document;
    } else if (is.ie4 || is.ie5) {
        if (! document.all[name]) panic("Layer_" + name);
        this.style = document.all[name].style;
        this.document = document.all[name].document;
        this.content = document.all[name];
    } 
    if (! this.style || ! this.document || ! this.content)
        panic("layer_creation_failed");
    
    this.getHeight = layer_getheight;
    this.getWidth = layer_getwidth;

    this.getImage = layer_getimage;
    this.name = name;

    return this;
}

function layer_getwidth()
{
    var width;
    if (is.dom) { // gecko/dom1
        width = this.document.offsetWidth;
    } else if (is.ns4) {
        width = this.style.clip.width;
    } else {
        width = this.content.clientWidth;
    }
    //if (! width) panic("getwidth_" + this.name);
    return width;
}

function layer_getheight()
{
    var height;
    if (is.dom) { // gecko/dom1
        height = this.document.offsetHeight;
    } else if (is.ns4) {
        height = this.style.clip.height;
    } else {
        height = this.content.clientHeight;
    }
    //if (! height ) panic("getheight_" + this.name);
    return height;
}

function layer_getimage(imagename)
{
    var img;
    if (is.dom) {
        img = document.images[imagename];
    } else img = this.document[imagename];
    if (! img) panic("getimage_" + this.name + "_" + imagename);
    return img;
}

/****** Content and units ********/
function writeContent(item, arg)
{
    if (is.ns4) {
        item.write(arg);
        item.close();
    } else {
        item.innerHTML = arg + "\n";
    }
}

function pixelLength(length)
{
    if (is.dom || is.macie) {
        return parseInt(length) + "px";
    } else return length;
}

/****** Preloading and image manipulation ********/
var imagesToLoad;
var imagesLoaded;

function createImage(name)
{
    var myImage = new Image();
    myImage.onload = is.ns4b? onImageLoaded() : onImageLoaded;
    myImage.src = name;

    return myImage;
}

function loadImage(image, name)
{
    image.onload = is.ns4b? onImageLoaded() : onImageLoaded;
    image.src = name;
    return image;
}

var callback;
var imagesLoaded;
var imagesToLoad;

function onImageLoaded()
{
    //writeProgress("loaded " + (imagesLoaded + 1) + " of " + imagesToLoad + " images");
    if (++imagesLoaded == imagesToLoad) {
        eval(callback);
    }
}

/****** 'Stripped down' sliding code ******/

// make sure slideTimer IS a timer
var slideTimer = setTimeout("true",1);
var sliding = false;
var timestep = 50;

function slideLayers(layers, coords, steps, notify, mode) 
{
    clearTimeout(slideTimer);
    if (! mode) { // == undefined) { 
	mode = 'normal';
    }
    sliding = true;
    // layers is an array of layers, coords is an array of 
    // displacements.
    pathArray = new Array();
    layersArray = layers;
    notifyFunction = notify;
    for (i=0; i<layersArray.length; i++) {
	pathArray[i] = new Array();
	start = parseInt(layersArray[i].top);
	end = coords[i];
	if (mode == 'normal') {
	    accel = 4*(end - start)/(steps*steps)*10000;
	    nsteps = steps/2;
	} else {
	    accel = 2*(end - start)/(steps*steps)*10000;
	    nsteps = steps;
	}
	if (mode !='decel') {
	    for (j=0; j<nsteps; j++) {
	        pathArray[i][j] = start + (accel*j*j)/20000;
            }
	}
	if (mode !='accel') {
    	    for (j=0; j<nsteps; j++) {
	        pathArray[i][steps-j-1] = end - (accel*j*j)/20000;
            }
	}
    }
    slideCount = 0;
    doSlide(layersArray, pathArray, slideCount, notifyFunction);
}


function doSlide(layers, path, count, notify)
{
    layersArray = layers;
    pathArray = path;
    slideCount = count;
    notifyFunction = notify;
    again = false;
    for (i=0; i<layersArray.length; i++) {
	if (slideCount<pathArray[i].length) {
	  layersArray[i].top = pixelLength(pathArray[i][slideCount]);
	  if (slideCount+1 < pathArray[i].length) again=true;
	}
    }
    slideCount++;
    if (again) slideTimer = setTimeout("doSlide(layersArray, " +
	"pathArray,slideCount,notifyFunction)", timestep);
    else {
	sliding = false;
	eval(notifyFunction);
    }
}

function openWindow(content, name, width, height, extras)
{
    var xMax, yMax;
    var myExtras = (extras != "") ? extras : "resizable=no";
    
    if (is.ie) {
        xMax = screen.width;
        yMax = screen.height;
    } else if (is.ns) {
        xMax = window.outerWidth;
        yMax = window.outerHeight;
    } else {
        xMax = 640;
        yMax=480;
    }
    
    var xOffset = (xMax - width)/2;
    var yOffset = (yMax - height)/2;

    return window.open(content, name, "width=" + width +
        ",height=" + height +
        ",screenX=" + xOffset +
        ",screenY=" + yOffset +
        ",left=" + xOffset +
        ",top=" + yOffset + "," +
        myExtras);
}


function openLink(url)
{
    if (window.opener) {
        window.opener.document.location = url;
        window.opener.focus();
    } else {
        document.location = url;
    }
}	

var panicTimer;
var panicInfo;

function beginCritical(info, timeout)
{
    panicInfo = info;
    if (! timeout) timeout = 10000;
    if (panicTimer) clearTimeout(panicTimer);
    panicTimer = setTimeout('panic()', timeout);
}

function endCritical()
{
    if (panicTimer) clearTimeout(panicTimer);
}

function panic(extra)
{
    endCritical();
    if (! panicInfo) panicInfo="unknown";
    if (extra) panicInfo += '%' + extra;
    openLink("/error.html?" + panicInfo);
    //alert (panicInfo);
}
