/*
message system
сообщения должны показываться в правом-нижнем углу экрана...?
*/
/*<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" >*/
var windowclass = function(){
	this.ID = null;
	this.TYPE = null;
	this.closetimer = null;
	this.visible = false;
}
windowclass.prototype.toclose = function (){
	clearTimeout(this.closetimer);
	this.closetimer = null;
	var obj = this;
	$('#'+this.ID).fadeTo("slow",0,function(){
			$('#'+obj.ID).remove();
			//alert($('#'+this.ID).html());
		});
//	$('#'+this.ID).slideUp("slow",function(){$('#'+this.ID).remove();});
	//$('#'+this.ID).remove();
	//alert(1);	
	//self.windows[0] = false;
}
windowclass.prototype.show = function(){
	var obj = this;
	$('#'+this.ID).show(500, function(){obj.visible=true;});
	
}





var systemMessages = function(){
	this.windows = Array(); //массив с окошками
	this.types = Object(); //Массив с типами сообщзений
	this.mainobject = 'document.body'; //т.к. основной объект может не существовать на данный период, поэтому делаем так.
	

}

systemMessages.prototype.getType = function(type){
	var ret = false;
	eval('var ret=this.types.'+type);
	return ret;
}

//после вызова надо сообщения поместить на свои места как-то
systemMessages.prototype.resize = function(){

	var cnt = this.windows.length;
	if (cnt>0){
		var y = $(document).scrollTop();
		for (var i=cnt-1; i>=0; i--){
			if ($('#'+this.windows[i].ID).html()!=null){
				$('#'+this.windows[i].ID).stop(true,true);
				$('#'+this.windows[i].ID).css('top',y+'px');
				$('#'+this.windows[i].ID).css('left','0px');				
				y += $('#'+this.windows[i].ID).outerHeight()+5;
			}
		}
	}
	
}

//Создать новое сообщение
systemMessages.prototype.post = function(type,header,text,timer){
	
	//Ищем последнее окошко...
	var cnt = this.windows.length;
	/*
	if (cnt>0){
		for (var i=0; i<cnt; i++){
			if (this.windows[i].visible==false){
				//ждем секунду и пробуем снова
				var obj = this;
				setTimeout (function(){obj.post(type,header,text,timer);},1000);
				return false;
			}

		}

	}	*/
	
	
	var html;
	var unique_id = 'SM_'+Math.round(Math.random()*10000); //Уникальный идентификатор окошка
	eval('var mainobject = '+this.mainobject+';'); 
	html = '<div style="display:none;" class="'+this.getType(type).CLASS+'" id="'+unique_id+'"><div class="msgheader">'+header+'</div><div class="msgbody">'+text+'</div></div>';
	//вставляем сообщение в объект...
	$(mainobject).append(html);
	//позиционируем
	$('#'+unique_id).css('top',Math.round($(document).scrollTop()/*+document.documentElement.clientHeight-$('#'+unique_id).outerHeight(true)*/ )+'px');	
	//$('#'+unique_id).css('left',Math.round($(document.window).scrollLeft()+document.documentElement.clientWidth-$('#'+unique_id).outerWidth(true))+'px');		
	$('#'+unique_id).css('left','0px');			
	$('#'+unique_id).css("opacity",1);

	//Ищем последнее окошко...
	var cnt = this.windows.length;
	
	if (cnt>0){
		for (var i=0; i<cnt; i++){
			var height = $('#'+unique_id).outerHeight()+5;
			$('#'+this.windows[i].ID).animate({"top":"+="+height+"px"},"fast");
		}
	}
	

//	$('#'+unique_id).css('display','block');
	//$('#'+unique_id).show(1000, function(){visible});

	//$('#'+unique_id).fadeTo("slow",0.33);
//('slow',function(){});
	var objWin = new windowclass();
	objWin.ID = unique_id;
	objWin.TYPE = type;
	objWin.timer = null;
	//var obj = this;
	objWin.closetimer = setTimeout(function(){objWin.toclose();},timer);
	this.windows.push(objWin);
	objWin.show();
	
}

systemMessages.prototype.closewin = function(){
	//alert(1);
	
}

systemMessages.prototype.addtype = function(type,DATA){
	eval('this.types.'+type+'=DATA');
}



var c_systemMessages = new systemMessages();
c_systemMessages.addtype('error',{TYPE:'error',CLASS:'msgerror'});
c_systemMessages.addtype('info',{TYPE:'info',CLASS:'msginfo'});
c_systemMessages.addtype('warning',{TYPE:'warning',CLASS:'msgwarning'});

$(document).ready(function(){
	$(window).resize(function(){
		c_systemMessages.resize();
	});

	$(window).scroll(function(){
		c_systemMessages.resize();
	});


	//c_systemMessages.post('error','Приветствие!','Тестовое сообщение',5000);			   
	//alert($('#'+c_systemMessages.windows[0].ID).html());  
//	c_systemMessages.post('error','Приветствие2!','Тестовое сообщение2',5000);						   
			   
	//alert($('#'+c_systemMessages.windows[0].ID).html());  
						   
						   });
