 
//Global settings
var rot_paused = 0;  //don't start paused
var rot_interval = 5000; //Time between changes (default - overridden by html)
var rot_text_op = 0.9;   //Opacity for text ('cause of IE)
var rot_control_op = 1;//Opacity for control div ('cause of IE)
var rot_t; //the timer

 
function imageRotator() {

    //Hide the play button (since we start out in play mode)
    $('rot_play').setStyle('display', 'none');
    
    //Hide all but the first image
    //Set only the first image to 'show' (just in case). This should already 
    // be done in the HTML to accomodate non-javascript browsers
    $$('div.rotator div.rot_content').each(function(el, i) {
        if (i == 0) {
            el.addClass('show');
        } else {
            el.getChildren().set('opacity', 0);
            el.removeClass('show');
        }
    }); //rot_content
    
    //Prevent the controls from taking us anywhere
    $$('.rot_controls a').addEvent('click', function(event) {
        event.preventDefault();
    });
    
    //IE doesn't work very well, so set opacity with mootools instead of
    // pure CSS.
    $$('.show div.rot_txt').set('opacity', rot_text_op);
    $$('div.rot_controls').set('opacity', rot_control_op);
    
    //Rotate the image after an interval. Use the time param in the
    //  main rotator div if present
    var tmp = $$('div.rotator').getProperty('time');
    rot_interval = (tmp == '') ? rot_interval : tmp;
    rot_t = setTimeout('rotate(1)', rot_interval);

} //theRotator()

function rotate(jump) { //jump can be 1 or -1 ONLY

    //Get elements
    var rot_all = $$('div.rotator div.rot_content');
    var rot_cur = $$('div.rotator div.show')[0];
    var rot_next = rot_all.indexOf(rot_cur) + jump;
    
    //Identify the prev/next element.  If jump takes us out of range, 
    // then wrap to the first or last element.
    rot_next = (rot_next > rot_all.length-1) ? rot_next = rot_all[0] : 
        rot_next < 0 ? rot_all.getLast() : rot_next = rot_all[rot_next];
    
    //Change class to show for the next content div
    rot_cur.removeClass('show');
    rot_next.addClass('show');
    
    //fade in/out
    //Fading must be applied to the content element's children to allow 
    // for proper operation in IE
    rot_cur.getChildren().tween('opacity', 0);
    rot_next.getChildren().each(function(el) {
        var op = el.hasClass('rot_txt') ? rot_text_op : 1;
        el.tween('opacity', op);
    });
    
    //Center the buttons vertically
    /*
    rot_next.getChildren().each(function(el) {
        if (el.hasClass('rot_txt')) {
            var h = el.getSize().y;
            h = (h/2) - 25/2;
            $$('div.rot_controls').each( function(el) {
                el.setStyle('bottom', h)
            });
        } //if
    });
    */

    //keep on rotating...
    if (!rot_paused) {
        rot_t = setTimeout('rotate(1)', rot_interval);
    }

} //rotate()

function pause() {
    rot_paused = 1;
    clearTimeout(rot_t);
    $("rot_pause").setStyle('display', 'none');
    $("rot_play").setStyle('display', 'inline');
}
function play() {
    rot_paused = 0;
    clearTimeout(rot_t);
    rot_t = setTimeout('rotate(1)', 1000); //1-sec delay
    $("rot_pause").setStyle('display', 'inline');
    $("rot_play").setStyle('display', 'none');
}
function next() {
    clearTimeout(rot_t);
    rotate(1);
}
function prev() {
    clearTimeout(rot_t);
    rotate(-1);
}

//Start the rotator
window.addEvent('domready', function() {
  imageRotator();
});
