

$(document).ready(function() 
{
    var base = $('base').attr('href');
    
    /*
        Response
    */
    function _response(response) 
    {
        $('#steps').html(response);
        $('form.ajax').ajaxize();
        // hide all edit forms
        //$('.step_form').css('display', 'none');
        $.getScript(base + 'scripts/steps.js');
    }
    
    $.fn.ajaxize = function() 
    {
        
        $(this).each(function() 
        {
            var form = this;
            var options = 
            {
                url: base + '?module=user&service=steps&action=edit&session=' + $('#session').val(), 
                dataType: 'html', 
                success: function(response) { _response(response); }
            };
            
            $(form).ajaxForm(options);
            
        })
    }
    
    $('form.ajax').ajaxize();
    
    // hide all edit forms
    $('.step_form').css('display', 'none');
    // show error form
    $('#step_form_' + $('#error_step').val()).css('display', 'block');
    
    // edit step
    $('span.edit').click(function() 
    {
        // hide all edit forms
        $('.step_form').css('display', 'none');
        // display nearest
        $(this).parent().next().next().css('display', 'block');
        
    });
    // delte step
    $('span.delete').click(function() 
    {
        $.post(
            base + '?module=user&service=steps&action=delete&session=' + $('#session').val(), 
            {step: $(this).prevAll('#step').val(), instruction: $(this).prev('#instruction').val() }, 
            function(data) { _response(data); }
        );
    });
    
});
