//////////////////////////////////////////////////////
//   PAGE FEATURES
////////////////////////////////////////////////////////

//alert("STARTING LOAD");


// Scroll handler to toggle classes.
window.addEventListener("scroll", function () { PageWhereAmI() });
window.addEventListener("keydown", function () { YourMove() });
//window.addEventListener("keypress", function () {YourMove() });
window.addEventListener("click", function () { CheckTheClicker() });

setTimeout(function () { CheckForNavigation(); }, 600);
setTimeout(function () { CheckForWashes(); }, 60);
setTimeout(function () { CheckForEventID(); }, 1000);


function CheckForNavigation() {
    //for the lower scroll to see how large the CSUF log should be
    try {
        var NavTest;
        NavTest = document.getElementById("navMain");
        // alert(NavTest.getElementsByTagName("li").length)
        if (NavTest.getElementsByTagName("li").length == 0) {
            document.getElementById("CSUFTopLink").className += " NoTabs";
        }
    } catch (err) {
        try {
            document.getElementById("CSUFTopLink").className += " NoTabs";
        } catch (err) { }


    }
}

var CheckForEventCount = 0;

function CheckForEventID() {
    var EventID = getUrlVars()["eventid"];
    if (EventID != "" && EventID != null) {
        //alert(EventID);
        try 
        {
            var EventJSData = document.getElementById("hdnEventID_" + EventID).value;
            
            //var section = document.getElementById("hdnEventID_div"+ i + "_Ajax_" + e).value;
                if (EventJSData != "") 
                {
                //alert(EventJSData);
                setTimeout(function () {EventFullDisplay(EventJSData); }, 300);
                }  
        }
        catch (err) {
           //alert(err.message);
            CheckForEventCount = parseInt(CheckForEventCount + 1);
            if (CheckForEventCount < 8) {
                setTimeout(function () { CheckForEventID(); }, 1200);
            } else {
                document.getElementById("SiteNotice").style.display = "block";
                document.getElementById("SiteNotice").className = "notice5";
                var strEventNotFound = "<h2>Event Information Not Found</h2>";

                strEventNotFound += "<p>We were unable to locate the event you tried to open.  It is possible that the event has passed and information is no longer available.</p>";
                strEventNotFound += "<p>Otherwise check the share url and try again or contact the department hosting the event for more information.</p>";
                strEventNotFound += "<p style=\"float:right\"><input style=\"font-size:12px; padding:3px;\" type=\"button\" class=\"glass button medBlue\" value=\"close\" onclick=\"HideNotice()\" /></p>";

                document.getElementById("SiteNoticeContents").innerHTML = strEventNotFound;
            }
        }

    }
}

function HideNotice() {
    document.getElementById("SiteNoticeContents").innerHTML = "";
    document.getElementById("SiteNotice").style.display = "none";
}


function getUrlVars() {
    var vars = {};
    var parts = window.location.href.toLowerCase().replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
        vars[key] = value;
    });
    return vars;
}

function PageWhereAmI() {
    var width;
    width = document.documentElement.clientWidth; // || document.documentElement.clientWidth || document.body.clientWidth;

    var B = document.body, H = document.documentElement, height;
    var Header = document.getElementById("Header");

    if (typeof document.height !== 'undefined') {
        height = document.height // For webkit browsers
    } else {
        height = Math.max(B.scrollHeight, B.offsetHeight, H.clientHeight, H.scrollHeight, H.offsetHeight);
    }


    // height = document.documentElement.clientHeight; // || document.documentElement.clientWidth || document.body.clientWidth;
   // if (document.getElementById("hdnHeaderSize").value == "0px") {
        //set the header height;
   //     document.getElementById("hdnHeaderSize").value = Header.clientHeight + "px";
  //  }

    // document.getElementById("test_info").style.display = "block";
   // document.getElementById("test_info").innerHTML = " ";s
    //document.getElementById("test_info").innerHTML += "top of page: " + window.pageYOffset + "<br/>";
   // document.getElementById("test_info").innerHTML += "Header height: " + Header.clientHeight + "<br/>";
   // document.getElementById("test_info").innerHTML += "overall height: " + height + "<br/>";
  //  document.getElementById("test_info").innerHTML += "Main Content CSS: " + document.getElementById("MainContent").className;

    //if (width > 800) {
    //    //user screen is not considered a mobile device

    //    if (parseInt(window.pageYOffset) > parseInt(Header.clientHeight + 80)) {
    //        //user scrolled down page so the top is no longer showing
    //        try {
    //            document.getElementById("back_to_top").className = "BTT_on";
    //            document.getElementById("Header").className = "PageHeader LowerScroll";
    //            document.getElementById("Navigation").className = "LowerScroll";
    //            document.getElementById("SiteNotice").className = "LowerScroll";
    //            document.getElementById("SiteName_Tabs").className = "LowerScroll";
    //            document.getElementById("CSUFLink_Search").className = "LowerScroll";
    //            document.getElementById("SearchNav").className = "LowerSearch";
    //            document.getElementById("MainContent").className = "LowerScroll_solo";
    //            document.getElementById("divFooter").className = "LowerScroll";
    //            //document.getElementById("MainContent").style.marginTop = document.getElementById("hdnHeaderSize").value;
    //        } catch (err) { }


    //    } else {
    //        //user is seeing the top of the page
    //        try {
    //            document.getElementById("back_to_top").className = "BTT_off";
    //            document.getElementById("Header").className = "PageHeader";
    //            document.getElementById("Navigation").className = "FullScreen";
    //            document.getElementById("SiteName_Tabs").className = "FullScreen";
    //            document.getElementById("CSUFLink_Search").className = "FullScreen";
    //            document.getElementById("MainContent").className = "FullScreen";
    //            document.getElementById("divFooter").className = "FullScreen";
    //            document.getElementById("MainContent").style.marginTop = "0px";
    //        } catch (err) {
    //            //alert(err);
    //        }

    //        try {
    //            if (document.getElementById("SearchNav") != null) {
    //                document.getElementById("SearchNav").className = "FullScreen";
    //            }

    //        }
    //        catch (err) { }

    //        try {
    //            if (document.getElementById("divTopMedia") != null) {
    //                document.getElementById("divTopMedia").className = "TopMedia ";
    //            }
    //            document.getElementById("PageBanner").className = "FullScreen";
    //        }
    //        catch (err) { }

    //        try {
    //            document.getElementById("Breadcrumb").className = "CrumbMover FullScreen";
    //        }
    //        catch (err) { }
    //    }
    //}

    CheckForAutoLoadEvents();
}


function getBrowserDimensions() {
    return {
        width: (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth),
        height: (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight)
    };
}

//var browser_dims = getBrowserDimensions();

var browser_dims = "1000:400";

function ShowMainNavMobile() {
    var NavMain = document.getElementById("navMain");
    var NavTabs2Main = document.getElementById("navTabs2Main");
    var NavMainCount, NavTabs2MainCount;

    try {
        NavMainCount = NavMain.childElementCount;
    }
    catch (err) {
        NavMainCount = 0;
    }

    try {
        NavTabs2MainCount = NavTabs2Main.childElementCount;
    }
    catch (err) {
        NavTabs2MainCount = 0;
    }

    if (NavMainCount > 0) {
        for (i = 0; i < NavMainCount; i++) {
            if (i != 0) {
                if (NavMain.children[i].className != "ShowMobileNav") {
                    NavMain.children[i].className = "ShowMobileNav";
                    document.getElementById("lnkShowMainNavMobile").className = "open";
                } else {
                    NavMain.children[i].className = "HideMobileNav";
                    document.getElementById("lnkShowMainNavMobile").className = "closed";
                }

            }
        }

    }

    var SkipFirst = 1;

    if (NavMainCount > 0) SkipFirst = 0;

    if (NavTabs2MainCount > 0) {
        for (i = 0; i < NavTabs2MainCount; i++) {
            if (i == 0 && SkipFirst == 1) { }
            else {
                if (NavTabs2Main.children[i].className != "ShowMobileNav") {
                    NavTabs2Main.children[i].className = "ShowMobileNav";
                    document.getElementById("lnkShowMainNavMobile").className = "open";
                } else {
                    NavTabs2Main.children[i].className = "HideMobileNav";
                    document.getElementById("lnkShowMainNavMobile").className = "closed";
                }

            }
        }

    }
    // alert(NavMain.children[1].style.display);
}

function ShowLeftNavMobile() {
    var LeftNav = document.getElementById("mobileLeftNav");

    if (LeftNav.className != "ShowMobileNav") {
        LeftNav.className = "ShowMobileNav";
        document.getElementById("lnkShowLeftNavMobile").className = "open";
    } else {
        LeftNav.className = "HideMobileNav";
        document.getElementById("lnkShowLeftNavMobile").className = "closed";
    }

}

function ShowLeftResourcesMobile() {
    var LeftResources = document.getElementById("mobileLeftResourcesNav");

    if (LeftResources.className != "ShowMobileNav") {
        LeftResources.className = "ShowMobileNav";
        document.getElementById("lnkShowLeftResourcesMobile").className = "open";
    } else {
        LeftResources.className = "HideMobileNav";
        document.getElementById("lnkShowLeftResourcesMobile").className = "closed";
    }
}

///////////////////////////////////////////////////////////////////////////////
// AJAX AUTO LOAD EVENTS
function CheckForAutoLoadEvents() {
    var TheA = "A".charCodeAt(0);
    var TheZ = "Z".charCodeAt(0);

    for (i = TheA; i <= TheZ; i++) {
        // alert(String.fromCharCode(i));
        try {
            var divIndex = 'div' + String.fromCharCode(i);
            var checkclass = document.getElementById(divIndex + "_Ajax_AutoLoadEvents").className.toLowerCase();

            if (checkclass.indexOf("load_data") >= 0) {
                // alert("found one in " + divIndex);
                LoadMoreEvents(divIndex + "_Ajax");
            }
        } catch (err) {
        }
    }
}

//////////////////////////////////////////////////////////////////////
// CHECK FOR WASHES

function CheckForWashes() {
    var TheA = "A".charCodeAt(0);
    var TheZ = "Z".charCodeAt(0);

    for (i = TheA; i <= TheZ; i++) {
        // alert(String.fromCharCode(i));
        try {
            var divIndex = 'div' + String.fromCharCode(i)
            var checkclass = document.getElementById(divIndex + "_shell").className.toLowerCase();

            if (checkclass.indexOf("wash_univblue") >= 0) {
                document.getElementById(divIndex + "_shell").className = document.getElementById(divIndex + "_shell").className + " wash_text_white";
                // alert("found one in " + divIndex);
                document.getElementById(divIndex + "_colorwash").className = "wash_univBlue_wash";
            }

            if (checkclass.indexOf("wash_medblue") >= 0) {
                document.getElementById(divIndex + "_shell").className = document.getElementById(divIndex + "_shell").className + " wash_text_white";
                // alert("found one in " + divIndex);
                document.getElementById(divIndex + "_colorwash").className = "wash_medBlue_wash";
            }

            if (checkclass.indexOf("wash_gray") >= 0) {
                document.getElementById(divIndex + "_shell").className = document.getElementById(divIndex + "_shell").className + " wash_text_white";
                // alert("found one in " + divIndex);
                document.getElementById(divIndex + "_colorwash").className = "wash_gray_wash";
            }

            if (checkclass.indexOf("wash_black") >= 0) {
                document.getElementById(divIndex + "_shell").className = document.getElementById(divIndex + "_shell").className + " wash_text_white";
                // alert("found one in " + divIndex);
                document.getElementById(divIndex + "_colorwash").className = "wash_black_wash";
            }
        } catch (err) {
        }
    }
}

//////////////////////////////////////////////////////
//KEYBOARD MOVES
function YourMove(e) {
    e = e || window.event;
    // alert(e.keyCode);

    try {
        //check for the image gallery
        if (document.getElementById('LightBox').className == "lightbox_on") {
            MoveTheGallery(e);
            return;
        }
    } catch (err) { }

    try {
        //move the location the user wants

        if (e.keyCode == '13') {
            // enter key
            //alert("Enter");
            //check to see if they are on a List_AboutItem H3
            //alert(document.activeElement.onclick);
            var checkOnclick = document.activeElement.onclick.toString();
            // alert(checkOnclick);
            if (checkOnclick.indexOf("AboutItem_Show") > 0) {
                //we are on an element that needs to be clicked
                document.activeElement.click();
            }
        }
        else if (e.keyCode == '38') {
            // up arrow
            //alert("Up");
        }
        else if (e.keyCode == '40') {
            // down arrow
            //alert("Down");
        }
        else if (e.keyCode == '37') {
            // left arrow
            //alert("Left");
        }
        else if (e.keyCode == '39') {
            // right arrow
            //alert("right");
        }
        else if (e.keyCode == '36') {
            //home key
            // alert("home");
        }
        else if (e.keyCode == '35') {
            //end key
            // alert("end");
        }
        else if (e.keyCode == '27') {
            //esc key
            // alert("esc");
            if (document.getElementById("SearchNav").className == "LowerSearch") {
                document.getElementById("SearchNav").className = "LowerSearch_hide";
                document.getElementById("SearchIcon").focus;
            }

            if (document.getElementById("blackout").className == "blackout_on") {
                try {
                    AboutItem_CloseAll(-1);
                } catch (err) { }
            }
        }
        else if (e.keyCode == '9') {
            //tab key
            // alert("tab");
        }
    }
    catch (err) { }
}

/////////////////////////////////////////////////////////////////////////

function MoveTheGallery(e) {
    try {
        if (document.getElementById('LightBox').className == "lightbox_on") {
            //move image gallery based on specific key press
            e = e || window.event;
            //alert(e.keyCode);

            if (e.keyCode == '38') {
                // up arrow
                //alert("Up");
            }
            else if (e.keyCode == '40') {
                // down arrow
                //alert("Down");
            }
            else if (e.keyCode == '37') {
                // left arrow
                lightbox_prev();
            }
            else if (e.keyCode == '39') {
                // right arrow
                lightbox_next();
            }
            else if (e.keyCode == '36') {
                //home key
                lightbox_first();
            }
            else if (e.keyCode == '35') {
                //end key
                lightbox_last();
            }
            else if (e.keyCode == '27') {
                //esc key
                lightbox_exit();
            }
        }
    }
    catch (err) { }
}

function CheckTheClicker(e) {
    //check to see if the gallery is open
    e = e || window.event;
    //alert(e.target.id);
    if (e.target.id == "" || e.target.id == null) {
        try {
            if (document.getElementById('LightBox').className == "lightbox_on") {
                lightbox_exit()
            }
        } catch (err) { }
    }

}

function ShowSearchAreas() {
    if (document.getElementById("lnkSearchDomains").className == "Open") {
        document.getElementById("searchRadioBtns").className = "Close";
        document.getElementById("searchTools").className = "Close";
        document.getElementById("lnkSearchDomains").className = "Close";
    } else {
        document.getElementById("searchRadioBtns").className = "Open";
        document.getElementById("lnkSearchDomains").className = "Open";
        document.getElementById("searchTools").className = "Open";
        document.getElementById("searchRadioBtns").focus();
    }
}

//PageWhereAmI();
//alert("END OF LOAD");

function ShowHideNav(navid) {
    var li = navid.replace("btn", "");

    btnnav = document.getElementById(navid);
    try {
        if (btnnav.innerHTML == "+") {
            btnnav.innerHTML = "-";
            btnnav.className = "menu_open";
            btnnav.title = "close";
            document.getElementById(li).className = "active";

        } else {
            btnnav.innerHTML = "+";
            btnnav.className = "menu_closed";
            btnnav.title = "open";
            document.getElementById(li).className = "";
        }
    } catch (err) { }
}


function ShowFocusNav(navid) {
    var li = navid.replace("btn", "");

    btnnav = document.getElementById(navid);
    try {
        if (btnnav.innerHTML == "+") {
            btnnav.innerHTML = "-";
            btnnav.className = "menu_open";
            btnnav.title = "close";
            document.getElementById(li).className = "active";

        }
    } catch (err) { }
}

//Left Nav Slider
function ExpandLeftMenu() {
    if (document.getElementById('MobileVertNavSlider').innerHTML == "&gt;") {
        document.getElementById('MobileVertNavSlider').className = "showright";
        document.getElementById('MobileVertNavSlider').innerHTML = "&lt;";
        document.getElementById('VerticalNavMenu').className = "showright";
    }
    else {
        document.getElementById('MobileVertNavSlider').className = "hideleft";
        document.getElementById('MobileVertNavSlider').innerHTML = "&gt;";
        document.getElementById('VerticalNavMenu').className = "hideleft";
    }
}

//move breadcrumbs
setTimeout(function () { BreadCrumbClass(); }, 500);

function BreadCrumbClass() {
    try {
        //  alert("crumbs");
        document.getElementById("Breadcrumb").className = "CrumbMover";
    }
    catch (err) {
        //alert(err.message)
    }
}

//hours start up: main page, lower scroll, and mobile
setTimeout(function () { Get_Site_Hours(); }, 500);

function Get_Site_Hours() {

    try {
        //  alert("crumbs");
        Hours_GetHours_ByDay_HeaderPreview();
    }
    catch (err) {
        //alert(err.message)
    }
}

function Display_Site_Hours(TheHours) {
    //alert(TheHours);
    try {
        document.getElementById("SiteHours").innerHTML = TheHours;
        document.getElementById("LowerScroll_SiteHours").innerHTML = TheHours;
        document.getElementById("mobile_SiteHours").innerHTML = TheHours;
    }
    catch (err) {
        //alert(err);
    }
}

//determine if IE then disable sitecues, it doesn't seem to launch in IE correctly
setTimeout('IE_version()', 50);

function IE_version() {

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    var trident = ua.indexOf("Trident/");

    //alert(ua);

    if (msie > 0 || trident > 0)  // If Internet Explorer, return version number
    {
        // document.getElementById("sitecues-badge").className = "hideme";
        //alert('IE');
        var HeaderStyles = document.getElementsByTagName("head")[0].getElementsByTagName("link");

        var resetCSS = HeaderStyles[0].href;


        var styles = resetCSS.replace("reset.css", "IEBackwards.css");
        styles = styles.replace("https:", "");
        styles = styles.replace("http:", "");


        var newCSS = document.createElement('link');
        newCSS.rel = 'stylesheet';
        newCSS.href = styles;
        newCSS.type = "text/css";
        document.getElementsByTagName("head")[0].appendChild(newCSS);

    }
    else  // If another browser, return 0
    {
        //alert('otherbrowser');
    }

    return false;
}

//MOBILE USE FUNCTIONS
function mobilemenu() {
    if (document.getElementById('Navigation').className != 'mobileopen') {
        document.getElementById('Navigation').className = 'mobileopen';
        document.getElementById('divHamburgerMenu').className = 'mobileon';
        window.scrollTo(0, 0);
    } else {
        document.getElementById('Navigation').className = 'mobileclosed';
        document.getElementById('divHamburgerMenu').className = 'mobileoff';
    }
}

function mobileexpand(div) {

    // if(document.getElementById(div).className == 'mobileexpand')
    // {
    //     document.getElementById(div).className = 'mobilecollapse';
    //	document.getElementById(div + "_button").innerHTML = '+';
    // }else
    //  {
    //      document.getElementById(div).className = 'mobileexpand';
    //	    document.getElementById(div + "_button").innerHTML = '-';
    //   }
}

function Mobile_ShowExtended(letter) {
    if (document.getElementById("extended" + letter).className != 'open') {
        document.getElementById("extended" + letter).className = 'open';
        document.getElementById("btnExtended" + letter).className = 'extendedmenu_open';
    } else {
        document.getElementById("extended" + letter).className = '';
        document.getElementById("btnExtended" + letter).className = 'extendedmenu_closed';
    }
}

function mobile_leftmenu() {
    if (document.getElementById('btnMobileLeftMenu').className != 'open') {
        document.getElementById('btnMobileLeftMenu').className = 'open';
        document.getElementById('btnMobileLeftMenu').innerHTML = 'Close Section Navigation';
        document.getElementById('VerticalNavMenu').className = 'open';
    } else {
        document.getElementById('btnMobileLeftMenu').className = 'closed';
        document.getElementById('btnMobileLeftMenu').innerHTML = 'Open Section Navigation';
        document.getElementById('VerticalNavMenu').className = 'closed';
    }
}



function SiteSearch() {
    var searchDomain = getRadioValue("as_sitesearch");
    var tempSearch = "";

    //alert(document.getElementById('hdnSearchType').value);
    UnderstandUsage(searchDomain, document.getElementById("SearchCSUF").value, 'site_search', 'search');

    if (searchDomain == 'www.fullerton.edu' || document.getElementById('hdnSearchType').value == 1) {
        //we are searching the main campus and not a site site search
        document.getElementById("frmSiteSearch").submit()
    }
    else {
        //we are doing a multi domain search
        document.getElementById("lnkSearchDomains").href = "https://www.google.com/search?q=" + document.getElementById('SearchCSUF').value + "+" + searchDomain;
        document.getElementById("lnkSearchDomains").click();
    }

}


function UnderstandUsage(link, linktext, category, action) {
    var EventInfo = "" + linktext.trim() + " [" + link + "]";

    if (document.getElementById('hdnIsDev').value != "1") {
        try {
            //gtag('event', 'login', {'method': 'Google'});
            if (action == 'click') {
                gtag('event', 'click', { 'event_category': category, 'event_label': EventInfo });
            }

            if (action == 'search') {
                gtag('event', 'search', { 'event_category': category, 'search_term': linktext });
            }
        }
        catch (err) {
            //alert(err.message);
        }
    }

    return true;
}



function getCheckValue(ElementID) {
    var test;

    if (document.getElementById(ElementID).checked) { test = 1; }
    else { test = 0; }

    return test;
}

function getRadioValue(ElementName) {
    var theValue, length;
    var theRadio = document.getElementsByName(ElementName);

    for (var i = 0, length = theRadio.length; i < length; i++) {
        if (theRadio[i].checked) {
            theValue = theRadio[i].value;
            break;
        }
    }
    return theValue;
}

//Test to see if the link you are on is included in a left nav that is auto collapsed.  
//if it is we will auto expand that set
setTimeout('AutoExpandLeftNav()', 200);

function AutoExpandLeftNav() {
    var currentURL = window.location.toString();
    currentURL = currentURL.replace("https:", "");
    currentURL = currentURL.replace("http:", "");

    try {
        var LeftNav = document.getElementById("VerticalNavMenu");
        var foundme = "Nope";

        if (LeftNav.children[0].className.indexOf("collapsingnav") > 0) {
            //we have a left nav that is collapsing
            var LI = LeftNav.getElementsByTagName("li");
            var LICount = LI.length
            var A_Test = "";
            var A_Href = "";
            var ParentID = "";
            var Attrb = "";
            var isParent = 0;

            for (var i = 0; i < LI.length; i++) {

                if (LI[i].getAttribute("id") != null) {
                    //alert(ParentID);
                    ParentID = LI[i].getAttribute("id");
                }

                // document.writeln(A_Test[0].getAttribute("href") + "<br/>");
                A_Test = LI[i].getElementsByTagName("a");

                try {
                    A_Href = A_Test[0].getAttribute("href").toString();
                    A_Href = A_Href.replace("https:", "");
                    A_Href = A_Href.replace("http:", "");

                    // document.writeln(A_Href.toString().trim() + "|" + currentURL.toString().trim() + "<br/>");
                    if (A_Href.toString().trim() == currentURL.toString().trim()) {
                        foundme = "You Betcha";

                        if (ParentID != '') {    // we are in a child link and we need to open the parent
                            var ParentButton = "left_lnavbtn_" + ParentID.replace("left_lnav_", "");
                            setTimeout(function () { ShowHideNav(ParentButton); }, 100);
                            LI[i].style.borderLeft = "solid 4px #E17000";
                            LI[i].style.backgroundColor = "#f9f9f9";
                            LI[i].style["boxShadow"] = "2px 0px 2px rgba(0,0,0,0.05)";
                        }

                    }
                } catch (err) {
                    //  document.write("<p>ERROR: " + err.message + "</p>");
                }
            }

            // alert(LICount);
            // alert(foundme);
        }
    } catch (err) { }
}

//Test to see the user needs to have a logout button  
//if they are already authenticated it will build one

//setTimeout('auth_NeedLogoutBtn()', 400);

////////////////////////////////////////////////////////////////////
// SEARCH & SETTINGS SLIDEOUT OPTIONS
////////////////////////////////////////////////////////////////////

function SearchSlideoutStart() {
    //var searchButton = "<a id=\"btnSearchSlider\" title=\"Search\" href=\"javascript:ShowHideSearchboxSlider()\"><span class=\"visually-hidden\">Search</span></a>";
    //var settingsButton = "<a id=\"btnSettingsSlider\" title=\"Site Resources\" href=\"javascript:ShowHideResourcesboxSlider()\"><span class=\"visually-hidden\">Site Resources</span></a>";
    //document.getElementById("SiteSearch_starting").innerHTML = settingsButton + "&nbsp;&nbsp;&nbsp;" + searchButton;
    
    if (document.getElementById("SiteSearch_panel").innerHTML != "") {
        //load the search box
   
        var ServerBase = document.getElementById("hdnServerBase").value;
        var Parameters = document.getElementById("hdnServerBase").value;
        var site_protocol = window.location.protocol;

        //alert(site_protocol);

        if (site_protocol.toLowerCase() == "https:") {
            //make sure the search string matches the current protocol used 
            ServerBase = ServerBase.toLowerCase().replace("http:", "https:");
            Parameters = Parameters.toLowerCase().replace("http:", "https:");
        }

        //alert(Parameters);

        AjaxMe(ServerBase, 'LoadSearchForm', Parameters, 'SiteSearch_panel');

        //startup search box
        setTimeout(function () { StartUpSearchBox(); }, 500);

     
        //load the site resources box
        //AjaxMe(ServerBase, 'LoadSiteResources', Parameters, 'SiteResources_panel');
    }

}

function StartUpSearchBox() {
    var cx = '003297517689816257862:da3id2041ie';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
}

function LoadSearchBucket(BucketType) {
    //load the search box
    var SearchTerm = "";

    try { SearchTerm = document.getElementById("gsc-i-id1").value; }
    catch (err) { }

    var ServerBase = document.getElementById("hdnServerBase").value;
    var Parameters = document.getElementById("hdnServerBase").value;
    var site_protocol = window.location.protocol;

        //alert(site_protocol);

    if (site_protocol.toLowerCase() == "https:") {
       //make sure the search string matches the current protocol used 
       ServerBase = ServerBase.toLowerCase().replace("http:", "https:");
       Parameters = Parameters.toLowerCase().replace("http:", "https:");
   }

    Parameters += "|" + BucketType + "|1";
    AjaxMe(ServerBase, 'LoadSearchBucket', Parameters, 'divSearchBucket');

    setTimeout(function () { StartUpSearchBox(); }, 400);
    setTimeout(function () { StartUpSearchBox(); }, 800);
    setTimeout(function () { StartUpSearchBox(); }, 900);

    if (SearchTerm != "") setTimeout(function () { ReSearch(SearchTerm); }, 950);

}

function ReSearch(SearchTerm) {
    try {
        document.getElementById("gsc-i-id1").value = SearchTerm;

        var SearchBucket = document.getElementById("divSearchBucket");
        var SearchBTN = SearchBucket.getElementsByTagName("button");
        //alert("test");
        //alert(SearchBTN[0].className);
        setTimeout(function () { SearchBTN[0].click(); }, 400);
    }
    catch (err) { }
}

function ShowHideSearchboxSlider() {
    if (document.getElementById("SiteSearch_panel").className == "HideSlider") {
        //slider is closed, need to open it
        document.getElementById("SiteSearch_panel").className = "ShowSlider";
        document.getElementById("gsc-i-id1").focus();

    } else {
        //slider is open, need to close it
        document.getElementById("SiteSearch_panel").className = "HideSlider";
    }
}

function ShowHideResourcesboxSlider() {
    if (document.getElementById("SiteResources_panel").className == "HideSlider") {
        //slider is closed, need to open it
        document.getElementById("SiteResources_panel").className = "ShowSlider";
    } else {
        //slider is open, need to close it
        document.getElementById("SiteResources_panel").className = "HideSlider";
    }
}

/*--------------------------------------------------------
  CHECK PAGE FOR SIZE AND MOVE ELEMENTS TO THE RIGHT LOCATION
 ------------------------------------------------------------- */

function CheckPageSetup() {
    var windowWidth;
    var MobileLeftNav, MobilePageMenu;
    var MobileAdditionalInfo, MobileAdditionalMenu;
    var SocialMenu, SocialHolder, SiteHours, SiteHoursHolder;

    windowWidth = window.innerWidth;

    //document.getElementById("test_info").style.display = "block";
    //document.getElementById("test_info").innerHTML = windowWidth;

    if (windowWidth <= 800) {
        //move left nav into mobile area
        if (document.getElementById("VerticalNavMenu") != null) {
            if (document.getElementById("VerticalNavMenu").innerHTML != '') {
                //we have a left nav
                MobileLeftNav = document.getElementById("VerticalNavMenu").innerHTML;
                MobileAdditionalInfo = document.getElementById("LeftSideblocks").innerHTML;

                MobilePageMenu = "<h3><a id=\"lnkShowLeftNavMobile\" href=\"javascript:ShowLeftNavMobile()\">Page Menu</a></h3>";
                MobilePageMenu += "<div id=\"mobileLeftNav\" role=\"complementary\">";
                MobilePageMenu += MobileLeftNav + "</div>";

                MobileAdditionalMenu = "<h3><a id=\"lnkShowLeftResourcesMobile\" href=\"javascript: ShowLeftResourcesMobile()\">Additional Information</a></h3>";
                MobileAdditionalMenu += "<div id=\"mobileLeftResourcesNav\" role=\"complementary\">";
                MobileAdditionalMenu += MobileAdditionalInfo + "</div>";

                document.getElementById("divMobileLeftNav").innerHTML = MobilePageMenu;

                if (MobileAdditionalInfo != "" && MobileAdditionalInfo.length > 1050) document.getElementById("divMobileLeftNav").innerHTML += MobileAdditionalMenu;

                document.getElementById("VerticalNavMenu").innerHTML = "";
                try { document.getElementById("LeftSideblocks").innerHTML = ""; }
                catch (err) { }
            }
        }

        //move social and time information to the top of the left menu
        if (document.getElementById("Social") != null) {
            if (document.getElementById("Social").innerHTML != "") {
                SocialMenu = document.getElementById("Social").innerHTML;
                document.getElementById("Social").innerHTML = "";

                document.getElementById("divMobileSocialHours").innerHTML = "<div id=\"divSocialMobile\">" + SocialMenu + "</div>";
            }
        }

        if (document.getElementById("SiteHours") != null) {
            if (document.getElementById("SiteHours").innerHTML != "") {
                SiteHours = document.getElementById("SiteHours").innerHTML;
                document.getElementById("SiteHours").innerHTML = "";

                document.getElementById("divMobileSocialHours").innerHTML += "<div id=\"divSiteHoursMobile\">" + SiteHours + "</div>";
            }
        }
        //document.getElementById("divMobleLeftNav").innerHTML = "";
        //document.getElementById("test_info").innerHTML += "<br/> Will be mobile nav";
    } else {
        //move the mobile left nav back to left nav proper
        if (document.getElementById("divMobileLeftNav") != null) {
            if (document.getElementById("divMobileLeftNav").innerHTML != '' && document.getElementById("VerticalNavMenu").innerHTML == '') {
                //we have a left nav
                try {
                    MobileLeftNav = document.getElementById("mobileLeftNav").innerHTML;
                } catch (err) {
                    MobileLeftNav = "";
                }

                try {
                    MobileAdditionalInfo = document.getElementById("mobileLeftResourcesNav").innerHTML;
                } catch (err) {
                    MobileAdditionalInfo = "";
                }
                document.getElementById("VerticalNavMenu").innerHTML = MobileLeftNav;

                if (MobileAdditionalInfo != "") document.getElementById("LeftSideblocks").innerHTML += MobileAdditionalInfo;

                document.getElementById("divMobileLeftNav").innerHTML = "";
                try { document.getElementById("").innerHTML = ""; }
                catch (err) { }
            }
        }

        //move social and time information to the top of the left menu
        if (document.getElementById("divSocialMobile") != null) {
            if (document.getElementById("divSocialMobile").innerHTML != "" && document.getElementById("Social").innerHTML == '') {
                SocialMenu = document.getElementById("divSocialMobile").innerHTML;
                document.getElementById("divSocialMobile").innerHTML = "";

                document.getElementById("Social").innerHTML = SocialMenu;
            }
        }

        if (document.getElementById("divSiteHoursMobile") != null) {
            if (document.getElementById("divSiteHoursMobile").innerHTML != "" && document.getElementById("SiteHours").innerHTML == '') {
                SiteHours = document.getElementById("divSiteHoursMobile").innerHTML;
                document.getElementById("divSiteHoursMobile").innerHTML = "";

                document.getElementById("SiteHours").innerHTML = SiteHours;
            }
        }
    }
}



/*-----------------------------------------------------------------------
//   SITE DIRECTORY BUILDER
-----------------------------------------------------------------------------*/
function SEO_BuildDirectory(Folder, Section) {
    var ServerBase = document.getElementById('hdnServerBase').value;
    var Parameters = ServerBase + "|" + Folder + "|" + Section;

    //alert(Parameters);
    AjaxMe(ServerBase, 'SEO_BuildDirectory', Parameters, Section);
}



//////////////////////////////////////////////////////////////////

setTimeout(function () { CheckPageSetup(); }, 150);
setTimeout(function () { CheckPageSetup(); }, 750);

function OnBodyResize() {
    collectMediaLayouts();
}


function collectMediaLayouts() {
    var myGalleryLayouts = document.querySelectorAll(".GalleryMediaLayout");

    window.addEventListener("resize", OnBodyResize);
    
    //alert(myGalleryLayouts.length);
    //alert(myGalleryLayouts[0].id);

    var maxWidth, originalMaxHeight, maxHeight, ulWidth, imgWidth, imgHeight, imgURL, myCanvas, myTextBlocks, rowspan, blockwidth;
    var RowspanHeight_3, RowspanHeight_2, RowspanHeight_1, canvasHeight;

    
    for (var u = 0; u < myGalleryLayouts.length; u++) {
        //get the ul holding images as an object
        var myGalleryUL = document.getElementById(myGalleryLayouts[u].id);

        //get the width of the UL, if too small we show things in mobile view
        ulWidth = myGalleryUL.offsetWidth;
        maxWidth = ulWidth;

        if (ulWidth < 500) {
            originalMaxHeight = 150;
        } else if (ulWidth < 700) {
            originalMaxHeight = 225;
        } else if (ulWidth < 900) {
            originalMaxHeight = 300;
        } else {
            originalMaxHeight = 400;
        }

        maxHeight = originalMaxHeight;
        //get the li's within that ul as an object
        var myGalleryLI = myGalleryUL.querySelectorAll("li");

        for (var l = 0; l < myGalleryLI.length; l++) {
            //get the main building block for the current row of images
            var myBlock = myGalleryLI[l].querySelectorAll("#block-" + l);
            //alert("block-" + l);
            var myDivs = myBlock[0].querySelectorAll(".MediaHolder");
            //alert(myDivs.length);

            //precheck the heights so we can just set them as needed by the blocks
            var heightLarge = 0;
            var heightMedium = 0;
            var heightSmall = 0;
            rowspan = 1;

            if (myDivs.length == 1) {

                //single Image 100% wide on Canvas
                myCanvas = myDivs[0].querySelectorAll("canvas");
                imgURL = myCanvas[0].style.backgroundImage.toLowerCase().replace(/url\((['"])?(.*?)\1\)/gi, '$2');
                imgHeight = GetImageHeight(imgURL);
                imgWidth = GetImageWidth(imgURL);

                //alert(ulWidth + " | " + imgWidth + " | " + imgHeight);
                maxWidth = ulWidth;
                maxHeight = originalMaxHeight;
                var imageSize = calculateAspectRatioFit(imgWidth, imgHeight, maxWidth, maxHeight);

                //alert("image[ height:" + imageSize.height + " width:" + imageSize.width + "] space[ height: " + maxHeight + " width: " + maxWidth + "]");

                if (imageSize.height < maxHeight) maxHeight = imageSize.height;
               
                myDivs[0].style.height = maxHeight + "px";

                myCanvas[0].style.width = maxWidth + "px";
                myCanvas[0].style.height = maxHeight + "px";

                //alert("single row: " + maxWidth + " | " + maxHeight + " | " + imgWidth + " | " + imgHeight);

            } else {

                maxWidth = ulWidth;
                maxHeight = originalMaxHeight;
                blockwidth = 0;
                rowspan = 0;
                //pre-check
                for (var d = 0; d < myDivs.length; d++) {

                    //alert("Rowspan: " + myDivs[d].getAttribute("rowspan") );

                    if (myDivs[d].hasAttribute("rowspan") === true)
                    {

                        if ((parseInt(myDivs[d].getAttribute("rowspan")) >= parseInt(rowspan)) && (parseInt(myDivs[d].getAttribute("blockwidth")) >= parseInt(blockwidth)))
                        {
                            rowspan = myDivs[d].getAttribute("rowspan");
                            blockwidth = myDivs[d].getAttribute("blockwidth");

                            myCanvas = myDivs[d].querySelectorAll("canvas");
                            for (var c = 0; c < myCanvas.length; c++) {
                                //alert("found one");

                                imgURL = myCanvas[c].style.backgroundImage.toLowerCase().replace(/url\((['"])?(.*?)\1\)/gi, '$2');
                                imgHeight = GetImageHeight(imgURL);
                                imgWidth = GetImageWidth(imgURL);

                                var imageSize = calculateAspectRatioFit(imgWidth, imgHeight, maxWidth, maxHeight);

                                if (imageSize.height < maxHeight) maxHeight = imageSize.height;
                            }
                        }
                    }
                }

                //alert("Multi part row: Rowspan " + rowspan + " | Blockwidth " + blockwidth);
                if (blockwidth == 100) {
                    maxWidth = ulWidth;
                } else {
                    maxWidth = ulWidth * ((100 - blockwidth) * .01);
                }

                //alert("Multi part row: " + maxWidth + " | " + maxHeight + " | " + imgWidth + " | " + imgHeight);
                //alert("Max height before: " + maxHeight + " | Rowspan: " + rowspan);
                switch (parseInt(rowspan)) {
                    case 3:
                        maxHeight = parseInt(maxHeight / 3);
                        RowspanHeight_3 = maxHeight * 3;
                        RowspanHeight_2 = maxHeight * 2;
                        RowspanHeight_1 = maxHeight;
                        break;

                    case 2:
                        maxHeight = parseInt(maxHeight / 2);
                        RowspanHeight_2 = maxHeight * 2;
                        RowspanHeight_1 = maxHeight;
                        break;

                    case 1:
                        RowspanHeight_1 = maxHeight;
                        break;
                }
                //alert("Max height After: " + maxHeight + "\nRowspan heights: " + RowspanHeight_1 + " | " + RowspanHeight_2 + " | " + RowspanHeight_3);

                //alert(maxWidth + " | " + maxHeight);
                for (var d = 0; d < myDivs.length; d++) {
                    //alert(myDivs[d].id + " | " + rowspan);
                    //alert("rowspan for " + myDivs[d].id + ": rowspan value " + myDivs[d].getAttribute("rowspan"));

                    switch (parseInt(myDivs[d].getAttribute("rowspan")))
                    {
                        case 3:
                            //alert("(3) div is " + RowspanHeight_3);
                            myDivs[d].style.height = RowspanHeight_3 + "px";
                            canvasHeight = RowspanHeight_3;
                            break;
                        case 2:
                            //alert("(2) div is " + RowspanHeight_2);
                            myDivs[d].style.height = RowspanHeight_2 + "px";
                            canvasHeight = RowspanHeight_2;
                            break;
                        case 1:
                            //alert("(1) div is " + RowspanHeight_1);
                            myDivs[d].style.height = RowspanHeight_1 + "px";
                            canvasHeight = RowspanHeight_1;
                            break;
                    }

                   // alert("Canvas Height: " + canvasHeight + " | Rowspan " + myDivs[d].getAttribute("rowspan"));

                    myCanvas = myDivs[d].querySelectorAll("canvas");
                    for (var c = 0; c < myCanvas.length; c++) {
                        myCanvas[c].style.height = canvasHeight + "px";
                    }

                    myTextBlocks = myDivs[d].querySelectorAll(".textblockholder");
                    for (var c = 0; c < myTextBlocks.length; c++) {
                        myTextBlocks[c].style.height = canvasHeight + "px";
                       // alert(myTextBlocks[c].style.height);
                    }
                }
            }
        }
    }
}

function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {

    var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);

    return { width: srcWidth * ratio, height: srcHeight * ratio };
}

function resizeCanvasHeight(maxWidth, maxHeight, imgWidth, imgHeight) {
    var ratio;

    if (imgWidth > maxWidth && imgWidth > imgHeight) {
        ratio = imgWidth / imgHeight;
        imgHeight = maxWidth / ratio;
    }

    return imgHeight;

}

function GetImageHeight(url) {
    var TestImage = new Image();
    TestImage.src = url;

    return TestImage.height;
}

function GetImageWidth(url) {
    var TestImage = new Image();
    TestImage.src = url;
    return TestImage.width;
}

function collectTableSearch() {
    var TheA = "A".charCodeAt(0);
    var TheZ = "Z".charCodeAt(0);

    for (i = TheA; i <= TheZ; i++)
    {
        // alert(String.fromCharCode(i));
        try
        {
            var divIndex = 'div' + String.fromCharCode(i);
            var checkclass = document.getElementById(divIndex + "_shell").className.toLowerCase();

            if (checkclass.indexOf("tablesearch") >= 0)
            {
                //alert("found a table that needs a search");
                var myContent = document.getElementById(divIndex + "_content");
                var myTableID = "tblSection" + String.fromCharCode(i);                
                var myTable = myContent.querySelector("table");
                myTable.setAttribute("id", "tblSection" + String.fromCharCode(i));
                
                var myFilterP = document.createElement("p");
                var myFilterInput = document.createElement("input");
                var myFilterLabel = document.createElement("label");
                var myFilterTextnode = document.createTextNode("Search Table: ");

                myFilterP.setAttribute("class", "TableFilter");
                
                myFilterLabel.setAttribute("for", "txtTblSection" + String.fromCharCode(i));
                myFilterLabel.appendChild(myFilterTextnode);

                myFilterInput.setAttribute("onkeyup", "SearchTable('Section" + String.fromCharCode(i) + "')");
                myFilterInput.setAttribute("id", "txtTblSection" + String.fromCharCode(i));
                
                myFilterP.appendChild(myFilterLabel);
                myFilterP.appendChild(myFilterInput);

                myContent.insertBefore(myFilterP, myTable);
            }

        } catch (err)
        {
               //alert(err.message);
        }
    }
}


function SearchTable(SectionName) {
    var input, filter, table, tr, td, t, i, txtValue, trdisplay;
    input = document.getElementById("txtTbl" + SectionName);
    filter = input.value.toUpperCase();
    table = document.getElementById("tbl" + SectionName);
    tr = table.querySelectorAll("tr");
    for (i = 1; i < tr.length; i++) {
        trdisplay = 0;
        if (input.value == "") trdisplay = 1;
        var tdALL = tr[i].querySelectorAll("td");
        for (t = 0; t < tdALL.length; t++) {
            td = tdALL[t];
            txtValue = td.textContent || td.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                trdisplay = "1";
            }
        }

        if (trdisplay == 0) {
            tr[i].style.display = "none";
        } else {
            tr[i].style.display = "table-row";
        }

    }
}
setTimeout(function(){fixGoogleATI()}, 2200);

///////////////////////////////////////////////////////////////
function fixGoogleATI() {
	
	//alert("Hi Mom");
    //create the holders for the new ATI parts
    var GSdivShell = document.createElement("div");
    var GSdivInput = document.createElement("div");
    var GSdivSearch = document.createElement("div");
    var GSdivClear = document.createElement("div");
    var GSdivResultCount = document.createElement("div");
    var GSdivResultOrderBy = document.createElement("div");
    var GSdivBottomDiv = document.createElement("div");
    var GSdivBottomHolderF = document.createElement("div");
    var GSdivBottomHolderE = document.createElement("div");
    var GSSearchLabel = document.createElement("label");

    //pull the elements created by Goolge, should be a form with a nested table inside
    var GSForm = document.querySelector(".gsc-search-box-tools");
    var GSTable = document.querySelector("table.gsc-search-box");
    var GSInputTD = GSTable.querySelector(".gsc-input");
    var GSInputDiv = GSInputTD.querySelector(".gsc-input-box");
    var GSInput = GSInputDiv.querySelector("input");
    var GSClearDiv = GSInputDiv.querySelector(".gsst_b");
    var GSClearA = GSInputDiv.querySelector(".gsst_a");
    var GSSearchTD = GSTable.querySelector("td.gsc-search-button");
    var GSSearchButton = GSSearchTD.querySelector("button");
    var GSResultTableDiv = document.querySelector("div.gsc-above-wrapper-area-invisible");
    var GSResultTable = GSResultTableDiv.querySelector("table");
    var GSBottomTable = document.querySelector("table.gssb_c");

    //assign our elements with the same attributes Google created
    GSdivShell.className = GSTable.getAttribute("class");

    //input box and holder
    GSdivInput.className = GSInputDiv.getAttribute("class");
    GSdivInput.id = GSInputDiv.getAttribute("id");

    GSSearchLabel.setAttribute("for", GSInput.getAttribute("id"));
    GSSearchLabel.setAttribute("class", "visually-hidden"); //change the class to something else if you don't have a style called ".visually-hidden" that is used for screen readers
    GSSearchLabel.innerHTML = "Search Term";
    GSInput.removeAttribute("title");

    GSdivInput.appendChild(GSSearchLabel);
    GSdivInput.appendChild(GSInput);

    //clear search feature and holder
    GSdivClear.className = GSClearDiv.getAttribute("class");
    GSdivClear.id = GSClearDiv.getAttribute("id");
    GSdivClear.setAttribute("dir", GSClearDiv.getAttribute("dir"));

    //add alternate text for accessiblity
    var GSClearAlternate = document.createElement("span");
    GSClearAlternate.innerHTML = "Clear Search Box";
    GSClearAlternate.setAttribute("class", "visually-hidden");

    GSClearA.appendChild(GSClearAlternate)
    GSdivClear.appendChild(GSClearA);

    //search button and holder
    GSSearchButton.setAttribute("type", "submit");
    GSdivSearch.className = GSSearchTD.getAttribute("class");
    GSdivSearch.appendChild(GSSearchButton);

    //results cound and order by
    GSdivResultCount.id = "gsc-result-time";
    GSdivResultOrderBy.id = "gsc-result-orderby";
    GSdivResultCount.appendChild(GSResultTable.querySelector("div.gsc-result-info-invisible"));
    GSdivResultOrderBy.appendChild(GSResultTable.querySelector("div.gsc-orderby-invisible"));

    //table created at the bottom of the page
    GSdivBottomDiv.setAttribute("style", GSBottomTable.getAttribute("style"));
    GSdivBottomDiv.setAttribute("class", GSBottomTable.getAttribute("class"));

    GSdivBottomHolderF.setAttribute("class", "gssb_f");
    GSdivBottomHolderE.setAttribute("class", "gssb_e");

    GSdivBottomDiv.appendChild(GSdivBottomHolderF);
    GSdivBottomDiv.appendChild(GSdivBottomHolderE);

    //if the table was found and we can replace stuff lets do it :)
    if (GSTable != null) {

        //load up the Shell with the elements
        GSdivShell.appendChild(GSdivInput)
        GSdivShell.appendChild(GSdivClear);
        GSdivShell.appendChild(GSdivSearch)

        //add our ATI compliant parts back to the Google Form
        GSForm.appendChild(GSdivShell);
        GSResultTableDiv.appendChild(GSdivResultCount);
        GSResultTableDiv.appendChild(GSdivResultOrderBy);
        document.body.appendChild(GSdivBottomDiv);

        //remove the tables from the page
        GSTable.remove();
        GSResultTable.remove();
        GSBottomTable.remove();
    }
}
//////////////////////////////////////////////////
//  END OF FILE
//////////////////////////////////////////////////