$(document).ready
(
    function()
    {
        PngFix.init();
        Promo.init();
        ContactForm.init();
        Dashboard.init();
        MP3Player.init();
        Quotes.init();
        Media.init();
        ZebraStripes.init();

        caponeTime = new Date(2010, 0, 1, 12).getTime();
        dayAfter = new Date(2010, 0, 1, 16).getTime();
        timer = now = new Date().getTime();

        if (now < caponeTime)
        {
            setInterval("caponeCountdown()", 500);

            // Before
            $('.promo-capone-countdown').show();
        } else if (now >= dayAfter) {
            // After
            $('.promo-capone-after').show();
        } else {
            // Day of
            $('.promo-capone-day-of').show();
        }
    }
);

function caponeCountdown() {
    if (caponeTime - timer > 0)
    {
        var diff = caponeTime - timer;

        var dayFactor   = 1000 * 60 * 60 * 24;
        var days        = Math.floor(diff / dayFactor).toString();


        var hourFactor  = 1000 * 60 * 60;
        var hours       = Math.floor((diff % dayFactor) / hourFactor).toString();
        
        var minuteFactor= 1000 * 60;
        var minutes     = Math.floor((diff - (days * dayFactor) - (hours * hourFactor)) / minuteFactor).toString();

        var seconds = Math.floor((diff - (days * dayFactor) - (hours * hourFactor) - (minutes * minuteFactor)) / 1000).toString();

        $('#countdown-days').html(days.length == 1 ? "0" + days : days);
        $('#countdown-hours').html(hours.length == 1 ? "0" + hours : hours);
        $('#countdown-minutes').html(minutes.length == 1 ? "0" + minutes : minutes);
        $('#countdown-seconds').html(seconds.length == 1 ? "0" + seconds : seconds);

        timer = new Date().getTime();
    } else {
        $('.promo-capone-countdown').hide();
        $('.promo-capone-day-of').show();
    }
}

var PngFix =
{
    init:function()
    {
//        $('body').supersleight();
    }
}

var Promo =
{
    init:function()
    {
        $('#email-list-form').ajaxForm
        (
            {
                beforeSubmit:Promo.showLoader,
                dataType:'json',
                success:Promo.success
            }
        );
    },

    showLoader:function()
    {
        $('#email-list-submit').find('input').fadeOut(
            'fast',
            function()
            {
                $('#email-list-submit').find('img').fadeIn('fast');
            }
        );
    },

    success:function(result)
    {
        $('#email-list-submit').find('img').fadeOut(
            'fast',
            function()
            {
                $('#email-list-submit').find('input').fadeIn(
                    'fast',
                    function()
                    {
                        if(result.errors)
                        {
                            var errorHtml = "";

                            for (var i = 0; i < result.errors.length; i++)
                            {
                                errorHtml += '<div class="error">' + result.errors[i] + '</error>';
                            }

                            $('#email-list-messages').html(errorHtml);
                        }
                        else
                        {
                            $('#email-list-messages').html('<div class="success">Thank You! You will receive an email confirmation shortly. If you have other emails you would like to sign up, feel free to send those as well.</div>');
                            $('#email-list-form input[name=email]').val("");
                        }
                    }
                );
            }
        );
    }
}

var Dashboard = {
    init:function() {
        $('#panel-showdates').load("/upcoming-dates", null, function() { ZebraStripes.init('#panel-showdates'); });
        $('#panel-news').load("/recent-news", null, function() { ZebraStripes.init('#panel-news'); });
    }
}

var Quotes = {
    _quotes:        '#quotes .quote',
    _block:         'p',
    _quoteCount:    0,
    _current:       0,
    _fadeLength:    10000,

    init:function()
    {
        this._setQuoteLength($(this._getQuotes()).length);
        this._setCurrent(Math.floor(Math.random() * this._getQuoteLength()));

        $(this._getQuoteBlock()).fadeIn(1000);
        setInterval('Quotes._change()', this._getFadeLength());
    },

    _change:function()
    {
        var next = (this._getCurrent() + 1 >= $(this._getQuotes()).length ? 0 : this._getCurrent() + 1);

        $(this._getQuoteBlock()).fadeOut(
            1000,
            function()
            {
                Quotes._setCurrent(next);
                $(Quotes._getQuoteBlock()).fadeIn(1000);
            }
        );
    },

    _getQuoteBlock:function()
    {
        return this._getQuotes()
            + ':eq(' + this._getCurrent() + '), '
            + this._getQuotes() + ':eq(' + this._getCurrent() + ') '
            + this._getBlock();
    },

    _getFadeLength:function()
    {
        return this._fadeLength;
    },

    _getBlock:function()
    {
        return this._block;
    },

    _getQuotes:function()
    {
        return this._quotes;
    },

    _getCurrent:function()
    {
        return this._current;
    },

    _setCurrent:function(value)
    {
        this._current = value;
    },

    _getQuoteLength:function()
    {
        return this._quoteLength;
    },

    _setQuoteLength:function(value)
    {
        this._quoteLength = value;
    }
}

var MP3Player = {
    init:function() {
        swfobject.embedSWF(
            '/flash/mp3Player.swf',
            'flash-placeholder',
            '320',
            '30',
            '8.0.0',
            null,
            null,
            { wmode:"transparent" }
        );

        swfobject.embedSWF(
            '/flash/mp3Player-bright.swf',
            'flash-placeholder-bright',
            '320',
            '30',
            '8.0.0',
            null,
            null,
            { wmode:"transparent" }
        );

        $('#mp3-player').show();

    }
};
var ContactForm =
{
    init:function()
    {
        $('#contact-form').ajaxForm
        (
            {
                beforeSubmit:ContactForm.showLoader,
                dataType:'json',
                success:ContactForm.success
            }
        );
    },

    showLoader:function()
    {
        $('#contact-submit').find('input').fadeOut(
            'fast',
            function()
            {
                $('#contact-submit').find('img').fadeIn('fast');
            }
        );
    },

    success:function(result)
    {
        $('#contact-submit').find('img').fadeOut(
            'fast',
            function()
            {
                $('#contact-submit').find('input').fadeIn(
                    'fast',
                    function()
                    {
                        if(result.errors)
                        {
                            var errorHtml = "";

                            for (var i = 0; i < result.errors.length; i++)
                            {
                                errorHtml += '<div class="error">' + result.errors[i] + '</error>';
                            }

                            $('#contact-messages').html(errorHtml);
                        }
                        else
                        {
                            $('#contact-messages').html('<div class="success">Thank you for your message!</div>');
                            $('#contact-form input[type=text], #contact-form textarea').val("");
                        }
                    }
                );
            }
        );
    }
}


var Media = {
    init:function()
    {
        $('#cover_list_off, #cover_list').hide();

        $('#cover_list_on, #cover_list_off').click(
            function(e)
            {
                e.preventDefault();

                $('#cover_list, #cover_list_on, #cover_list_off').toggle();
            }
        );
    }
}

var ZebraStripes = {
    init:function(table)
    {
        $((table ? table +' ' : '') + 'table tbody tr').each(
            function(i)
            {
                $(this).addClass(i % 2 == 0 ? 'even' : 'odd');
            }
        );
    }
}