jQuery.noConflict();

function isIE() {
    return navigator.userAgent.match(/MSIE \d\.\d+/);
}

/* alphabetize the dropdown lists */
jQuery.fn.sort = function() {
  return this.pushStack([].sort.apply(this, arguments), []);
};

function sortAscending(a, b) {
  return jQuery(a).text() > jQuery(b).text() ? 1 : -1;
};

jQuery(document).ready(function() {
  jQuery('.zoomr').jqzoom({title:false, zoomWidth: 350, zoomHeight: 348, xOffset :2, yOffset: -2, fadeoutSpeed: 'slow', hideEffect: 'fadeout'});

  // sort mega menus
  jQuery('.mega-dd').each(function(){
    sorted_items = jQuery(this).find('li').sort(sortAscending);
    jQuery(sorted_items).appendTo(this); 
  }); 

  // split mega menus into columns
	jQuery('.mega-dd').each(function(i) {
		width = jQuery(this).attr('class');
		if (width.indexOf('triple-wide') > 0) {
			var items = jQuery(this).find("li");
			var colsize = Math.round(items.size() / 3);
			items.slice(0, colsize).wrapAll("<ul class='first'></ul>").end().slice(colsize, colsize * 2).wrapAll("<ul class='second'></ul>").end().slice(colsize * 2, items.size()).wrapAll("<ul class='third'></ul>");
		} else if (width.indexOf('double-wide') > 0) {
			var items = jQuery(this).find("li");
			var colsize = Math.round(items.size() / 2);
			items.slice(0, colsize).wrapAll("<ul class='first'></ul>").end().slice(colsize, items.size()).wrapAll("<ul class='second'></ul>");
		} else {
			jQuery(this).find("li").wrapAll("<ul class='first'></ul>")
		}
	})

  /* primary nav hover for ie6 */	
  jQuery('.primary-nav li').hover(function() {
    jQuery(this).addClass('hover');
  }, function() {
    jQuery(this).removeClass('hover');
  });

  /*
  ** IE z-index bug workaround.
  **
  ** For each div with class menu
  */
  if(isIE()) {
  jQuery(".primary-nav li").parents().each(function() {
    var p = jQuery(this);
    var pos = p.css("position");

    // If it's positioned,
    if (pos == "relative" || pos == "absolute" || pos == "fixed") {
      /* add "on-top" class name when the mouse is hovering over it. */
      p.hover(function() {
        jQuery(this).addClass("on-top");
      }, function() {
        jQuery(this).removeClass("on-top");
      });
    }
  });
  }

});
