addLoadEvent(initLightbox); // run initLightbox onLoad

// Outbound Link Tracker
function out(a) {if (a.getAttribute("out") == ".") {_gaq.push(['_trackPageview', '/out/' + a.href.replace('http://', '').replace('https://', '')]); a.setAttribute("out", a.href); a.href = "/out.aspx?url=" + escape(a.href); }}
function unout(a) {if (a.getAttribute("out") != ".") {a.href = a.getAttribute("out");a.setAttribute("out", "."); }}

// 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; var x; if (typeof (e.offsetX) != 'undefined') { x = e.offsetX; } else { x = e.clientX - getPosition(id, "x"); } return x; }
function clickCoordsY(e, id) { e = (window.event) ? event : e; var y; if (typeof (e.offsetX) != 'undefined') { y = e.offsetY; } else { 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 = '';
function getKey(e) { var keycode = (window.event) ? event.keyCode : e.keyCode; var escape = (window.event) ? 27 : e.DOM_VK_ESCAPE; if (keycode == escape) { hideLightbox(); } }
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) { } }
function showLightbox(objLink) { var objLightbox = document.getElementById('lightbox'); var objCaption = document.getElementById('lightboxCaption'); var objImage = document.getElementById('lightboxImage'); var objLightboxDetails = document.getElementById('lightboxDetails'); imgPreload = new Image(); imgPreload.onload = function () { objImage.src = objLink.href; var lightboxTop = getScrollY + ((getWindowHeight() - 35 - imgPreload.height) / 2); var lightboxLeft = ((getWindowWidth() - 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.innerHTML = objLink.getAttribute('title'); } else { objCaption.style.display = 'none'; } if (navigator.appVersion.indexOf("MSIE") != -1) { pause(250); }; objLightbox.style.display = 'block'; showOverlay(); listenKey(); return false; }; imgPreload.src = objLink.href; }
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>'); }
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; }
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; }
function updateHtmlbox(html) { setProperty('videobox', 'innerHTML', html); }
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; }
        }
    }

    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 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 () { return hideLightbox(); }
    objLightbox.appendChild(objLink);

    // 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="#" onclick="return 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; }
