  
/* -----------------------------------
   rotating home page items
   ----------------------------------- */
var image_count;
var headline_count;
var word_count;
var current_image=0;
var current_headline=0;
var current_word=0;
var ieMenuWidthAdj=4;
var ffMenuWidthAdj=17;
var menuWidth;

$(document).ready(function(){

  image_count = $("#banner div").hide().size();
  headline_count = $("div.headline").hide().size();
 
  $("#banner div:eq("+current_image+")").show();
  $("div.headline:eq("+current_headline+")").show();
  setInterval(feature_rotate,5000); //time in milliseconds
});

function feature_rotate() {
  old_image = current_image%image_count;
  new_image = ++current_image%image_count;
  $("#banner div:eq(" + new_image + ")").fadeIn("slow", function() {
    $("#banner div:eq(" + old_image + ")").fadeOut("slow");
  });

   old_headline = current_headline%headline_count;
   new_headline = ++current_headline%headline_count;
  $("div.headline:eq(" + new_headline + ")").fadeIn("slow", function() {
    $("div.headline:eq(" + old_headline + ")").fadeOut("slow");
  });
}

/* -----------------------------------
   drop-down menu
   ----------------------------------- */

function getElementLeft() {

	var el = $('#primary-nav > ul > li:last')[0];
	var parentEl;
	var elLeft = el.offsetLeft;
	while(el.offsetParent!=null) {
		parentEl = el.offsetParent;
		elLeft += parentEl.offsetLeft ;
		el = parentEl;
	}
	return elLeft;
}

$(document).ready(function(){

	// How do we automatically find the width of this MENU?
	var $mi = $('#primary-nav > ul > li:first > ul');
	if ($mi) {
		$mi.css({'display':'block', marginLeft: '-9000em'});
		menuWidth = $mi.width();
		$mi.css({marginLeft: '0','display':'none'});
	}

	var ro = $(window).width() - getElementLeft();
	var leftMargin = (menuWidth + parseInt($mi.css('borderLeftWidth'), 10) + parseInt($mi.css('borderRightWidth'), 10) - ro) * -1;

  $('#primary-nav > ul > li').each(function(i) {
    var timeOut;
    $('li:last',this).css({height: '15px'});
		if ($.browser.msie) { $(this).addClass('ie'); }
    $(this).hover(function(){
      $(this).css('zIndex', 13);
     clearTimeout(timeOut);
     $('#primary-nav > ul > li > ul').css('display','none');
     $('> ul', this).css('display','block');
     if ($.browser.msie) {
       var $this_ul = $('> ul', this);
       if ($this_ul.length) {
         $this_ul.prepend("<iframe></iframe>");
         $('iframe', this).css({height: $this_ul.get(0).offsetHeight + 'px', width: menuWidth + 'px'});
       }
     };
    },function(){  // on mouseOut
     var $thisli = $(this);
          $thisli.css('zIndex','');
          timeOut = setTimeout(function() {
            $('> ul', $thisli).css('display','none');
            if ($.browser.msie) {
              $thisli.find('iframe').remove();  
            };
          },300);
   });
 }).filter(':last').addClass('last-nav').find('ul').css({'margin-left': leftMargin + 'px'});

	//sub menus (3rd level)
  $('#primary-nav > ul > li:lt(3) ul > li').each(function(i) {
    var timeOutSub;

    if ($('> ul', this).size() > 0) {
      $('> a', this).addClass('has-subnav');
      $('> a.active', this).removeClass().addClass('active-has-subnav');
      $(this).hover(function(){ //this part is for mouseOver
      
        var $this_ul = $('> ul', this);
        var wHeight = $(window).height();
        var thisTop = $(this).offset().top;
        var childHeight = $this_ul.outerHeight();
        var diff = wHeight - thisTop - childHeight;
        if (thisTop + childHeight > wHeight) {
          $this_ul.css({top: diff < -thisTop ? -thisTop - 5 : diff});
        }
        clearTimeout(timeOutSub);
        $(this).parent().find('li:not(this) ul').css('display','none');
        
        $this_ul.css({display:'block',visibility:'visible'});
        if ($.browser.msie) {
          $this_ul.prepend("<iframe></iframe>");
          $('iframe', this).css({height: $this_ul.get(0).offsetHeight + 'px'});
        };
      },function(){ // on mouseOut
        var thisli = this;
        timeOutSub = setTimeout(function() {
          $('> ul', thisli).css({display:'none',visibility:'hidden'});
          if ($.browser.msie) {
            $(thisli).find('iframe').remove();  
          };
        },300);
      });
    }      
  });
}); // end document ready

/* -----------------------------------
   calculate feature word text color
   ----------------------------------- */
   
$(document).ready(function() {
  $('div.headline-container').each(function(i) {
    var feature_red = 0;
    var feature_green = 0;
    var feature_blue = 0;
    var headline_background = $('div.headline', this).css('backgroundColor');
    if (headline_background.match(/^#/)) {
      var headline_rgb = $('div.headline', this).css('backgroundColor').match(/^#(..)(..)(..)/);
      var feature_red = Math.floor(50 + (parseInt(headline_rgb[1],16)/1.25));
      var feature_green = Math.floor(50 + (parseInt(headline_rgb[2],16)/1.25));
      var feature_blue = Math.floor(50 + (parseInt(headline_rgb[3],16)/1.25)); 
    } else {
      var headline_rgb = $('div.headline', this).css('backgroundColor').match(/^rgb\(([\d]*),\s*([\d]*),\s*([\d]*)/);
      var feature_red = Math.floor(50 + (parseInt(headline_rgb[1])/1.25));
      var feature_green = Math.floor(50 + (parseInt(headline_rgb[2])/1.25));
      var feature_blue = Math.floor(50 + (parseInt(headline_rgb[3])/1.25));
    }
    $('div.feature-word', this).css('color','rgb(' + feature_red + ',' + feature_green + ',' + feature_blue + ')');   

  });
});

/* -----------------------------------
   show and hide product info
   ----------------------------------- */

var show_hide;

$(document).ready(function(){
  $(document).bind('unload', function(event) {
    $('li').unbind('mouseout').unbind('mouseover');
    show_hide = null;
  });
  
  $('div.results').hide();

  show_hide = function() {
    $('div.product-info a.inactive-link').click(show_hide);
    $('a.show-hide').removeClass('inactive-link');
    $(this).unbind("click");
    $(this).click(function() {
      return false;
    });
    $(this).addClass('inactive-link');
    $('div.results:visible').slideUp('medium');
    $(this).parent().find('div.results:hidden').slideDown('medium');
    return false;
  };

  $('a.show-hide').click(show_hide);

});


/* Product Popups */

$(document).ready(function() {
  $('<div id="product-overlay"></div>').appendTo('body').jqm({
    trigger: '.product-grid a.product-overlay-link',
    onShow: function(h) {
      h.w.html('<div class="loading-indicator"></div>');
      h.w.show();
      h.w.css('top',$(document).scrollTop() + 50);
      h.w.load($(h.t).attr('href'), function() {
        $('.product-overlay-tabs > div').click(function() {
          $(this).siblings().removeClass('active').end().addClass('active');
          var tabName = this.id;
          $('.product-overlay-specs').hide();
    			$('.product-overlay-tab-content > .' + tabName).fadeIn('fast');
    			h.w.removeClass().addClass(tabName);
          h.w.jqmAddClose('.jqmClose');
        });
        
        $('.product-overlay-specs').hide();
        $('#tab-overview').click();
      });
    },
    onHide: function(h) { 
      h.o.remove();
      h.w.fadeOut(200);
    }
  });
});
