
/* binding onload event to function */
addEventHandler(window, 'load', init, false);

function init() {
    specialLinks();

    // vertically align gallery images in IE7
    if(navigator.appVersion.indexOf("MSIE 7.0") != -1){
        $('.image-gallery li img').each(function(i){
            var elem = $(this);
            var imgHeight = elem.height();
            if (imgHeight < elem.width()) {
                var parHeight = elem.parent().height();
                $(this).css("marginTop", ((parHeight-imgHeight)*0.5)+"px");
            }
        });
    }
}

/* open a new window */
function launchWindow(objAnchor, objEvent, width, height) {
    var iKeyCode;
    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) {
            return true;
        }
    }
    var win;
    if(relType && relType=="window") { // open smaller popup
        win = window.open(objAnchor,'win','width=' + (width ? width : "640") + ',height=' + (height ? height : "650") + ',scrollbars=yes,resizable=yes');
    }
    if(relType && relType=="glossary") { // open smaller popup
        win = window.open(objAnchor,'win','width=' + (width ? width : "400") + ',height=' + (height ? height : "400") + ',scrollbars=yes,resizable=yes');
    }
    else { // open full external page
        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;
}


/* add classname to element */
function addClassName(objClassName, appendix) {
    if (objClassName.length != 0) {
        objClassName += ' ';
    }
    objClassName += appendix;
    return objClassName;
}


/* remove classname from element */
function removeClassName(objClassName, appendix) {
    var classNames = objClassName.split(' ');
    objClassName = '';

    for (var i=0; i<classNames.length; i++) {
        if (classNames[i] != appendix) {
            objClassName += classNames[i] + ' ';
        }
    }
    return objClassName.substring(0, objClassName.length-1);
}


/* crawler for external and internal links which open in a new window
   xhtml strict: target workaround
 */
function specialLinks() {
    if (document.getElementsByTagName) {
        var objAnchors = document.getElementsByTagName('a');
        var linkTitlePrefix, linkTitleHint;

        for (var i=0; i<objAnchors.length; i++) {
            if (objAnchors[i].getAttribute('href') && objAnchors[i].getAttribute('rel')) {
                objAnchors[i].onclick = function(evt) { return launchWindow(this, evt); }
                objAnchors[i].onkeypress = function(evt) { return launchWindow(this, evt); }
            }
        }
    }
}


// Helper Function for scrolling to top of page when links within IFrames are clicked.
function scrollToTop(){
	try {
		this.scrollTo(0,0); 
	} catch (anothertry) { 
		try {
			window.scrollTo(0,0)
		} catch (wontwork) {}
	}
}
