﻿/**
*@namespace dk.marten.development
*/


/**
* Konstanter der benyttes af FormAutomation
* 
* @author Marten Ølgaard
* @created 9/9/2013
* @copyright 
* @todo 
* @class FormAutomationType
* @static
*/
var FormAutomationType = FormAutomationType || (function () {

    var _r = new Object();

    /**
    *
    * @property {String} FILLIN_AND_SUBMIT_AS_SEQUENCE
    */
    _r.FILLIN_AND_SUBMIT_AS_SEQUENCE = "sequential";
    /**
   *
   * @property {String} FILLIN_AND_SUBMIT
   */
    _r.FILLIN_AND_SUBMIT = "submit";
    /**
   *
   * @property {String} FILLIN
   */
    _r.FILLIN = "fillin";

    return _r;
})();



(function () {

    /**
    * Benyttes til at teste formularer
    * 
    * @author Marten Ølgaard
    * @created 9/9/2013
    * @copyright
    * @todo 
    * @class FormAutomation
    * @using jQuery
    * @see <a href="/Javascript/Eksempler/dk/marten/development/FormAutomation.html" target="_blank">Eksempel</a>
    * @constructor
    */
    var FormAutomation = function () {
        if (window["jQuery"] === undefined) {
            throw new Error("FormAutomation benytter sig af jQuery. jQuery er ikke tilgænglelig. Tilføj den til html-en");
            return;
        }

    }

    /**
    * De felter der skal indgå i automationen. Indeholder InputMetaData objekter eller lignende
    * @property {Array} inputFelter
    */
    FormAutomation.prototype.inputFelter = null;
    /**
    * Pause mellem hvert submit forsøg. Benyttes kun hvis runSequential er sat til true når run kaldes.
    * @property {Number} sequencePause
    */
    FormAutomation.prototype.sequencePause = 500;
    /**
    * Det javascript der benyttes når submit knappen er klikket.
    * @property {Function} submitAction
    */
    FormAutomation.prototype.submitAction = null;
    /**
    * Parametre som benyttes til det javascript der benyttes når submit knappen er klikket.
    * @property {Array} submitArguments
    */
    FormAutomation.prototype.submitArguments = null;
    /**
    * Afvikler automationen
    * @method run
    * @param {String} runSequential Angiver om værdierne i inputfelterne skal tilføjes et ad gangen med et submit imellem hver gang, for at teste flere situationer
    */
    FormAutomation.prototype.run = function (type) {
        switch (type) {
            case FormAutomationType.FILLIN_AND_SUBMIT_AS_SEQUENCE:
                this.showOverlay();
                this.stopSequence = false;
                this.next();
                break;
            case FormAutomationType.FILLIN_AND_SUBMIT:
                for (var i = 0; i < this.inputFelter.length; i++) {
                    jQuery('#' + this.inputFelter[i].id).val(this.inputFelter[i].value);
                }
                this.submitAction.apply(this, this.submitArguments);
                break;
            case FormAutomationType.FILLIN:
                for (var i = 0; i < this.inputFelter.length; i++) {
                    jQuery('#' + this.inputFelter[i].id).val(this.inputFelter[i].value);
                }
                break;
        }

    }

    FormAutomation.prototype.next = function () {
        if (this.stopSequence || this.sequenceIndex >= this.inputFelter.length) {
            this.closeOverlay();
            return;
        }
        var self = this;
        for (var i = 0; i <= this.sequenceIndex; i++) {
            jQuery('#' + this.inputFelter[i].id).val(this.inputFelter[i].value);
        }
        this.submitAction.apply(this, this.submitArguments);
        this.sequenceIndex++;
        setTimeout(function () { self.next() }, this.sequencePause);
    }

    FormAutomation.prototype.sequenceIndex = 0;
    FormAutomation.prototype.stopSequence = false;

    /**
    * Fader overlayet ind
    * Metoden er primært til internt brug, men kan også benyttes hvis man i anden sammenhæng har behov for at benytte et overlay.
    * @method showOverlay
    */
    FormAutomation.prototype.showOverlay = function () {
        if (document.getElementById("Overlay") == null) {
            jQuery("body").append("<div id=\"Overlay\" style=\"position:absolute;background-color:#FFF;opacity:0.4;z-index:10000;top:0px;left:0px;width:100%;\"></div>");
        }

        var self = this;
        jQuery("#Overlay").fadeIn(200);
        jQuery("#Overlay").height(jQuery(document).height());
        jQuery("#Overlay").on("click", { self: this }, self.stopSequence);
    }
    /**
    * Fader overlayet ud
    * Metoden er primært til internt brug, men kan også benyttes hvis man i anden sammenhæng har behov for at benytte et overlay.
    * @method closeOverlay
    */
    FormAutomation.prototype.closeOverlay = function () {
        if (document.getElementById("Overlay") != null) jQuery("#Overlay").fadeOut(100);
        jQuery("#Overlay").off("click", this.stopSequence);
    }

    /**
    * Stopper afviklingen af en sekvens
    * @method stopSequence
    */
    FormAutomation.prototype.stopSequence = function (e) {

        e.data.self.stopSequence = true;
    }

    window.FormAutomation = FormAutomation;
}());