var quantity = 1;

var fi_protie_cookbook = {
    'cancel': 'Peruuta',
    'remove': 'Poista'
}

function _(string)
{
    if (fi_protie_cookbook[string])
    {
        return fi_protie_cookbook[string];
    }
    
    return string;
}

jQuery(document).ready(function()
{
    jQuery('#recipe_quantities').set_quantity_magick();
    
    jQuery('#recipe_quantities tr').hover(
        function()
        {
            jQuery(this).show_tools();
        },
        function()
        {
            jQuery(this).hide_tools();
        }
    );
    
    jQuery('#recipe_quantities tr td.editable input')
        .focus(
            function()
            {
                jQuery(this).parents('tr').show_tools();
            }
        )
        .blur(
            function()
            {
                jQuery(this).parents('tr').hide_tools();
            }
        );
    
    jQuery('#recipe_quantities tr td.heading input[type="checkbox"]')
        .change(function()
        {
            if (jQuery(this).attr('checked'))
            {
                jQuery(this).parents('tr').find('td.quantity, td.unit').css('display', 'none');
                jQuery(this).parents('tr').find('td.raw_material')
                    .addClass('long')
                    .attr('colspan', 3);
            }
            else
            {
                jQuery(this).parents('tr').find('td.quantity, td.unit').css('display', '');
                jQuery(this).parents('tr').find('td.raw_material')
                    .removeClass('long')
                    .attr('colspan', 1);
            }
        });
    
    jQuery('#shopping_list_toolbar form').submit(function()
    {
        var quantity = Number(jQuery('#quantity').attr('value'));
        var rel = Number(jQuery('#quantity').attr('rel'));
        
        var factor = quantity / rel;
        
        jQuery('#column_1 p').each(function(i)
        {
            var id = jQuery(this).attr('id');
            
            if ((regs = id.match(/ingredient_([0-9]+)$/)))
            {
                var index = regs[1];
                var count = Number(jQuery(this).find('span.quantity').html());
            }
            else
            {
                return;
            }
            
            var value = Math.ceil(count * factor);
            jQuery('#shopping_list_row_' + index).find('span.quantity').html(value);
        });
        
        var href = jQuery('#print_shopping_list').attr('href');
        
        if (!href)
        {
            return false;
        }
        
        if (href.match(/q=[0-9]+/))
        {
            href = href.replace(/q=[0-9]+/, 'q=' + quantity);
        }
        else
        {
            if (href.match(/\?/))
            {
                href += '&';
            }
            else
            {
                href += '?';
            }
            
            href += 'q=' + quantity;
        }
        
        jQuery('#print_shopping_list').attr('href', href);
        
        return false;
    });
    
    jQuery('body.autoprint').each(function()
    {
        window.print();
    });
});

jQuery.fn.set_quantity_magick = function()
{
    jQuery('td.remove_row input[type="checkbox"]').css('display', 'none');
    
    jQuery('td.remove_row').click(
        function()
        {
            if (jQuery(this).hasClass('clicked'))
            {
                return true;
            }
            
            jQuery(this).addClass('clicked');
            jQuery(this).oneTime(100, function()
            {
                jQuery(this).removeClass('clicked');
            });
            
            if (jQuery(this).find('input[type="checkbox"]').attr('checked'))
            {
                jQuery(this).find('input[type="checkbox"]').attr('checked', '');
                jQuery(this).find('span')
                    .html(_('remove') + '');

                jQuery(this).parent()
                    .addClass('row')
                
                jQuery(this).parent().find('td.editable input')
                    .attr('readonly', '')
                    .fadeTo(500, 1);
                
                jQuery(this).find('span').stopTime();
            }
            else
            {
                jQuery(this).find('input[type="checkbox"]').attr('checked', 'checked');
                jQuery(this).find('span')
                    .html(_('cancel'));
                
                jQuery(this).parent()
                    .removeClass('row')
                
                jQuery(this).parents('tr').find('td.editable input')
                    .attr('readonly', 'readonly')
                    .fadeTo(500, 0.1);
                
                
                jQuery(this).find('span').oneTime(50, function()
                {
                    jQuery(this).html(_('cancel') + ' (5)');
                });
                
                jQuery(this).find('span').everyTime(
                    1000,
                    function(times)
                    {
                        var i = 5 - times;
                        
                        if (i == -1)
                        {
                            jQuery(this).stopTime('undo');
                            jQuery(this).parents('tr')
                                .css('display', 'none');
                            return;
                        }
                        
                        jQuery(this).html(_('cancel') + ' (' + i + ')');
                    }
                );
            }
            
            return true;
        }
    );
    
    // Hide the submit button
    jQuery(this).find('.add_row input').remove();
    
    jQuery('<span></span>')
        .html(_('add row') + ' »')
        .click(function()
        {
            // Clone the last table body row and set new values
            var index = jQuery(this).parents('table').find('tbody tr').size() + 1;
            var new_row = jQuery(this).parents('table').find('tbody tr.row:last')
                .clone(true)
                .appendTo(jQuery(this).parents('table').find('tbody'));
            
            new_row.find('td.editable input').each(function()
            {
                var name = jQuery(this).attr('name');
                
                name = name.replace(/row\[.+?\]/, 'row[' + index + ']');
                
                jQuery(this).attr({
                    value: '',
                    name: name
                });
            });
            
            new_row.find('input[type="hidden"]').attr('value', index);
        })
        .appendTo(jQuery(this).find('.add_row'));
}

jQuery.fn.show_tools = function()
{
    jQuery(this).addClass('hover');
    
    if (jQuery(this).find('div.toolbar').size() == 0)
    {
        jQuery('<div></div>')
            .addClass('toolbar')
            .prependTo(jQuery(this).find('td:first'));
        
        jQuery('<img />')
            .attr({
                alt: 'up',
                src: MIDCOM_STATIC_URL + '/stock-icons/16x16/up.png'
            })
            .addClass('up')
            .click(function()
            {
                jQuery(this).parents('tr').each(function(i)
                {
                    if (jQuery(this).prev('tr').size() == 0)
                    {
                        return;
                    }
                
                    jQuery(this).hide_tools();
                    jQuery(this).insertBefore(jQuery(this).prev('tr'));
                });
            })
            .appendTo(jQuery(this).find('div.toolbar'));
        
        jQuery('<img />')
            .attr({
                alt: 'down',
                src: MIDCOM_STATIC_URL + '/stock-icons/16x16/down.png'
            })
            .addClass('down')
            .click(function()
            {
                jQuery(this).parents('tr').each(function(i)
                {
                    if (jQuery(this).next('tr').size() == 0)
                    {
                        return;
                    }
                
                    jQuery(this).hide_tools();
                    jQuery(this).insertAfter(jQuery(this).next('tr'));
                });
            })
            .appendTo(jQuery(this).find('div.toolbar'));
    }
    
    if (!jQuery(this).find('div.toolbar').hasClass('visible'))
    {
        jQuery(this).find('div.toolbar').fadeIn(0, function()
        {
            jQuery(this).addClass('visible');
        });
    }
}

jQuery.fn.hide_tools = function()
{
    jQuery(this).find('div.toolbar').oneTime(1000, function()
    {
        if (jQuery(this).parents('tr').hasClass('hover'))
        {
            return;
        }
        
        jQuery(this)
            .removeClass('visible')
            .fadeOut(500);
        
        jQuery(this).parents('tr.hover').removeClass('hover');
    });
    
    jQuery(this).removeClass('hover');
}


