/**
 * global.js
 *
 * Global JavaScript functions and variables for MMA Connect website.
 *
 * @author Martin Bean <martin@yourfightsite.com>
 */
$(document).ready(function() {
    Cufon.replace('#navigation li', {
        hover: true,
        textShadow: '2px 2px 2px rgba(0,0,0,0.75)'
    });
    Cufon.replace('#sponsors h2', {
        textShadow: '2px 2px 2px #000'
    });
    $('a[rel="external"]').attr('title', function() {
        return this + ' (opens in a new window)';
    }).click(function() {
        window.open(this.href);
        return false;
    });
    $('a[rel="popup"]').fancybox({
        overlayColor: '#171717',
        overlayOpacity: 0.75
        
    });
    $('form.poll').submit(function() {
        var form = this;
        var response = $('div.response', form);
        if ($('input:checked', form).length == 0) {
            response.fadeOut('fast', function() {
                $(this).html('<p class="error">Please select an option.</p>').fadeIn('fast');
            })
        }
        else {
            var action = $(form).attr('action');
            $.post(action, $(form).serialize(), function(data) {
                console.log(data);
                if (data.result == 'success') {
                    $('fieldset', form).fadeOut('fast');
                }
                response.fadeOut('fast', function() {
                    $(this).html('<p class="' + data.result + '">' + data.message + '</p>').fadeIn('fast');
                });
            }, 'json');
        }
        return false;
    });
    $('.cycle').cycle({
        fx: 'fade',
        pager: '.cycle .nav',
        slideExpr: 'div[class!="nav"]',
        speed: 5000
    });
    $('.gallery').photoGallery();
    $('.tabs').tabs();
    $('#home_banners .inner').jCarouselLite({
        btnPrev: '#home_banners .prev',
        btnNext: '#home_banners .next',
        visible: 1
    });
    $('#sponsors .inner').jCarouselLite({
        btnPrev: '#sponsors .prev',
        btnNext: '#sponsors .next',
        visible: 3
    });
});

/**
 * Photo gallery plugin.
 *
 * @author Martin Bean <martin@yourfightsite.com>
 * @version 0.1
 */
jQuery.fn.photoGallery = function() {
    return this.each(function() {
        var count = $('.slider .thumbs a', this).size();
        var current = 0;
        var gallery = this;
        if (count < 7) {
            $('.slider a.next', gallery).addClass('disabled');
        }
        $('.stage a.next').click(function() {
            // get next photo
            return false;
        });
        $('.stage a.prev').click(function() {
            // get previous photo
            return false;
        });
        $('.slider a.next', this).click(function() {
            if ($(this).hasClass('disabled')) {
                return false;
            }
            alert('Moving right.');
            return false;
        });
        $('.slider a.prev', this).click(function() {
            if ($(this).hasClass('disabled')) {
                return false;
            }
            alert('Moving left.');
            return false;
        });
        $('.slider .thumbs a', this).click(function() {
            $('.stage img', gallery).attr('src', $(this).attr('href'));
            current = $('.thumbs a', gallery).index(this);
            return false;
        });
    });
};
