﻿/* globals
    jQuery, CsLogger
 */
/* exported
 */
"use strict";
(function ($, window, document, undefined) {
    // default settings.
    var _pluginName = "text1";
    var _defaultsOptions = {
        debug: false,
        scrollReveal: true
    };
    var _logger = new CsLogger(_pluginName);


    // constructor.
    function Text1(element, options) {
        this._element = element;
        this._options = $.extend({}, _defaultsOptions, options);
        this._defaultOptions = _defaultsOptions;
        this._name = _pluginName;
        this.init();
    }

    // remove empty <p> tags.
    function removeEmptyParagraphs(element) {
        var elementContainer = document.getElementById(element.id);
        var totalParagraphs = elementContainer.getElementsByTagName('p');
        $(totalParagraphs).each(function () {
            if ($(this).text().trim() === '') {
                $(this).remove();
            }
        });
    }

    // (void) validate the options passed into the plugin.
    function validateOptions(options) {
        if (typeof (options.debug) == "boolean" && options.debug == true) {
            _logger.enable();
        }
        _logger.obj("validateOptions() | opts:", options);
    }

    // (void) ensure the selected element is valid.
    function validateElement(element) {
        _logger.log("validateElement()");
        if (element == undefined || $(element).length == 0) {
            _logger.error("selected element is not valid.");
            return;
        }
    }

    // prototype extensions.
    Text1.prototype = {

        // (void) initialise.
        init: function () {
            validateOptions(this._options);
            validateElement(this._element);
            removeEmptyParagraphs(this._element);
            _logger.log("init()");
        }
    };

    // A really lightweight plugin wrapper around the constructor,
    // preventing against multiple instantiations
    $.fn[_pluginName] = function (options) {
        return this.each(function () {
            if (!$.data(this, "plugin_" + _pluginName)) {
                $.data(this, "plugin_" + _pluginName,
                    new Text1(this, options));
            }
        });
    };

})(jQuery, window, document);