var app = {
		
	effectEnd : true, 
	effectInterval : false,
		
	init : function()
	{
		this.prepareMainFlash();
		this.prepareSearchForm();
		this.prepareMainTabs();
		this.preparePageSize();
		this.prepareSelectors();
		this.prepareProductPictures();
		this.prepareProductTabs();
		this.prepareIndexNews();
		this.prepareSliders();
		this.fixChrome();
	},
	
	fixChrome : function()
	{
		if(!navigator.userAgent.match('Chrome')) return;
		
		$('query').setStyle({marginTop: "0px"});
	},
	
	prepareMainFlash : function()
	{
		if ($('mp-flash'))
		{
			var flashvars = {
				url_xml : '/do/Producer/getList'
			};
			
			var params = {
				vmode : 'transparent',
				bgcolor : '202020'
			};
			
			var attributes = {};
			
			swfobject.embedSWF(
				'/swf/logotypy.swf', 
				'mp-flash', 
				830, 
				24, 
				'9.0.0', 
				'expressInstall.swf', 
				flashvars, 
				params, 
				attributes
			);
		}
	},

	prepareIndexNews : function()
	{
		if ($$('#product-main #news').length)
		{
			this.changeTab = true;
			
			this.currentTab = 0;
			this.lastTab 	= $$('#product-main .news').length - 1;
			
			$$('#product-main .news-action').each(
					
				function(el)
				{
					el.onclick = function()
					{
						app.changeIndexNews(this.id.split('-')[2])
						
						return false;
					},
					
					el.onmouseover = function() { app.changeTab = false; } 
					el.onmouseout = function() { app.changeTab = true; }
				}
			)
			app.effectInterval = window.setInterval(
				function()
				{
					if (app.changeTab)
					{
						var tabId = app.currentTab + 1;
						
						if (tabId > app.lastTab)
						{
							tabId = 0;
						}
						
						app.changeIndexNews(tabId);
					}
				}, 
				3000
			);
		}
	},
	
	isChanging : false,
	
	changeIndexNews : function(nextTab)
	{
		if (!app.isChanging)
		{
			if ($('news-' + nextTab) && $('news-action-' + nextTab))
			{
				app.isChanging = true
				$$('#news div.active').each(
					function(elm)
					{
						elm.className = 'news';
					}
				);
				
				$$('#news img.news-action').each(
					function(elm)
					{
						elm.className = 'news-action';
					}
				);
				
				$('news-' + nextTab).className = 'news active';
				$('news-action-' + nextTab).className = 'news-action selected';
				
				app.currentTab = nextTab;
				
				app.isChanging = false;
			}
		}
	},
	
	prepareProductTabs : function()
	{
		if ($('product'))
		{
			var tabId = $('tabs-action-7') ? 7 : 1;
			var tabs = 
			{
				'specyfikacja' 	: 1,
				'multimedia' 	: 2,
				'testy' 		: 3,
				'porady' 		: 4,
				'artysci' 		: 5,
				'do-pobrania'	: 6,
                'opis'          : 7
			};
			
			var location = document.location.href.split('#');
			
			if (location[1] && tabs[location[1]])
			{
				tabId = tabs[location[1]]; 
			}
			
			this.productTabAction(tabId);
			
			$$('#tabs-actions a').each(
				function(el)
				{
					el.onclick = function()
					{
						el.blur();
						return app.productTabAction(
							this.id.sub('tabs-action-', '', 1) * 1
						);
					}
				}
			)
		}
	},
	
	productTabAction : function(tabId)
	{
		for (var i = 1 ; i <= 7 ; i++)
		{
			if ($('tabs-action-' + i))
			{
				if (i == tabId)
				{
					$('tabs-action-' + i).className = 'selected';
					$('tabs-content-' + i).setStyle({'display' : 'block'})
				}
				else
				{
					$('tabs-action-' + i).className = '';
					$('tabs-content-' + i).setStyle({'display' : 'none'})
				}
			}
		}
		
		return true;
	},
	
	prepareSliders : function()
	{
		if ($('artists'))
		{
			this.prepareSlider('slider-artists', 3);
			this.prepareSlider('slider-young', 1);
		}
	},
	
	prepareSlider : function(id, ceils)
	{
		if ($(id))
		{
			if ($$('#' + id + '-items li').length > 0)
			{
				var width = $$('#' + id + '-items li')[0].getWidth() + 2;
				var count = Math.ceil($$('#' + id + '-items li').length);
				
				$(id + '-items').setStyle({'width' : (width * count) + 'px'});
				
				this[id + '-items'] = {
					id 			: id + '-items',
					stepWidth	: width * ceils,
					currentPos 	: 0,	
					totalPos 	: Math.floor(count / ceils)
				}
				
				$(id + '-next').onclick = function()
				{
					return app.moveSlider(1, this.id);
					
				}.bind(this[id + '-items'])
				
				$(id + '-prev').onclick = function()
				{
					return app.moveSlider(-1, this.id);
				}.bind(this[id + '-items']);
				
				
				var lis = $$('#' + id + '-items li');
				
				var step = 0;
				
				for (var i = 0 ; i < lis.length ; i++)
				{
					if 
					(
						lis[i].down(0).hasClassName('artist-item-selected') ||
						(lis[i].down(0).next(0) && lis[i].down(0).next(0).hasClassName('artist-item-selected')) ||
						(lis[i].down(0).next(1) && lis[i].down(0).next(1).hasClassName('artist-item-selected'))
					)
					{
						step = Math.floor(i / ceils);
					}
				}
				
				if (step > 0) 
				{
					this.moveSlider(step, this[id + '-items'].id);
					
				}
			}
		}
	},
	
	moveSlider : function(pos, id)
	{
		if 
		(
			this[id].currentPos + pos > -1 &&
			this[id].currentPos + pos < this[id].totalPos &&
			app.effectEnd
		)
		{
			var x = (this[id].currentPos + pos) * this[id].stepWidth * -1;

			this[id].currentPos = this[id].currentPos + pos;
			
			new Effect.Move(
				id, 
				{ 
					x: x, 
					y: 0, 
					mode: 'absolute',
					beforeStart : function()
					{
						app.effectEnd = false;
					},
					afterFinish : function()
					{
						app.effectEnd = true;
					}
				}
			);
		}
		
		return false;
	},
	
	prepareProductPictures : function()
	{
		if ($('photos-small'))
		{
			this.currentPhoto 	= 0;
			this.lastPhoto 		= 0;
			
			$$('#photos-small a').each(
				function(el)
				{
					app.lastPhoto = el.id.sub('photo-', '', 1);
					
					el.onclick = function()
					{
						this.blur();
						
						app.currentPhoto = this.id.sub('photo-', '', 1);
						
						if (app.currentPhoto < 0)
						{
							app.currentPhoto = app.lastPhoto;
						}
						else if (app.currentPhoto > app.lastPhoto)
						{
							app.currentPhoto = 0;
						}
						
						$$('#photos-small a.selected').each(
							function(elm)
							{
								elm.className = '';
							}
						);
						$$('#photo-big .selected').each(
							function(elm)
							{
								elm.className = '';
							}
						);
						
						$('photo-big-' + app.currentPhoto).className = 'selected';
						$('photo-' + app.currentPhoto).className = 'selected';
						
						return false;
					}
				}
			)
		}
	},
	
	prepareSelectors : function()
	{
		$$('select.selector').each(
			function(el)
			{
				el.onchange = function()
				{
					document.location = el.options[el.selectedIndex].value;
				}
			}
		)
	},
	
	preparePageSize : function()
	{
		if (false)
		{
			var height1 = $('list-bottom-orange').getHeight() + 46;
			var height2 = $('nav').getHeight();
			
			var height = (height1 > height2) ? height1 : height2;
			
			$('list-bottom-orange').setStyle({'height' : (height - 46) + 'px'})
			$('nav').setStyle({'height' : (height) + 'px'})
			$('nav-top').setStyle({'height' : (height-25) + 'px'})
		}
	},
	
	setMainTabsAutoChange : function(autoChange)
	{
		if (autoChange)
		{
			app.effectInterval = window.setInterval(
				function()
				{
					var switchId = $$('#main-page-tabs .tabs a.selected')[0].id.sub('tab-', '', 1) * 1 + 1;
					
					if (switchId > 3)
					{
						switchId = 1;
					}
					
					$('tab-' + switchId).onclick();
					
				}, 
				5000
			);
		}
		else if (app.effectInterval != false)
		{
			window.clearInterval(app.effectInterval);
		}
	},
	
	prepareMainTabs : function()
	{
		if ($('main-page-tabs'))
		{
			$$('#main-page .tab-content').each(
				function(el)
				{
					el.previous(0).setStyle({height : (el.getHeight() + 15) + 'px'})
				}
			)
			
			$('tab-content-1').setStyle({zIndex : 10, display : 'none'});
			$('tab-content-2').setStyle({zIndex : 20, display : 'block'});
			$('tab-content-3').setStyle({zIndex : 10, display : 'none'});
			
			$$('#main-page-tabs .tabs a').each(
				function(el)
				{
					el.onclick = function()
					{
						this.blur();
						
						if (this.className != 'selected' && app.effectEnd)
						{
							var obj = {
								id_new : el.id.sub('tab-', '', 1),
								id_old : $$('#main-page-tabs .tabs a.selected')[0].id.sub('tab-', '', 1)
							};

							app.effectEnd = false;
							$('tab-content-' + obj.id_new).setStyle({zIndex : 20, display: 'none'});
							$('tab-content-' + obj.id_old).setStyle({zIndex : 15});
							
							Effect.Appear(
								'tab-content-' + obj.id_new,
								{
									duration : 1.2,
									afterFinish : function()
									{
										app.effectEnd = true;
										$('tab-content-' + this.id_old).setStyle({zIndex : 10});
										
									}.bind(obj)
								}
							);
							
							$('tab-' + obj.id_old).className = '';
							$('tab-' + obj.id_new).className = 'selected';
						}
						
						return false;
						
					}.bind(el)
				}
			)
			
			app.setMainTabsAutoChange(true);
			
			$('main-page-tabs').onmouseover = function(event)
			{
				app.setMainTabsAutoChange(false);
			}
			$('main-page-tabs').onmouseout = function(event)
			{
				app.setMainTabsAutoChange(true);
			}
		}
	},
	
	prepareSearchForm : function()
	{
		if ($('search-form'))
		{
			$('query').onblur = function()
			{
				if ($('query').value == '')
				{
					$('query').value = 'Szukaj';
				}
			}
			$('query').onfocus = function()
			{
				if ($('query').value == 'Szukaj')
				{
					$('query').value = '';
				}
			}
			
			$('search-form').onsubmit = function()
			{
				if ($('query').value == 'Szukaj')
				{
					$('query').value = '';
				}
				
				return true;
			}
			
			if ($('query').value == '')
			{
				$('search-form').focusFirstElement();
			}
		
			if ($('query'))
			{
				new Ajax.Autocompleter(
					$('query'),
					'query_suggest',
					'/do/Index/getSuggest',
					{
						minChars	: 3,
						paramName 	: 'query',
						method 		: 'get'
					}
				);
			}
		}
	}
}

function goToUrl(url)
{
	document.location = url;
}

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