$(document).ready(init);

function init()
{
    $('.e2ma_signup_form_element input').attr('data-pretext','email address');
    doPretext();
    doTweets();
    doCarousels();
    doOverlays();
    doButtons();
    doColorbox();
    doForm();
}

function doForm()
{
    $('#submit-nomination-button').click(function(e){  e.preventDefault(); submitNominationForm(false); });
    $('#submit-contact-button').click(function(e){  e.preventDefault(); submitNominationForm(true); });
    $('#submit-signup-button').click(function(e){ e.preventDefault(); $('#e2ma_signup').submit(); })
}

function submitNominationForm(contact_form)
{
    var form_id = '#nominate-form';
    if(contact_form) form_id = '#contact-form';
    var values = $(form_id).serialize() + '&f=' + (contact_form ? 'contact' : 'nominate');
    var success = true;
    $(form_id + ' .required-field').focus(function(){ $(this).removeClass('required-field-error'); });
    $(form_id + ' .required-field').each(function(){
        if(!$(this).val())
        {
            $(this).addClass('required-field-error');
            success = false;
        } else {
            $(this).removeClass('required-field-error');
        }
    });
    if(success)
    {
        $.post('/wordpress/wp-content/themes/tedx/ajax.php?f=' + (contact_form ? 'contact' : 'nominate'),values,function(tweet){
            alert('Your ' + (contact_form ? 'message' : 'nomination') +  ' has been sent! Thank you.');
            clearNominationForm(contact_form);
        });
    } else {
        alert("Please correct the fields marked in red.");
        return false;
    }
}

function clearNominationForm(contact_form)
{
    $(':input').each(function(){
        $(this).val('');
    });
    if(!contact_form) $('#nomination-form').slideUp();
}

function doPretext()
{
    $('body > header nav aside form input').each(function(){
        if($(this).attr('data-pretext'))
        {
            $(this).val($(this).attr('data-pretext'));
            $(this).focus(checkPretext);
            $(this).blur(checkPretext);
        }
    });
}

function checkPretext()
{
    if($(this).val() == $(this).attr('data-pretext'))
    {
        $(this).val('');
    } else {
        if($(this).val().replace(' ','') == '')
        {
            $(this).val($(this).attr('data-pretext'));
        }
    }
}

function doColorbox()
{
    $(document).ready(function(){
        $(".colorbox-youtube").colorbox({iframe:true, innerWidth:480, innerHeight:360,
            onOpen:function(){ $('object, embed, iframe').css({'visibility':'hidden'}); },
            onClosed:function(){ $('object, embed, iframe').css({'visibility':'inherit'}); }
        });
        $(".colorbox-photo").colorbox({});
    });

}

function doButtons()
{
    $('#nomination-button').click(function(e){ e.preventDefault(); $('#nomination-form').slideToggle(); });
}

function doOverlays()
{
    $('.video-list li').mouseenter(function(){  $(this).find('.overlay').show(); });
    $('.video-list .overlay').mouseleave(function(){ $(this).hide(); });
    $('.carousel li').mouseenter(function(){  $(this).find('.overlay').show(); });
    $('.carousel .overlay').mouseleave(function(){ $(this).hide(); });
}

function doTweets()
{
    $.getJSON('/wordpress/wp-content/themes/tedx/ajax.php?f=tweets&s=TEDxMileHigh',function(tweet){
        var c = $('.ted-tweets');
        var pattern = /(http:\/\/[^\s]+)/g;
        for(var i=0;i<4;i++)
        {
            c.prepend('<li><p>' + tweet[i].text.replace(pattern, "<a href='$1'>$1</a>") + '</p><div class="author"><img src="/wordpress/wp-content/themes/tedx/ajax.php?f=avatar&s=' + tweet[i].user.screen_name + '"><a href="http://www.twitter.com/' + tweet[i].user.screen_name + '">@' + tweet[i].user.screen_name + '</a></div></li>');
        } 
    });
}

function doCarousels()
{
    $(".carousel").jCarouselLite({
        btnNext: ".forward",
        btnPrev: ".back",
        visible:5
    });
}
