sfHover = function () {
    var sfEls = document.getElementById("navbar").getElementsByTagName("li");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function () {
            this.className += " hover";
        }
        sfEls[i].onmouseout = function () {
            this.className = this.className.replace(new RegExp(" hover\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


//ARE WE USING EXPLORER?
var g_isIE = navigator.appName.indexOf("Microsoft") != -1;
var userAgent = navigator.userAgent.toLowerCase();
var version = parseFloat((userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1]);
if (g_isIE && version <= '7') {
    var oldIE = true;
}
//ABSTRACTED CONNECT EVENT FUNCTION FOR ANY BROWSER
function connectEvent(element, event, handler) {
    if (g_isIE) {
        element.detachEvent("on" + event, handler);
        element.attachEvent("on" + event, handler);
    } else {
        element.addEventListener(event, handler, false);
    }
}
function activeSections() {
    var section = "home";
    var pathString = window.location.href.split("/");

    if (typeof pathString[3] != "undefined") {
        var section = pathString[3];
        var section = section.replace(".html", "");

        //EXCEPTIOM MAPPING
        if (section == "about_esri") { var section = "company"; }
        if (section == "user_showcase") { var section = "industries"; }
        if (section == "adpages") { var section = "company"; }
        if (section == "aprimoemails") { var section = "company"; }
        if (section == "atponly") { var section = "training"; }
        if (section == "blogs") { var section = "news"; }
        if (section == "cd") { var section = "company"; }
        if (section == "censuswatch") { var section = "industries"; }
        if (section == "cigt") { var section = "services"; }
        if (section == "data") { var section = "products"; }
        if (section == "directnews") { var section = "news"; }
        if (section == "disaster_response") { var section = "services"; }
        if (section == "download") { var section = "products"; }
        if (section == "eeap") { var section = "services"; }
        if (section == "enewsletters") { var section = "news"; }
        if (section == "esripress") { var section = "training"; }
        if (section == "federalgsa") { var section = "industries"; }
        if (section == "getting_started") { var section = "home"; }
        if (section == "gisday") { var section = "events"; }
        if (section == "grant") { var section = "industries"; }
        if (section == "grants") { var section = "industries"; }
        if (section == "homepage") { var section = "home"; }
        if (section == "htmlemails") { var section = "news"; }
        if (section == "htmlform") { var section = "news"; }
        if (section == "legal") { var section = "company"; }
        if (section == "intldist") { var section = "company"; }
        if (section == "library") { var section = "news"; }
        if (section == "mapmuseum") { var section = "home"; }
        if (section == "newsletters") { var section = "news"; }
        if (section == "partners") { var section = "company"; }
        if (section == "showcase") { var section = "industries"; }
        if (section == "sag") { var section = "industries"; }
        if (section == "site") { var section = "company"; }
        if (section == "software") { var section = "products"; }
        if (section == "surveys") { var section = "company"; }
        if (section == "systemsint") { var section = "services"; }
        if (section == "technology_trends") { var section = "home"; }
        if (section == "universities") { var section = "industries"; }
        if (section == "usersupport") { var section = "company"; }
        if (section == "webmaster") { var section = "company"; }
    }

    if (typeof actSection != "undefined") {
        var section = actSection;
    }

    if (typeof section != "undefined") {
        if (document.getElementById(section)) {
            document.getElementById(section).setAttribute((oldIE ? 'className' : 'class'), "on");
        } else {
            document.getElementById("home").setAttribute((oldIE ? 'className' : 'class'), "on");
        }
    }

    var myfilename = window.location.href.replace("http://", "");
    var myfilename = myfilename.replace(pathString[2], "");

    if (myfilename == "") {
        var myfilename = "index.html";
    }
    // Gets all <a> tags in html
    // var links = document.getElementsByTagName("A"); // removed due to incompatibility with productsmenu.js
    // Gets only <a> tags in id #navbar div
    var links = document.getElementById('navbar').getElementsByTagName("a");
    for (i = 0; i < links.length; i++) {
        if (links[i].getAttribute('href') == myfilename || links[i].getAttribute("href", 2) == myfilename) {
            links[i].setAttribute((oldIE ? 'className' : 'class'), "on");
        }
    }
    if (document.getElementById("homeTopper")) {

        var images = [];
        var links = [];

        images[0] = 'v23_transportation8.jpg';
        links[0] = '/mapmuseum/mapbook_gallery/volume23/transportation8.html';

        images[1] = 'v23_safety8.jpg';
        links[1] = '/mapmuseum/mapbook_gallery/volume23/safety8.html';

        images[2] = 'v23_cartography9a.jpg';
        links[2] = '/mapmuseum/mapbook_gallery/volume23/cartography9.html';

        images[3] = 'v23_cartography12.jpg';
        links[3] = '/mapmuseum/mapbook_gallery/volume23/cartography12.html';

        images[4] = 'v23_transportation11.jpg';
        links[4] = '/mapmuseum/mapbook_gallery/volume23/transportation11.html';

        images[5] = 'v23_transportation13.jpg';
        links[5] = '/mapmuseum/mapbook_gallery/volume23/transportation13.html';

        images[6] = 'v23_cartography8.jpg';
        links[6] = '/mapmuseum/mapbook_gallery/volume23/cartography8.html';

        var min = 1;
        var max = images.length;

        var i = Math.floor(Math.random() * (max - min + 1)) + min;
        ht = document.getElementById("homeTopper");
        ht.style.backgroundImage = "url(/graphics/" + images[0] + ")";
        connectEvent(ht, "click", goTo);
        imgLink = links[0];

        if (typeof images[i] != "undefined") {
            document.getElementById("homeTopper").style.backgroundImage = "url(/graphics/" + images[i] + ")";
            imgLink = links[i];
        }
    }
}

function goTo() {
    document.location.href = imgLink;
}

var imgLink = "";

connectEvent(window, "load", activeSections);

/**
* Fixes the xhtml strict lack of target attribute on links.
* 
* Usage:
* Add the class "blankTarget" to your link and this script will open the link in a new window.
* <a href="someLocation.html" class="blankTarget" title="Some Title">Popup</a>  
* 
*/
function showPopup() {
    var popup = window.open(this.href, "popup", "status=1,toolbar=1,location=1,menubar=1,scrollbars=1,resizable=1");
    popup.focus();
    return false;
}

function blankTargetLinks() {

    if (!document.getElementsByTagName) return;

    var anchors = document.getElementsByTagName("a");

    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];

        if (anchor.getAttribute("href")) {
            var external = /blankTarget/;
            if (anchor.getAttribute("class")) {
                var classValue = anchor.getAttribute("class");
            }
            else {
                var classValue = anchor.getAttribute("className");
            }
            if (external.test(classValue)) {
                anchor.onclick = showPopup;
            }
        }
    }

}
function DivTargetLinks(section) {
    //var anchors = document.getElementById(section).getElementsByTagName("a");
    var links = ["home","products", "training", "support", "News", "services", "events", "company"];//หากเพิ่ม Industries 
    for (var i = 0; i < links.length; i++) {
        if (links[i] == section) {
            document.getElementById(section).setAttribute((oldIE ? 'className' : 'class'), "on");
        }
        else {
            document.getElementById(links[i]).setAttribute((oldIE ? 'className' : 'class'), "");
        }
    }
}
window.onload = blankTargetLinks;
