/*
 * --------------------------------------------------------------------
 * Simple Scroller
 * by Siddharth S, www.ssiddharth.com, hello@ssiddharth.com
 * Version: 1.0, 05.10.2009 	
 * --------------------------------------------------------------------
 */
(function ($) {
    $(document).ready(function () {
        var index = 0;
        var images = $("img.slide");
        var thumbs = $("#thumbs img");
        var content = $("#contentBox div");
        var imgHeight = $(thumbs).attr("height");
        for (i = 0; i < thumbs.length; i++) {
            $(thumbs[i]).addClass("thumb-" + i);
            $(images[i]).addClass("image-" + i);
        }
        $(thumbs).slice(0, 2).clone().appendTo("#thumbs");

        $("#next").click(sift);
        show(index);
        var interval = setInterval(sift, 5000);

        function sift() {
            if (index < (thumbs.length - 1)) { index += 1; }
            else { index = 0 }
            show(index);
        }

        function show(num) {
            $(images).fadeOut(400);
            $(content).fadeOut(400);
            $(".image-" + num).fadeIn(400);
            $(".content-" + num).fadeIn(400);
            var scrollPos = (num) * imgHeight;
            $("#thumbs").stop().animate({ scrollTop: scrollPos }, 400);
        }

        $("#thumbs img").click(function () {
            var theclass = $(this).attr("class");
            var numberclass = theclass.replace("thumb-", "");
            var scrollPos = (numberclass) * imgHeight;
            if (scrollPos != $("#thumbs").scrollTop()) {
                show(numberclass);
            }
        });

        $("#gallery, #thumbs").hover(
        function () {
            clearInterval(interval);
        },
        function () {
            interval = setInterval(sift, 5000);
        });

        $("#thumbs img").hover(
        function () {
            var theclass = $(this).attr("class");
            var numberclass = theclass.replace("thumb-", "");
            var scrollPos = (numberclass) * imgHeight;
            if (scrollPos != $("#thumbs").scrollTop()) {
                var imageName = $(this).attr("src");
                $(this).attr("src", $(this).attr("src").replace("_Thumb.jpg", "_Thumb_Hover.jpg"));
            }
        },
        function () {
            var imageName = $(this).attr("src");
            $(this).attr("src", $(this).attr("src").replace("_Thumb_Hover.jpg", "_Thumb.jpg"));
        }
        )

    });
})(jQuery);
