var currentBanner = 0;
var maxBanners = 0;
var bannerTimer = 0;
var bannerOptions = { width: 1025, delay: 4500, controlColor: '#000000', currentControlColor: '#2d6286' };

function popup(url, track)
{
  if(track && _gaq) { _gaq.push(['_trackPageview', track]); }
  var newwindow = window.open(url, '_blank', 'height=600,width=800');
  if (window.focus && newwindow) {newwindow.focus()}
  return false;
}

function slide_left(hide, show)
{
    $(hide).animate({ left: '-' + bannerOptions['width'] + 'px' });
    $(show).css({ left: bannerOptions['width'] + 'px'}).animate({left: '0px'});
}
function slide_right(hide, show)
{
    $(hide).animate({ left: bannerOptions['width'] + 'px' });
    $(show).css({ left: '-' + bannerOptions['width'] + 'px'}).animate({left: '0px'});
}

function show_banner(index)
{
    var nextBanner = index;
    var banners = $('div.animation-frame');
    var hide = banners[currentBanner];
    if(index > maxBanners) { nextBanner = 0; }
    if(index < 0) { nextBanner = maxBanners; }
    var show = banners[nextBanner];
    
    if(nextBanner > currentBanner)
    { slide_left(hide, show); }
    else
    { slide_right(hide, show);  }
    $('#show-slide-' + currentBanner).css({background: bannerOptions['controlColor']});
    $('#show-slide-' + nextBanner).css({background: bannerOptions['currentControlColor']});
    currentBanner = nextBanner;
}

function auto_rotate()
{
    if(bannerTimer) { clearTimeout(bannerTimer); }
    show_banner(currentBanner + 1);
    bannerTimer = setTimeout('auto_rotate();', bannerOptions['delay']);
}

$(document).ready(function() {
    var parent = $('#top-banner');
    $(parent).append('<div id="animation-controls"></div>');

    $('#top-banner>.clickable').each(function(index, element) {
        var left = parent.width() * index;
        maxBanners = index;
        $(element).css({ "left": left + 'px', visibility: 'visible', top: 0 });
        $('#animation-controls').append("<div class='animation-picker' id='show-slide-" + index + "'></div>");
        $('#show-slide-' + index).click(function(eventData) {
            if(bannerTimer) { clearTimeout(bannerTimer); }
            if (index != currentBanner) { show_banner(index); }
            bannerTimer = setTimeout('auto_rotate();', bannerOptions['delay']);
            eventData.preventDefault();
        });
            
    });
    $('#show-slide-' + currentBanner).css({background: bannerOptions['currentControlColor']}); 
    bannerTimer = setTimeout('auto_rotate();', bannerOptions['delay']);


});
