// highlihgting current page
$(document).ready(function() {
  
  var timeout = 250,
      hide = false,
      menu = null,
      navRoot = document.getElementById("topnav");
  
  if (navRoot) {
    for (var i=0, len = navRoot.childNodes.length; i < len; i++) {
      var node = navRoot.childNodes[i];
      if (node.nodeName.toUpperCase() == "LI") {
        node.onmouseover = function() {
          hide = false;
          if (menu) {
            menu.className = menu.className.replace(" over", "");
          }
          menu = this;
          this.className += " over";
        };
        node.onmouseout=function() {
          hide = true;
          setTimeout(function(){
            if (hide) {
               menu.className = menu.className.replace(" over", "");
            }
          }, timeout);
        };
      }
    }
  }
  
  var sPath = window.location.pathname;
  var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
  var searchString = "a[href*="+ sPage+"]";

  // for pages not listed in the menu
  var sColPrefix = "col-"; // name pages with this prefix
  var sColHTML = "collection-status.shtml"; //put this link on the li that needs to be highlighted
  
  if (sPage.indexOf(sColPrefix) == 0) {
    searchString = "a[href*="+ sColHTML +"]";
  }

  // for pages not listed in the menu
  var sTrialPrefix = "trial-"; // name pages with this prefix
  var sTrialHTML = "trial-access.shtml"; //put this link on the li that needs to be highlighted
  
  if (sPage.indexOf(sTrialPrefix) == 0) {
    searchString = "a[href*="+ sTrialHTML +"]";
  }

  // for pages not listed in the menu
  var sPartPrefix = "part-"; // name pages with this prefix
  var sPartHTML = "part-contact.shtml"; //put this link on the li that needs to be highlighted
  if (sPage.indexOf(sPartPrefix) == 0) {
    searchString = "a[href*="+ sPartHTML +"]";
  }

  // for pages not listed in the menu
  var sBecomePrefix = "be-"; // name pages with this prefix
  var sBecomeHTML = "becoming-participant.shtml"; //put this link on the li that needs to be highlighted
  if (sPage.indexOf(sBecomePrefix)==0) {
    searchString = "a[href*="+ sBecomeHTML +"]";
  }

  // for pages not listed in the menu
  var sValuePrefix = "value-"; // name pages with this prefix
  var sValueHTML = "why-choose-artstor.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sValuePrefix)==0) {
    searchString = "a[href*="+ sValueHTML +"]";
  }

  // for pages not listed in the menu
  var sStaffPrefix = "staff-"; // name pages with this prefix
  var sStaffHTML = "trustees-and-staff.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sStaffPrefix)==0) {
    searchString = "a[href*="+ sStaffHTML +"]";
  }

  // for pages not listed in the menu
  var sAnnouncePrefix = "an-"; // name pages with this prefix
  var sAnnounceHTML = "announcements.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sAnnouncePrefix)==0) {
    searchString = "a[href*="+ sAnnounceHTML +"]";
  }

  // for pages not listed in the menu
  var sNewsPrefix = "send-me-artstor-news-"; // name pages with this prefix
  var sNewsHTML = "send-me-artstor-news.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sNewsPrefix)==0) {
    searchString = "a[href*="+ sNewsHTML +"]";
  }

  // for pages not listed in the menu
  var sContactPrefix = "contact-us-"; // name pages with this prefix
  var sContactHTML = "contact-us.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sContactPrefix)==0) {
    searchString = "a[href*="+ sContactHTML +"]";
  }
  
  // for pages not listed in the menu
  var sContactpartPrefix = "contact-participate-"; // name pages with this prefix
  var sContactpartHTML = "contact-participate.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sContactpartPrefix)==0) {
    searchString = "a[href*="+ sContactpartHTML +"]";
  }

  // for pages not listed in the menu
  var sHostingPrefix = "services-hosting-"; // name pages with this prefix
  var sHostingHTML = "services-hosting.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sHostingPrefix)==0) {
    searchString = "a[href*="+ sHostingHTML +"]";
  }

  // for pages not listed in the menu
  var sPromotePrefix = "promoting-artstor-"; // name pages with this prefix
  var sPromoteHTML = "promoting-artstor.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sPromotePrefix)==0) {
    searchString = "a[href*="+ sPromoteHTML +"]";
  }

  // for pages not listed in the menu
  var sLeadTrainPrefix = "leading-training-"; // name pages with this prefix
  var sLeadTrainHTML = "leading-training.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sLeadTrainPrefix)==0) {
    searchString = "a[href*="+ sLeadTrainHTML +"]";
  }


  // for pages not listed in the menu
  var sCollectionStatusPrefix = "collection-status-"; // name pages with this prefix
  var sCollectionStatusHTML = "collection-status.shtml"; //put this link on the li that needs to be highlighted
  if(sPage.indexOf(sCollectionStatusPrefix)==0) {
    searchString = "a[href*="+ sCollectionStatusHTML +"]";
  }

  //setup the tab
  $(searchString).parent("li").parent("ul").parent("li").addClass("main-selected");
  //setup the selected element
  $(searchString).parent("li").addClass("selected");

});

function launchwin(winurl, winname, winfeatures) {
	var win = window.open(winurl, winname, winfeatures);
	setTimeout(function(){
	  win.focus();
	}, 125);
}

var isPWB = 1 ;

function blockError() {
    isPWB = 0;
    this.opener = '';
    this.name = "dummy";
    this.close();
    return true;
}

var winName = 'ARTstor';
var winIE = null;
var winPWB = null;

function relocate(url) {
  window.location.replace(url);
}

function showCollectionsPWB() {
  showCollectionsReal(0);
}

function showCollectionsReal(pwb) {
    theURL = '/artstor/AccessLibrary.do';
    // Pass along parameter
    url = document.location.href;
    var idx = url.indexOf("?");
    var param = null;
    if (idx != -1) {
        param = url.substr(idx);
        theURL = theURL + param;
    }

    var newWinName = winName;
    var thisName = this.name;
    if (thisName == newWinName)
        newWinName = newWinName + '8';
    winName = newWinName;
    // ARTstor browser support: (if not supported, will see 'sorry page')
    // ** Mac: IE 5.2+, Mozilla 1.5+, Netscape 7.0+
    // ** PC:  IE 6+, Mozilla 1.5+, Netscape 6.2+
    var ie = getIEVer();
    var mz = getMzVer();
    if ((isMAC() && (ie >= 5.2 || mz >= 1.0 || isSafari())) ||
        (isLinux() && (mz >= 0.9)) ||
        (isPC() && (ie >= 5.5 || mz >= 0.9)) ||
        (isSun() && (mz >= 0.9))
            )
    {
        var maxX = 20;
        var maxY = 20;
        var topX = (screen.availWidth - 600) / 2;
        var topY = (screen.availHeight - 300) / 2;
        //        alert('before winPWB=' + winPWB + ', winIE=' + winIE);
        if ((pwb == 1) || isSafari()) {
            if (winIE != null) {
                //            winIE.close();
                winIE = null;
            }
            if (winPWB != null) {
                return;
            }
            topX = 0;
            topY = 0;
            maxX = screen.availWidth - 2;
            maxY = screen.availHeight - 22;
            newWinName = winName + 'pwb';
            //          alert('opening PWB ' + newWinName + ', url=' + theURL);
        }

        var winTemp;
        //        alert('maxX=' + maxX + ',maxY=' + maxY);
        winTemp = window.open(theURL, newWinName, ' status=no,resize=no, menu=no, location=no, top=' + topY
                + ',left=' + topX + ',width=' + maxX + ', height=' + maxY);
        if (winTemp != null && !(winTemp.closed)) {
            // make sure it stays on top.
            winTemp.focus();
            if (pwb == 0)
                winIE = winTemp;
            else
                winPWB = winTemp;
        } else {
            showDialog();
        }

        //        alert('opened winPWB=' + winPWB + ', winIE=' + winIE);
    }
    else
    {
        window.open('/global/g-html/browser-notice.html', 'ARTstor');
    }
    //      closethis();
}

function isSafari() {
    var agt = navigator.userAgent.toLowerCase();
    var is_safari = ((agt.indexOf('safari') != -1) && (agt.indexOf('mac') != -1) && (agt.indexOf('85.8.1') == -1))?true:false;
    return is_safari;
}

function getWinName() {
    var agent = navigator.userAgent.toLowerCase() ;
    if (agent.indexOf("msie") != -1) {
        this.opener = '';
    }
    this.name = "dummy";
    return this.name;
}

function closethis() {
    var agent = navigator.userAgent.toLowerCase() ;
    if (agent.indexOf("msie") != -1) {
        window.external.PWBExecute();
        if (isPWB == '0') { //Browser is IE
            this.opener = '';
            this.name = "dummy";
            this.close();
        }
    } else {
        winc = window.open(window.location.href, "_self");
        winc.close();
    }
}

function getXpos(win) {
  if (win.screenLeft) {
    return win.screenLeft;
  }
  else if (win.screenX) {
    return win.screenX;
  }
}

function getYpos(win) {
  if (win.screenTop) {
    return (win.screenTop - 22);
  }
  else if (win.screenY) {
    return (win.screenY - 22);
  }
}

