addLoadEvent(initLightbox); // run initLightbox onLoad

// core functions
function addLoadEvent(func) { var onload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { onload(); func(); } } }
function addUnloadEvent(func) { var onunload = window.onunload; if (typeof window.onunload != 'function') { window.onunload = func; } else { window.onunload = function() { onunload(); func(); } } }
function clickCoordsX(e, id) { e = (window.event) ? event : e; if (typeof (e.offsetX) != 'undefined') { var x = e.offsetX; } else { var x = e.clientX - getPosition(id, "x"); } return x; }
function clickCoordsY(e, id) { e = (window.event) ? event : e; if (typeof (e.offsetX) != 'undefined') { var y = e.offsetY; } else { var y = e.clientY - getPosition(id, "y"); } return y; }
function getCentreX(id) { return getScrollX() + Math.round((getWindowWidth() - 520) / 2); }
function getCentreY(id) { return getScrollY() + Math.round((getWindowHeight() - 360) / 3); }
function getElementHeight(id) { var y = getProperty(id, 'offsetHeight');if (y == 0) { y = getStyle(id, 'pixelHeight'); }return y;}
function getElementWidth(id) { var x = getProperty(id, 'offsetWidth'); if (x == 0) { x = getStyle(id, 'pixelWidth'); } return x; }
function getCookie(name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(name + "="); if (c_start != -1) { c_start = c_start + name.length + 1; c_end = document.cookie.indexOf(";", c_start); if (c_end == -1) c_end = document.cookie.length; return unescape(document.cookie.substring(c_start, c_end)); } } return ""; }
function setCookie(name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString()) + "; path=/"; }
function getPageHeight() { var y = 0; if (window.innerHeight && window.scrollMaxY) { y = window.innerHeight + window.scrollMaxY; } else if (document.body.scrollHeight > document.body.offsetHeight) { y = document.body.scrollHeight; } else { y = document.body.offsetHeight; } if (y < getWindowHeight()) { y = getWindowHeight(); } return y; }
function getPageWidth() { var x = 0; if (window.innerHeight && window.scrollMaxY) { x = document.body.scrollWidth; } else if (document.body.scrollHeight > document.body.offsetHeight) { x = document.body.scrollWidth; } else { x = document.body.offsetWidth; } if (x < getWindowWidth()) { x = getWindowWidth(); } return x; }
function getPosition(id, dr) { var e = document.getElementById(id); (dr == "x") ? p = e.offsetLeft : p = e.offsetTop; t = e.offsetParent; while (t != null) { p += (dr == "x") ? t.offsetLeft : t.offsetTop; t = t.offsetParent; } return p; }
function getProperty(id, nm) { try { return document.getElementById(id)[nm]; } catch (e) { return false; } }
function setProperty(id, nm, vl) { try { document.getElementById(id)[nm] = vl; } catch (e) { } }
function getScrollX() { if (self.pageXOffset) { return self.pageXOffset; } else if (document.documentElement && document.documentElement.scrollLeft) { return document.documentElement.scrollLeft; } else if (window.body) { return window.body.scrollLeft; } else { return 0; } }
function getScrollY() { if (self.pageYOffset) { return self.pageYOffset; } else if (document.documentElement && document.documentElement.scrollTop) { return document.documentElement.scrollTop; } else if (window.body) { return window.body.scrollTop; } else { return 0; } }
function getStyle(id, nm) { try { return document.getElementById(id).style[nm]; } catch (e) { return false; } }
function setStyle(id, nm, vl) { try { document.getElementById(id).style[nm] = vl; } catch (e) { } }
function getWindowHeight() { if (self.innerHeight) { return self.innerHeight; } else if (document.documentElement && document.documentElement.clientHeight) { return document.documentElement.clientHeight; } else if (document.body) { return document.body.clientHeight; } }
function getWindowWidth() { if (self.innerWidth) { return self.innerWidth; } else if (document.documentElement && document.documentElement.clientWidth) { return document.documentElement.clientWidth; } else if (document.body) { return document.body.clientWidth; } }
function parseXml(xml) { var xmlDoc; if (window.DOMParser) { parser = new DOMParser(); xmlDoc = parser.parseFromString(xml, "text/xml"); } else { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = "false"; xmlDoc.loadXML(xml); } return xmlDoc; }
function pause(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; while (true) { now = new Date(); if (now.getTime() > exitTime) return; } }

// ****** Ajax ****** //
// usage: http://www.mad4milk.net/entry/moo.ajax
// new ajax('sleep.php', { postBody: 'name=value&name=value', update: $('myelementid'), onComplete: myFunction });
// function myFunction(request) {alert(request.responseText);}
var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } }
function $() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') element = document.getElementById(element); if (arguments.length == 1) return element; elements.push(element); } return elements; }
Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; }
Function.prototype.bind = function(object) { var __method = this; return function() { return __method.apply(object, arguments); } }

ajax = Class.create();
ajax.prototype = {
    initialize: function(url, options) { this.transport = this.getTransport(); this.postBody = options.postBody || ''; this.method = options.method || 'get'; this.onComplete = options.onComplete || null; this.update = $(options.update) || null; this.request(url); },
    request: function(url) { this.transport.open(this.method, url, true); this.transport.onreadystatechange = this.onStateChange.bind(this); if (this.method == 'post') { this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close'); } this.transport.send(this.postBody); },
    onStateChange: function() { if (this.transport.readyState == 4 && this.transport.status == 200) { if (this.onComplete) { setTimeout(function() { this.onComplete(this.transport); } .bind(this), 10); } if (this.update) { setTimeout(function() { this.update.innerHTML = this.transport.responseText; } .bind(this), 10); } this.transport.onreadystatechange = function() { }; } },
    getTransport: function() { if (window.ActiveXObject) { return new ActiveXObject('Microsoft.XMLHTTP'); } else if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { return false; }}
};

// ****** Lightbox ****** //
var lastLightBox = '';

// getKey(key)
// Gets keycode. If ESC is pressed then it hides the lightbox.
function getKey(e) {
    var keycode = (window.event) ? event.keyCode : e.keyCode;  // MSIE:Firefox
    var escape = (window.event) ? 27 : e.DOM_VK_ESCAPE;  // MSIE:Firefox
    if (keycode == escape) { hideLightbox(); }
}

// listenKey()
function listenKey() { document.onkeypress = getKey; }

function showOverlay() {
    try {
        var o = document.getElementById('overlay');
        o.onclick = function() { return false; }
        o.style.height = getPageHeight() + 'px';
        o.style.display = 'block';
    }
    catch (e) { } 
}

// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
function showLightbox(objLink) {
    // prep objects
    showOverlay();
    var objLightbox = document.getElementById('lightbox');
    var objCaption = document.getElementById('lightboxCaption');
    var objImage = document.getElementById('lightboxImage');
    //var objLoadingImage = document.getElementById('loadingImage');
    var objLightboxDetails = document.getElementById('lightboxDetails');


    var arrayPageSize = getPageSize();

    // center loadingImage if it exists
    //if (objLoadingImage) {
    //    objLoadingImage.style.top = (getScrollY + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
    //    objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
    //    objLoadingImage.style.display = 'block';
    //}

    // preload image
    imgPreload = new Image();

    imgPreload.onload = function() {
        objImage.src = objLink.href;

        // center lightbox and make sure that the top and left values are not negative
        // and the image placed outside the viewport
        var lightboxTop = getScrollY + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
        var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);

        objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
        objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


        objLightboxDetails.style.width = imgPreload.width + 'px';

        if (objLink.getAttribute('title')) {
            objCaption.style.display = 'block';
            //objCaption.style.width = imgPreload.width + 'px';
            objCaption.innerHTML = objLink.getAttribute('title');
        } else {
            objCaption.style.display = 'none';
        }

        // A small pause between the image loading and displaying is required with IE,
        // this prevents the previous image displaying for a short burst causing flicker.
        if (navigator.appVersion.indexOf("MSIE") != -1) {
            pause(250);
        }

        //if (objLoadingImage) { objLoadingImage.style.display = 'none'; }
        objLightbox.style.display = 'block';

        // After image is loaded, update the overlay height as the new image might have
        // increased the overall page height.
        arrayPageSize = getPageSize();
        //objOverlay.style.height = (arrayPageSize[1] + 'px');

        // Check for 'x' keypress
        listenKey();

        return false;
    }

    imgPreload.src = objLink.href;
}

// showVideobox()
function showVideobox(link) {
    var url = link.href;
    showHtmlbox('<object width="425" height="355"><param name="movie" value="' + url + '" /><param name="rel" value="false" /><param name="autoplay" value="true" /><param name="egm" value="false" /><embed src="' + url + '" rel="false" autoplay="true" egm="false" type="application/x-shockwave-flash" width="425" height="355" /></object>');
}

/// showPostbackBox()
function showPostbackbox(boxId) {
    setStyle(boxId, 'left', getCentreX(boxId) + 'px');
    setStyle(boxId, 'top', getCentreY(boxId) + 'px');
    setStyle(boxId, 'zIndex', 100);
    setStyle(boxId, 'display', 'block');
    lastLightBox = boxId;
    showOverlay();
    listenKey();
    return false;
}

/// showHtmlBox()
function showHtmlbox(html) {
    var boxId = 'videobox';
    setStyle(boxId, 'left', getCentreX(boxId) + 'px');
    setStyle(boxId, 'top', getCentreY(boxId) + 'px');
    setProperty(boxId, 'innerHTML', html);
    setStyle(boxId, 'display', 'block');
    lastLightBox = boxId;
    showOverlay();
    listenKey();
    return false;
}

/// updateHtmlBox
function updateHtmlbox(html) {
    setProperty('videobox', 'innerHTML', html);
}

// hideLightbox()
function hideLightbox() {
    setStyle('lightbox', 'display', 'none');
    setStyle('videobox', 'display', 'none');
    setProperty('videobox', 'innerHTML', '');
    if (lastLightBox != '') { setStyle(lastLightBox, 'display', 'none'); lastLightBox = ''; }
    setStyle('overlay', 'display', 'none');
    document.onkeypress = '';
    return false;
}

// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
function initLightbox() {

    if (!document.getElementsByTagName) { return; }
    var anchors = document.getElementsByTagName("a");

    // loop through all anchor tags
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];

        if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")) {
            anchor.onclick = function() { showLightbox(this); return false; }
        }


        if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "videobox")) {
            anchor.onclick = function() { showVideobox(this); return false; }
        }
    }

    // the rest of this code inserts html at the top of the page that looks like this:
    //
    // <div id="overlay">
    //	<a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
    // </div>
    // <div id="lightbox">
    //	<a href="#" onclick="hideLightbox(); return false;" title="Click anywhere to close image">
    //		<img id="closeButton" />		
    //		<img id="lightboxImage" />
    //	</a>
    //	<div id="lightboxDetails">
    //		<div id="lightboxCaption"></div>
    //		<div id="keyboardMsg"></div>
    //	</div>
    // </div>
    // <div id="videobox">
    // </div>

    var objBody = document.getElementsByTagName("body").item(0);

    // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
    var objOverlay = document.createElement("div");
    objOverlay.setAttribute('id', 'overlay');
    //objOverlay.onclick = function() { hideLightbox(); return false; }
    objOverlay.style.display = 'none';
    objOverlay.style.position = 'absolute';
    objOverlay.style.top = '0';
    objOverlay.style.left = '0';
    objOverlay.style.zIndex = '90';
    objOverlay.style.width = '100%';
    objBody.insertBefore(objOverlay, objBody.firstChild);

    //var arrayPageSize = getPageSize();

    // preload and create loader image
    //var imgPreloader = new Image();

    // if loader image found, create link to hide lightbox and create loadingimage
    //imgPreloader.onload = function() {

    //    var objLoadingImageLink = document.createElement("a");
    //    objLoadingImageLink.setAttribute('href', '#');
    //    objLoadingImageLink.onclick = function() { hideLightbox(); return false; }
    //    objOverlay.appendChild(objLoadingImageLink);

    //    var objLoadingImage = document.createElement("img");
    //    objLoadingImage.src = loadingImage;
    //    objLoadingImage.setAttribute('id', 'loadingImage');
    //    objLoadingImage.style.position = 'absolute';
    //    objLoadingImage.style.zIndex = '150';
    //    objLoadingImageLink.appendChild(objLoadingImage);

    //    imgPreloader.onload = function() { }; //	clear onLoad, as IE will flip out w/animated gifs

    //    return false;
    //}

    //imgPreloader.src = loadingImage;

    // create lightbox div, same note about styles as above
    var objLightbox = document.createElement("div");
    objLightbox.setAttribute('id', 'lightbox');
    objLightbox.style.display = 'none';
    objLightbox.style.position = 'absolute';
    objLightbox.style.zIndex = '100';
    objBody.insertBefore(objLightbox, objOverlay.nextSibling);

    // create link
    var objLink = document.createElement("a");
    objLink.setAttribute('href', '#');
    objLink.setAttribute('title', 'Click to close');
    objLink.onclick = function() { hideLightbox(); return false; }
    objLightbox.appendChild(objLink);

    // preload and create close button image
    //var imgPreloadCloseButton = new Image();

    // if close button image found, 
    //imgPreloadCloseButton.onload = function() {

    //    var objCloseButton = document.createElement("img");
    //    objCloseButton.src = closeButton;
    //    objCloseButton.setAttribute('id', 'closeButton');
    //    objCloseButton.style.position = 'absolute';
    //    objCloseButton.style.zIndex = '200';
    //    objLink.appendChild(objCloseButton);

    //    return false;
    //}

    //imgPreloadCloseButton.src = closeButton;

    // create image
    var objImage = document.createElement("img");
    objImage.setAttribute('id', 'lightboxImage');
    objLink.appendChild(objImage);

    // create details div, a container for the caption and keyboard message
    var objLightboxDetails = document.createElement("div");
    objLightboxDetails.setAttribute('id', 'lightboxDetails');
    objLightbox.appendChild(objLightboxDetails);

    // create caption
    var objCaption = document.createElement("div");
    objCaption.setAttribute('id', 'lightboxCaption');
    objCaption.style.display = 'none';
    objLightboxDetails.appendChild(objCaption);

    // create keyboard message
    var objKeyboardMsg = document.createElement("div");
    objKeyboardMsg.setAttribute('id', 'keyboardMsg');
    objKeyboardMsg.innerHTML = '<a href="javascript:hideLightbox();">close</a>';
    objLightboxDetails.appendChild(objKeyboardMsg);

    // create videobox div, same note about styles as above
    var objVideobox = document.createElement("div");
    objVideobox.setAttribute('id', 'videobox');
    objVideobox.style.display = 'none';
    objVideobox.style.position = 'absolute';
    objVideobox.style.zIndex = '100';
    objBody.insertBefore(objVideobox, objOverlay.nextSibling);
}


// ****** Lightbox ****** //
// Menu Format: <ul id="menuName" class="dropdownmenu" onmouseover="dropDownMenuClear();" onmouseout="dropDownMenuHide();"><li><a href="">Menu Item</a></li></ul>
// Link Format: <a id="linkName" href="#" onclick="dropDownMenuShow(this.id,'menuId');"><img...></a>
var dropDownMenuTimeout;
var dropDownMenuId = "";
function dropDownMenuClear() { clearTimeout(dropDownMenuTimeout); }
function dropDownMenuClose() { setStyle(dropDownMenuId, 'display', 'none'); clearTimeout(dropDownMenuTimeout); }
function dropDownMenuHide() { clearTimeout(dropDownMenuTimeout); dropDownMenuTimeout = setTimeout('dropDownMenuClose()', 250); }
function dropDownMenuShow(buttonId, menuId) { dropDownMenuClose(); dropDownMenuId = menuId; var p = getPageWidth(); var w = getElementWidth(buttonId); var x = getPosition(buttonId, 'x'); var y = getPosition(buttonId, 'y') + getElementHeight(buttonId); setStyle(menuId, 'left', x + 'px'); setStyle(menuId, 'top', y + 'px'); setStyle(menuId, 'display', 'block'); var m = getElementWidth(menuId); if (x + m > p) { x = x + w - m; setStyle(menuId, 'left', x + 'px'); } return false; }