// define all the cakes
// type 1 = cake, 0 = cupcake
// availability 1 = all, 0 = selected stores
// images are cake name lowercased with underscores (ex: Classic Elegance = classic_elegance.jpg & classic_elegance.thumb.jpg)
var cakes = [
    { 'name': 'La Petite Belle',      'type': 1, 'totalservings': 115, 'topservings': 16, 'price': 287.50, 'availability': 0 },
    { 'name': 'Love Lines',      'type': 1, 'totalservings': 163, 'topservings': 16, 'price': 407.50, 'availability': 0 },
    { 'name': 'Epiphany',      'type': 1, 'totalservings': 154, 'topservings': 16, 'price': 385.00, 'availability': 0 },
    { 'name': 'Daydream',      'type': 1, 'totalservings': 64, 'topservings': 8, 'price': 160, 'availability': 0 },
    { 'name': 'Simplicity',     'type': 1, 'totalservings': 140, 'topservings': 18, 'price': 350.00, 'availability': 0 },
    { 'name': 'Wrapped in Elegance',      'type': 1, 'totalservings': 265, 'topservings': 29, 'price': 662.50, 'availability': 0 },
    { 'name': 'Large Cupcake Cake with Cutting Cake',    'type': 0, 'totalservings': 125,                    'price': 2.00,   'availability': 0 },
    { 'name': 'Purple n White cups',    'type': 0, 'totalservings': 125,                    'price': 2.00,   'availability': 0 },
    { 'name': 'Cloud Nine',      'type': 1, 'totalservings': 112, 'topservings': 8, 'price': 280, 'availability': 0 },
    { 'name': 'Kashmir',      'type': 1, 'totalservings': 99, 'topservings': 27, 'price': 362.50, 'availability': 0 },
    { 'name': 'Desire',      'type': 1, 'totalservings': 53, 'topservings': 8, 'price': 132.50, 'availability': 0 },
    { 'name': 'Classic Elegance',      'type': 1, 'totalservings': 217, 'topservings': 29, 'price': 499.99, 'availability': 1 },
    { 'name': 'New Traditions',        'type': 1, 'totalservings': 110, 'topservings': 18, 'price': 259.99, 'availability': 1 },
    { 'name': 'Forever Fantasy',       'type': 1, 'totalservings': 167, 'topservings': 29, 'price': 519.99, 'availability': 1 },
    { 'name': 'Enchanted Romance',     'type': 1, 'totalservings': 154, 'topservings': 16, 'price': 469.99, 'availability': 1 },
    { 'name': 'Silk and Satin',        'type': 1, 'totalservings': 115, 'topservings': 16, 'price': 349.99, 'availability': 1 },
    { 'name': 'Chantilly Lace 2 Tier', 'type': 1, 'totalservings': 68,  'topservings': 18, 'price': 219.99, 'availability': 1 },
    { 'name': 'Lilies and Lace',       'type': 1, 'totalservings': 128, 'topservings': 18, 'price': 389.99, 'availability': 1 },
    { 'name': 'Antique Lace',          'type': 1, 'totalservings': 68,  'topservings': 18, 'price': 159.99, 'availability': 1 },
    { 'name': 'Floral Cascade',        'type': 1, 'totalservings': 217, 'topservings': 29, 'price': 669.99, 'availability': 1 },
    { 'name': 'Festive Gifts',         'type': 1, 'totalservings': 126, 'topservings': 18, 'price': 299.99, 'availability': 1 },
    { 'name': 'Forever Yours',         'type': 1, 'totalservings': 99,  'topservings': 29, 'price': 229.99, 'availability': 1 },
    { 'name': 'Perfectly Pink',        'type': 1, 'totalservings': 110, 'topservings': 18, 'price': 259.99, 'availability': 1 },
    { 'name': 'Radiant Roses',         'type': 1, 'totalservings': 183, 'topservings': 16, 'price': 559.99, 'availability': 1 },
    { 'name': 'Showers Of Gifts',      'type': 1, 'totalservings': 126, 'topservings': 18, 'price': 299.99, 'availability': 1 },
    { 'name': 'Traditional Elegance',  'type': 1, 'totalservings': 140, 'topservings': 18, 'price': 350,    'availability': 0 },
    { 'name': 'Large Cupcake Cake',    'type': 0, 'totalservings': 125,                    'price': 2.00,   'availability': 0 },
    { 'name': 'Summer Radiance',       'type': 1, 'totalservings': 147, 'topservings': 29, 'price': 367.50,    'availability': 0 },
    { 'name': 'Chocolate Decadent',    'type': 1, 'totalservings': 64,  'topservings': 16, 'price': 160,    'availability': 0 },
    { 'name': 'Small Cupcake Cake',    'type': 0, 'totalservings': 75,                     'price': 2.00,   'availability': 0 },
    { 'name': 'Small Cupcake 2',   'type': 0, 'totalservings': 75,                     'price': 2.00,   'availability': 0 },
    { 'name': 'Small Cupcake with Cutting Cake',    'type': 0, 'totalservings': 75,                     'price': 2.00,   'availability': 0 },
    { 'name': 'Cascade of Lilies',     'type': 1, 'totalservings': 172, 'topservings': 18, 'price': 430,    'availability': 0 },
    { 'name': 'Garden Basket',         'type': 1, 'totalservings': 252, 'topservings': 16, 'price': 630,    'availability': 0 },
    { 'name': 'Forever Roses',         'type': 1, 'totalservings': 147, 'topservings': 29, 'price': 367.50,    'availability': 0 },
    { 'name': 'Ribbon and Pearls',     'type': 1, 'totalservings': 163, 'topservings': 16, 'price': 407.50, 'availability': 0 },
    { 'name': 'Traditional Roses',     'type': 1, 'totalservings': 281, 'topservings': 16, 'price': 702.50, 'availability': 0 },
    { 'name': 'Natural Garden',        'type': 1, 'totalservings': 217, 'topservings': 29, 'price': 542.50,    'availability': 0 },
    { 'name': 'Garden Fantasy',        'type': 1, 'totalservings': 154, 'topservings': 16, 'price': 385,    'availability': 0 },
    { 'name': 'Simply Orchid',         'type': 1, 'totalservings': 99,  'topservings': 29, 'price': 247.5,  'availability': 0 },
    { 'name': 'Magic Moments',         'type': 1, 'totalservings': 183, 'topservings': 16, 'price': 457.50, 'availability': 0 },
    { 'name': 'Simply Irresistible',   'type': 1, 'totalservings': 217, 'topservings': 29, 'price': 542.50, 'availability': 0 },
    { 'name': 'Ribbon of Dreams',      'type': 1, 'totalservings': 163, 'topservings': 16, 'price': 407.50, 'availability': 0 },
    { 'name': 'Secret Passion',        'type': 1, 'totalservings': 108, 'topservings': 8,  'price': 270.00, 'availability': 0 },
    { 'name': 'Fountain of Youth',     'type': 1, 'totalservings': 415, 'topservings': 29, 'price': 1037.50,'availability': 0 }
];

$(document).ready( function() {

    $('.print').click(function(){window.print();return(false);});

    // Populate selector box with available cake thumbnails
    var thumbnails = '';
    var maxcakes = cakes.length;
    for (x = 0; x < maxcakes; x++) {
        var cakename = cakes[x].name.replace(/\s/g, '_').toLowerCase();
        thumbnails += '<li><a href="#"><span><img src="../../safeway-weddings/images/cakes/' + cakename + '.thumb.jpg" alt="' + cakes[x].name + '" /></span></a></li>\n';
    }
    $("ul.grid").append(thumbnails);

    // Attach slider collapse/expand behavior
    $("ul.grid li").click(
        function() {
            var selectedthumb = $("ul.grid li.active");
            if (this != selectedthumb) {
    
                // highlite selected thumbnail
                $(selectedthumb).removeClass('active');
                $(this).addClass('active');

                // which cake # is selected?  now load that cake.
                selectedcake = $("ul.grid li").index(this)
                showCake(selectedcake);
            }
            return(false);
        }
    );
    // select the default cake thumbnail
    $("ul.grid li:first").click();
    
    // attach layer check and guests input behavior
    $('#cake-keep-layer').click( function() { calculateCake(selectedcake); } );
    $('#cake-guests').keyup( function(e) { filterNumeric(this); calculateCake(selectedcake); } );
    
    // prevent non-numerics from being entered in numeric fields
    $(".numeric").keydown( function (e) { return(isNumeric(e.which) || isCommand(e.which)); } );

});

// update cake image, then refresh price totals
function showCake(index) {
    var cakename = cakes[index].name.replace(/\s/g, '_').toLowerCase();     
    $("#cake-selected").css("background-image", "url('../../safeway-weddings/images/cakes/" + cakename + ".jpg')");
    calculateCake(index);
}

// refresh price totals, taking user input into account
function calculateCake(index) {

    // how many guests did the user specify
    var guests = parseInt($('#cake-guests').val());
    guests = guests ? guests : 0;

    // now add up the total cost
    var totalprice = 0;
    var cake = 0, cakeprice = 0;
    var fullsheets = 0, fullsheetprice = 0;
    var halfsheets = 0, halfsheetprice = 0;
    var quartersheets = 0, quartersheetprice = 0;
    if (guests > 0) {
    
        // for cupcakes
        if (cakes[index].type == 0) {
        
            // price is guests * price per serving
            totalprice = guests * cakes[index].price;
        }
        // for cakes
        else {

            // how many guests does the main cake feed?
            var servings = 0;
            if ($('#cake-keep-layer').attr('checked')) { servings = cakes[index].totalservings - cakes[index].topservings; }
            else { servings = cakes[index].totalservings; }

            // start with the cake, and subtract those servings from the total guests
            cake = 1; guests -= servings;
            // then add sheets until no guests remain
            while (guests > 0) {
                if (guests > 48) { fullsheets++; guests -= 96; }
                else if (guests > 24) { halfsheets++; guests -= 48; }
                else if (guests > 0) { quartersheets++; guests -= 24; }
            }
            
            // calculate the total of cake + sheets
            cakeprice = cakes[index].price;
            fullsheetprice = (fullsheets * 54.99);
            halfsheetprice = (halfsheets * 26.99);
            quartersheetprice = (quartersheets * 21.99);
            totalprice = cakeprice + fullsheetprice + halfsheetprice + quartersheetprice;
        }
        
    }    
        
    // now update html with totals and show/hide some areas
    if (cakes[index].type == 1) { $('#cake-note').show(); $('#field-keep-layer').show(); }
    else { $('#cake-note').hide(); $('#field-keep-layer').hide(); }
    $('#cake-serves').html(servings);
    if (cakeprice) { $('#estimate-cake').show(); } else { $('#estimate-cake').hide(); }
    $('#price-cake').html(formatPrice(cakeprice));
    if (fullsheets) { $('#estimate-fullsheets').show(); } else { $('#estimate-fullsheets').hide(); }
    $('#price-fullsheets').html(formatPrice(fullsheetprice));
    $('#count-fullsheets').html(fullsheets + ' Full Sheet' + ((fullsheets != 1) ? 's' : ''));
    if (halfsheets) { $('#estimate-halfsheets').show(); } else { $('#estimate-halfsheets').hide(); }
    $('#price-halfsheets').html(formatPrice(halfsheetprice));
    if (quartersheets) { $('#estimate-quartersheets').show(); } else { $('#estimate-quartersheets').hide(); }
    $('#price-quartersheets').html(formatPrice(quartersheetprice));
    $('#cake-availability').html(cakes[index].availability ? 'All Stores' : 'Only in Washington, Idaho, Alaska, and Northern Montana.');
    $('#cake-subtotal').html(formatPrice(totalprice));
    $('#total').html(formatPrice(totalprice));
}
