////
// Plugin - errorDialog
// adds the functionality for showing a simple modal error dialogue
// this plugin makes use of the jquery UI plugin
//
// @author OstrickiDa
//
(function(){
  // Constructor
  (ErrorDialogHandler = function(element, options) {
    this.dialogDiv = jQuery(element);
    // Merge options with default values
    this.settings = jQuery.extend({}, this.defaultValues, options || {});
    this.init();
  }).prototype = {

    defaultValues : {

    	'defaultTitle' : 'Error',
    	'defaultErrorMsg' : 'An unknown error occured.',


    	'errorTitles' : 	{},
    	'errorMessages' : 	{},

    	'class' : 'ErrorMsg',

    	'dialogDivSelector' : 'div#dialog-error',

    	//layout configuration
		'dialog' : {
			  resizable: false,
	          height:160,
	          modal: true
		}
    },

    /* ****************************************************************
     * 						initialization
     * ***************************************************************/

    init : function() {},

    showErrorMsgByKey : function(titleMsgKey, errorMsgKey) {
    	var errorMsg = this.settings['errorMessages']['errorMsgKey'];
    	if(!errorMsg){
    		errorMsg = this.settings['defaultErrorMsg'];
    	}
    	var title = this.settings['errorTitles']['titleMsgKey'];
    	if(!title){
    		title = this.settings['defaultTitle'];
    	}
    	this.showErrorMsg(this.umescapeHtmlText(title), errorMsg);
    },

	showErrorMsg : function(errorTitleNotHtmlEscaped, errorMsgHtmlEscaped) {
		var clazz = this.settings['class'];
		this.dialogDiv.html('<p class="'+clazz+'">'+errorMsgHtmlEscaped+'</p>');
		var dialogOptions = jQuery.extend({}, this.settings['dialog'], {'title': errorTitleNotHtmlEscaped});
		var dialog = this.dialogDiv.dialog(dialogOptions);
		this.replaceCloseButton(dialog);
	},

	getErrorMsg : function (msgKey) {
		var errorMsgMap = this.settings['errorMessages'];
		if(errorMsgMap){
			var msg = errorMsgMap[msgKey];
			if(msg && msg!=''){
				return this.umescapeHtmlText(msg);
			}else{
				return "";
			}
		}
		var changedFeed = this.form.af_isDirty(),
			hasVariables = this.links && this.links.length > 0;
		return changedFeed || hasVariables;
	},

	umescapeHtmlText : function (text) {
		return jQuery("<div/>").html(text).text()
	},

	replaceCloseButton : function (dialog) {
		var dialogCloseButton = dialog.parent().find('.ui-dialog-titlebar-close');
		dialogCloseButton.addClass( 'CloseButtonContainer');
		dialogCloseButton.empty();
		dialogCloseButton.removeAttr('title');
		dialogCloseButton.prepend(this.settings['closeButtonSvg']);
		dialogCloseButton.blur();

		jQuery('.ui-widget-overlay').on('click touchstart onmsgesturestart', jQuery.proxy(function () {
			this.dialogDiv.dialog('close');
		}, this));
	}

  };

  jQuery.fn.errorDialog = function(options) {
    return this.each(function() {
      var element = jQuery(this);
      // Return early if this element already has a plugin instance
      if (element.data('errorDialogHandler')) {
        return;
      }
      // Store plugin object in this element's data
      element.data('errorDialogHandler',  new ErrorDialogHandler(this, jQuery.extend({}, options)));
    });
  };
})();
