var nutty = {};



jQuery(document).ready(function ()

{

    var $ = jQuery;

    var base_price = $('#nutty-mix div.bag p.price strong').text();

    base_price = parseFloat(base_price.substring(1));

    

    // mouseover for ingredient images

    $('#nutty-mix div.ingredients div.tier li div.thumb a').hover(function ()

    {

        var zoomed = $(this).parent().find('img.zoomed');

        if (!zoomed.length)

        {

            zoomed = $('<img style="display:none;" class="zoomed" src="'+$(this).attr('href')+'" alt="" />');

            zoomed.appendTo($(this).parent());

        }

        zoomed.css('display', 'block');

    },

    function ()

    {

        var zoomed = $(this).parent().find('img.zoomed');

        if (zoomed.length)

        {

            zoomed.css('display', 'none');

        }

    });

    

    var removeIngredient = function (e)

    {

        e.preventDefault();

        var item_p = $(this).parents('p').slice(0,1);

        // selection-x ("selection-" is 10 characters)

        var ing_num = item_p.attr('class').substring(10);

        $("#nutty-mix .ingredients li select[name='ings\["+ing_num+"\]']").val('0').change();

    };

    

    var disableOption = function (opt)

    {

        var raw = opt.get(0);

        if (!raw._select)

        {

            raw._select = opt.parent();

        }

        opt.hide().appendTo(opt.parents('div').slice(0,1));

        //opt.attr('disabled', 'disabled');

    };

    

    var enableOption = function (opt)

    {

        if (opt.is(':visible'))

        {

            return;

        }

        var raw = opt.get(0);

        if (!raw._select)

        {

            alert('can\'t enable option');

        }

        opt.appendTo(raw._select).show();

        //opt.removeAttr('disabled');

    };

    

    var parseOz = function (str)

    {

        return parseInt(str);

    };

    

    var parsePrice = function (str)

    {

        return parseFloat(str.substr(6));

    };

    

    $('#nutty-mix form').submit(function (e)

    {

        // make sure the bag is full

        var total = 0;

        $('#nutty-mix div.ingredients select').each(function ()

        {

            total += parseOz($(this).val());

        });

        if (total != 16)

        {

            e.preventDefault();

            $('#nutty-mix p.keep-going').show().fadeOut('fast').fadeIn('fast');

        }

    });

    

    $('#nutty-mix .ingredients li select').change(function (e)

    {

        // figure out total oz and total price

        var total = 0;

        var total_price = base_price;

        $('#nutty-mix div.ingredients select').each(function ()

        {

            var oz = parseOz($(this).val());

            total += oz;

            total_price += (oz/2)*parsePrice($(this).attr('class'));

        });

        

        // adjust total price

        $('#nutty-mix div.bag p.price strong').text('$'+total_price.toFixed(2));

        

        // adjust available options

        $('#nutty-mix div.ingredients div.select').each(function ()

        {

            var sel = $('select', $(this));

            var val = parseOz(sel.val());

            $('option', $(this)).each(function ()

            {

                var opt = parseOz($(this).val());

                if (opt <= val)

                {

                    //$(this).removeAttr('disabled');

                    enableOption($(this));

                }

                else

                {

                    var diff = opt-val;

                    if (diff+total > 16)

                    {

                        //$(this).attr('disabled', 'disabled');

                        disableOption($(this));

                    }

                    else

                    {

                       //$(this).removeAttr('disabled');

                       enableOption($(this));

                    }

                }

            });

        });

        

        // set hidden form fields

        var ings = [];

        var options_set = 0;

        var opt_fields = $('#nutty-mix div.info div.button input.choice');

        $('#nutty-mix div.ingredients select').each(function()

        {

            var sel_val = parseOz($(this).val());

            if (sel_val == 0)

            {

                return true;

            }

            var sel = $(this);

            var sel_name = sel.attr('name');

            var ing_num = sel_name.substring(5, sel_name.length-1);

            var opt_list = nutty.ing_map['ing_'+ing_num];

            var to_use = sel_val/2;

            for (var i = 0; i < to_use; i++)

            {

                $(opt_fields.get(options_set)).val(opt_list[options_set]);

                options_set++;

            }

        });

        for (var i = options_set; i < 8; i++)

        {

            $(opt_fields.get(i)).val('');

        }

        

        // update Keep going! message

        var keep_going = $('#nutty-mix p.keep-going');

        if (total >= 16)

        {

            if (keep_going.is(':visible'))

            {

                keep_going.fadeOut();

            }

        }

        else

        {

            $('span', keep_going).text((16-total)+' oz.');

            if (!keep_going.is(':visible'))

            {

                keep_going.fadeIn();

            }

        }

        

        // rebalance the bag

        var sel = $(this);

        var sel_name = sel.attr('name');

        var ing_num = sel_name.substring(5, sel_name.length-1);

        var ing_div = $('#nutty-mix div.bag div.contents .ing-'+ing_num);

        // Sets px size per oz
		
		var target_height = 20*sel.val(); // was 10

        var quantity = (target_height/20);
		// was 10

        var diff = 0;

        var added = ($('#nutty-mix div.selections div.selected p.selection-'+ing_num).length == 1) ? false : true;

        if (!ing_div.is(':visible'))

        {

            ing_div.prependTo('#nutty-mix div.bag div.contents');

            ing_div.height(0);

            ing_div.show();

        }

        var start_height = 0;

        ing_div.nextAll().each(function ()

        {

            var this_height = $(this).is(':visible') ? $(this).height() : 0;

            if (this_height > start_height)

            {

                start_height = this_height;

            }

        });

        if (added)

        {

            ing_div.height(start_height);

        }

        diff = target_height + start_height - ing_div.height();

        var removed = (target_height == 0) ? true : false;

        ing_div.prevAll().andSelf().animate(

            { height : '+='+diff+'px' },

            function ()

            {

                if (removed)

                {

                    ing_div.hide();

                }

            });

        

        // add/remove to list of ingredients or adjust amount

        var selected = $('#nutty-mix div.selections div.selected');

        if (added)

        {

            var name = sel.parents('li').slice(0,1).find('div.name').text();

            selected.find('p.regret').hide();

            selected.append('<p style="display:none;" class="selection-'+ing_num+'"><a class="remove">Remove</a> <span class="name">'+name+'</span> <span class="quantity">('+quantity+' oz)</span></p>');

            selected.find('p.selection-'+ing_num+' .remove').click(removeIngredient);

            selected.find('p.selection-'+ing_num).fadeIn();

        }

        else if (removed)

        {

            var sel_p = selected.find('p.selection-'+ing_num);

            sel_p.fadeOut(function ()

            {

                sel_p.remove();

                if (selected.find('p').length == 1)

                {

                    selected.find('p.regret').show();

                }

            });

        }

        else

        {

            selected.find('p.selection-'+ing_num+' span.quantity').text('('+quantity+' oz)');

        }

    });

});