//global variables
var collapsedElements = new Array();
var elementTimers = new Array();
var videoFile = "";
var myTimer;
var browser = "FF";
if(navigator.userAgent.indexOf ("MSIE") != -1)
{ browser = "IE";
}
else if(navigator.userAgent.indexOf("Mozilla") != -1)
{ browser = "FF";
}

function init() {
    //alert("init");
    activateSmoothCollapse();
    activateBreadcrumbs();
    activateExternalLinks();
  if (videoFile != "") {
      var theDiv = document.getElementById("videoContainer2");
      if (!theDiv) { //no div found, exit the function
          return;
      }
      else {
          theDiv.innerHTML = "<img src='../video2.jpg' id='vidImg' alt='Click to Play Video' style='cursor:pointer'/>";
      }
  }
}

//function setVideo(name) {
//    videoFile = name;
//}

//function submitSearch()
//{ var theQuery = document.getElementById("searchQuery");
//  theQuery.value = "site:hcss.com " + theQuery.value;
//  document.getElementById("globalSearch").submit();
//}

function showPopupNavigation(id) {
    clearTimeout(myTimer);
    //hidePopupNavigationNow();

    myTimer = setTimeout("showPopupNavigation2('" + id + "')", 500);
}

function showPopupNavigation2(id)
{

  clearTimeout(myTimer);
  hidePopupNavigationNow();
  document.getElementById(id).style.display = "block";   
  
}

function hidePopupNavigationNow()
{ clearTimeout(myTimer);
  var hcssNavigation = document.getElementById("hcssNavigation");
  if(hcssNavigation)
  { for(var i = 0; i < hcssNavigation.childNodes.length; i++)
    { if(hcssNavigation.childNodes[i].id)
      { if(hcssNavigation.childNodes[i].id.substring(0,6) == "popup_")
        {  hideElement(hcssNavigation.childNodes[i].id);
        }
      }

      for(var j = 0; j < hcssNavigation.childNodes[i].childNodes.length; j++)
      { if(hcssNavigation.childNodes[i].childNodes[j].id)
        { if(hcssNavigation.childNodes[i].childNodes[j].id.substring(0,6) == "popup_")
          { hideElement(hcssNavigation.childNodes[i].childNodes[j].id);
          }
        }

        for(var k = 0; k < hcssNavigation.childNodes[i].childNodes[j].childNodes.length; k++)
        { if(hcssNavigation.childNodes[i].childNodes[j].childNodes[k].id)
          { if(hcssNavigation.childNodes[i].childNodes[j].childNodes[k].id.substring(0,6) == "popup_")
            { hideElement(hcssNavigation.childNodes[i].childNodes[j].childNodes[k].id);
            }
          }
        }
      }
    }
  }
}

function hidePopupNavigation()
{ clearTimeout(myTimer);
  myTimer = setTimeout("hidePopupNavigationNow()", 300);
}

function hideElement(id)
{ document.getElementById(id).style.display = "none";
}

function activateBreadcrumbs() {
    var global, navigation, subnavigation, localnavigation, localpage, localitem;
    //var URLCurrent = location.href.replace("hcss2010/", "");  // added this replace to account for sub directory hcss2009.
    //URLCurrent = URLCurrent.replace("hcss2010/", "");  // added this replace to account for sub directory hcss2009.

    var URLCurrent = location.href;
    URLCurrent = URLCurrent.replace("default.aspx", "");  //needed for main product pages to show nav
    //URLCurrent = URLCurrent.replace("hcss2010/", "");  // added this replace to account for sub directory hcss2009.
    var URLParser = new RegExp("http:\/\/([^\.]+)\.([^\.]+)\.com(.+)", "i"); //
    var debug = 0;
    if (URLCurrent.indexOf("?") != -1) {
        URLCurrent = URLCurrent.substring(0, URLCurrent.indexOf("?"));
        //alert(URLCurrent);
    }

  if(URLParser.test(URLCurrent)) {
      var results = URLParser.exec(URLCurrent);

    global = document.getElementById("global_" + results[2].toLowerCase());
    //Commented out to apply the style to the global nav with the style sheet
	//if(global)
    //{ global.style.backgroundImage = "url('/images/black_arrow.gif')";
	//  global.style.backgroundPosition = "center left";
    // global.style.backgroundRepeat = "no-repeat";
    //  global.style.paddingLeft = "5px";
    //  global.style.color = "black";
    //}
    if(results[3]) {
          URLCurrent = results[3];
          var pathToFile = URLCurrent.split("/");
          if(pathToFile[1] != null)
            {   if(pathToFile[1] == "" || pathToFile[1] == "default.aspx")
                { URLCurrent = "home";
                }
                else
                { URLCurrent = pathToFile[1];
                }
                if(URLCurrent.indexOf(".") != -1)
                { URLCurrent = URLCurrent.substring(0, URLCurrent.indexOf("."));
                }
                navigation = document.getElementById("navigation_" + URLCurrent.toLowerCase());
                if(navigation && URLCurrent != "home")
                { //navigation.style.backgroundImage = "url('images/mainNavBackground_roll.gif')";
                  navigation.style.backgroundPosition = "3px 5px";
                  navigation.style.backgroundRepeat = "repeat-x";
                  //navigation.style.paddingLeft = "5px";
                  //navigation.style.backgroundOffset = "0px 3px";
                  navigation.style.backgroundColor = "#219907";  // green background on top nav bar
                  //navigation.style.border = "1px solid #FFFFFF";
                }
          } //if(pathToFile[1] != null)
          if(pathToFile[2] != null && pathToFile[2] != "") {
              if (debug == 1) alert("subnavigation : path to file 2");
            URLCurrent = pathToFile[2];
            if (debug == 1) alert(URLCurrent);
            if(URLCurrent.indexOf(".") != -1)
            { URLCurrent = URLCurrent.substring(0, URLCurrent.indexOf("."));
            }
            if (debug == 1) alert("path to file 2:" + URLCurrent);
            subnavigation = document.getElementById("subnavigation_" + URLCurrent.toLowerCase());
            if(subnavigation)
            { subnavigation.style.backgroundImage = "url('/images/black_arrow2.gif')";
              subnavigation.style.backgroundPosition = "center left";
              subnavigation.style.backgroundRepeat = "no-repeat";
              subnavigation.style.paddingLeft = "0px";
              //subnavigation.style.backgroundColor = "#444444";
              //subnavigation.style.border = "1px solid #c0c0c0";
            }
        } //if(pathToFile[2] != null && pathToFile[2] != "")
        if (pathToFile[3] != null && pathToFile[3] != "" && pathToFile[1] != "Services") {
            if (debug == 1) alert("path to file 3");
                URLCurrent = pathToFile[3];
            if (debug == 1) alert("path3: " + URLCurrent);
              if (URLCurrent.indexOf(".") != -1) {
                  URLCurrent = URLCurrent.substring(0, URLCurrent.indexOf("."));
              }
              switch (URLCurrent.toLowerCase()) {
                  case "subscriptionfees":
                      if (debug == 1) alert("hit subscription fees case");
                      smoothShow("subscriptionfeesList"); // Call to open the left side menu
                      if (pathToFile[4] == "") {
                          localpage = document.getElementById("page_subscriptionfees");
                          if (localpage) {
                              localpage.childNodes[0].style.color = "#00CC00";
                              localpage.childNodes[0].focus();
                          }
                      }
                      break;
                case "profiles":
                case "testimonials":
                case "bid-results":
                    subnavigation = document.getElementById("subnavigation_" + URLCurrent.toLowerCase());
                    if (subnavigation) {
                        subnavigation.style.backgroundImage = "url('/images/black_arrow2.gif')";
                        subnavigation.style.backgroundPosition = "center left";
                        subnavigation.style.backgroundRepeat = "no-repeat";
                        subnavigation.style.paddingLeft = "5px";
                        subnavigation.style.backgroundColor = "#444444";
                        subnavigation.style.border = "1px solid #c0c0c0";
                    }
                    break;
                default:
                          localnavigation = document.getElementById("local_" + URLCurrent.toLowerCase());
                          if (debug == 1) alert("localnavigation : " + "local_" + URLCurrent.toLowerCase());
                          if (localnavigation) {
                              localnavigation.style.color = "#00CC00";
                              localnavigation.style.fontWeight = "bold";
                              smoothShow(URLCurrent.toLowerCase() + "List");
                              localitem = pathToFile[4].replace(".aspx", "");
                          }
            } // switch (URLCurrent)
          }  //if (pathToFile[3] != null && pathToFile[3] != "" && pathToFile[1] != "Services")
          else {
              URLCurrent = pathToFile[2];
              if (debug == 1) alert("path to file 2 end");
              switch (URLCurrent){
                  case "HeavyJob":
                  case "TheDispatcher":
                  case "GPS":
                  case "BidHistory":
                  case "bidhistory":
                      localNavUniqueDisplay('featuresList');
                      break;
                  case "HeavyBid":
                  case "Equipment360":
                      localNavUniqueDisplay('benefitsList');
                      break;
                  case "Support":
                      localNavUniqueDisplay('supportList');
                      break;
                  case "Training":
                      localNavUniqueDisplay('trainingList');
                      break;
                  case "Helpinars":
                      localNavUniqueDisplay('helpinarsList');
                      break;
                  case "UsersGroupMeetings":
                      localNavUniqueDisplay('ugmList');
                      break;
                  case "CustomProgramming":
                      localNavUniqueDisplay('customizedProgrammingList');
                      break;
                  case "ProductInfoSeminars":
                      localNavUniqueDisplay('productInfoSeminarsList');
                      break;
                  case "Universities":
                      localNavUniqueDisplay('universitiesList');
                      break;
                  default:
                      // Services left menu the length== 4 part is to exclude the main Services page which has no left menu
              } // switch (URLCurrent)
              if (pathToFile[1] == "Services" && (pathToFile[3] == null || pathToFile[3] == "")) { //&& pathToFile.length == 4
                  /// Handle Services side menu
                  debug = 0;
                  if (debug == 1) { alert("Services: " + URLCurrent) }
                  localpage = document.getElementById("page_" + URLCurrent.toLowerCase() );
                  if (debug == 1) { alert(localpage) }
                  if (localpage) {
                      localpage.childNodes[0].style.color = "#00CC00";
                      localpage.childNodes[0].focus();
                  }
              } //if (pathToFile[1] == "Services" )
          }  // if (pathToFile[3] != null && pathToFile[3] != "" && pathToFile[1] != "Services")  else

          if (pathToFile[1] == "Services") {
              if (debug == 1) alert("path to file 3  Services");
            if (pathToFile[3] != null && pathToFile[3] != "") {
                  URLCurrent = pathToFile[3];
                if(URLCurrent.indexOf(".") != -1)
                { URLCurrent = URLCurrent.substring(0, URLCurrent.indexOf("."));
                }
                localpage = document.getElementById("page_" + URLCurrent.toLowerCase());
                if(localpage)
                { localpage.childNodes[0].style.color = "#00CC00";
                  localpage.childNodes[0].focus();
                }
            }  //if (pathToFile[3] != null && pathToFile[3] != "")
          }  // if (pathToFile[1] == "Services")
          else {
              if (pathToFile[4] != null && pathToFile[4] != "") {
                  if (debug == 1) alert("path to file 4 Not Services");
                  URLCurrent = pathToFile[4];
                if(URLCurrent.indexOf(".") != -1)
                { URLCurrent = URLCurrent.substring(0, URLCurrent.indexOf("."));
                }
                localpage = document.getElementById("page_" + URLCurrent.toLowerCase());
                if(localpage)
                { localpage.childNodes[0].style.color = "#00CC00";
                  localpage.childNodes[0].focus();
                }
              }    //if (pathToFile[4] != null && pathToFile[4] != "")
          }  // if (pathToFile[1] == "Services") {
  } //if(results[3])
  } //if(URLParser.test(URLCurrent))
}  //function

function activateSmoothCollapse()
{ var theParent = document.getElementById("LocalNavigationList");
  if(theParent)
  { var theHidden = theParent.getElementsByTagName("ul");
    var tempId;
    var fullSize;
    var minSize = 200;
    var maxSize = 400;
    for(var i = 0; i < theHidden.length; i++)
    { if(theHidden[i].id)
      { tempId = theHidden[i].id;
        theHidden[i].style.overflowY = "scroll";
        if(browser == "IE")
        { fullSize = myParseInt(theHidden[i].style.height);
        }
        else
        { fullSize = myParseInt(theHidden[i].style.height);
        }
        if(fullSize < minSize)
        { fullSize = minSize;
        }
        if(fullSize > maxSize)
        { fullSize = maxSize;
        }
        collapsedElements[tempId] = fullSize;
        elementTimers[tempId] = 0;
      }
    }
  }
}

function smoothHide(elementId)
{ var theElement = document.getElementById(elementId);
  if(theElement.style.display == "none")
  { return;  //do nothing
  }
  else
  { adjustHeight(1, elementId, 40);
  }
}

function smoothShow(elementId) {
var theElement = document.getElementById(elementId);
  if(theElement.style.display == "block")
  { return; //do nothing
  }
  else
  { if(browser == "IE")
    { theElement.height = "1";
    }
    else
    { theElement.style.height = "1px";
    }
    theElement.style.display = "block";
    adjustHeight(collapsedElements[elementId], elementId, 40);
  }
}

function toggleDisplay(elementId)
{ var theElement = document.getElementById(elementId);
  if(theElement.style.display == "none" || theElement.style.display == "")
  { //theElement.style.display = "block";
    smoothShow(theElement.id);
  }
  else
  { //theElement.style.display = "none";
    smoothShow(theElement.id);
  }
}

//this function hides all sibling elements and then shows the selected element
function uniqueSiblingDisplay(elementId)
{ var theElement = document.getElementById(elementId);
  if(theElement)
  { if(theElement.parentNode)
    { var theParent = theElement.parentNode;
      for(var i = 0; i < theParent.childNodes.length; i++)
      { smoothHide(theParent.childNodes[i].id);
        //theParent.childNodes[i].style.display = "none";
      }
    }
    smoothShow(theElement.id);
    //theElement.style.display = "block";
  }
}

function localNavUniqueDisplay(elementId) {
    var theElement = document.getElementById(elementId);

  if(theElement)
  { if(theElement.parentNode.parentNode)
    { var theGrandaddy = theElement.parentNode.parentNode;
      var theHiddenLists = theGrandaddy.getElementsByTagName("ul");
      for(var i = 0; i < theHiddenLists.length; i++)
      { if(theHiddenLists[i].id)
        { smoothHide(theHiddenLists[i].id);
        }
        else
        { theHiddenLists[i].style.display = "none";
        }
      }
  }

    if(theElement.id)
    { smoothShow(theElement.id);
    }
    else
    { theElement.style.display = "block";
    }
  }
}

function adjustHeight(desiredHeight, elementId, millisec)
{ clearTimeout(elementTimers[elementId]);
  var somethingChanged = false;
  var units = "";
  if(browser == "FF")
  { units = "px";
  }

  var theElement = document.getElementById(elementId);
  var theElementH;
  theElementH = myParseInt(theElement.style.height);

  var theElementdH = Math.abs(desiredHeight - theElementH) / 8 + 1;
  //alert(theElementdH);
  //resize theElement's height
  if(theElementH > desiredHeight)
  { theElement.style.height = (theElementH - theElementdH) + units;
    somethingChanged = true;
  }
  else if(theElementH < desiredHeight)
  { theElement.style.height = (theElementH + theElementdH) + units;
    somethingChanged = true;
  }
  else if(theElementH == desiredHeight)
  { if(desiredHeight == 1)
    { theElement.style.display = "none";
    }
    else
    { theElement.style.display = "none";
      theElement.style.display = "block";
    }
  }

  if(somethingChanged)
  { elementTimers[elementId] = setTimeout("adjustHeight(" + desiredHeight + ", '" + elementId + "', " + millisec + ");", millisec);
  }
  return somethingChanged;
}

function myParseInt(theString)
{ theString += "";
  if(theString.length == 0)
  { return 0;
  }
  else if(!isNaN(parseInt(theString)))
  { return parseInt(theString);
  }
  else if(!isNaN(parseInt(theString.replace(/[^0-9\-\+\.]/g, ""))))
  { return parseInt(theString.replace(/[^0-9\-\+\.]/g, ""));
  }
  else
  { return 0;
  }
}

function activateExternalLinks()
{ var externalLinks;
  externalLinks = document.getElementsByTagName("a");
  for(var I = 0; I < externalLinks.length; I++)
  { if(externalLinks[I].rel == "external")
    { externalLinks[I].onclick = "window.open(" + externalLinks[I].href + "); return false;";
      externalLinks[I].target = "_blank";
    }
  }
}

function thumbnailWindow(imgsrc){
    var x;
    x = window.open(imgsrc.src, "thumbnailPreview","toolbar=0,location=0,width=1024");
    x.focus();
}


