/*
 *  Contents
 *    close(what)
 *    elongateWhiteBox()
 *    importGetVars()
 *    learnMore()
 *    loadSWF(container, swf, width, height)
 *    shortenPage()
 *    showContent(divID)
 *    showFAQ(faqId)
 *    showObj(obj) / hideObj(obj) deprecated
 *    showSearch(el) / hideSearch(el)
 *    tNavOver(el)
 *    tNavOut(el)
 *    toggleCareer(what)
 *    toggleFlagBox(id)
 *    trim(s)
 *    whatIs(what)
 */

// For search drop-down DEPRECATED
function showObj(obj)
{
  document.getElementById(obj).nowShowing = 1;
  document.getElementById(obj).style.visibility = 'visible';
}

// For search drop-down
function hideObj(obj)
{
  document.getElementById(obj).nowShowing = 0;
  setTimeout("if (document.getElementById('" + obj + "').nowShowing == 0) { document.getElementById('" + obj + "').style.visibility = 'hidden'; }", 500);
}

// To enlongate the white box if there's minimal content
function elongateWhiteBox()
{
  if(document.getElementById('rightCol')) {
    document.getElementById('whiteBoxContent').style.height = 'auto';
    var rc = document.getElementById('rightCol').clientHeight;
    if(document.getElementById('whiteBoxContent').clientHeight < (rc - 63)) {
      document.getElementById('whiteBoxContent').style.height = (rc - 63) + 'px';
    }
  }
}

// To show and hide sections of the FAQs
function showFAQ(faqId)
{
  var faqSections = document.getElementsByTagName("div");
  for(var i = 0; i < faqSections.length; i++) {
    if (faqSections[i].className == "faqSection") {
      faqSections[i].style.display = "none";
    }
  }
  document.getElementById(faqId).style.display = "block";
  elongateWhiteBox();
}

// For product selection
function showContent(divID)
{
  var divs = document.getElementsByTagName('div');
  var products = new Array();
  for (var i = 0; i < divs.length; i++) {
    if(divs[i].className == 'product') {
      products[products.length] = divs[i];
    }
  }
  for (var i = 0; i < products.length; i++) {
    products[i].style.display = (i == divID)? 'block' : 'none';
  }
  elongateWhiteBox();
}

// To keep the background from extending the page downward
function shortenPage()
{
  if(document.getElementById('whiteBoxContent')) {
    var whiteBoxHeight = (document.getElementById('whiteBoxContent').clientHeight > 0) ?
      document.getElementById('whiteBoxContent').clientHeight :
      document.getElementById('whiteBoxContent').offsetHeight;
    if (whiteBoxHeight < 1000) {
      var wb = whiteBoxHeight + 60;
      var rc = document.getElementById('rightCol').clientHeight + 60;
      var elementsBottom = (wb > rc)? wb : rc;
      
      if (self.innerHeight) {
        var viewportHeight = self.innerHeight;
      }
      else if (document.documentElement && document.documentElement.clientHeight) {
        var viewportHeight = document.documentElement.clientHeight;
      }
      else if (document.body) {
        var viewportHeight = document.body.clientHeight;
      }

      if (viewportHeight) {
        if(viewportHeight < elementsBottom) {
          document.getElementById('main').style.height = elementsBottom + 'px';
          document.getElementById('main').style.overflow = 'hidden';
        }
      }
      else {
        document.getElementById('background').style.height = '1096px';
      }
    }
  }
}

// For "Want to learn more" links
function learnMore()
{
  document.getElementById('learnMore').style.display = 'block';
}

// pop-ups
function whatIs(what)
{
  if (what == 'aPledge')
    var id = 'pledgesWhatIsThis';
  if (what == 'connections')
    var id = 'connectionsWhatIsThis';

  document.getElementById(id).style.visibility = 'visible';
}

function close(what)
{
  document.getElementById(what).style.visibility = 'hidden';
}

// To load Right Bar SWFs
function loadSWF(container, swf, width, height)
{
  if (document.all) {
    swfObj = document.createElement('object');
    container.appendChild(swfObj);
    swfObj.classid= 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'; 
    swfObj.movie = swf;
    swfObj.width = '100%';
    swfObj.height = '100%';
    swfObj.allowScriptAccess = 'sameDomain';
    swfObj.wmode = 'transparent';
    swfObj.swLiveConnect='true';
  }
  else {
    container.innerHTML = '<embed src="' + swf +
      '" wmode="transparent" width="' + width +
      '" height="' + height + '" />';
  }
}

function toggleCareer(what)
{
  if (what.style.height == "auto") {
    what.style.height = "28px";
    what.style.backgroundImage = "url(/media/images/meetus_careersToggle0.gif)";
  }
  else {
    var careers = document.getElementsByTagName("div");
    for(var i = 0; i < careers.length;i++) {
      if (careers[i].className == "career") {
        careers[i].style.height = "28px";
        careers[i].style.backgroundImage = "url(/media/images/meetus_careersToggle0.gif)";
      }
    }
    what.style.height = "auto";
    what.style.backgroundImage = "url(/media/images/meetus_careersToggle1.gif)";

    var a = what.getElementsByTagName("a");
    var loc = window.location.href;
  }
}

function toggleCareer(what)
{
  if(what.style.height == 'auto') {
    what.style.height = '28px';
    what.style.backgroundImage = 'url(/media/images/meetus_careersToggle0.png)';
    // Fix for bug KVII-94. Bring the user to the top of the page when the user
    // closes the job.
    window.scrollTo(0,0);
  }
  else {
    var careers = document.getElementsByTagName('div');
    for (var i = 0; i < careers.length; i++) {
      if (careers[i].className == 'career') {
        careers[i].style.height = '28px';
        careers[i].style.backgroundImage = 'url(/media/images/meetus_careersToggle0.png)';
      }
    }
    what.style.height = 'auto';
    what.style.backgroundImage = 'url(/media/images/meetus_careersToggle1.png)';

    elongateWhiteBox();

    var a = what.getElementsByTagName('a');
    var loc = window.location.href;

    if (document.all)  {
      window.scrollTo(0, a[0].offsetParent.offsetTop+65);
    } else {
      window.location.href = loc.substring(0, loc.indexOf('#')) + '#' + a[0].name;
    }
  }
}

var getVar = new Object(); // object for get-request variables
/**
 *  Creates object 'getVar' with name/value pairs from get request,
 *  e.g. '?key=value' results in getVar.key = "value". Keys are
 *  converted to lower case.
 *
 *  @return boolean     if get variables present then true else false
 */
function importGetVars()
{
  var queryStr;
  var getVars;  // an array of get variables
  var tmpArray;

  // is there a query string?
  if (document.location.search != "") {
    queryStr = unescape(document.location.search.substr(1));
    getVars = queryStr.split("&");  // split on the '&' char

    // separate the name/value pairs
    for (var i = 0; i < getVars.length; i++) {
      tmpArray = getVars[i].split("=");
      getVar[tmpArray[0].toLowerCase()] = tmpArray[1];
    }
    return true;
  }
  return false;
}

/**
*  trim whitespace from beginning and end of string
*  @param  String s
*  @return String
*/
function trim(s)
{
  return s.replace(/^\s+/, "").replace(/\s+$/, "");
}

var currentFlag = null;
var currentWhat = null;

/**
 *  Toggle the flag-this box
 *
 *  @param String id
 *  @return void
 */
function toggleFlagBox(id)
{  
  el = document.getElementById(id);
  if (el.style.visibility == "visible") {
    el.style.visibility = "hidden";
    currentFlag = null;
  }
  else {
    if (currentFlag) {
      document.getElementById(currentFlag).style.visibility = "hidden";
    }
    if (currentWhat) {
      document.getElementById(currentWhat).style.visibility = "hidden";
      currentWhat = null;
    }
    el.style.visibility = "visible";
    currentFlag = id;
  }
}

function toggleWhatBox(id)
{
  el = document.getElementById(id);
  if (el.style.visibility == "visible") {
    el.style.visibility = "hidden";
    currentWhat = null;
  }
  else {
    el.style.visibility = "visible";
    currentWhat = id;
  }
}

/**
 *  explicitly move the top nav images on mouse over
 *
 *  @param  object  el    image element to move
 *  @return void
 */
function tNavOver(el)
{
  el.style.top = "-31px";
}

function tNavOut(el)
{
  el.style.top = "0";
}

/**
 *  show and hide the search dropdown area
 *
 *  @param  string  id        element id
 *  @global boolean isSearch  true if search input has focus
 *  @return void
 */
function showSearch(id)
{
  document.getElementById(id).nowShowing = 1;
  document.getElementById(id).style.visibility = 'visible';
}

var isSearch = false;

function hideSearch(id)
{
  if (!isSearch) {
    document.getElementById(id).nowShowing = 0;
    setTimeout("if (document.getElementById('" + id +
      "').nowShowing == 0) { document.getElementById('" + id +
      "').style.visibility = 'hidden'; }", 500);
  }
}
