/**
 * Obsługa menu użytkownika
*/
	
	/*---| Start: Lang |---*/
	'DD1Studio.UserMenu.Lang'.namespace();
	
	DD1Studio.UserMenu.Lang = function(){};
	
	DD1Studio.UserMenu.Lang.prototype = {
		DATA : {},
		
		get: function( name ) {
			return this.DATA[name];
		},
		
		set: function( mix, val ) {
			if( typeof mix == 'string' ) {
				this.DATA[mix] = val;
			} else {
				for( var i in mix )
					this.DATA[i] = mix[i];
			}
		}
	};
	/*---| End: Lang |---*/

	'DD1Studio.UserMenu.Menu'.namespace();
	
	DD1Studio.UserMenu.Menu = {
		/**
		 * ID elementu menu
		 *
		 * @var	string
		*/
		_id: 'w_menu_usermenu',
		
		/**
		 * ID okna logowania
		 *
		 * @var	string
		*/
		_idLogIn: 'dialog-log-in',
		
		/**
		 * ID okna rejestracji
		 *
		 * @var	string
		*/
		_idSignUp: 'dialog-sign-up',
		
		/**
		 * ID okna przypomnienia hasła
		 *
		 * @var	string
		*/
		_idForgotPassword: 'dialog-forgot-password',
		
		/**
		 * ID okna newslettera
		 *
		 * @var	string
		*/
		_idNewsletter: 'dialog-newsletter',
		
		/**
		 * CSS klasa linków
		 *
		 * @var	string
		*/
		_classAction: 'a-action',
		
		/**
		 * Nazwa aktywnego okna
		 *
		 * @var	string
		*/
		_windowActive: null,
		
		/**
		 * Użytkownik zalogowany
		 *
		 * @var	bool
		*/
		_userLogin: false,
		
		/**
		 * Link do pobierania plików
		 *
		 * @var	string
		*/
		URL_DOWNLOAD: null,
		
		/**
		 * Inicjuj
		 *
		 * @return	void
		*/
		init: function( userLogin ){
			var self	= this;
			
			self._userLogin = userLogin;
			
			$('a[class*="' + self._classAction + '"]').each(function(){
				var href 	= $(this).attr('href');
				var action	= href.match( /(\/|#)([a-z\-]+)(\.html)?$/ );
				
				if( action && action[2] ) {
					($(this).attr('class').match(/parent/) ? $(this).parent() : $(this)).click(function(){
						return self._action( action[2], $(this) );
					});
				}
			});
			
			/**
			 * Inicjuj logowanie, rejestrację, przypomnienie hasła
			*/
			$('#' + self._idLogIn + ',#' + self._idSignUp + ',#' + self._idForgotPassword)
				.each(function(){
					/**
					 * Usuń ew. komunikaty błędów
					*/
					$(this).find('ul[class="errors"]').remove();
				})
				.dialog({
					'modal'			: true,
					'autoOpen'		: false,
					'dialogClass'	: 'dialog-class',
					'width'			: 640,
					'close'			: function(){
						self._closeDialog( true );
					}
				})
				.find('form').submit(function(){
					var $this 	= $(this);
					var formID	= $this.parent().parent().attr('id').replace( 'dialog-', '');
					
					/**
					 * Dodaj loader
					*/					
					$this.find('button').after($('<img>')
						.attr('src', APP_TEMPLATE + 'images/layout/refresh.gif')
						.addClass('img-ajaxLoader')
						.css({
							'margin-left' 	: '5px',
							'margin-top'	: '4px'
						})
					);
					
					/**
					 * Wyślij formularz
					*/
					$this.ajaxSubmit({
						success: function( response, status ){
							$this.find('img[class="img-ajaxLoader"]').remove();							
							
							self._ajaxSubmitSuccess( $this, response, formID );
						}
					});
					
					return false;
				});
			
			/**
			 * Inicjuj newsletter'a
			*/
			$('#' + self._idNewsletter)
				.dialog({
					'dialogClass'	: 'dialog-newsletter',
					'width'			: 300,
					//'height'		: 35,
					'minHeight'		: 35,
					'autoOpen'		: false
				})
				.find('form').submit(function(){
					var $this = $(this);
					var formID	= 'newsletter';
					
					/**
					 * Dodaj loader
					*/
					$this.find('input').addClass('ajaxLoading');
					
					/**
					 * Wyślij formularz
					*/
					$this.ajaxSubmit({
						success: function( response, status ){
							$this.find('input').removeClass('ajaxLoading');
							
							self._ajaxSubmitSuccess( $this, response, formID );
						}
					});
					
					return false;
				});
		},
		
		_ajaxSubmitSuccess: function( $this, response, formID ) {
			var self	= this;
			
			$this.find('ul[class="errors"]').remove();
			
			response = JSON.parse( response );
							
			if( response.status != true ) {
				for( var i in response.messages ) {
					var html = '';
					
					for( var j in response.messages[i] )
						html += response.messages[i][j];
					
					$this.find('[name*="' + i + '"]:last' ).after($('<ul>')
						.addClass('errors')
						.html( '<li>' + html + '</li>' )
					);
				}
								
				$captcha = $this.find('[name*="captcha"]:last');
								
				if( $captcha.length ) {
					$captcha.val('');
					
					$captcha.parent().find('img').attr('src', response.captchaImg);
				}
			} else {
				/**
				 * Wyczyść wszystkie pola tekstowe formularza
				*/
				$this.find('input[type="text"]').val('');
				
				switch( formID ) {
					case 'log-in' : {
						window.location = self.URL_DOWNLOAD;
						
						break;
					}
					case 'forgot-password' : {
						self._closeDialog();
						
						break;
					}
					case 'newsletter' : {
						$('#' + self._idNewsletter).dialog('close');
						
						break;
					}
					case 'sign-up' : {
						self._openDialog( self._idLogIn );
						
						break;
					}
				}
			}
								
			if( response.message != '' )
				alert( response.message );
		},
		
		/**
		 * Wykonaj akcję
		 *
		 * @param	string	name
		 * @return	void
		*/
		_action: function( name, $this ) {
			var self	= this;
			
			switch( name ) {
				case 'log-in' : {
					self._openDialog( self._idLogIn );
					
					break;
				}
				case 'sign-up' : {
					self._openDialog( self._idSignUp );
					
					break;
				}
				case 'forgot-password' : {
					self._openDialog( self._idForgotPassword );
					
					break;
				}
				case 'newsletter' : {
					$('#' + self._idNewsletter)
						.dialog('open')
						.dialog('option', 'position', [$this.offset().left + $this.width() - $('#' + self._idNewsletter).width() - $(window).scrollLeft(), $this.offset().top - $(window).scrollTop()] );
					
					$('body').bind('mousedown.newsletterdialog', function( e ){						
						var pX 	= $('#' + self._idNewsletter).parent().offset().left;
						var pY 	= $('#' + self._idNewsletter).parent().offset().top;
						var w	= $('#' + self._idNewsletter).parent().width();
						var h	= $('#' + self._idNewsletter).parent().height();
						
						if( e.pageX >= pX && e.pageX <= pX + w && e.pageY >= pY && e.pageY <= pY + h ) {
							// kliknięto na oknie newsletter'a
						} else {
							$('body').unbind('mousedown.newsletterdialog');
						
							$('#' + self._idNewsletter).dialog('close');
						}
					});
					
					break;
				}
				case 'download' : {
					if( self._userLogin ) {
						return true;
					} else {
						self._openDialog( self._idLogIn );
					}
					
					break;
				}
				default : {
					return true;
				}
			}
			
			return false;
		},
		
		/**
		 * Otwórz okno
		 *
		 * @param	string	name
		 * @return	void
		*/
		_openDialog: function( name ) {
			var self	= this;
			
			self._closeDialog();
			
			$('#' + name).dialog('open');
			
			$(document).find('object').css('visibility','hidden');
			
			self._windowActive = name;
		},
		
		/**
		 * Zamknij aktywne okno
		 *
		 * @return	void
		*/
		_closeDialog: function( status ) {
			var self	= this;
			
			if( ! self._windowActive ) return;
			
			if( status != true )
				$('#' + self._windowActive).dialog('close');
			
			$(document).find('object').css('visibility','visible');
			
			self._windowActive = null;
		}
	};
