var app = {
		
	init : function()
	{
		this.prepareMainPageSlider();
		this.prepareProjectPhotos();
		this.prepareContactForm();
	},
	
	prepareProjectPhotos : function()
	{
		if ($$('.project')[0])
		{
			var photosCount = 0;
			
			$$('#photos img').each(
				function(el)
				{
					el.style.zIndex = (photosCount == 0) ? 100 : 50;
					
					++photosCount;
				}
			)
			
			this.effectFinish = true;
			
			this.nextPhotoId 	= 0;
			this.currentPhotoId = 0;
			
			this.photosCount = 0;
			this.changePhoto = true;
			
			$$('ol.listProjectPhotosSmall li a').each(
				function(el)
				{
					app.photosCount++;
					
					el.onmouseover = function()
					{
						app.changePhoto = false;
					}
					el.onmouseout = function()
					{
						app.changePhoto = true;
					}
					
					el.onclick = function()
					{
						this.blur();
					
						if (app.effectFinish)
						{
							app.nextPhotoId = this.id.sub('action-', '', 1);
							
							if (app.nextPhotoId != app.currentPhotoId)
							{
								$('photo-' + app.nextPhotoId).setStyle(
									{
										zIndex : 75,
										display : 'block'
									}
								);
								
								app.effectFinish = false;
								
								$$('ol.listProjectPhotosSmall li a.selected').each(
									function(elm)
									{
										elm.className = '';
									}
								)
								
								Effect.Fade(
									'photo-' + app.currentPhotoId,
									{
										afterFinish : function()
										{
											$('photo-' + app.nextPhotoId).setStyle(
												{
													zIndex : 100,
													display : 'block'
												}
											);
											$('photo-' + app.currentPhotoId).setStyle(
												{
													zIndex : 50
												}
											);
											
											app.currentPhotoId = app.nextPhotoId;
											
											app.effectFinish = true;
										}
									}
								);
								
								this.className = 'selected';
								
							}
						}
						
						return false;
					}
				}
			)
			
			window.setInterval(
				function()
				{
					if (app.changePhoto)
					{
						var changeId = app.currentPhotoId*1 + 1;
						
						if (changeId >= app.photosCount)
						{
							changeId = 0;
						}
						
						$$('#ol.listProjectPhotosSmall li a')[changeId].onclick();
					}
				},
				5000
			);
		}
	},
	
	prepareContactForm : function()
	{
		if ($$('.contact')[0])
		{
			$('submit-form').onsubmit = function()
			{
				$('submit-preload').setStyle({'display' : 'block'});
				$('submit-form').setStyle({'display' : 'none'});
				
				$$('.error').each(
					function(el)
					{
						el.setStyle({'display' : 'none'});
					}
				)
				
				new Ajax.Request(
					'/do/Page/submitContact',
					{
						method : 'post',
						parameters : $('submit-form').serialize(true),
						onComplete : function(req)
						{
							$('submit-preload').setStyle({'display' : 'none'});
						
							eval('var ret = ' + req.responseText);

							if (ret.success && ret.success == 1)
							{
								$('submit-form').reset();
								$('submit-form').setStyle({'display' : 'none'});
								$$('.msgInfo')[0].setStyle({'display' : 'block'});
							}
							else if (ret.errors)
							{
								$('submit-form').setStyle({'display' : 'block'});
								
								$H(ret.errors).each(
									function(el)
									{
										if ($$('p[rel=error_' + el[0] + ']')[0])
										{
											$$('p[rel=error_' + el[0] + ']')[0].setStyle({'display' : 'block'})
										}
									}
								)
							}
						}
					}
				)
				
				return false;
			}
		}
	},
	
	prepareMainPageSlider : function()
	{
		if ($$('.homepage')[0])
		{
			var photosCount = 0;
			
			$$('#photos img').each(
				function(el)
				{
					el.style.zIndex = (photosCount == 0) ? 100 : 50;
					
					++photosCount;
				}
			)
			
			this.PhotoInterval = false;	
			
			this.effectFinish = true;
			
			this.photosCount = photosCount;
			
			this.currentPhotoId = 0;
			
			var button_left  = $$("body div#wrapper div.homepage ul.listNavigationIndex li a.left")[0];
			var button_right = $$("body div#wrapper div.homepage ul.listNavigationIndex li a.right")[0];
			
			button_left.onclick = function()
			{
				this.blur();
				return app.mainPageChangePhoto(-1);
			}
			button_right.onclick = function()
			{
				this.blur();
				return app.mainPageChangePhoto(1);
			}
			
			button_left.onmouseover = function()
			{
				if (app.PhotoInterval !== false)
				{
					window.clearInterval(app.PhotoInterval);
					app.PhotoInterval = false;
				}
			}
			
			button_right.onmouseover = button_left.onmouseover;
			
			button_left.onmouseout = function()
			{
				if (app.PhotoInterval == false)
				{
					app.PhotoInterval = window.setInterval(
						function()
						{
							app.mainPageChangePhoto(1);
						},
						5000
					);
				}
			}
			button_right.onmouseout = button_left.onmouseout;
			button_right.onmouseout();
		}
	},
	
	mainPageChangePhoto : function(step)
	{
		if (this.effectFinish)
		{
			this.nextPhotoId = this.currentPhotoId + step;
			
			if (this.nextPhotoId >= this.photosCount)
			{
				this.nextPhotoId = 0;
			}
			else if (this.nextPhotoId < 0)
			{
				this.nextPhotoId = this.photosCount - 1;
			}
			
			this.effectFinish = false;
			
			$('photo-' + this.nextPhotoId).setStyle(
				{
					zIndex : 75,
					display : 'block'
				}
			);
			
			Effect.Fade(
				'photo-' + app.currentPhotoId,
				{
					afterFinish : function()
					{
						$('photo-' + app.nextPhotoId).setStyle(
							{
								zIndex : 100,
								display : 'block'
							}
						);
						$('photo-' + app.currentPhotoId).setStyle(
							{
								zIndex : 50
							}
						);
					}
				}
			);
			
			
			Effect.Fade(
				'project-' + app.currentPhotoId,
				{
					afterFinish : function()
					{
						Effect.Appear(
							'project-' + app.nextPhotoId,
							{
								afterFinish : function()
								{
									app.currentPhotoId = app.nextPhotoId;
									app.effectFinish = true;
								}
							}
						);
					}
				}
			);
		}

		
		
		
		return false;
	}
}

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