function preloadHoverImages() {
  jQuery("img").each(function() {
    var src = jQuery(this).attr("src");
    if(src && src.indexOf("base") != -1) {
      var img = new Image();
      img.src = src.replace(/base/, "hover");
    }
  });
} 

function preloadDetailImages() {
  jQuery("img").each(function() {
    var src = jQuery(this).attr("src");
    if(src && src.indexOf("thumbs") != -1) {
      var img = new Image();
      img.src = src.replace(/thumbs/, "details");
    }
  });
}

var loginMenuRefCount = 0;


//
// On Document Ready...
//
// ...roll over code for images
//
jQuery(document).ready(function() {

   // determine font size cookie
   if(getCookie('fontSizeCookie'))
   {
    var fsc = parseInt(getCookie('fontSizeCookie'));
    var i;
    if(fsc > 0)
    {
      for(i=fsc; i > 0; i--)
      {
        increaseFontSize();
      }
    }
    else if(fsc < 0)
    {
      for(i=Math.abs(fsc); i > 0; i--)
      {
        decreaseFontSize();
      }
    }
   }
   else
   {
    setCookie('fontSizeCookie',0,999);
   }


   // load the hover images
   preloadHoverImages();

   // load the images for the detail page
   preloadDetailImages();

   // assign the rollover code
   jQuery(".ro").hover(
     function() {

       var src = jQuery(this).attr("src");
       if (src) {
         jQuery(this).attr("src", src.replace(/base/, "hover"));
       }
     },
     function() {

       var src = jQuery(this).attr("src");
       if (src) {
         jQuery(this).attr("src", src.replace(/hover/, "base"));
       }
     }
   );

   jQuery(".client-holder img").hover(
     function() {

       var src = jQuery(this).attr("src");
       if (src) {
         jQuery(this).attr("src", src.replace(/base/, "hover"));
       }
     },
     function() {

       var src = jQuery(this).attr("src");
       if (src) {
         jQuery(this).attr("src", src.replace(/hover/, "base"));
       }
     }
   );


  // scroll clients on homepage
  var index = 0;
  var num_images = jQuery(".client-holder li img").length;
  jQuery("a.clients-prev img").css('display','none');

  jQuery('a.clients-prev').click(function() {
      if(index == 0) { return false; }

      jQuery(".client-holder ul").animate({left:"+=229"});
      index--;

      if(index == 0) { 
        jQuery("a.clients-prev img").css('display','none');
      }

      if(num_images - 1 > index) { 
        jQuery("a.clients-next img").css('display','inline');
      }
      return false;
    }
  );
  jQuery('a.clients-next').click(function() {
      if(num_images - 1 == index) { return false; }

      jQuery(".client-holder ul").animate({left:"-=229"});
      index++;

      if(num_images - 1 == index) { 
        jQuery("a.clients-next img").css('display','none');
      }

      if(index > 0) {
        jQuery("a.clients-prev img").css('display','inline');
      }
      return false;
    }
  );


  // Reset/Increase/Decrease font size of article content
  jQuery('#reset-font-size').click( function() {
      resetFontSize();
      setCookie('fontSizeCookie',0,999);
      return false;
    }
  );
  jQuery('#increase-font-size').click( function() {
      
      var fsc = parseInt(getCookie('fontSizeCookie'));
      if(fsc < 2) {
        fsc++;
        increaseFontSize();
        setCookie('fontSizeCookie', fsc, 999);
      }
      return false;
    }
  );
  jQuery('#decrease-font-size').click( function() {
      var fsc = parseInt(getCookie('fontSizeCookie'));
      if(fsc > -1) {
        fsc--;
        decreaseFontSize();
        setCookie('fontSizeCookie', fsc, 999);
      }
      return false;
    }
  );

  // LOGIN display dropdown menu list
  jQuery('#login-block').hover(
    function() {

      loginMenuRefCount++;
      window.setTimeout(showLoginMenu,100);
    },
    function() {

      hideLoginMenu(jQuery(this));
      loginMenuRefCount = 0;
    }
  );

  // NEWS/BLOG toggle
  jQuery('#news-button-ui').click(
    function() {
      var src1 = jQuery("img.news").attr("src");
      var src2 = jQuery("img.blog").attr("src");
      jQuery("img.news").attr("src", src1.replace(/base/, "active"));
      jQuery("img.blog").attr("src", src2.replace(/active/, "base"));
      jQuery("#news-content-block").css("display","block");
      jQuery("#blog-content-block").css("display","none");
      jQuery("img.news-rss").css("display","block");
      jQuery("img.blog-rss").css("display","none");
    }
  );
  jQuery('#blog-button-ui').click(
    function() {
      var src1 = jQuery("img.news").attr("src");
      var src2 = jQuery("img.blog").attr("src");
      jQuery("img.news").attr("src", src1.replace(/active/, "base"));
      jQuery("img.blog").attr("src", src2.replace(/base/, "active"));
      jQuery("#news-content-block").css("display","none");
      jQuery("#blog-content-block").css("display","block");
      jQuery("img.news-rss").css("display","none");
      jQuery("img.blog-rss").css("display","block");
    }
  );


   // Clear default text from text box when clicked 
   jQuery('.search input').focus(function(){
     var defaultText = jQuery(this).val();
     if(defaultText == 'SEARCH'){
       jQuery(this).val('');
     }
   }).mouseover(function() {
     
     hideLoginMenu(jQuery(this));
     loginMenuRefCount = 0;
   });


   // toggle on a click on the parent list element
   jQuery(".menu li.parent").click(
     function() {
       var img = jQuery(this).css("backgroundImage");
       jQuery(this).children("ul").toggle("fast");
       if(img.indexOf("-hide") == -1) {
         jQuery(this).css("backgroundImage", img.replace(/show/, "hide"));
       } else {
         jQuery(this).css("backgroundImage", img.replace(/hide/, "show"));
       }
       return false;
     });

   // but, make sure that the links within the list still work...
   jQuery(".menu li.parent a").click(
     function() {
       var loc = jQuery(this).attr("href");
       if(loc != undefined && loc != '') {
         window.location = loc;
       }
       return false;
     });

   // Apply sprite effect to main navigation
   generateSprites(".nav", "current-", true, 200, "fade");
   generateSprites(".suppliers-list", "current-", true, 200, "fade");
   generateSprites(".drivers-list", "current-", true, 200, "fade");
});

// Javascript to ensure that the Recommend Article button rolls over in IE 6
jQuery(document).ready(function() {
  jQuery("#center-col-block input.button").hover(function() {

    jQuery(this).css("background", "#aaa url(/images/stories/recommend-article-hover.png) no-repeat 0 0");
  }, function() {

    jQuery(this).css("background", "#aaa url(/images/stories/recommend-article.png) no-repeat 0 0");
  });
});

// Javascript for roll over effect for the video testimonial left-hand module
jQuery(document).ready(function() {
  jQuery("#left-col-block .video-testimonial").hover(function() {

    jQuery("img", this).each(function() {

       var src = jQuery(this).attr("src");
       if (src) {
         jQuery(this).attr("src", src.replace(/base/, "hover"));
       }

    });

  }, function() {

    jQuery("img", this).each(function() {

       var src = jQuery(this).attr("src");
       if (src) {
         jQuery(this).attr("src", src.replace(/hover/, "base"));
       }
    });
  });
});

// Fix for subnavigation elements in the menu.  IE6 doesn't respenct multiple
// class definitions for CSS (li.parent.active is interpreted as li.active).
// This is causing the background image to display even for elements that are
// not parents
jQuery(document).ready(function() {
  // just hide all background images for list elements that are not parents
  jQuery("ul.menu li").not(".parent").css('background','none');
});

function increaseFontSize() 
{
      jQuery("#center-col-block").find("p,a,td,li,h1,h2,h3,h4,h5,h6").not("p.iteminfo").each(function() {
        jQuery(this).css('font-size',parseInt(jQuery(this).css('font-size'))*1.2+"px");
      });
      return false;
}

function decreaseFontSize() {
      jQuery("#center-col-block").find("p,a,td,li,h1,h2,h3,h4,h5,h6").not("p.iteminfo").each(function() {
        jQuery(this).css('font-size',parseInt(jQuery(this).css('font-size'))*.8+"px");
      });
      return false;
}

function resetFontSize() {
      jQuery("#center-col-block").find("p,a,td,li,h1,h2,h3,h4,h5,h6").removeAttr('style');
      return false;
}

function hideLoginMenu() {

    jQuery("#login-list-ui").hide('fast');

    var img = jQuery('#login-block img');
    var src = img.attr("src");
    if(src) {
       img.attr("src", src.replace(/hover/, "base"));
    }
}

function showLoginMenu() {

    if(loginMenuRefCount == 1) {
      jQuery("#login-list-ui").show('fast');

      // don't use the default roll over code
      var img = jQuery('#login-block img');
      var src = img.attr("src");
      if(src) {
         img.attr("src", src.replace(/base/, "hover"));
      }
    }
}

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}
