var App = Class.create();

App.prototype = 
{
	initialize : function ()
	{
		this.prepareContentSize();
		this.prepareFocus();
		
		if ($('comment_form'))
		{
			this.prepareAddCommentForm();
		}
		else if ($('expert_faq_form'))
		{
			this.prepareAddQuastionForm();
		}
		else if ($('advice_form'))
		{
			this.prepareAddAdvideForm();
		}
		else if ($('sample_form'))
		{
			this.prepareSampleForm();
		}
		else if ($('sample_confirm_form'))
		{
			this.prepareSelects('70');
			this.prepareSelects('120');
			this.prepareSampleConfirmForm();
		}
		
		if ($('send_friend_button'))
		{
			this.prepareSendFriend();
		}
	},
	
	prepareSendFriend : function()
	{
		$('send_friend_button').onclick = function()
		{
			$('send_friend_form').reset();
			
			$('send_friend_form').style.display = 'block';
			$('send_friend_form').focusFirstElement();
			
			$$('#send_friend_form .close').each(
				function(el)
				{
					el.onclick = function()
					{
						$('send_friend_form').style.display = 'none';
						this.blur();
					}
				}
			)
			
			$('send_friend_form').onsubmit = function()
			{
				this.showFormPreload('send_friend_form');
				
				new Ajax.Request(
						'/do/Index/sendToFriend',
						{
							parameters  : $('send_friend_form').serialize(true),
							method		: 'post',
							onComplete	: function(req)
							{
								this.hideFormPreload('send_friend_form');
								
								eval('var ret=' + req.responseText);
								
								if (ret.success && ret.success == 1)
								{
									this.showFormResult(
										'send_friend_form', 
										'<div class="success">Wiadomość została wysłana.</div>'
									);
									$('send_friend_form').reset();
								}
								else if (ret.errors)
								{
									var errorText = 
										'<div class="errors">' +
											'<span>Formularz zawiera błędy</span>' +
											'<div class="mt10">';
											
									if (ret.errors.from_email)
									{
										errorText += '<div>Twój email jest nie poprawny lub </div>';
									}
									if (ret.errors.from_name)
									{
										errorText += '<div>proszę podać swoje imię</div>';
									}
									
									if (ret.errors.to_email)
									{
										errorText += '<div>email Znajomego jest nie poprawny</div>';
									}
									if (ret.errors.to_name)
									{
										errorText += '<div>proszę podać imię znajomego</div>';
									}
									errorText += 
											'</div>' +
											'<div class="mt10 center">' +
											'kliknij aby kontynuować...' +
											'</div>' +
										'</div>';
									
									this.showFormResult('send_friend_form', errorText);
								}
							
							}.bind(this)
						}
					)
				
				return false;
				
			}.bind(this)			
			
			this.blur();
			
			return false;
		}.bind(this)
	},

	prepareSelects : function(type)
	{
		$$('.select' + type + ' .input').each(
			function(el)
			{
				el.onclick = function()
				{
					var id = el.id.sub('select_', '', 1);
					
					if (el.hasClassName('up'))
					{
						el.className = 'input down';
						
						$('options_' + id).setStyle({'display' : 'none'});
					}
					else
					{
						el.className = 'input up';

						$('options_' + id).setStyle({'display' : 'block'});
						
						$$('#options_' + id + ' a').each(
							function(subEl)
							{
								subEl.onclick = function()
								{
									$(id).value = $(id  + '_' + subEl.className.sub('option_', '', 1)).value;
									el.update(subEl.innerHTML);
									
									subEl.blur();
									el.onclick();
									return false;
								}
							}
						);
					}
					
					return false;
				}
			}
		)
		
	},
	
	prepareContentSize : function()
	{
		if ($('page') && $('page').getHeight() < 400)
		{
			$('page').setStyle(
				{
					'height' : '400px'
				}
			);
		}
	},
	
	prepareFocus : function()
	{
		$$('form.focus').each(
			function(el)
			{
				el.focusFirstElement();
			}
		)
	},
	
	prepareAddCommentForm : function()
	{
		$('comment_form').onsubmit = function()
		{
			this.showFormPreload('comment_form');
			
			new Ajax.Request(
				'/do/Comment/add',
				{
					parameters  : $('comment_form').serialize(true),
					method		: 'post',
					onComplete	: function(req)
					{
						this.hideFormPreload('comment_form');
						
						eval('var ret=' + req.responseText);
						
						if (ret.success && ret.success == 1)
						{
							this.showFormResult(
								'comment_form', 
								'<div class="success">Komentarz oczekuje na akceptację moderatora.</div>'
							);
							$('comment_form').reset();
						}
						else if (ret.errors)
						{
							var errorText = 
								'<div class="errors">' +
									'<span>Formularz zawiera błędy</span>' +
									'<div class="mt10">';
									
							if (ret.errors.author_name)
							{
								errorText += '<div>Nick jest za krótki</div>';
							}
							if (ret.errors.author_email)
							{
								errorText += '<div>adres e-mail jest nieprawidłowy</div>';
							}
							if (ret.errors.body)
							{
								errorText += '<div>treść komentarza jest za krótka</div>';
							}
							if (ret.errors.terms)
							{
								errorText += '<div>proszę zaakceptować regulamin</div>';
							}
							errorText += 
									'</div>' +
									'<div class="mt10 center">' +
									'kliknij aby kontynuować...' +
									'</div>' +
								'</div>';
							
							this.showFormResult('comment_form', errorText);
						}
					
					}.bind(this)
				}
			)
			
			return false;
			
		}.bind(this)
	},	
	
	prepareSampleForm : function()
	{
		$('sample_form').onsubmit = function()
		{
			this.showFormPreload('sample_form');
			
			new Ajax.Request(
				'/do/SampleRegistration/add',
				{
					parameters  : $('sample_form').serialize(true),
					method		: 'post',
					onComplete	: function(req)
					{
						this.hideFormPreload('sample_form');
						
						eval('var ret=' + req.responseText);
						
						if (ret.success && ret.success == 1)
						{
							this.showFormResult(
								'sample_form', 
								'<div class="success">Odbierz pocztę.</div>',
								true
							);
							$('sample_form').reset();
						}
						else if (ret.errors)
						{
							var errorText = 
								'<div class="errors">' +
									'<span>Formularz zawiera błędy</span>' +
									'<div class="mt10">';
									
							if (ret.errors.fname)
							{
								errorText += '<div>podane imię jest za krótkie</div>';
							}
							if (ret.errors.sname)
							{
								errorText += '<div>podane nazwisko jest za krótkie</div>';
							}
							if (ret.errors.email)
							{
								errorText += '<div>adres e-mail jest nieprawidłowy</div>';
							}
							if (ret.errors.email_dup)
							{
								errorText = 
								'<div class="errors">' +
									'<span>Dziękujemy za zainteresowanie.</span>' +
									'<div class="mt10">';
								
								errorText += '<div>Z naszych statystyk wynika, że zarejestrowałaś się już w poprzedniej edycji programu Lovela Care. Niestety, nie możemy zarejestrować Cię ponownie.</div>';
							}
							errorText += 
									'</div>' +
									'<div class="mt10 center">' +
									'kliknij aby kontynuować...' +
									'</div>' +
								'</div>';
							
							this.showFormResult('sample_form', errorText);
						}
					
					}.bind(this)
				}
			)
			
			return false;
			
		}.bind(this)
	},
	
	prepareSampleConfirmForm : function()
	{
		$('sample_confirm_form').onsubmit = function()
		{
			this.showFormPreload('sample_confirm_form');
			
			new Ajax.Request(
				'/do/SampleRegistration/update',
				{
					parameters  : $('sample_confirm_form').serialize(true),
					method		: 'post',
					onComplete	: function(req)
					{
						this.hideFormPreload('sample_confirm_form');
						
						eval('var ret=' + req.responseText);
						
						if (ret.success && ret.success == 1)
						{
							this.showFormResult(
								'sample_confirm_form', 
								'<div class="success"><br /><br />' + 
									'Dziękujemy za rejestrację.<br /><br />' + thankyou_page + 
								'</div>', // Do CMSA
								true
							);
							$('sample_confirm_form').reset();
						}
						else if (ret.errors)
						{
							var errorText = 
								'<div class="errors">' +
									'<span>Formularz zawiera błędy</span>' +
									'<div class="mt10">';
									
							if (ret.errors.accepted)
							{
								errorText += '<div>proszę zaakceptować regulamin 1</div>';
							}
							if (ret.errors.accepted_1)
							{
								errorText += '<div>proszę zaakceptować regulamin 2</div>';
							}
							if (ret.errors.accepted_2)
							{
								errorText += '<div>proszę zaakceptować regulamin 3</div>';
							}
							if (ret.errors.adress_street)
							{
								errorText += '<div>proszę podać ulicę</div>';
							}
							if (ret.errors.adress_post_code)
							{
								errorText += '<div>proszę podać kod pocztowy</div>';
							}
							if (ret.errors.adress_city)
							{
								errorText += '<div>proszę podać miasto</div>';
							}
							if (ret.errors.phone)
							{
								errorText += '<div>proszę podać numer telefonu</div>';
							}
							if (ret.errors.trim)
							{
								errorText += '<div>proszę podać trymestr</div>';
							}
							if (ret.errors.age)
							{
								errorText += '<div>proszę podać wiek</div>';
							}
							if (ret.errors.trim_age)
							{
								errorText += '<div>proszę podać wiek LUB trymestr</div>';
							}
							if (ret.errors.get_date_month)
							{
								errorText += '<div>proszę wybrać miesiąc</div>';
							}
							if (ret.errors.get_date_year)
							{
								errorText += '<div>proszę wybrać rok</div>';
							}
							errorText += 
									'</div>' +
									'<div class="mt10 center">' +
									'kliknij aby kontynuować...' +
									'</div>' +
								'</div>';
							
							this.showFormResult('sample_confirm_form', errorText);
						}
					
					}.bind(this)
				}
			)
			
			return false;
			
		}.bind(this)
	},
	
	prepareAddAdvideForm : function()
	{
		$('advice_form').onsubmit = function()
		{
			this.showFormPreload('advice_form');
			
			new Ajax.Request(
				'/do/Advice/add',
				{
					parameters  : $('advice_form').serialize(true),
					method		: 'post',
					onComplete	: function(req)
					{
						this.hideFormPreload('advice_form');
						
						eval('var ret=' + req.responseText);
						
						if (ret.success && ret.success == 1)
						{
							this.showFormResult(
								'advice_form', 
								'<div class="success">Twoja porada oczekuje na akceptację moderatora.</div>',
								true
							);
							
							$('advice_form').reset();
						}
						else if (ret.errors)
						{
							var errorText = 
								'<div class="errors">' +
									'<span>Formularz zawiera błędy</span>' +
									'<div class="mt10">';
									
							if (ret.errors.author_fname)
							{
								errorText += '<div>prane imię jest za krótkie</div>';
							}
							if (ret.errors.author_sname)
							{
								errorText += '<div>podane nazwisko jest za krótkie</div>';
							}
							if (ret.errors.author_email)
							{
								errorText += '<div>podany adres e-mail jest nieprawidłowy</div>';
							}
							if (ret.errors.src_url)
							{
								errorText += '<div>podany adres do filmu jest nieprawidłowy</div>';
							}
							if (ret.errors.title)
							{
								errorText += '<div>podany tytuł jest za krótki</div>';
							}
							if (ret.errors.description)
							{
								errorText += '<div>podany opis jest za krótki</div>';
							}
							errorText += 
									'</div>' +
									'<div class="mt10 center">' +
									'kliknij aby kontynuować...' +
									'</div>' +
								'</div>';
							
							this.showFormResult('advice_form', errorText);
						}
					
					}.bind(this)
				}
			)
			
			return false;
			
		}.bind(this)		
	},
	
	prepareAddQuastionForm : function()
	{
		$('expert_faq_form').onsubmit = function()
		{
			this.showFormPreload('expert_faq_form');
			
			new Ajax.Request(
				'/do/ExpertFaq/add',
				{
					parameters  : $('expert_faq_form').serialize(true),
					method		: 'post',
					onComplete	: function(req)
					{
						this.hideFormPreload('expert_faq_form');
						
						eval('var ret=' + req.responseText);
						
						if (ret.success && ret.success == 1)
						{
							this.showFormResult(
								'expert_faq_form', 
								'<div class="success">Pytanie zostały wysłane.</div>'
							);
							$('expert_faq_form').reset();
						}
						else if (ret.errors)
						{
							var errorText = 
								'<div class="errors">' +
									'<span>Formularz zawiera błędy</span>' +
									'<div class="mt10">';
									
							if (ret.errors.author_name)
							{
								errorText += '<div>imię i nazwisko jest za krótkie</div>';
							}
							
							if (ret.errors.author_email)
							{
								errorText += '<div>adres e-mail jest nieprawidłowy</div>';
							}
							if (ret.errors.question)
							{
								errorText += '<div>treść pytania jest za krótka</div>';
							}
							if (ret.errors.terms)
							{
								errorText += '<div>proszę zaakceptować regulamin</div>';
							}
							errorText += 
									'</div>' +
									'<div class="mt10 center">' +
									'kliknij aby kontynuować...' +
									'</div>' +
								'</div>';
							
							this.showFormResult('expert_faq_form', errorText);
						}
					
					}.bind(this)
				}
			)
			
			return false;
			
		}.bind(this)
	},
	
	showFormResult : function(formId, text, dontHide)
	{
		
		var content 		= $(formId);
		var resultBg 		= $(formId + '_result_bg');
		var resultContent 	= $(formId + '_result_content');

		resultBg.setStyle(
			{
				'height' : content.getHeight() + 'px',
				'width'  : content.getWidth() + 'px'
			}
		)
		resultContent.setStyle(
			{
				'width'  : content.getWidth() + 'px'
			}
		)
		
		resultContent.update(text);
		
		resultBg.setStyle({'display' : 'block'});
		resultContent.setStyle({'display' : 'block'});
		
		if (typeof dontHide == 'undefined')
		{
			resultBg.onclick = function()
			{
				this.hideFormResult(formId);
				
			}.bind(this)
			resultContent.onclick = function()
			{
				this.hideFormResult(formId);
				
			}.bind(this)
		}
	},
	
	hideFormResult : function(formId)
	{
		$(formId + '_result_bg').setStyle({'display' : 'none'});
		$(formId + '_result_content').setStyle({'display' : 'none'});
	},
	
	showFormPreload : function(formId)
	{
		var content = $(formId);
		var preload = $(formId + '_preload');
		
		preload.setStyle(
			{
				'height' : content.getHeight() + 'px',
				'width'  : content.getWidth() + 'px'
			}
		)
		
		preload.setStyle({'display' : 'block'});
	},
	
	hideFormPreload : function(formId)
	{
		$(formId + '_preload').setStyle({'display' : 'none'});
	}
}

function show(obj)
{
	if (console && console.log)
	{
		console.log(obj);
	}
	else
	{
		alert(obj);
	}
}
