WP = { 
    onload_functions:   [ ],
    unload_functions:   [ ],
    engaged:            { }
};

WP.tooltip = function(content) {
    return $('<span class="tip"/>')
        .append(content)
        .append($('<span class="arrow"/>'));
};

WP.onload = function(callback) {
    WP.onload_functions.push(callback);
    if (! WP.engaged.onload) {
        $(document).ready(WP.onloader);
        WP.engaged.onload = true;
    }
};

WP.unload = function(callback) {
    WP.unload_functions.push(callback);
    if (! WP.engaged.unload) {
        $(window).unload(WP.unloader);
        WP.engaged.unload = true;
    }
};

WP.call_each = function(fns) {
    for(var i = 0; i < fns.length; i++) {
        fns[i]();
    }
};

WP.onloader = function() {
    WP.call_each(WP.onload_functions);
};

WP.unloader = function() {
    WP.call_each(WP.unload_functions);
};

WP.nothing = function() { 
    // dummy function which does nothing - useful for do-nothing callbacks
    return false;
};

WP.prevent_default = function(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    else if (window.event) {
        window.event.returnValue = false;
    }
    return false;
};

WP.init_lightbox = function(e) {
    if (WP.init_lightbox_run) {
        return;
    }
    else {
        WP.init_lightbox_run = true;
        $('#overlay').click( WP.hide_lightbox );
    }
};

WP.hide_lightbox = function(e) {
    $('#lightbox').hide();
    $('#overlay').hide();
    $('select').show();
    document.onkeypress = '';
    return WP.prevent_default(e);
};

WP.show_lightbox = function(link) {
    var adjust_up   = 20;
    var page_size   = WP.page_size();
    var page_scroll = WP.page_scroll();
    var overlay     = $('#overlay');
    var lightbox    = $('#lightbox');
    var loading     = overlay.find('#loading');
    var image       = lightbox.find('#image');
    var that        = $(link);
    var href        = that.attr('href');
    var title       = that.attr('title');

    WP.init_lightbox();

    // display overlay over whole page
    overlay.css({
        height: page_size[1] + 'px'
    });
    overlay.show();
    
    // center loading image
    loading.css({
        top:  (page_scroll[1] + (page_size[3] - loading.height()) / 2) + 'px',
        left: ((page_size[0] - loading.width()) / 2) + 'px'
    });
    loading.show();

    // set title
    lightbox.find('#title').html(title);

    if (! href.match(/.(jpg|png|gif)$/))
        throw "Non-image link";
    
    // preload image and define onload() handler to display it
    var preload = new Image();

    preload.onload = function() {
        // set original image src to this image
        image.attr('src', this.src);

        // What a surprise!  We have to hack around limitations in IE
        if ($.browser.msie) {
            // pause between loading the image and displaying to prevent
            // the previous image from displaying 
            WP.pause(250);  // ugly hack
            // hide select boxes that will display through the image 
            $('select').hide();
        } 

        // center lightbox and make sure that the top and left values are not negative
        // and the image placed outside the viewport
        var box_top  = page_scroll[1] + ((page_size[3] - this.height) / 2) - adjust_up;
        var box_left = ((page_size[0] - this.width) / 2) - 20;
        box_top  = box_top  < 0 ? 0 : box_top; 
        box_left = box_left < 0 ? 0 : box_left;

        // make caption same width as image
        lightbox.find('.caption').css('width', this.width + 'px');

        // move lightbox to middle of screen and display
        lightbox.css({
            top:  box_top  + "px",
            left: box_left + "px"
        });
        lightbox.show();

        // After image is loaded, update the overlay height as the new image might have
        // increased the overall page height.
        page_size = WP.page_size();
        overlay.css({
            height: page_size[1] + 'px'
        });

        // hide loading image
        loading.hide();

        // Check for any keypress
        document.onkeyup = WP.hide_lightbox;

        return false;
    };

    preload.src = href;

    return false;
};

WP.page_scroll = function() {
    // based on code from Quirksmode.org via Lightbox JS
    var yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
    }
    return new Array('', yScroll);
};

WP.page_size = function() {
    // based on code from Quirksmode.org via Lightbox JS
    // Edit for Firefox by pHaez
    var xScroll, yScroll;
    var windowWidth, windowHeight;
    var pageWidth, pageHeight;
    
    if (window.innerHeight && window.scrollMaxY) {  
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   
    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){  
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
};

WP.pause = function(numberMillis) {
    // Pauses code execution for specified time. Uses busy code, not good.
    // Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
};


WP.debug = (window.console && window.console.log && window.console.log.apply)
    ? function() { window.console.log.apply(window.console, arguments); }
    : WP.nothing;


jQuery.fn.extend({
    // add a class to an element and remove it from all siblings
    solo: function(c) {
        this.addClass(c).siblings('.' + c).removeClass(c);
        return this;
    },
    // add 'warm' as a solo class among siblings
    warm: function() {
        this.solo('warm');
        return this;
    },
    // remove 'warm' class
    cool: function() {
        this.removeClass('warm');
        return this;
    },
    delay_fade: function(delay) {
        var self = this;
        setTimeout( 
            function() { self.fadeOut(); },
            delay || 5000
        );
    },
    tooltip: function() {
        return this.each(
            function () {
                var link  = $(this);
                var title = link.attr('title'); //.replace(/\\n/g, "<br>");
                link.attr('title','');
//              WP.debug("tooltip: %s", title);
                if (title && ! link.find('span.tip').size()) {
                    link.append( WP.tooltip(title) );
                }
            }
        );
    }
});




/*------------------------------------------------------------------------
 * about(text)
 * 
 * Set the HTML content of the 'hint' element
 *------------------------------------------------------------------------*/

function about(text) {
  document.getElementById("hint").innerHTML = text;
}


/*------------------------------------------------------------------------
 * unabout()
 * 
 * Reset the HTML content of the 'hint' element to the default message
 *------------------------------------------------------------------------*/

function unabout() {
  about("<b>Hint:<\/b> rollover a menu link for further information");
}


/*------------------------------------------------------------------------
 * index_redirect()
 * 
 * Perform a redirect when an option is selected in the alphabetical index
 * pulldown.
 *------------------------------------------------------------------------*/

function index_redirect(select) {
  var linkurl = select.options[select.selectedIndex].value;
  if (linkurl)
    document.location.href = linkurl;
  return false;
}


/*------------------------------------------------------------------------
 * get_style()
 * 
 * Returns the title of the current active stylesheet.
 *------------------------------------------------------------------------*/

function get_style() {
  var elems = document.getElementsByTagName("link");
  var n, elem, title;

  for (n = 0; (elem = elems[n]); n++) {
     if (elem.getAttribute("rel").indexOf("style") != -1 
     && (title = elem.getAttribute("title"))
     && !elem.disabled)
       return title;
  }
  return null;
}


/*------------------------------------------------------------------------
 * set_style(title)
 * 
 * Set the active stylesheet by enabling the <link rel="style" ...> 
 * element that has a title attribute matching the title argument,
 * and disabling all others.
 *------------------------------------------------------------------------*/

function set_style(title) {
  var elems = document.getElementsByTagName("link");
  var n, elem, tattr;

  for (n = 0; n < elems.length; n++) {
    elem = elems[n];

    if (elem.getAttribute("rel").indexOf("style") != -1 
    && (tattr = elem.getAttribute("title"))) {
      elem.disabled = true;
      if (tattr == title) 
        elem.disabled = false;
    }
  }
}


/*------------------------------------------------------------------------
 * set_cookie(name, value, days)
 * 
 * Set a cookie with the name and value passed as the first two arguments, 
 * set to expire in the number of days specified in the third argument.
 *------------------------------------------------------------------------*/

function set_cookie(name, value, days) {
  var expires;

  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    expires = "; expires=" + date.toGMTString();
  }
  else 
    expires = "";

  document.cookie = name + "=" + value + expires + "; path=/";
}


/*------------------------------------------------------------------------
 * get_cookie(name)
 * 
 * Returns the value of the cookie identified by the name argument.
 *------------------------------------------------------------------------*/

function get_cookie(name) {
  var namestr  = name + "=";
  var cookbits = document.cookie.split(';');
  var n;

  for(n = 0; n < cookbits.length; n++) {
    var c = cookbits[n];

    /* remove leading whitespace */
    while (c.charAt(0) == ' ') 
      c = c.substring(1, c.length);

    /* if the name start this cookie fragment, return the value */
    if (c.indexOf(namestr) == 0) 
      return c.substring(namestr.length, c.length);
  }
  return null;
}


/*------------------------------------------------------------------------
 * hide_menu()
 * 
 * Set the stylesheet to 'Hide Menu'
 *------------------------------------------------------------------------*/

function hide_menu() {
  unabout();
  set_style('Hide Menu');
  return false;
}


/*------------------------------------------------------------------------
 * show_menu()
 * 
 * Set the stylesheet to 'Show Menu'
 *------------------------------------------------------------------------*/

function show_menu() {
  unabout();
  set_style('Show Menu');
  return false;
}


function clear_link(name) {
  document.getElementById(name).href = '#';
}

