var CNewsletter = {
		/**
		 * Nazwa id elementu DOM, w którym wyświetlane są komunikaty
		 * 
		 * @var String
		 */
		_sNewsletterMsg: 'newsletter_msg',
		
		/**
		 * Nazwa id elementu DOM, w którym wpisywany jest e-mail
		 * 
		 * @var String
		 */
		_sNewsletterEmail: 'newsletter_email',
		
		/**
		 * Obiekt jQuery, w którym wyświetlane sa komunikaty
		 * 
		 * @var null | Object | jQuery
		 */
		oMsgBox: null,
		
		/**
		 * Obiekt jQuery, w którym wpisywany jest e-mail
		 * 
		 * @var null | Object | jQuery
		 */
		oMailInput: null,
		
		/**
		 * Prosta metoda wyświetlająca komunikaty
		 * 
		 * Wyświetla komunikat sTxt we wskazanym miejscu
		 * lub gdy to miejsce nie udało się ustawić w onInit()
		 * wyrzuca komunikat funkcją alert().
		 * 
		 * @param String sTxt treść komunikatu
		 */
		setMsg: function(sTxt) {
			if( this.oMsgBox != null )
			{
				this.oMsgBox.html("");
				this.oMsgBox.html(sTxt);
			}
			else
			{
				alert(sTxt);
			}
		},
		
		/**
		 * Wyświetla loader
		 */
		showLoader: function() {
			if( typeof(CConfig.ajaxLoaderPath) == 'undefined')
			{
				this.setMsg("Ładuję...");
			}
			else
			{
				this.setMsg('<img src="' + CConfig.ajaxLoaderPath + '" alt="Ładuję..." />');
			}
		},
		
		/**
		 * Metoda inicjacyjna. 
		 * 
		 * Powinna być uruchamiona na końcu dokumentu (X)HTML. Tak, 
		 * aby prawie całe drzewo DOM zostało wczytane.
		 */
		onInit: function() {
			this.oMsgBox = $("#" + this._sNewsletterMsg);
			this.oMailInput = $("#" + this._sNewsletterEmail);
		},
		
		/**
		 * Obsługa zdarzenia kliknięcia "Zapisz" przy polu newslettera
		 */
		onAddEmail: function() {
			this.showLoader();
			
			if( typeof(CConfig) == 'undefined' ) {
			//brak pliku konfiguracji JS
				this.setMsg("Błąd: Brak pliku konfiguracyjnego JavaScript");
			}
			else
			{
				if( typeof(CConfig.ajaxEvents) != 'string' || CConfig.ajaxEvents.search("do-add-newsletter") == -1 ) 
				{
				//mozna tutaj uzyc jakiegos innego rozwiazania, jesli glowny modul AJAX nie obsluguje
				//zapytania ajax -- np. mozna wyslac zapytanie AJAX do zdarzenia obslugiwanego przez
				//obecny modul albo po prostu debuggowac w ten sposob lub poinformowac uzytkownika,
				//ze cos jest nie tak
					this.setMsg('Błąd: Próba wysłania zapytania AJAX zakończona niepowodzeniem (do-add-newsletter)');
				}
				else
				{
				//jesli główny modul AJAX obsluguje to zdarzenie, to wykonaj zapytanie AJAX
					if( this.oMailInput != null 
					&& (this.oMailInput.val() == '' || this.oMailInput.val() == 'Wpisz swój e-mail' ) )
					{
					//jesli ktos nic nie wprowadzil do pola z adresem e-mail
						this.setMsg('Proszę wpisać adres e-mail');
					}
					else
					{
						if( this.oMailInput != null ) {
							this.showLoader();
							
							jQuery.ajax({
								type: "POST",
								url: CConfig.newsletter_add_api,
								data: {klient_id: 46, email: CNewsletter.oMailInput.val()},
								dataType: "json",
								success: CNewsletter.onAjaxAddToNewsletterSent
							});
						}
						else
						{
							this.setMsg('Błąd: brak pola z adresem e-mail');
						}
					}
				}
			}
			
			return false;
		},
		
		/**
		 * Odbiera odpowiedź na żądanie wysłane AJAXem
		 */
		onAjaxAddToNewsletterSent: function(oRes) {
			if( typeof(oRes) == 'string')
			{
				CNewsletter.setMsg(oRes);
			}
			else
			{
				if( oRes != null 
				&& typeof(oRes.status) != 'undefined' )
				{
					if( typeof(oRes.info) != 'undefined' )
					{
						if( oRes.info == 'adres istnieje w bazie danych' )
						{
							CNewsletter.setMsg("Ten adres istnieje już na liście subskrybentów.");
						}
						else if( oRes.info == 'błędny adres e-mail' )
						{
							CNewsletter.setMsg('Podany adres e-mail jest błędny.');
						}
						else
						{
							CNewsletter.setMsg(oRes.info);
						}
					}
					else
					{
						if( oRes.status == 'ok' )
						{
							CNewsletter.setMsg('Dodano adres e-mail do listy subskrybentów.');
						}
						else
						{
							CNewsletter.setMsg('Nieznana odpowiedź z serwera.');
						}
					}
				}
				else
				{
					CNewsletter.setMsg('Błąd: Wystąpił nieznany błąd. Nieznana odpowiedź z serwera.');
				}
			}
		},
		
		/**
		 * Prosta metoda debugująca
		 * 
		 * @param Object hObj obiekt/tablica do debugowania
		 * @parma Integer iDepth glebokosc
		 */
		doDebug: function(hObj, iDepth) {
			var msg = '';
			
			jQuery.each( hObj, function(i, j) {
				msg += i+": "+j+"\r\n";
				
				if( iDepth >= 1 ) {
					jQuery.each( j, function(k, l) {
						msg += k+": "+l+"\r\n";
						
						if( iDepth >= 2 ) {
							jQuery.each( l, function(m, n) {
								msg += m+": "+n+"\r\n";
							});
						}
					});
				}
			});
			
			alert(msg);
		}
};
