$( document ).ready( function( )
{
	/**
	 * SPRAWDZENIE ADRESU E-MAIL W NEWSLETTERZE
	 */
	 
	/* akcje wykonane przy kliknięciu na przycisk zatwierdzający */
	$( 'input#newsletterCheckMail' ).click( function( )
	{
		/* pobranie adresu e-mail z inputa textowego */
		$sMail = $( this ).parent().parent().children( 'div.InputContainer' ).children( 'div.InputBox' ).children( 'input#email' ).val();
		$sRegulamin = $( this ).parent().parent().children( 'div.CheckBoxArea' ).children( 'span' ).children( 'input#regulamin' );
				
		/* jeżeli nie wpisano adresu e-mail */
		if ( $sMail == '' )
		{
			$( 'p#newsletterMessage' ).text( 'Proszę wpisać adres e-mail!' );
			$( 'p#newsletterMessage' ).dialog({
				minHeight: 70,
				modal: true,
				buttons:
				{
					OK: function()
					{
						$( this ).dialog( 'close' );
					}
				}
			});
			$( 'div#mainNewsletterContainer' ).append( '<p id="newsletterMessage"></p>' );
		}
		/* jeżeli adres e-mail nie jest poprawny */
		else if ( !( /^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i.test( $sMail ) ) )
		{
			$( 'p#newsletterMessage' ).text( 'Adres niepoprawny' );
			$( 'p#newsletterMessage' ).dialog({
				minHeight: 70,
				modal: true,
				buttons:
				{
					OK: function()
					{
						$( this ).dialog( 'close' );
					}
				}
			});
			$( 'div#mainNewsletterContainer' ).append( '<p id="newsletterMessage"></p>' );
		}
		/* jeżeli nie wybrano checkboxa */
		else if ( $sRegulamin.attr( 'checked' ) != true )
		{
			$( 'p#newsletterMessage' ).text( 'Proszę zaakceptować regulamin' );
			$( 'p#newsletterMessage' ).dialog({
				minHeight: 70,
				modal: true,
				buttons:
				{
					OK: function()
					{
						$( this ).dialog( 'close' );
					}
				}
			});
			$( 'div#mainNewsletterContainer' ).append( '<p id="newsletterMessage"></p>' );
		}
		else
		{			
			/* sprawdzenie maila poprzez AJAX */
			$.ajax({
				url: '/ajax/checkmail',
				data : 'mail=' + $sMail,
				dataType : 'text',
				type : 'post',
				async: false,
				beforeSend: function()
				{				
					$( 'div#mainNewsletterContainer' ).append( '<div id="ajaxLoad"><img src="/images/user/ajax-loading.gif" alt="" /></div>' );
				},
				success: function ( txt )
				{					
					$( 'div#mainNewsletterContainer div#ajaxLoad' ).remove();
					
					/* jeżeli e-mail znajduje się w bazie */
					if ( txt == 1 )
					{
						$( 'p#newsletterMessage' ).text( 'Wpisany adres e-mail znajduje się w bazie. Czy chcesz się wypisać?' );
						$( 'p#newsletterMessage' ).dialog({
							minHeight: 70,
							modal: true,
							buttons:
							{
								Anuluj: function()
								{
									$( this ).dialog( 'close' );
									$( 'div#mainNewsletterContainer' ).append( '<p id="newsletterMessage"></p>' );
								},
								OK: function()
								{
									$( this ).dialog( 'close' );
									$( 'div#mainNewsletterContainer' ).append( '<p id="newsletterMessage"></p>' );
									
									$.ajax({
									url: '/ajax/unsignmail',
									data : 'mail=' + $sMail,
									dataType : 'text',
									type : 'post',
									async: false,
									beforeSend: function()
									{				
										$( 'div#mainNewsletterContainer' ).append( '<div id="ajaxLoad"><img src="/images/user/ajax-loading.gif" alt="" /></div>' );
									},
									success: function ( txt )
									{
										$( 'div#mainNewsletterContainer div#ajaxLoad' ).remove();
										$( 'p#newsletterMessage' ).text( 'Zadeklarowałeś chęć rezygnacji z otrzymywania newslettera. Dalsze instrukcje zostaną wysłane na podany adres e-mail.' );
										$( 'p#newsletterMessage' ).dialog({
											minHeight: 70,
											modal: true,
											buttons:
											{
												OK: function()
												{
													$( this ).dialog( 'close' );
												}
											}
										});
										
										$( 'div#mainNewsletterContainer' ).append( '<p id="newsletterMessage"></p>' );
									}});
								}
							}	
						});						
					}
					/* jeżeli e-mail jeszcze nie znajduje się w bazie */
					else if ( txt == 0 )
					{
						$( 'p#newsletterMessage' ).text( 'Zadeklarowałeś chęć otrzymywania newslettera. Dalsze instrukcje zostaną wysłane na podany adres e-mail.' );
						$( 'p#newsletterMessage' ).dialog({
							minHeight: 70,
							modal: true,
							buttons:
							{
								OK: function()
								{
									$( this ).dialog( 'close' );
								}
							}
						});
						$( 'div#mainNewsletterContainer' ).append( '<p id="newsletterMessage"></p>' );
					}
				}
			});
		}		
		
		return false;
	});
	
	/**
	 * AJAXOWE WYSŁANIE WIADOMOŚCI DO UŻYTKOWNIKA
	 */
	 
	/* akcje wykonane przy kliknięciu na link pisania wiadomości */
	$( 'a#writeMessage' ).click( function( )
	{		
		$( 'div#profileInformations' ).hide();
		
		/* pobranie adresu do wysłania formularza */
		$sAdres = $( this ).attr( 'href' ); 
				
		$( 'div#profilePersonalData' ).after( '<div id="writeMessageContainer"><form method="post" action="' + $sAdres + '"><div><textarea style="width: 100%; height: 350px;" name="tresc_wiadomosci"></textarea><input class="redButton" style="float: right;" type="submit" value="wyślij" /></div></form></div>' );
		
		return false;
	});
	
	/* akcje wykonane przy wybraniu dziedziny konkursu */
	$( 'select#changeContestArea' ).change( function( )
	{
		$( this ).parent().parent().submit();
	});
});