/*
*	@author	Marek Lewandowski
*/

function CInterface()
{
	// uchwyt do timera czyszczacego kolejne komunikatow
	this.hCleanerTimer = null;
	this.messageContenerHolder = null;
	
	// zmienna pomocnicza do funkcji czyszczacej komunikaty
	this.hInitLoopPassed = false;
	this.messagesInQueue = function()
	{
		if(this.messageContenerHolder == null)
			return alert('Odwołanie się do pustej kolejki komunikatow, linia: 19');
		
		return (this.messageContenerHolder.find('div.messageBox').size() > 0);
	}
	this.clearMessages = function()
	{
		if(this.hCleanerTimer == null)
			this.hCleanerTimer = setInterval('INTERFACE.clearMessages()', 4200);
		
		if(!this.hInitLoopPassed)
		{
			this.hInitLoopPassed = true;
			return;
		}
		
		if(!this.messagesInQueue())
		{
			this.unsetClearingTimer();
			return false;
		}
		this.messageContenerHolder.find('div.messageBox:last').fadeOut(900, function(){
			$(this).css('display', 'block');
			$(this).css('visibility', 'hidden');
			$(this).slideUp(400, function(){ $(this).remove(); });
			});
		
		return true;
	}
	
	this.startClearingMessages = function()
	{
		// zerowanie petli inicjacyjnej
		this.hInitLoopPassed = false;
		// odpalenie funkcji
		this.clearMessages();
	}
	
	this.unsetClearingTimer = function()
	{
		if(this.messageContenerHolder == null) return false;
		
		clearInterval(this.hCleanerTimer);
		this.hCleanerTimer = null;
	}
	
	/*
	*	@param			int			typeId			-	typ komunikatu
	*	@param			string		content			-	tresc komunikatu
	*	@param			event		e				-	obiekt eventu
	*	@param			function	addCallback		-	dodatkowy callback, jako parametr otrzymuje event (e)
	*/
	this.appendMessage = function(typeId, content, e, addCallback)
	{
		var addClass = '';
		switch(typeId)
		{
			case 0: addClass = 'errorMessage'; break;
			case 2: addClass = 'infoMessage'; break;
		}
		var messageBody = '<div class="messageBox '+addClass+'" style="display: none;">'+content+'</div>';
		
		this.messageContenerHolder.html( this.messageContenerHolder.html() + messageBody );
		
		//this.messageContenerHolder.find('div.messageBox:last').hide();
		this.messageContenerHolder.find('div.messageBox:last').fadeIn(600);
		
		// czyscimy timer zeby nam nie wymazal wiadomosci za szybko jesli juz odlicza
		this.unsetClearingTimer();
		
		// odpalenie oczyszczacza
		this.startClearingMessages();
		
		if(addCallback != null)
		{
			addCallback();
		}
	}
	
	/*	maluje odpowiadajace elementy
	*	@param		jQuery		jQueryObj		-	selektor pod elementy ktore maja zostac pomalowane
	*	@param		string		color			-	kolor jakim maja zostac zaznaczone
	*	@param		bool		efekciarsko		-	wypróbuj
	*/
	this.markElements = function(jQueryObj, color, efekciarsko)
	{
		if(color == null) color = 'F30';
		if(efekciarsko == null) efekciarsko = false;
		
		if(!jQueryObj) return false;
		
		var tmpUchwyt = function(domEl, kolor){ $(domEl).css('background', kolor); }
		jQueryObj.each(function()
		{
			var defaultBg = $(this).css('background');
			var defaultBorder = $(this).css('border');
			$(this).parent().css('border', '1px solid #'+color);
			$(this).parent().css('background', '#fbe5e5');
			
			$(this).parent().bind('mouseover.interfaceErrorBinding', 
			function(){
				jQueryObj.each(function(){
					$(this).parent().css('border', defaultBorder);
					$(this).parent().css('background', defaultBg);
					$(this).parent().unbind('.interfaceErrorBinding');
					});
				});
		});
	}
}

var INTERFACE = new CInterface();

$(document).ready(function(){
	
	INTERFACE.messageContenerHolder = $('#msgArea');

	INTERFACE.clearMessages();
});
