﻿/*
 * @author stuartb
 * @date 2008.7.24
 * @description Wizard forms made easy.
 */
jQuery.fn.wizard = function(settings)
{

    settings = jQuery.extend({
         show: function(element) { return true; }
      }, settings);

    // Hide all pages save the first.
    $(this).children(".wizardpage").hide();
    $(this).children(".wizardpage:first").show();
    settings.show($(this).children(".wizardpage:first"));
    
    // Also highlight the first nav item.
    $(this).children(".wizard-nav").children("a:first").addClass("active");
    
    // Wire progress thingy
    $(this).children(".wizard-nav").children("a").click(function(){
        var target = $(this).attr("href");
        $(this).parent().parent().children(".wizardpage").hide();
        $(target).fadeIn('slow');
        settings.show($(target));
        $(this).parent().children('a').removeClass('active', 'slow');
        $(this).addClass('active', 'slow');
        return false;
    });
    
    // Add prev/next step buttons
    $(this).children(".wizardpage")
    .append('<div class="row wizardcontrols"></div>')
    .children(".wizardcontrols")
        .append('<input type="button" class="wizardprev" value="< Back" /><input type="button" class="wizardnext" value="Next >" />');
    $('.wizardpage:first input[type="button"].wizardprev').hide(); // hide prev button on first page
    $('.wizardpage:last input[type="button"].wizardnext').hide();  // hide next button on last page
    
    // Wire prev/next step buttons
    $(this).children(".wizardpage")
    .children(".wizardcontrols")
    .children('input[type="button"].wizardprev').click(function(){
        var wizardpage = $(this).parent().parent(); // wizardcontrols div, wizardpage div
        var wizardnav  = wizardpage.parent().children(".wizard-nav")
        
        wizardpage.hide();
        wizardpage.prev().fadeIn();
        settings.show(wizardpage.prev());
        
        wizardnav.children('a').removeClass('active', 'slow');
        wizardnav.children('a[href="#' + wizardpage.attr('id') + '"]').prev().addClass('active', 'slow');
    });
    $(this).children(".wizardpage")
    .children(".wizardcontrols")
    .children('input[type="button"].wizardnext').click(function(){
        var wizardpage = $(this).parent().parent(); // wizardcontrols div, wizardpage div
        var wizardnav  = wizardpage.parent().children(".wizard-nav")
        
        wizardpage.hide();
        wizardpage.next().fadeIn();
        settings.show(wizardpage.next());
        
        wizardnav.children('a').removeClass('active', 'slow');
        wizardnav.children('a[href="#' + wizardpage.attr('id') + '"]').next().addClass('active', 'slow');
    });
};