var $slides;
var slideAnimDur = 500;
var slideDuration = 6000;
var slideTimeout;
var switching = false;

jQuery(document).ready(
    function ($)
    {
        $slides = $('#slider').find('.slide');

        if (1 < $slides.length)
        {
            slideTimeout = setTimeout(nextSlide, slideDuration);
        }
        
        $('#email').bind('focus', function (e)
            {
                var $this = $(this);
                
                if ('email address (required)' == $this.val())
                {
                    $this.val('');
                }
            }
        ).bind('blur', function (e)
            {
                var $this = $(this);
                
                if ('' == $this.val())
                {
                    $this.val('email address (required)');
                }
            }
        );
        
        $('#birthday').bind('focus',
            function (e)
            {
                var $this = $(this);
                
                if ('birthday (mm/dd/yy)' == $this.val())
                {
                    $this.val('');
                }
            }
        ).bind('blur',
            function (e)
            {
                var $this = $(this);
                
                if ('' == $this.val())
                {
                    $this.val('birthday (mm/dd/yy)');
                }
            }
        );
        
        $('#join-newsletter').bind('submit', function (e)
            {
                var $this = $(this);
                
                var email = $this.find('#email').val();
                var emailRegxp = new RegExp('^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$');
                var bday = $this.find('#birthday').val();
                var bdayRegxp = new RegExp('^[0-9]{2}/[0-9]{2}/[0-9]{2,4}$');
                
                if (emailRegxp.test(email) && (null === bday || undefined === bday || '' === bday || bdayRegxp.test(bday)))
                {
                    var info = {};
                    info.email = email;
                    info.ajax = true;
                    info.birthday = $('#birthday').val();
                    
                    $.ajax(
                        {
                            data: info,
                            dataType: 'json',
                            success: function (data, status, xhr)
                            {
                                if (data.success)
                                {
                                    $this.detach();
                                }
                                $('p.msg').html(data.html);
                            },
                            type: 'post',
                            url: '/'
                        }
                    );
                }
                else
                {
                    $('#newsletter .msg').html('<span class="error">Invalid e-mail address or birthday.</span>');
                }
                
                return false;
            }
        );
    }
);

function nextSlide()
{
    var $ = jQuery;
    var $cur = $slides.filter('.active');
    var nextIdx = $cur.index() + 1;
    if (nextIdx >= $slides.length)
    {
        nextIdx = 0;
    }
    var $next = $slides.eq(nextIdx);

    if ( ! $.browser.msie)
    {
        $cur.fadeOut(slideAnimDur);
        $next.fadeIn(slideAnimDur, function ()
            {
                $cur.removeClass('active');
                $next.addClass('active');

                slideTimeout = setTimeout(nextSlide, slideDuration);
            }
        );
    }
    else
    {
        $cur.removeClass('active');
        $next.addClass('active');
        slideTimeout = setTimeout(nextSlide, slideDuration);
    }
}

