window.addEvent('domready', function(){

  // provides a number of private methods for parsing image paths, and exchanging images
  // public methods apply rolllovers to provided items
  var rolloverManager = function(){

    //----------- PRIVATE METHODS ----------------------------//

      //get the url of the background image
      var getBGImage = function(which){
        var fulltext = which.getStyle('backgroundImage');
        //could use a regex here too, but it's pretty straightforward as it is
        var path = fulltext.substring(4, fulltext.length -1);

        //firefox 3.6.5 inserts quotes around the url
        return path.replace(/"/g,'');
      }

      //regex to break down the path into three components: everything before the dot, the dot, and everything afterwards
      var path = /(.*)(\.)(.{3})/;

      var getRolloverSrc = function(src){
        var result = path.exec(src);
        return result[1] + '_h.' + result[3];
      }

     //Return our object with only the public methods
     return {

       //-------------- PUBLIC METHODS -----------------------//

       //prepare standard rollovers
       prepareRollover : function(item){
        var src = item.getProperty('src');
        item.rollonImg = new Asset.image(getRolloverSrc(src));
        item.rolloffImg = new Asset.image(src);

        item.addEvent('mouseover', function(){
          item.setProperty('src',item.rollonImg.src);
        })

        item.addEvent('mouseout', function(){
          item.setProperty('src',item.rolloffImg.src);
        })
      },

       //prepare background rollovers
      prepareBGRollover : function(item){
        var src = getBGImage(item);
        item.rollonImg = new Asset.image(getRolloverSrc(src));
        item.rolloffImg = new Asset.image(src);

        item.addEvent('mouseover', function(){
          item.style.backgroundImage = "url(" + item.rollonImg.src + ")";
        })

        item.addEvent('mouseout', function(){
          item.style.backgroundImage = "url(" + item.rolloffImg.src + ")";
        })
      }
     };
  }();

  //prepare rollovers on all classes marked with activenav
  $$('.activenav').each(function(item){
    rolloverManager.prepareRollover(item);
  });


  //prepare rollovers on all classes marked with activebg
  $$('.activebg').each(function(item){
    rolloverManager.prepareBGRollover(item);
  });
  $$('.activebackground').each(function(item){
    rolloverManager.prepareBGRollover(item);
  });

});
