/*global js used sitewide: mostly ui related*/
$(document).ready(function()
{
    $('.toggleBlock h3').click(function(){
      $(this).siblings('div').toggle();
       toggleIcons($(this).children('.icon'));
    });
    
    $('#helpNav h5').click(function(){
       showToggleElement('#'+$(this).attr('id'));
    });
    
    //stretch footer if needed.
    var heightDiff = $(document).height() - $('.pageWrapper').height();
    if(heightDiff > 3){ // diff seems to always be 1 when tall enough, but give room for error, set to 3.
      var footerHeight = $('#footer').height();
      var footerContentMargin = $('#footer p').css('margin-top');
      footerContentMargin = parseInt(footerContentMargin.split('px'));
      $('#footer').animate({height: (footerHeight + heightDiff) -1}, 0);
      $('#footer p').animate({marginTop: (footerContentMargin + heightDiff)}, 0);
    }
    
    if($.browser.msie){
      var ver = $.browser.version;
      ver = ver.charAt(0);
      if(ver == 8){
        $('body').addClass('ie8');
      }
    }
});

/* prevent double form submissions where necessary by overlaying loading indicators */
function animateSubmit(btn){
  $('.btnLoader').remove();
  $(btn).after('<div class="btnLoader"><p></p><span></span></div>');
  var btnWidth = $(btn).width();
  
  if($(btn).is('a')){ //styling is slightly different for input elements vs a tags: 
    btnWidth = btnWidth + 30;
    $('.btnLoader').addClass('aTag');
  }
  else{
    btnWidth = btnWidth + 16;
  }
  
  $('.btnLoader, .btnLoader p, .btnLoader span').animate({width: btnWidth}, 0);
}

function setupAnimateSubmit(allBtns){
  $(allBtns).each(function(){
     $(this).click(function(){
         animateSubmit($(this)); 
     });
  });
}

/*used for helpnav*/
function showToggleElement(el) 
{   
    $(el).toggleClass('active').siblings('ul').toggle();
    toggleIcons($(el).children('.icon'));
}

function toggleIcons(target){
  if($(target).html() == "+") {
    $(target).html("-");
  }
  else {
    $(target).html("+");
  }
}

function positionIndicator(){
  var docHeight = $(document).height();
  var scrollHeight = $(window).scrollTop();
  var elHeight = $("#loading div").height();
  $("#loading span").animate({height : docHeight}, 0);
  $("#loading div").animate({marginTop : (scrollHeight + elHeight)}, 0);
}


function highlightActiveHelpLink(helpLinkTarget)
{
     $("ul#helpNav").find(helpLinkTarget).addClass("activeLink");     
}


function getPage(section){
     //active link styling for topnav
     $('#' + section + '_lnk').addClass("active");
     
     if(section == 'support'){
         $('.toggleBlock').find('div').hide();
     }
     
     if($('#breadcrumb').length > 0){
     $("#breadcrumb li:first").addClass("first");
     }
};

 //for highlighting active pagination link based on page index
function highlightPagination()
{
    var str = document.location.toString();          
    var getIndexValue = str.split("p=", 2);
    
    //need to fix this to check and fix for the first time, when page doesn't exist
    var currentValue = getIndexValue[1]

    var highlightPag = $(".pagination").find("a").eq(currentValue -1);
    highlightPag.addClass("activeLink");
}

