var origContent = '';
var origBCrumb = '';
var contentTimeout;

function isInteger(val)
{
    //alert(val.value);
    if(val==null)
    {
        //alert(val);
        return false;
    }
    if (val.length==0)
    {
        //alert(val);
        return false;
    }
    for (var i = 0; i < val.length; i++)
    {
        var ch = val.charAt(i)
        if (i == 0 && ch == "-")
        {
            continue
        }
        if (ch < "0" || ch > "9")
        {
            return false
        }
    }
    return true
}

function setNav()
{
  if(level1 != "")
  {
    if(!jQuery("#"+level1+" > .sub-menu").length)
    {
      jQuery("#"+level1+" > p").addClass("active");
      var leftBarHeight = parseInt(jQuery("#"+level1).height());
      jQuery("#"+level1+" > .left-bar-single div").css("height",leftBarHeight+"px");
      jQuery("#"+level1+" > .left-bar-single").show();
    }
    else
    {
      jQuery("#"+level1+" > p").addClass("parent-active");
      jQuery("#"+level1+" > .sub-menu").show();
    }

    var leftBarHeight = parseInt(jQuery("#"+level1).height());
    jQuery("#"+level1+" > .left-bar").css("height",leftBarHeight+"px");
    jQuery("#"+level1+" > .left-bar").show();

    if(level2 != "")
    {
      if(!jQuery("ul.sub-sub-menu[id=sub-"+level2+"]").length)
      {
        jQuery("#"+level1+" > .sub-menu > li#"+level2+" > p").addClass("active");
      }
      else
      {
        jQuery("#"+level1+" > .sub-menu > li#"+level2+" > p").addClass("parent-active");
        jQuery("#"+level1+" > #sub-"+level2).show();

        ////////////////////////////////////////////////////////////////////////////////////
        // Holy Cow - OK, so we are figuring out the pixel position of the sub-sub menu
        // plus it's height as well as the pixel position of the sub-menu plus it's height
        // in order to accurately get the padding-bottom correct for sub-sub menus that
        // arent tall enought to reach the sub-menu item. Crazy, I know......
        //
        var subSubMenuPosition = jQuery("#"+level1+" > #sub-"+level2).offset();
        var subMenuPosition = jQuery("#"+level1+" > .sub-menu > li#"+level2).offset();

        if((parseInt(subSubMenuPosition.top) + parseInt(jQuery("#"+level1+" > #sub-"+level2).height())) < parseInt(subMenuPosition.top))
        {
          var paddingBottom = parseInt(subMenuPosition.top) - parseInt(subSubMenuPosition.top) - parseInt(jQuery("#"+level1+" > #sub-"+level2).height()) + parseInt(jQuery("#"+level1+" > .sub-menu > li#"+level2).height()/2);
          jQuery("#"+level1+" > #sub-"+level2).css("padding-bottom",paddingBottom+'px');
        }
      }

      if(level3 != "")
        jQuery("#"+level1+" > #sub-"+level2+" li#"+level3+" > a").addClass("active");
    }
  }
}

function adminNav(what)
{
  $("#"+what+" > p").addClass("active");
}

function swapContent(what)
{
  var newContent = $('#ajax-' + what.replace(/ /g, '_')).html();
  var newBCrumb = $('#ajax-bc-' + what.replace(/ /g, '_')).html();
  $('#main-inside').html(newContent);
  $('#breadcrumbs').html(newBCrumb);
  $('#allPeopleSearch').hide();
  $('#locationNames').hide();
}

function slideIt(what, allOfThem)
{
  // Determine whether or not we're closing or opening this - just close if it's already open
  var openIt = true;
  if ($('.'+what).css('display') == 'block')
  {
    openIt = false;
  }

  // Close everything (if defined)
  if (allOfThem != undefined)
  {
    $('#'+allOfThem+' div:visible').hide('fast');
    $('#'+allOfThem+' a').removeClass('open');
  }

  // Open it if that`s what we want
  if (openIt)
  {
    $('.'+what).show('normal');
    $('#link_'+what).addClass('open');
  }
}

function showDetails(what, slider)
{
  what = what || 'moreDetails';
  slider = slider || 'slider';
  $('#'+what).toggle('normal');
  $('a#'+slider).toggleClass('open');
}

function validate(theForm)
{
  var why = "";
  why += checkField(theForm.Name.value, "Name");
  why += checkEmail(theForm.Email.value, "Email");

  if (why != "")
  {
    alert(why);
    return false;
  }
  return true;
}

function checkField (val, field)
{
  var error = "";
  if (val == "") {
    error = "You must enter a value in the " + field + " field\n";
  }

  return error;
}

function checkEmail(val, field)
{
    var error = "";
    if ((val == "" || val.length < 3) || (val.indexOf("@") == "-1") || (val.indexOf(".") == "-1"))
    {
        error = "You must enter a valid " + field + " address.\n"
    }

    return error;
}
