﻿// jquery should be loaded before this
(function($){

    // javascript for the rotating images on the front page
    var rotate_callbackcount = 0;
    var rotate_time = 8;
    var click_delay = 4;

    $().ready(function () {

        $('.rotate_item_window').wrap('<div class="rotate_container"></div>').each(function() {
            var count = $('.rotate_item',$(this)).hide().length;
            if (count > 1) 
            {
                var nav = $('<div class="rotate_location"></div>');
                for (var i = 0; i < count; i++)
                    nav.append('<img class="rotate_marker" />');
                nav.insertAfter($(this));
            }
                
            rotate_header($(this), Math.floor(Math.random() * count)); // this will set the image sources for the rotate markers
        });
        
        // apply css styles to override no-javascript cases
        $('.rotate_item_window').css('overflow','hidden');
        $('.rotate_item').css('position','absolute');
        
        $('.rotate_marker').click( function () {
            var markers = $(this).parent().children('.rotate_marker');
            var index = markers.index($(this));
            
            rotate_header($(this).parents('.rotate_container').children('.rotate_item_window'), index);
            rotate_callbackcount++;
            setTimeout(rotate, 1000 * (rotate_time + click_delay));
        });
        
        rotate_callbackcount = 1;
        setTimeout(rotate, 1000 * rotate_time);
    });

    function rotate_header(block, index)
    {
        var headers = $('.rotate_item', block);
        
        // force any animation on the items to stop
        headers.stop(true, true);
            // forcing the animation to stop is critical
            // because we're using :visible to determine 
            // the current selection. if we wanted to
            // stack the animations we could use a class
            // instead. don't feel like adding that
            // complexity now.
        
        var current = $('.rotate_item:visible', block);
        var current_index = headers.index(current);
        
        if (index != current_index)
        {
            // cross-fade image
            
            current.css('z-index','100');
            headers.eq(index).css('z-index','50').show();
            current.fadeOut(500);
        }
        
        // change marker
        
        var markers = $(block).parents('.rotate_container').find('.rotate_marker');
        markers.attr('src','/theme/pm/image/scroll_marker.gif').eq(index).attr('src','/theme/pm/image/scroll_marker_solid.gif');
    }

    function rotate()
    {
        // terminate if there's another rotate
        if (rotate_callbackcount > 1)
        {
            rotate_callbackcount--;
            return;
        }

        $('.rotate_item_window').each( function () {
            var headers = $('.rotate_item',$(this));
            var current = $('.rotate_item:visible',$(this));
            rotate_header($(this),(headers.index(current) + 1) % headers.length);
        });
        
        setTimeout(rotate, 1000 * rotate_time);
    }

})(jQuery);

