jQuery(document).ready(function() {
    jQuery("div.tabroll").each(function() {
        var tabrollCont = jQuery(this);

        tabrollCont.children("a").hover(function() {

            SetOver(jQuery(this));

        }, function() {

            var aTag = jQuery(this);

            if (!aTag.hasClass("tabClicked"))
                SetOut(aTag);

        });

        tabrollCont.children("a").click(function() {

            // Get all images and reset them
            jQuery("div.tabroll a").each(function() {
                var aTag = jQuery(this);

                SetOut(aTag);

                aTag.removeClass("tabClicked");
            });

            var clickedA = jQuery(this);
            clickedA.addClass("tabClicked");
            SetOver(clickedA);
        });
    });
});

function SetOver(aTag) {
    // Get the iamge tag
    var imgTag = aTag.children("img").eq(0);
    // Get the extension of the image file in use
    var ext = GetExtension(imgTag.attr("src"));

    // Check to make sure -over isn't already there
    if (GetFilename(imgTag.attr("src")).lastIndexOf("-over" + ext) == -1) {

        // Add -over
        imgTag.attr("src", imgTag.attr("src").replace(ext, "-over" + ext));
    }
}

function SetOut(aTag) {
    // Get the image tag
    var imgTag = aTag.children("img").eq(0);
    // Get the extension of the image file
    var ext = imgTag.attr("src").substr(imgTag.attr("src").lastIndexOf("."));

    // Remove -over
    imgTag.attr("src", imgTag.attr("src").replace("-over" + ext, ext));
}

function GetExtension(name) {
    return name.substr(name.lastIndexOf("."));
}

function GetFilename(path) {
    return path.split("/")[path.split("/").length - 1];
}