﻿$(function() {
    kp.rollover.init();
});

kp.rollover = {
    init: function() {
        this.preload();
        $('.ro').hover(
                     function() { $(this).attr('src', kp.rollover.newimage($(this))); },
                     function() { $(this).attr('src', kp.rollover.oldimage($(this))); }
                  );
    },

    preload: function() {
        $(window).bind('load', function() {
            $('.ro').each(function(key, elm) { $('<img>').attr('src', kp.rollover.newimage($(this))); });
        });
    },

    newimage: function(jq) {
        var src = jq.data('src');
        if (src == null)
            src = jq.attr('src');
        var src_active = jq.attr('src_active');
        if (src_active == null)
            src_active = src.substring(0, src.search(/(\.[a-z]+)$/)) + '-active' + src.match(/(\.[a-z]+)$/)[0];
        jq.data('src', src);
        return src_active;
    },

    oldimage: function(jq) {
        return jq.data('src');
    }
};

