var fb_share = false;
var twitter_share = false;

$(document).ready(function () {
    $('#social-media-savings').jqm();
    
    $('#twitter-share').click(function() {
        // Update the person's Twitter status.
        post_data = $('.twitter-field').serialize();
        jQuery.post(twitter_url, post_data, update_twitter_status, 'json');
    });
    
    $('a[name=fb_share]').click(function() {
        // Create an entry that says somebody shared it. We'll
        // actually check it on the next page.
        url = $(this).attr('share_url');
        jQuery.post(fb_url, 'url='+url, update_fb_status, 'json');
    });

    $('#id_tweet_preview').click(function() {
        $('#id_tweet').slideToggle();
    });
});

function set_action(value) {
    
    // If they enter a promo code but don't hit update cart, remind them to.
    if (value == 'pay' && $('#id_promo').val() != '' && !has_promo) {
        jAlert('Looks like you entered a promo code. Remember to hit "Update Cart" to get your discount.', 'Oops!');
        return;
    }

    $('#action').attr('value', value);
    if (sharing && value == 'pay') {
        // If they haven't selected a promo previously, allow them to share
        // socially to claim a discount.
        $('#social-media-savings').jqmShow();
    }
    else {
        $('#id_checkout_form').submit();             
    }
}

function update_twitter_status(data, text) {
    $('span.warning').remove();
    if (data.status == 'success') {
        $('#twitter-complete').html('Thanks! You just saved 5% off your order');
        twitter_share = true;
    }
    else {
        $('#twitter-complete').html('Your username / password didn\'t validate.')
    }
}

function update_fb_status(data, text) {
    $('#fb-complete').html('Thanks!');
    fb_share = true;
}

function disable_share() {
    // The user has opted out of having the social share promo displayed.
    $.cookie('opt_out_share', 1);
    $('#social-media-savings').jqmHide();
    $('#id_checkout_form').submit();             
}

function verify_share() {
    if (twitter_share || fb_share) {
        document.location = verify_url;
    } 
    else {
        disable_share();
    }
}

function show_offer() {
    $('#initial-buttons').hide();
    $('.offer').slideToggle();
}
