﻿//Assign images a class of "ro" to make them rollover
//The rollover image should be the same name but with a _o for the name.

$(document).ready(function() {
    PEPS.rollover.init();
});

PEPS = {};

PEPS.rollover =
{
    init: function() {
        this.preload();

        $(".ro").hover(
         function() { $(this).attr('src', PEPS.rollover.newimage($(this).attr('src'))); },
         function() { $(this).attr('src', PEPS.rollover.oldimage($(this).attr('src'))); }
      );
    },

    preload: function() {
        $(window).bind('load', function() {
            $('.ro').each(function(key, elm) { $('<img>').attr('src', PEPS.rollover.newimage($(this).attr('src'))); });
        });
    },

    newimage: function(src) {
    try {
        return src.substring(0, src.search(/(\.[a-z]+)$/)) + '_o' + src.match(/(\.[a-z]+)$/)[0];
        }
        catch(err) {
        return null;
        }
    },

    oldimage: function(src) {
        return src.replace(/_o\./, '.');
    }
};
