DawnWizard

DawnWizard v1.0 Originally made for Dawn's question in StackOverflow http://stackoverflow.com/questions/6378814/ Made by Kalle H. Väravas http://stackoverflow.com/users/721553/ No direct copyright or licences. Use however you want, just give me lots of +1 :)

by hobobne

HTML

<div id="put_wizard_here" />

CSS

body {font-family: Arial, Verdana; font-size: 12px;}
h1 {font-size:18px;}
p {margin: 10px 0px;}
#question {display: block; margin-bottom: 5px;}
#message {display: none; margin: 5px 0px 0px; font-weight: bold;}
#message.error {color: red;}
#message.success {color: green;}

JavaScript

(function ($) {

    // This is additional core-function to make it more easier
    $.fn.exists = function () {return $(this).length > 0;}

    // The actual plugin start
    $.fn.DawnWizard = function (input_setup) {

        // Default setup array
        var default_setup = {
            title: 'Demo wizard',
            description: 'Welcome text..'
        };

        // Merges input_setup with default_setup
        setup = jQuery.extend(default_setup, input_setup || {});

        // Lets set $(this), because there will be other $(this)'es
        var wizard_container = $(this);

        // Lets set start for the questions count
        var questions_count = 0;

        // Count the questions, in this case you cant use .size() ;(
        jQuery.each(setup['questions'], function () {
            questions_count++;
        });

        // This function resets the results and displays the start page
        load_startup = function () {
            
            // Unset the results
            results = [];
            
            // Display the startup text
            wizard_container.empty().append(
                $('<h1>').html(setup['wizard']['title']),
                content_container = $('<div id="content">').append(
                    $('<p>').html(setup['wizard']['description']),
                    $('<button id="start_wizard" />').text('Start the Wizard')
                )
            );

            // Check for start-event
            $('#start_wizard').click(function () {
                load_question(current_qid = 1);
            });
        };
        
        // Display results function
        load_results = function () {
            
            // Generate a wrapping container for the results, also clears the old content
            content_container.empty().append(results_container = $('<p>'));
            
            // Loops the question and gets the results
            jQuery.each(setup['questions'], function (i, question) {
        ...