/**
 * @author Joe Kotvas
 */
var interval = 6000;
var banner;
var current_banner;
var show;
$('document').ready(function()
{
    bindEvents();
    banners = $('#rotating_banner .banner');
    current_banner = new Array();
    set_current_banner(0);
    start_timer();
});
function bindEvents()
{
    $('#previous_button').click(function()
    {
        change_banner('previous');
    });
    $('#next_button').click(function()
    {
        change_banner('next');
    });
    $('#rotating_banner').mouseover(function()
    {
    	$('#controls').show();
        clearTimeout(show);
    });
    $('#rotating_banner').mouseout(function()
    {
    	$('#controls').hide();
        start_timer();
    });
    $('#rotating_banner input').focus(function()
    {
        clearTimeout(show);
    });
    $('#rotating_banner input').blur(function()
    {
        start_timer();
    });
    $('#rotating_banner a').click(function()
    {
        clearTimeout(show);
    });
    //Subscribe-specific handlers
    $('#subscribe_banner input').focus(function()
    {
        if (this.value == 'Type your email here...') this.value = '';
    });
    $('#subscribe_banner input').blur(function()
    {
        if (this.value == '') this.value = 'Type your email here...';
    });
}

function change_banner(direction)
{
    $('#' + banners[current_banner['current']].id).fadeOut('normal');
    $('#' + banners[current_banner[direction]].id).fadeIn('normal');
    set_current_banner(current_banner[direction]);
    start_timer();
}

function set_current_banner(num)
{
    current_banner['current'] = num;
    if (banners[num + 1]) 
        current_banner['next'] = num + 1;
    else current_banner['next'] = 0;
    if (num > 0) 
        current_banner['previous'] = num - 1;
    else current_banner['previous'] = banners.length - 1;
}

function start_timer()
{
    clearTimeout(show);
    show = setTimeout("change_banner('next');", interval);
}

