var App = Class.create();

App.prototype = 
{
	initialize : function ()
	{
		if ($('post-photos-imgs'))
		{
			$$('#post-photos-imgs img').each(
				function(el)
				{
					el.onclick = function()
					{
						var img = new Image();
							
						img.onload = function()
						{
							$('photo_content').src = this.src;
						};
						
						img.src = $(this.id + '_src').value;
						
						$$('#post-photos-imgs img.selected').each(function(el){ el.className = ''; });
						
						el.className = 'selected';
					}
				}
			)
			
			if ($('photo_content'))
			{
				$('photo_content').setStyle({'cursor' : 'pointer'});
				$('photo_content').onclick = function()
				{
					if ($('photo_content').src)
					{
						var parts = $('photo_content').src.split('-');
						
						var src = '';
						
						for( var i = 0, j = parts.length - 1; i <= j ; i++)
						{
							if (i == 0)
							{
								src += parts[i];
							}
							else if (i < j)
							{
								src += '-' + parts[i];
							}
							else if (i == j)
							{
								src += '.' + parts[i].split('.')[1];
							}
						}
						
						this.showPicturePopup(src);
					}
					
					return false;
					
				}.bind(this)
			}
		} 
		else if ($('main'))
		{
			var nav 		= 0;
			var width	 	= 0;
			var widthTotal 	= 0;
			
			this.block = false;
			
			$$('#main div.post').each(
				function(el) 
				{ 
					++nav;
					width		= el.getWidth(); 
					widthTotal += el.getWidth(); 
				}
			)
			
			$('main-posts').setStyle({'width' : widthTotal + 'px' });
			
			if (nav > 0)
			{
				var className = '';
				
				for (var i = 0 ; i < nav ; i++)
				{
					className = (i == 0) ? ' class="active"' : '';
					
					new Insertion.Bottom(
						'main-nav', 
						'<a href="#" id="nav-' + i + '"' + className + '>' + (i+1) + '</a>'
					);
					
					$('nav-' + i).onclick = function()
					{
						$$('#main-posts .post').each(
							function(el)
							{
								el.setStyle({'display' : 'none'})
							}
						)
						
						$$('#main-posts .post')[this.i].setStyle({'display' : 'block'})
						
						$$('#main-nav a.active').each(function(el) { el.className = ''; })
							
						this.el.className = 'active';
						this.el.blur();
						
						return false;
						
					}.bind(
						{
							i	: i,
							obj : this,
							el  : $('nav-' + i)
						}
					)
				}
			}
		}
	},
	
	setCenter : function(element, top)
	{
		element.setStyle(
			{
				'marginTop' : (Position.realOffset(element)[1] - Position.positionedOffset(element)[1] + top) + 'px'
			}
		);
	},
	
	showPicturePopup : function(src)
	{
		$$('body')[0].setStyle({'overflow' : 'hidden'});
		
		$('popup').setStyle({'display' : 'block'});
		
		$('popup-bg').setStyle({'width' : $('popup').getWidth() + 'px'});
		$('popup-content').setStyle({'width' : $('popup').getWidth() + 'px'});
		$('popup-content').update('<div class="preload" id="popup-content-preload">Proszę czekać...</div>');
		
		this.setCenter($('popup-content-preload'), 100);
		
		var img = new Image();
		
		img.onload = function()
		{
			$('popup-content').update('<div class="img" id="popup-content-image"><div class="info">Kliknij aby zamknąć</div></div>');
			
			this.obj.setCenter($('popup-content-image'), 10);
			
			$('popup-content-image').setStyle(
				{
					backgroundColor		: '#FFF',
					backgroundImage 	: 'url(' + this.img.src + ')',
					backgroundPosition 	: 'center center',
					backgroundRepeat	: 'no-repeat' 
				}
			)
			$('popup-content-image').onclick = function()
			{
				$('popup-content').update('');
				$('popup').setStyle({'display' : 'none'});
				$$('body')[0].setStyle({'overflow' : 'auto'});
			}
		}.bind(
			{
				obj : this,
				img : img
			}
		)
		
		
		img.src = src;
	}
}

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