var c_alerts = {};

Console = function(){};


Console.show_busy = function(val){
	var def = '';

	switch (val) {
		case 1:  // save
	    	def = 'Пожалуйста подождите<br /><b>идет сохранение...</b>';
	    	break;
		case 2:  // load
	    	def = 'Пожалуйста подождите<br /><b>идет загрузка...</b>';
	    	break;
		case 3:  // indexing
	    	def = 'Пожалуйста подождите<br /><b>идет индексация...</b>';
	    	break;
	    default:
    		def = val;
    }
	
	$('#dbusy div.editor_busytxt').html(def);	
	$('#dbusy').show();

};


Console.hide_busy = function(){
	$('#dbusy').hide();
}


Console.notice = function (msg, id, opts){
	Console.message('message', id, msg, opts);
}

Console.warning = function (msg, id, opts){
	Console.message('warning', id, msg, opts);
}

Console.error = function (msg, id, opts){
//	alert(msg);
	if ('undefined' == typeof(opts))
		var opts = {permanent:1};
	Console.message('error', id, msg, opts);
}


//opts - {permanent}
Console.message = function (mode, id, msg, opts){
	if ('undefined' == typeof(id))
		var id = 0;
	if ('undefined' == typeof(opts))
		var opts = {};
	opts = Console._process_opts(id, opts);
	var msg_div = this;
	var msg_id = id;
	if (!id)
		msg_id = Math.random();
	if ('undefined' == typeof(c_alerts[msg_id])) {
		c_alerts[msg_id] = {};
	}
	
	if (id) {
		if ('undefined' != typeof(c_alerts[msg_id]['tid'])) {
			if (c_alerts[msg_id]['tid'])
				clearTimeout(c_alerts[msg_id]['tid']);
			$('#msg_'+id).fadeOut(400).fadeIn(400).fadeOut(800).fadeIn(800, function(){c_alerts[msg_id]['tid'] = 0; Console._start_alert(msg_id, this, opts)});
			return;
		} 
	}

//	c_alerts[msg_id]['container'] = $('<div class="'+mode+'" id="msg_'+msg_id+'"><span class="close" onclick="Console.hide_alert(\''+msg_id+'\')">&nbsp;</span><div class="txt">'+msg+'</div></div><div class="sadow"></div>')
	c_alerts[msg_id]['container'] = $('<div class="item" id="msg_'+msg_id+'"><div class="'+mode+'"><span class="close" onclick="Console.hide_alert(\''+msg_id+'\')">&nbsp;</span><div class="txt">'+msg+'</div></div><div class="sadow"></div></div>')
		.appendTo('#alert_container')
		.slideDown('fast', function(){Console._start_alert(msg_id, this, opts)} );
			
}

Console.hide_alert = function (msg_id) {
	if ('undefined' == typeof(c_alerts[msg_id])) return;
	if ('undefined' == typeof(c_alerts[msg_id]['container'])) return;
	var msg_div = c_alerts[msg_id]['container'];
	$(msg_div).slideUp('slow', function(){Console._remove_alert(msg_id)});
}

// Internal functions
Console._start_alert = function(msg_id, msg_div, opts){
	if (opts['permanent']) {
		tid = 0;
	} else {
		var tid = setTimeout(function(){
				$(msg_div).slideUp('slow', function(){Console._remove_alert(msg_id)});
				if (msg_id) {
					c_alerts[msg_id]['tid'] = 0;
				} 
			}, 4000);
	}
	
	if (msg_id) {
		c_alerts[msg_id]['container'] = msg_div;
		c_alerts[msg_id]['tid'] = tid;
		c_alerts[msg_id]['opts'] = opts;
	}
}


Console._process_opts = function (id, opts) {
	if ('undefined' == typeof(opts['permanent']) || !id) {
		opts['permanent'] = 0; 
	}
	return opts;
}

Console._remove_alert = function (msg_id){
	if ('undefined' == typeof(c_alerts[msg_id])) return;
	if ('undefined' == typeof(c_alerts[msg_id]['container'])) return;
	var msg_div = c_alerts[msg_id]['container'];
	$(msg_div).remove();
	c_alerts[msg_id] = {};
}

//init
/*
//Replase standard alert() function
window.alert = function(txt) {
	Console.alert('message', 0, txt);
}
*/

