/*
 *  Initializations on page load.
 */
$(document).ready(function(){
    processWindowLinks();
    verticallyAlignImageGallery();
});


/*
 *  Crawler for external and internal links which open in a new window.
 *  All links with the "rel" attribute will launch a new window when
 *  clicked, the window size will vary depending on the value of the
 *  rel attribute (see launchWindow function below).
 *  (xhtml strict: target workaround)
 */
function processWindowLinks() {
    var objAnchors, linkTitlePrefix, linkTitleHint, i;
    if (document.getElementsByTagName) {
        objAnchors = document.getElementsByTagName('a');
        for (i=0; i<objAnchors.length; i++) {
            if (objAnchors[i].getAttribute('href') && objAnchors[i].getAttribute('rel') && objAnchors[i].getAttribute('rel') != 'shadowbox;width=500') {
                objAnchors[i].onclick = function(evt) { return launchWindow(this, evt); };
               	objAnchors[i].onkeypress = function(evt) { return launchWindow(this, evt); };
            }
        }
    }
}


/*
 *  Runs through all images in image gallery that have
 *  a landscape format (height < width) and centers
 *  them vertically within their parent element.
 *  Used as a bugfix for IE7.
 */
function verticallyAlignImageGallery() {
    var parHeight;
    if(navigator.appVersion.indexOf("MSIE 7.0") != -1){ // check if IE7
        $('.image-gallery li img').each(function(i){
            var elem = $(this);
            var imgHeight = elem.height();
            if (imgHeight < elem.width()) {
                parHeight = elem.parent().height();
                $(this).css("marginTop", ((parHeight-imgHeight)*0.5)+"px");
            }
        });
    }
}


/*
 *  Opens a new window, where the size is generally determined
 *  by the value of the link's "rel" attribute.
 *  Currently used values:
 *  window: popup 640x650
 *  glossary: small popup 400x400
 *  all other values: full sized 800x650
 *
 *  @objAnchor - reference to activated ahref
 *  @objEvent  - reference to Event object
 *  @width     - desired window width (optional)
 *  @height    - desired window height (optional)
 */
function launchWindow(objAnchor, objEvent, width, height) {
    var iKeyCode, win;
    var relType = objAnchor.getAttribute('rel');

    if (objEvent && objEvent.type == 'keypress') {
        if (objEvent.keyCode) {
            iKeyCode = objEvent.keyCode;
        } else if (objEvent.which) {
            iKeyCode = objEvent.which;
        }
        if (iKeyCode != 13 && iKeyCode != 32) { // if not <enter> or <space>
            return true;
        }
    }
    if(relType && relType=="window") { // open popup 640x650
        win = window.open(objAnchor,'win','width=' + (width ? width : "640") + ',height=' + (height ? height : "650") + ',scrollbars=yes,resizable=yes');
    }
    if(relType && relType=="glossary") { // open smaller popup 400x400
        win = window.open(objAnchor,'win','width=' + (width ? width : "400") + ',height=' + (height ? height : "400") + ',scrollbars=yes,resizable=yes');
    }
    else { // open full external page 800x650
        win = window.open(objAnchor,'win','width=' + (width ? width : "800") + ',height=' + (height ? height : "650") + ',scrollbars=yes,resizable=yes,location=yes,menubar=yes,status=yes,toolbar=yes');
    }
    win.focus();
    return false;
}


// Helper Function for scrolling to top of page when links within IFrames are clicked.
function scrollToTop(){
    window.scrollTo(0,0);
}

