Event.observe(window, "load", function() { ThumbsRollover.init(); });

var ThumbsRollover = {
    beforeRollOver: null,
    interval: null,
    current: 1,
    blArray: Array(),

    init: function() {
        var tbs = $$("img.rollover");
        var parent;

        $$("img.rollover").each(function(tb) {
            parent = tb.parentNode.parentNode;
            if(parent.className == "contentThumb") {

                //need to do $() again for it to be extended with prototype Element class
                //so we can call select method
                newtb = $(parent).select(".overlay img");
                newtb = newtb[0];
                Actions.attach( newtb, "onmouseover", "ThumbsRollover.rollOver", "'" + tb.id + "'" );
                Actions.attach( newtb, "onmouseout", "ThumbsRollover.rollOut", "'" + tb.id + "'" );

            } else if (parent.className == "search_items") {

                newtb = $(parent).select(".search_overlay img");
                newtb = newtb[0];
                Actions.attach( newtb, "onmouseover", "ThumbsRollover.rollOver", "'" + tb.id + "'" );
                Actions.attach( newtb, "onmouseout", "ThumbsRollover.rollOut", "'" + tb.id + "'" );

            } else {

                Actions.attach( tb, "onmouseover", "ThumbsRollover.rollOver", "'" + tb.id + "'" );
                Actions.attach( tb, "onmouseout", "ThumbsRollover.rollOut", "'" + tb.id + "'" );

            }
        });
    },

    change: function(id, url) {
        ThumbsRollover.current = (parseInt(ThumbsRollover.current)+1)%10;
        if (ThumbsRollover.current == 0) ThumbsRollover.current = 10;
        while(ThumbsRollover.blArray.indexOf(ThumbsRollover.current) != -1) {
            ThumbsRollover.current   = (parseInt(ThumbsRollover.current)+1)%10;
            if (ThumbsRollover.current == 0) ThumbsRollover.current = 10;
        }
        $(id).src = Configuration.THUMB_URL + "/" + url + "/thumb/" + ThumbsRollover.current + ".jpg";
    },

    rollOver: function(id) {
        elmnt = $(id);
        blacklist = elmnt.getAttribute("rel");
        ThumbsRollover.blArray = new Array();
        if(blacklist != "" && blacklist != null) {
            var j = 0;
            for(i=0;i<blacklist.length;i++) {
                ThumbsRollover.blArray[j++] = parseInt(blacklist.charAt(i)) + 1;
            }
        }
        ThumbsRollover.beforeRollOver = elmnt.src;
        var src = elmnt.src;
        var expression = /http:\/\/thumbs.diff.abrutis.com\/(\w{32})\/thumb\/(\d+).jpg/;
        expression.exec(src);
        url = RegExp.$1;
        ThumbsRollover.current = RegExp.$2;
        ThumbsRollover.interval = setInterval("ThumbsRollover.change('" + id + "', '" + url + "')", 500);
    },

    rollOut: function(elmnt) {
        elmnt.src = ThumbsRollover.beforeRollOver;
        clearInterval(ThumbsRollover.interval);
    }

};
