/*
 * Simple Image Rotator
 * author: M. de Vries
 *
 */

var Rotator = {
    _pause : false,
    _container : '#divCoolGuides',
    _reel : '#imageReel',
    _pager : '#paging'
};

function Rotate(cmd) {

    if ( cmd === parseInt(cmd) ) {
        // cmd is Integer
        // Show image nr equal to cmd
        pagerID = cmd - 1;

        $(Rotator._pager + ' a').removeClass('active');
        $active = $(Rotator._pager + ' a').eq(pagerID).addClass('active');

        // Determine new postition
        imageOffset = pagerID * $(Rotator._reel + ' img:first').width();

        // Animate view
        $(Rotator._reel).animate({
                            left: -imageOffset
                            }, 500);

    } else if (cmd == 'init') {
        // Determine width of imageReel
        imageWidth = $(Rotator._container).width();
        imageSum = $(Rotator._reel + ' img').size();
        imageReelWidth = imageWidth * imageSum;

        // Set width
        // Shuffle slides
        $(Rotator._reel)
            .css({'width' : imageReelWidth})
            .children('a').shuffle();

        // Bind pager controls
        $(Rotator._pager + ' a').click(function() {
                pagerID = $(this).attr('rel') - 0;
                Rotate(pagerID);
                return false;
            });

        // Bind mouse hover events for pausing of slideshow
        $(Rotator._reel).hover(function() {
                Rotator._pause = true;
            },
            function() {
                Rotator._pause = false;
            });

        // Autoplay
        setInterval("Rotate('next')", 4000);


    } else if (cmd == 'next') {
        // Show next image
        if (Rotator._pause == false) {
            $active = $(Rotator._pager + ' a.active').parent().next().children('a');
            if ($active.length === 0) {
                $active = $(Rotator._pager + ' a:first');
            }

            pagerID = $active.attr('rel') - 0;
            Rotate(pagerID);

        } else {
            // on hover, don't proceed to next slide
            return true;
        }
    }
}

