/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Rotator
 *
 * Controls a series of cross fading images with a simple
 * control bar.
 *
 * javascript
 *
 * @author     Tom Martin <tmartin@retaildimensions.com>
 * @copyright  2005-2011 Retail Dimensions Inc.
 */

var rotInt = '';
    
var pace = 10000;

function theRotator() {
    var numR = $('#numR').val();
    var lastR = numR - 1;

    if (numR > 1) {
        $('#rotRBut').show();
        $('#rotRBut').bind('click', function() {rotate(1);});
        $('#rotLBut').show();
        $('#rotLBut').bind('click', function() {rotate(lastR);});
    }

    //Set the opacity of all images to 0
    $('#billboard ul li').css({opacity: 0.0});

    //Get the first image and display it (gets set to full opacity)
    $('#billboard ul li:first').css({opacity: 1.0});
    $('#billboard ul li:first .rDescBox').show();

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    rotInt = setInterval("rotate(-1)",pace);
}

function rotate(rotIx) {

    var numR = $('#numR').val();
    var lastR = numR - 1;
    var curR = $('#curR').val();

    if (rotIx > -1) {
        clearInterval(rotInt);
    }

    //Get the first image
    var current = ($('#billboard ul li.show')?  $('#billboard ul li.show') : $('#billboard ul li:first'));
    var currentTxt = ($('#billboard ul li.show')?  $('#billboard ul li.show .rDescBox') : $('#billboard ul li:first .rDescBox'));

    if ( current.length == 0 ) {
        current = $('#billboard ul li:first');
        currentTxt = $('#billboard ul li:first .rDescBox');
    }

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = '';
    if (rotIx > -1) {
        next = $('#billboard ul li:eq(' + rotIx + ')');
        nextTxt = $('#billboard ul li:eq(' + rotIx + ') .rDescBox');
        $('#curR').val(rotIx);
    } else {
        next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#billboard ul li:first') :current.next()) : $('#billboard ul li:first'));
         nextTxt = next.children("a").children(".rDescBox");
      //  nextTxt = next.html();
       // alert(nextTxt.toSource());
        //nextTxt = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#billboard ul li:first .rDescBox') :current.next('.rDescBox')) : $('#billboard ul li:first .rDescBox'));
        var nextCurR = (curR * 1) + 1;
        if (nextCurR > lastR) {nextCurR = 0;}
        $('#curR').val(nextCurR);
    }
    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({opacity: 0.0})
    .addClass('show')
    .animate({opacity: 1.0}, 1000);
    

    //Hide the current image
    current.animate({opacity: 0.0}, 1000)
    .removeClass('show');
    
    currentTxt.hide();
    nextTxt.show();

    // buttons
    var currentBut = ($('#rotButs div.rotC')?  $('#rotButs div.rotC') : $('#rotButs div:first'));

    if ( currentBut.length == 0 ) {
        currentBut = $('#rotButs div:first');
    }

    //Get next image, when it reaches the end, rotate it back to the first image
    var nextBut = '';
    if (rotIx > -1) {
        nextBut = $('#rotButs div:eq(' + rotIx + ')');
    } else {
        nextBut = ((currentBut.next().length) ? ((currentBut.next().hasClass('show')) ? $('#rotButs div:first') :currentBut.next()) : $('#rotButs div:first'));
    }

    currentBut.removeClass('rotC');
    currentBut.addClass('rotB');
    nextBut.removeClass('rotB');
    nextBut.addClass('rotC');

    if (rotIx > -1) {
        rotInt = setInterval('rotate(-1)',pace);
    }



    if (numR > 1) {
        var newCurR = $('#curR').val();
        var nextIx = (newCurR * 1) + 1;
        if (nextIx > lastR) {nextIx = 0;}
        var prevIx = (newCurR * 1) - 1;
        if (prevIx < 0) {prevIx = lastR;}
        $('#rotRBut').unbind('click');
        $('#rotLBut').unbind('click');
        $('#rotRBut').bind('click', function() {rotate(nextIx);});
        $('#rotLBut').bind('click', function() {rotate(prevIx);});
    }
}


$(document).ready(function() {
    //Load the slideshow
    theRotator();
    $('#billboard').fadeIn(1000);
    $('#billboard ul li').fadeIn(1000); // tweek for IE
});
