WS = {};

WS.Purchase = {
    TASTY_SINGLE: {
        id: 277597,
        price: 29
    },
    
    TASTY_UNLIMITED: {
        id: 277598,
        price: 49
    },
    
    get_ejunkie: function(type) {
        var type = type.toUpperCase();
        return WS.Purchase["TASTY_"+type];
    },
    
    get_paypal_link: function(type) {
        var purchase = this,
            ejunkie = purchase.get_ejunkie(type);
        return '<a class="btn gateway" href="https://www.e-junkie.com/ecom/gb.php?i='+ejunkie.id+'&c=single&cl=2342" target="_blank"><span>Pay $'+ejunkie.price+' with PayPal</span></a>'
    },
    
    get_google_link: function(type) {
        var purchase = this,
            ejunkie = purchase.get_ejunkie(type);
        return '<a class="btn gateway" href="https://www.e-junkie.com/ecom/gb.php?i='+ejunkie.id+'&c=gc&cl=2342&ejc=4" target="_blank"><span>Pay $'+ejunkie.price+' with Google Checkout</span></a>'
    },
    
    init: function() {
        var purchase = this,
            chooseDialog = [
            '<div class="chooseVariant">',
                '<p>Select the version of Tasty you would like to purchase:<br/>',
                '<strong>$5 will be donated to Unicef for every purchase!</strong></p>',
                '<ul class="variantChoices">',
                    '<li class="top"><label>',
                        '<input type="radio" name="themeChoice" value="single" checked="checked" />',
                        '<span class="name">Single &mdash; $'+this.TASTY_SINGLE.price+'</span>',
                        '<span class="description">Purchase the single license to install Tasty on one blog.</span>',
                    '</label></li>',
                    '<li><label>',
                        '<input type="radio" name="themeChoice" value="unlimited" />',
                        '<span class="name">Unlimited &mdash; $'+this.TASTY_UNLIMITED.price+'</span>',
                        '<span class="description">Purchase the unlimited license to install Tasty on as many blogs as you\'d like.</span>',
                    '</label></li>',
                '</ul>',
            '</div>'
            ];
        
        $(chooseDialog.join('')).dialog({
            modal: true,
            title: 'Choose Single or Unlimited',
            width: 500,
            height: 300,
            buttons: {
                "Continue": function() {
                    // first step, calculate the chosen theme
                    var theme = $('.chooseVariant').find('input:checked').val();
                    purchase.themeChosen(theme, this);
                }
            }
        });
        
    },
    
    themeChosen: function(theme, oldDialog) {
        var purchase = this,
            payDialog = [
                '<div class="payDialog">',
                    '<p><strong>Simply choose one of the methods below to pay for your '+theme+' copy of Tasty.</strong></p>',
                    '<div class="buttons top bottom center">'];
            
            // Add google and paypal buttons
            payDialog.push(purchase.get_paypal_link(theme));
            payDialog.push(purchase.get_google_link(theme));
            
            payDialog.push('</div><p>You\'ll receive a download link immediately after the payment has been made.</p>');
            payDialog.push('</div>');
            
            $(oldDialog).dialog('destroy');

            $(payDialog.join('')).dialog({
                modal: true,
                title: 'Choose your Payment Method',
                width: 500,
                height: 200,
                focus: function() {
                    var _dialog = $(this);
                    $('a.gateway').bind('click', function(event) {
                        _dialog.dialog('destroy');
                        // purchase.toGateway();
                    });
                }
            });
    }
}