﻿(function ($) {

	var settings = {
		'title': 'Alert',
		'message': 'Are you sure',
		'affirmative': 'Yes',
		'negative': 'No',
		'proceed': 'Continue',
		'confirmTemplateName': 'StandardConfirmTemplate',
		'alertTemplateName': 'StandardAlertTemplate',
		'templatePath': '_assets/js/templates/WarnTemplates.htm'
	};


	var methods =
	{
		init: function (options) { // templatePath, heading, message, affirmative, negative, templateName

			if ($initDelayTimer) {
				window.clearTimeout($initDelayTimer);
			}

			$.extend(settings, options);

			$.get(settings.templatePath, function (msg) {
				$("head").append(msg);
				$("script[type='text/x-jquery-tmpl']", "head").each(
              function (index, item) {

              	$.template(item.id, item.innerHTML);
              });
			}, "html");
		},
		confirm: function (arguments) { // heading, message, affirmative, negative, templateName
			return $.fn.ustWarn.process(arguments, true);
		},
		alert: function (arguments) {
			return $.fn.ustWarn.process(arguments, false);
		}
	};

	$.fn.ustWarn = function (method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		}
		else {
			$.error('Method ' + method + ' does not exist on jQuery.ustWarn');
		}
	};

	$.fn.ustWarn.process = function (arguments, confirmMode) {
		arguments = $.extend({}, settings, arguments);

		var dfd = $.Deferred();

		var $mc = $.tmpl(confirmMode ? arguments.confirmTemplateName : arguments.alertTemplateName, arguments).modal({ backdrop: 'static', show: true, keyboard: true });

		$mc.find('.mcyes').click(function () {
			$mc.modal("hide");
			dfd.resolve();
		});

		$mc.find('.mcno').click(function () {
			$mc.modal("hide");
			dfd.reject();
		});

		return dfd;
	};

	// default initialization
	var $initDelayTimer = setTimeout(function () { $.fn.ustWarn(); }, 1000);

})(jQuery);
