

Shadowbox.loadSkin('skin', '/');



$(document).ready(function(){

    setupShadowbox();

    addInputEventListener();

    validateAndSubmitForm();

});





/*

 * Supporting Functions

 */



function setupShadowbox() {

    // Shadowbox options used for both cases

    var options = "animSequence:\'sync\',overlayOpacity:0.9";



    $('.product a').each(function(){

        // Make image previews with an inside view part of a series

        if ($(this).parent().hasClass('inside')) {

            $(this).attr('rel', 'shadowbox[' + $(this).attr('title') + '];options={' + options + ',counterType:\'skip\',continuous:true}');

        } else {

            $(this).attr('rel', 'shadowbox;options={' + options + '}');

        }

    });



    Shadowbox.init();

}



function addInputEventListener() {

    // Update cart display when item quantities are changed

    $('input[type=text]').keyup(function(){

        $('#cart p span').text("You have selected " + getTotalCards() + " cards");

    });

}



function getTotalCards() {

    var cards = 0;

    $('input[type=text]').each(function(){

        cards += parseInt($(this).val(), 10);

    });

    return cards;

}



function validateAndSubmitForm() {

    $('form').submit(function(e){

        var totalCards = getTotalCards();



        // Ensure total is multiple of 12

        if (totalCards == 0 || totalCards % 12) {

            alert("Your total number of selected cards must be a multiple of 12.");

            return false;

        } else {

            // Set shopping cart form to include only the number of boxes

            // of cards and any necessary Paypal stuff.

            $('form').html(

                '<input type="image" name="submit" src="img/btn_checkout.png" />' +

                '<input type="hidden" name="cmd" value="_cart"/>' +

                '<input type="hidden" name="upload" value="1"/>' +

                '<input type="hidden" name="business" value="orders@crowelladv.com"/>' +

                '<input type="hidden" name="currency_code" value="USD"/>' +

                '<input type="hidden" name="item_name_1" value="Box of twelve awesome cards"/>' +

                '<input type="hidden" name="quantity_1" value="' + totalCards/12 + '"/>' +

                '<input type="hidden" name="amount_1" value="20.00"/>' +

                '<input type="hidden" name="shipping" value="6.95"/>'

            );



            // Add quantities of individual cards to shopping cart form

            var i = 2;  // Box is item_1, individual cards start with item_2

            $('.product').each(function(){

                var quantity = parseInt($(this).children('input').val(), 10);

                if (quantity) {

                    $('form').append('<input type="hidden" name="item_name_' + i + '" value="' + $(this).children('a').attr('title') + '"/>');

                    $('form').append('<input type="hidden" name="quantity_' + i + '" value="' + quantity + '"/>');

                    $('form').append('<input type="hidden" name="amount_' + i + '" value="0.00"/>');

                    i++;

                }

            });



        }

    });

}

