/*
* Controls the highlighting on the pages that have tabbed navigation. 
*/
jQuery(function(){
var foundCount = 0;
var clean = function(str){
  // strip punctuation
  var punc = ['.',':','\'',',',';','(',')','[',']','-','–'];
  return jQuery.trim(TM.util.array.strip(str.split(''),punc).join('')).toLowerCase();
};
//----- start each
  jQuery(".tab ul li").each(function(idx){
    // words in tab
    var tabStrings = clean(jQuery(this).text()).split(' ');
    // words in pageTitle
    var pageTitle = (jQuery('.addToCartServices').length==0) ? clean(jQuery('#pageTitle').text()).split(' ') : clean(jQuery('#pageContent h2').eq(0).text()).split(' ');
    // ignore these words when counting matches
    var ignore = ['a','by','for','the','of','with','telus','le','de','à','la','un','une','du'];
    tabStrings = TM.util.array.strip(tabStrings,ignore);
    pageTitle = TM.util.array.strip(pageTitle,ignore);
    // Check to see if any of the words in the tab appear in the title
    var found = (function(){
    // first try "overview" match or exact match
    if(tabStrings.join(' ')==pageTitle.join(' ') || (jQuery(".tab.isOverview").length>0 && idx==0)){
      foundCount = -1;
      return true;
    };
    // then try matching words
    var cnt = 0;
    for (var i = tabStrings.length - 1; i >= 0; i--){
      if(jQuery.inArray(tabStrings[i],pageTitle)>=0){
        cnt += 1;
      };
    };
    // turn on the one with the most matches
    if(cnt>=foundCount && foundCount>-1){
      foundCount = cnt;
      return true;
    } else {
      return false;
    };
    })();
    // add/remove class names
    if(found){
      jQuery(this).parents('.tab').find('.active').removeClass('active');
      jQuery(this).addClass('active');
    };
//----- end each
  });
});