(function($) {

    $.register = $.extend({
        indexScript: function(json) {
            $(document).ready(function() {
                var wrapper = $(json.wrapper);
                wrapper.find("div.scrollable").scrollable({ globalNav: true, clickable: false, vertical: true, size: 3 });
            });
        }
    }, $.register);

})(jQuery);


var arrowNb = 1;
var animInter = "";
var tbAnimLink = new Array(
    "",
    "/Browse/Category/1/Electrical-Products",
    "/Browse/Category/8/Recycling-Products",
    "/Browse/Category/13/Heating-Products",
    "/Browse/Category/18/Kits");
$(document).ready(function() {
    //Execute the slideShow
    slideShow();
});

function slideShow() {
    $('#gallery div').css({ opacity: 0.0 });
    $('#gallery div:first').css({ opacity: 1.0 });
    $('.tdAnimArrow div').css({ opacity: 0.0 });
    $('.tdAnimArrow div:first').css({ opacity: 1.0 });
    animInter = window.setInterval('gallery()', 3000);
}
function gallery() {
    //if no IMGs have the show class, grab the first image
    var current = ($('#gallery div.show') ? $('#gallery div.show') : $('#gallery div:first'));
    var currentArrow = $('#divAnimArrow' + arrowNb);
    arrowNb = ($('#divAnimArrow' + (arrowNb + 1)).length ? arrowNb + 1 : 1);


    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $('#gallery div:first') : current.next()) : $('#gallery div:first'));
    var nextArrow = $('#divAnimArrow' + arrowNb);
    //alert(current.nextAll('.divAnimArrow:first').length);

    //Set the fade in effect for the next image, show class has higher z-index
    next.show().css({ opacity: 0.0 })
		.addClass('show')
		.animate({ opacity: 1.0 }, 1000);
    nextArrow.css({ opacity: 0.0 })
		.animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000)
		.removeClass('show').hide();
    currentArrow.animate({ opacity: 0.0 }, 1000);
    arrowNb
}
function stopDiapo() {
    window.clearInterval(animInter);
    animInter = "";
}
function selectAnim(xid) {
    stopDiapo();
    $('#gallery div').hide();
    $('#divAnimPhoto' + xid).css({ opacity: 1.0 }).show();
    $('.tdAnimArrow div').hide();
    $('#divAnimArrow' + xid).css({ opacity: 1.0 }).show();
}
function animLink(xid) {
    selectAnim(xid);
    document.location.href = tbAnimLink[xid];
}
