﻿Type.registerNamespace("Finatica.Controls");
Type.registerNamespace("Finatica.Controls.Notification");

Finatica.Controls.Notificator = function(element) {

	
	Finatica.Controls.Notificator.initializeBase(this, [element]);
	this.redirectUrl = null;
	$(document).ready
	(
		function()
		{
			$("div.notificator-container").slideDown("fast", function()
				{
					var closeAnchor = $("div.info > div > a.close");
					for(var i=0; i< closeAnchor.length; i++)
						$(closeAnchor[i]).oneTime(3000,"notification-timer",function()
								{									
									$(this).trigger("click");
								});
					closeAnchor = $("div.success > div > a.close");
					for(var i=0; i< closeAnchor.length; i++)
						$(closeAnchor[i]).oneTime(3000,"notification-timer",function(){ $(this).trigger("click"); });
				});
		
		}
	);
}

Finatica.Controls.Notificator.prototype =
{
	addMessage: function(type, message, closeTime) {
		type = type.toString().toLowerCase();
		if (type != 'alert' && type != 'success' && type != 'error' && type != 'info') {
			alert("Неправильный тип сообщения!");
			return;
		}
		var mainDiv = $(document.createElement('div'));
		mainDiv.addClass("notificator")
				.addClass(type)
				.css("display", "none");
		var innerDiv = $(document.createElement('div'));
		innerDiv.html(message);

		var closeAnchor = $(document.createElement('a'));
		closeAnchor.attr("href", "#")
					.click(this.closeMessage)
					.addClass("close")
					.text("закрыть")
					.appendTo(innerDiv);

		innerDiv.appendTo(mainDiv);

		var outerDiv = $("div.notificator-container");
		mainDiv.appendTo(outerDiv);
		if (outerDiv.css("display") == "none")
			outerDiv.show();

		mainDiv.slideDown("fast");

		if (type == "success" || type == "info") {
			closeAnchor.oneTime(3000, "notification-timer", function() { closeAnchor.trigger("click"); });
		}
		else if (closeTime != null) { closeAnchor.oneTime(closeTime, "notification-timer", function() { closeAnchor.trigger("click"); }); }


		return false;
	},

	addMessageAndRedirect: function(type, message, redirectUrl) {
		this.addMessage(type, message);
		$(document).oneTime(3000, "close-timer", function() { window.location.replace(redirectUrl); });
	},

	closeMessage: function(e) {
		$(e.target).parent().parent().slideUp("fast", function() { $(this).remove(); });
		return false;
	},



	initialize: function() {
		Finatica.Controls.Notificator.callBaseMethod(this, 'initialize');		
		if (this.redirectUrl != null && this.redirectUrl != '') {
			$("div > a.close").attr('href', this.redirectUrl)
			$("div > a.close").click(function() { window.location.replace($(this).attr('href')); })
		}
		else
			$("div > a.close").click(this.closeMessage);
	},

	dispose: function() {
		Finatica.Controls.Notificator.callBaseMethod(this, 'dispose');
	}
}

Finatica.Controls.Notificator.descriptor = 
{
		properties: [{ name: 'redirectUrl', type: String }]
}
Finatica.Controls.Notificator.registerClass("Finatica.Controls.Notificator", Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();