﻿var Alert = {
	
	Body: null,
	IsInit: false,
	Div: null,
	TimeShow: 750,
	TimeHide: 1500,
	TimeRemove: 4000,
	ToDelete: Array(),
	ContainerStart: '<table class="corners"><tr><td class="ac1"><span /></td><td class="top"></td><td class="ac2"><span /></td></tr><tr><td class="left"></td><td class="content">',
	ContainerEnd: '</td><td class="right"></td></tr><tr><td class="ac3"><span /></td><td class="bottom"></td><td class="ac4"><span /></td></tr></table>',
	
	Init: function() {
		Alert.Body = document.body;
		
		if(Alert.Body) {
			Alert.Div = document.createElement('div');
			Alert.Body.insertBefore(Alert.Div, Alert.Body.firstChild);
			Alert.Div.className = 'alert_container';
			
			if(Alert.Div) {
				Alert.IsInit = true;
			}
		}
	},
	
	AddNotice: function(_text) {
		if(!Alert.Body || !Alert.Div) {
			Alert.Init();
		}
			
		if(Alert.IsInit) {
			var id = Alert.GenerateId();
			
			var div = document.createElement('div');
			div.setAttribute('id', id);
			div.className = 'item';
			div.innerHTML = Alert.ContainerStart + _text + Alert.ContainerEnd;
					
			Alert.Div.insertBefore(div, Alert.Div.firstChild);
			
			div.style.top = '-' + (div.offsetHeight ? div.offsetHeight : div.clientHeight) + 'px';
			
			$(div).fadeTo('fast', 0);
			$(div).animate({top: 0, opacity: 1}, Alert.TimeShow, 'linear');
			
			setTimeout(
				function () {
					$('#' + id).fadeOut(Alert.TimeHide, Alert.Remove(id));
				},
				5000
			);
		}
	},
	
	AddError: function(_text) {
		if(!Alert.Body || !Alert.Div) {
			Alert.Init();
		}
		
		if(Alert.IsInit) {
			var id = Alert.GenerateId();
			
			var div = document.createElement('div');
			div.setAttribute('id', id);
			div.setAttribute('title', 'Нажмите чтобы закрыть');
			div.className = 'error_item';
			div.innerHTML = Alert.ContainerStart + _text + Alert.ContainerEnd;
					
			Alert.Div.insertBefore(div, Alert.Div.firstChild);
			
			div.style.top = '-' + (div.offsetHeight ? div.offsetHeight : div.clientHeight) + 'px';
			
			$(div).fadeTo('fast', 0);
			$(div).animate({top: 0, opacity: 1}, Alert.TimeShow, 'linear');
			$(div).click(
				function() {
					var is_exists = false;
					for(var i = 0; i < Alert.ToDelete.length; i++) {
						if(Alert.ToDelete[i] == id) is_exists = true;
					}
					if(!is_exists) {
						Alert.ToDelete[Alert.ToDelete.length] = id;
						$('#' + id).fadeOut(Alert.TimeHide, Alert.Remove(id))
					}
				}
			);
			//$(div).fadeOut(Alert.TimeHide, Alert.Remove(id));
		}
	},
	
	Remove: function(_id) {
		if(Alert.IsInit) {
			var _ele = document.getElementById(_id);
			setTimeout(
				function() {
					for(var i = 0; i < Alert.ToDelete.length; i++) {
						if(Alert.ToDelete[i] == _ele.getAttribute('id')) Alert.ToDelete[i] = false;
					}
					_ele.parentNode.removeChild(_ele);
				},
				Alert.TimeRemove
			);
		}
	},
	
	GenerateId: function() {
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var string_length = 8;
		var randomstring = '';
		
		for (var i = 0; i < string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum, rnum+1);
		}
		return randomstring;
	}
};

