﻿jQuery(document).ready(function() { 
  // Fix z-index issue in IE6
  if (jQuery.browser.msie && jQuery.browser.version < 7) {
  jQuery("div.TopNavRow1 > ul > li > ul").bgiframe();
  jQuery("div.TopNavRow2 > ul > li > ul").bgiframe();
  }
  
  // Initialize keyboard support	
	var drow1 = new DickerFisch("div.TopNavRow1",{NavTimeout: new Array(30,250),Fit2ViewportClass:'leftover',LimitLinks:0,Effect:0});
	var drow2 = new DickerFisch("div.TopNavRow2",{NavTimeout: new Array(30,250),Fit2ViewportClass:'leftover',LimitLinks:0,Effect:0});
  // Even Out the Navigation Categories (requires jQuery, jQuery Dimension plugin  
  
  jQuery("div.TopNavRow1 > ul > li").hoverIntent(
    function() {
		// Clear any keyboard popups
		jQuery("#MenuCell * .over").removeClass("over");
		// Check to see if menu should display to right or to left
		checkPulldownDirection(this);
    },
    function() {
      jQuery("div.TopNavRow1 > ul > li > ul").css({ left: ""});
    }
  );
  
  jQuery("div.TopNavRow2 > ul > li").hoverIntent(
    function() {
		// Clear any keyboard popups
		jQuery("#MenuCell * .over").removeClass("over");    
		// Check to see if menu should display to right or to left
		checkPulldownDirection(this);
    },
    function() {
      jQuery("div.TopNavRow2 > ul > li > ul").css({left: ""});
    }
  );  
});

function distributeListItems(selector, tagname)
{
  // Distribute "Extra Padding" by getting the width of multiple items in a container
	var numCategories=0,totalWidth=0,currentLeftover=0,itemPaddingLeft=0,itemPaddingRight=0,currentCounterLeft=0,currentCounterRight=0,itemCounterLeft=0,itemCounterRight=0;

  // Add up the overall width, increment number of categories
  jQuery(selector + " > " + tagname).each(function() {
    totalWidth += jQuery(this).outerWidth();
    numCategories++;
  });
  
  var containerWidth = jQuery(selector).width();  
  // Make sure last li has no margin-right and subtract the last li's margin-right from the total width of the tabs
  var marginRight = jQuery(selector + " > " + tagname + ":last-child").css('marginRight');
  marginRight = (marginRight == 'auto') ? 0 : parseInt(marginRight);
  totalWidth -= marginRight;
  // Set right margin to 0
  jQuery(selector + " > " + tagname + ":last-child").css({marginRight: 0});
  // Determine the number of "leftover" pixels
  var availWidth = containerWidth - totalWidth;  
  // Get the remaining width averaged by number of items
  // If positive, use floor() otherwise use ceil()
  var remainderPerItem = 0;  
  if(availWidth > 0) {
          remainderPerItem = Math.floor(availWidth / numCategories);
  }
  else {
          remainderPerItem = Math.ceil(availWidth / numCategories);
  } 	
  currentLeftover = remainderPerItem + availWidth % numCategories;
  // Different L/R padding if uneven remaining pixels...
  if(remainderPerItem > 0) {
          itemCounterLeft = itemCounterRight = Math.floor(remainderPerItem / 2);
  }
  else {
          itemCounterLeft = itemCounterRight = Math.ceil(remainderPerItem / 2);
  }
  itemCounterRight += remainderPerItem % 2;
  if(currentLeftover > 0) {
          currentCounterLeft = currentCounterRight = Math.floor(currentLeftover / 2);
  }
  else {
          currentCounterLeft = currentCounterRight = Math.ceil(currentLeftover / 2);
  }
  currentCounterRight += currentLeftover % 2;
  var totalRemainder = availWidth - ((itemCounterLeft*numCategories) + (itemCounterRight*numCategories));
  
	//console.log("currentLeftover:" + currentLeftover);
	//console.log("Overall Container:" + containerWidth);
	//console.log("Used Width:" + totalWidth);	
	//console.log("Available Width:" + availWidth);
	//console.log("RemainderPerItem:" + remainderPerItem);  
	//console.log("itemCounterLeft:" + itemCounterLeft); 
	//console.log("itemCounterRight:" + itemCounterRight);
	//console.log("totalRemainder:" + totalRemainder);
  
  // Add leftover width to padding of each category link
  jQuery(selector + " > " + tagname).each(function(i) {
    var padLeft = 0;
    var padRight = 0;
    var li = jQuery(this), 
            padLeft = itemCounterLeft + itemPaddingLeft, 
            padRight = itemCounterRight + itemPaddingRight;
    
    if(totalRemainder > 0)
    {
		padLeft = padLeft + 1;
		totalRemainder--;
	}           
	
    // Last category takes uses leftover pixels / 2 
    //if(i === (numCategories-1)) {
    //        padLeft = currentCounterLeft + itemPaddingLeft;
    //        padRight = currentCounterRight + itemPaddingRight;
    //}
	
    // Padding must be positive
    if(padLeft < 0) {
            padLeft = 0;
    }
    if(padRight < 0) {
            padRight = 0;
    }
	// assign padding results to container
    jQuery('a:first', li).css({paddingLeft: padLeft +'px', paddingRight: padRight +'px'});
  });
}