
/* === KIT LAUNCHER === */

    var ltbhWindow;

    function launchCourse(folder, activity, width, height) {
        if (typeof width == "undefined" || isNaN(width)) width = 780;
        if (typeof height == "undefined" || isNaN(height)) height = 580;
        if (ltbhWindow && !ltbhWindow.closed) ltbhWindow.close();
        ltbhWindow = popup("/kits/" + folder + "/Activity" + activity + "/start.aspx", "ltbhKit", width, height, "no", "no");
    }


/* === TOGGLE === */

    function toggle(divName) {
        div = document.getElementById(divName);
        div.style.display = (div.style.display == "none" || div.style.display == "") ? "block" : "none";
        return false;
    }

/* === POPUPS === */

    function popup(cPopUrl, cPopName, iPopWidth, iPopHeight, bScrollBars, resizable) {
	    var iPopLeft, iPopTop;
	    var oPopWindow;
	    if (!oPopWindow || oPopWindow.closed){
		    //Center popup window inside parent window.
		    iPopLeft = (window.screen.width/2) - ((iPopWidth/2) + 10);
		    iPopTop = (window.screen.height/2) - ((iPopHeight/2) + 50);
		    //Open popup window
		    params = "height="+ iPopHeight +",width="+ iPopWidth +",left=" + iPopLeft + ",top=" + iPopTop + ",screenX=" + iPopLeft + ",screenY=" + iPopTop + ",scrollbars=" + bScrollBars + ",resizable=" + resizable + ", status=yes";
		    //alert(params);
		    oPopWindow=open(cPopUrl, cPopName, params);
		    oPopWindow.location.href = cPopUrl;
		    oPopWindow.focus()
        } else {
    	    oPopWindow.focus()
	    }
	    return oPopWindow;
    }

/* === XHTML COMPATABILE LINKS IN NEW BROWSER WINDOWS === */

    onLoadFuncs = new Array();
    function addOnLoad(func){
    	onLoadFuncs[onLoadFuncs.length] = func;
    }
    function runOnLoad(){
    	for(i in onLoadFuncs){
    		eval(onLoadFuncs[i]+"()");
    	}
    }
    window.onload = runOnLoad;

    function handleExternalLinks() {
        var internalLinkCriteria = Array(
            "ltbh07",
            "localhost",
            "learntobehealthy",
            "jplhosting"
        );

        var externalLinkCriteria = Array(
            ".gif",
            ".jpeg",
            ".jpg",
            ".pdf"
        );

    	var anchors = document.getElementsByTagName("a");
    	var i, href;
    	var match_internal, match_external;

    	for(i = 0; i < anchors.length; i++){
    		if(!anchors[i].href) continue;
    		href = anchors[i].href;
            match_internal = false;
    	    match_external = false;

    		// look for things that must open in the same browser window.
            for (var j = 0; j < internalLinkCriteria.length; j++) {
                if (href.toLowerCase().indexOf(internalLinkCriteria[j].toLowerCase()) >= 0) {
                    match_internal = true;
                }
            }

    		if(!match_internal){ // Href is not a file on my server
    			if(href.indexOf("javascript:") == -1){ // Href is not a javascript call
    				if(!anchors[i].onclick){ // Href does not have an onclick event
    					if(href.indexOf("mailto:") == -1){ // Href is not a mailto:
    						if(href.indexOf("http://") != -1 || href.indexOf("https://") != -1) { // Href is not relative (for Safari)
    							anchors[i].setAttribute("target","_blank");
    						}
    					}
    				}
    			}
    		}

            for (var j = 0; j < externalLinkCriteria.length; j++) {
                if (href.toLowerCase().indexOf(externalLinkCriteria[j].toLowerCase()) >= 0) {
                    match_external = true;
                }
            }
    		if (match_external) anchors[i].setAttribute("target","_blank");
    	}
    }

    if(document.getElementsByTagName){
    	addOnLoad("handleExternalLinks");
    }

