/*
* Dandelion Admin v1.0 - Wizard Form JS
*
* This file is part of Dandelion Admin, an Admin template build for sale at ThemeForest.
* For questions, suggestions or support request, please mail me at maimairel@yahoo.com
*
* Development Started:
* March 25, 2012
*
* 'Highly configurable' mutable plugin boilerplate
* Author: @markdalgleish
* Further changes, comments: @addyosmani
* Licensed under the MIT license
*
*/

; (function (d, c, b, e) 
{ 
var a = function (g, f) 
    { 
        this.$elem = d(g); 
        this.options = f }; 
        a.prototype = 
            {   defaults: 
                    { 
                        element: "fieldset", 
                        labelElement: "legend", 
                        buttonContainerClass: "da-button-row", 
                        nextButtonClass: "da-button red", 
                        prevButtonClass: "da-button gray left", 
                        submitButtonClass: "da-button green", 
                        nextButtonLabel: "Next", 
                        prevButtonLabel: "Prev", 
                        submitButtonLabel: "Submit", 
                        forwardOnly: true, 
                        onLeaveStep: null, 
                        onShowStep: null, 
                        onBeforeSubmit: null 
                    }, 
             init: function ()
             { 
             this.config = d.extend({}, this.defaults, this.options); 
             this.steps = this.$elem.children(this.config.element); 
             this.$elem.addClass("da-wizard-form"); 
             this.data = d.extend({}, this._buildNavigation(), this._buildButtons()); 
             this.initSteps(); return this }, initSteps: function () { this.steps.hide().first().show(); this.wizardNav.find(".da-wizard-progress").animate({ width: Math.floor(100 / this.steps.size()).toString() + "%" }, "slow").end().find("ul").children("li").first().addClass("active"); this.data.activeStep = 0; this._processSteps() }, goTo: function (f) { if (f !== this.data.activeStep) { if (this.config.forwardOnly && f < this.data.activeStep) { return } var g = this; if (this.config.onLeaveStep && d.isFunction(this.config.onLeaveStep)) { if (false === this.config.onLeaveStep.apply(this, [this.data.activeStep, this.steps.eq(this.data.activeStep)])) { return } } this.steps.eq(this.data.activeStep).data("done", true).fadeOut("fast", function () { g.steps.eq(f).fadeIn("fast"); g.wizardNav.find(".da-wizard-progress").animate({ width: Math.floor((100 / g.steps.size()) * (f + 1)).toString() + "%" }, "slow").end().find("ul").children("li").removeClass("active").eq(g.data.activeStep).addClass("done").end().eq(f).addClass("active"); g.data.activeStep = f; g._processSteps(); if (g.config.onShowStep && d.isFunction(g.config.onShowStep)) { g.config.onShowStep.apply(g, [f, g.steps.eq(f)]) } }) } }, goBackward: function () { if (this.data.activeStep > 0) { this.goTo(this.data.activeStep - 1) } }, goForward: function () { if (this.data.activeStep < this.steps.size() - 1) { this.goTo(this.data.activeStep + 1) } }, submitForm: function () { var f = true; if (this.config.onBeforeSubmit && d.isFunction(this.config.onBeforeSubmit)) { f = this.config.onBeforeSubmit.apply(this, []) } if (false !== f) { this.$elem.submit() } }, _navigate: function (f) { if (this._isStepDone(f)) { this.goTo(f) } }, _isStepDone: function (f) { return (typeof (this.steps.eq(f).data("done")) !== "undefined") }, _buildNavigation: function () { var g = this, h = d("<ul></ul>"), j = d("<li></li>").css({ width: Math.floor(100 / this.steps.size()).toString() + "%" }), f = d("<span></span>"), i = d('<a href="#"></a>'); this.steps.each(function (k, l) { val = d(g.config.labelElement, l); val.hide(); h.append(j.clone().append(i.clone().text(k + 1).bind("click", function (m) { g._navigate(k); m.preventDefault() })).append(f.clone().addClass("da-wizard-label").text(val.length != 0 ? val.text() : ("Step " + (k + 1).toString())))) }); this.wizardNav = d("<div></div>").addClass("da-wizard-nav").append(h).append(f.clone().addClass("da-wizard-progress")).insertBefore(this.$elem); return { activeStep: -1} }, _buildButtons: function () { var f = d(this.config.buttonContainer, this.$elem), h = this, j = d("<input />").attr("type", "button"), g = j.clone().val(this.config.prevButtonLabel).addClass(this.config.prevButtonClass).bind("click", function (l) { if (!h.config.forwardOnly) { h.goBackward() } l.preventDefault() }), k = j.clone().val(this.config.nextButtonLabel).addClass(this.config.nextButtonClass).bind("click", function (l) { h.goForward(); l.preventDefault() }), i = j.clone().val(this.config.submitButtonLabel).addClass(this.config.submitButtonClass).bind("click", function (l) { h.submitForm(); l.preventDefault() }); if (!f.get(0)) { f = d("<div></div>").addClass(this.config.buttonContainerClass).appendTo(this.$elem) } if (!h.config.forwardOnly) { f.append(g) } f.append(k).append(i); return { nextButton: k, prevButton: g, submitButton: i} }, _processSteps: function () { this.data.prevButton.toggle((this.data.activeStep > 0) && !this.data.forwardOnly); this.data.nextButton.toggle((this.data.activeStep < this.steps.size() - 1)); this.data.submitButton.toggle((this.data.activeStep >= this.steps.size() - 1)) } }; a.defaults = a.prototype.defaults; d.fn.daWizard = function (f) { return this.each(function () { new a(this, f).init() }) } })(jQuery, window, document);