
    homepageRotationInterval = null;
    homepageRotationImg = 0;
    homepageRotationFXList = [];
    homepageRotationImgList = [];
    homepageRotationNavList = [];
    
    $(document).ready(function() {

      homepageRotationImgList = $('#imgRotation li');
      homepageRotationNavList = $('#imgRotation li.nav a');
	   
      //check nav list, since imglist contains li.nav
      if (homepageRotationNavList.length > 1){
        setupNav();

        homepageRotationImgList.hide();
        homepageRotationImgList.first().addClass("selected").show();

        homepageRotationImgList.last(".nav").addClass("selected").show();

        homepageRotationInterval = setInterval("rotateNextImage()", 10000);
        $('#imgRotation li.nav a.selected')
          .animate({"backgroundPosition": "(26px 0)"}, 10000, function(){
              $('#imgRotation li.nav a').css("background-position","");
          });
        
        $("#imgRotation li.nav img").replaceWith(homepageRotationImgList.eq(1).find("img").clone()
          .click(rotateNextImage)
          .css("cursor","pointer")
          .fadeIn(500)); 
        $('#imgRotation li.nav div').fadeTo(0, 0.8);

        // curvy corners screws up the layout real bad if it tries to 
        // apply curvy corners to the nav
        if(!$.browser.msie){
          $("#imgRotation li.nav a")
            .css("-webkit-border-radius","2px")
            .css("-moz-border-radius","2px")
            .css("border-radius","2px")
        }
      }
      //else only 1 homepage image, nav is hidden by default.
      
    });

    function rotateNextImage(){
      rotateToIdx((homepageRotationImg + 1) % (homepageRotationImgList.length - 1));
    }

    function rotateToIdx(idx){
      if (idx != homepageRotationImg){

        oldIdx = homepageRotationImg;
        homepageRotationImg = idx;
        nextIdx = (homepageRotationImg + 1) % (homepageRotationImgList.length - 1);

        //cross fade main img
        homepageRotationImgList.eq(oldIdx).fadeOut(1000, 0);
        homepageRotationImgList.eq(idx).fadeIn(1000);

        //update nav
        homepageRotationNavList.eq(oldIdx).stop().css("background-position","").removeClass("selected");
        homepageRotationNavList.eq(homepageRotationImg).addClass("selected")
          .css("background-position", "13px 0")
          .animate({"backgroundPosition": "(26px 0)"}, 10000, function(){
              $('#imgRotation li.nav a').css("background-position","");
          });

        $("#imgRotation li.nav div").hide(); 
        $("#imgRotation li.nav img").replaceWith(homepageRotationImgList.eq(nextIdx).find("img").clone()
          .click(rotateNextImage)
          .css("cursor","pointer")
          .fadeIn(500)); 
        $("#imgRotation li.nav div").fadeIn(500); //prevent blank frame

      }
    }
	
    function setupNav(){ 
      //save numbers to data and remove text
      $('#imgRotation li.nav a').each(function(){
          $(this).data("idx", $(this).find("span").text()); 
        });

      homepageRotationNavList.css("cursor", "pointer");
      homepageRotationNavList.click(function(){
        clearInterval(homepageRotationInterval);
        $('#imgRotation li.nav a').stop().css("background-position","");

        homepageRotationInterval = setInterval("rotateNextImage()", 10000);
        rotateToIdx($(this).data("idx")-1);
      });

      homepageRotationNavList.first().addClass("selected");  //select first nav item 
      homepageRotationImgList.find(".nav").addClass("selected");  // show nav

    }

