/**
 * PeriodeVisning i Mussam.
 * 
 * @depends dago.js
 */
(function($) {
	$.PeriodeVis = function(el, options) {
		// Setup base
		var base = this; // the javascript object. Always refer to base to
							// avoid
		// scope errors in inline functions
		base.$el = $(el); // the jQuery element
		base.el = el; // the DOM element
		base.pluginName = "PeriodeVis";
		base.$el.data(base.pluginName, base); // Add a reverse reference to
		// the DOM object

		// options
		base.options = $.extend({}, $.PeriodeVis.defaultOptions, options);
		// logging
		var log = MUSSAM.setupPluginLog(base);
		// Properties

		// Now where everything is declared and defined above, we are ready to
		// initialize.
		base.init = function() {
			log.info("init()");
			// Events

			base.render();
		}

		// Methods

		base.render = function() {
			log.info("render()");
			base.$el.empty();
			if (base.options.textDisplayElm) {
				base.$textDisplay = $(base.options.textDisplayElm);
			} else {
				base.$textDisplay = $("<div></div>");
				base.$el.append(base.$textDisplay);
			}
		
			base.renderTextDisplay(base.options.input);
			
		};

		base.renderTextDisplay = function(input) {
			log.info("renderTextDisplay(): " + JSON.stringify(input) );
			if (input == null || input['isExactPeriod'] == null) {
				log.warn("renderTextDisplay() - no input");
				return;
			}
			var html = "";
			if (input['isExactPeriod']) {
			  html = "<span class='fieldTitle'>" + MUSSAM.message("mussam.vis.genstand.felt.periode") + "</span> ";
				html += input.periodAsText + " " + input.matchText;
			} else if (input['matchText'] > "") {
			  html = "<span class='fieldTitle'>" + MUSSAM.message("mussam.vis.genstand.felt.periode") + "</span> ";			
				html += input['periodAsText'] + " - <span>" + input['matchLongText'] + "</span>";
			} else {
				html = "";
			}
			log.info("html generated: " + html) ;
			base.$textDisplay.html(html);
		}

		base.renderTextDisplayError = function(errorText) {
			base.$textDisplay.html("<span style='color: red'>" + errorText + "</span>");
		}
    // Event handlers

    base.init();
  };


	$.PeriodeVis.defaultOptions = {
		textDisplayElm : null,
		input : {
			startAar : null,
			slutAar : null,
			kode : "",
			isExactPeriod : false,
			periodAsText : "",
			matchText : "",
			matchLongText : ""
		},
		useLogging : true
	};

	$.fn.PeriodeVis = function(options) {
		return this.each(function() {
			(new $.PeriodeVis(this, options));
		});
	};

	// This function breaks the chain, but returns
	// the object if it has been attached to the object.
	$.fn.getPeriodeVis = function() {
		this.data("PeriodeVis");
	};

})(jQuery);
