// JavaScript Document

		var ie = document.all;
		var stripeBackgroundColor 	= '#271e2c';
		var backgroundColor 		= '#edeeef';
		var yellow 					= '#ff8';
		var gray 					= '#777';
		var white					= '#FFF';
		var projektyBackground		= '#372e3c';
		var activeMenuElement = null;
		var activeSubmenuElement = null;
		var mouseOverMenu = 0;
		var loadedImages = new Array(); var imgOnlist = false;
		var langfx; var closefx; var firmafx; var slider;
		
	
		window.addEvent('domready', function(){
//			window.addEvent('load',function () { $('allover').fade('out');});
			$('firma_spanner').innerHTML = firma['pl'];
			langfx = new Fx.Tween('lang_wrapper',{'duration': 200});
			closefx = new Fx.Tween('close',{'duration': 200});
			firmafx = new Fx.Tween('firma_spanner',{'duration': 500});
			$('close').setStyle('opacity',0);
			$('close').style.visibility = 'visible';
			$('lang_wrapper').setStyle('opacity',0);
			$('lang_wrapper').style.visibility = 'visible';
			$$('.tag').each(function(tag){tag.setStyle('opacity',1); });
			var szNormal = 172, szSmall  = 50, szFull   = 650;
			var kwicks = $$('#kwicks .kwick');
			var fx = new Fx.Elements(kwicks, {wait: false, duration: 400, transition: Fx.Transitions.Quint.easeOut});
			kwicks.each(function (kwick,i){
				kwick.addEvent('mouseenter', function (event){
					var o = {};
					o[i] = {'width': szFull}
					kwicks.each(function(other, j) {
						if(i != j) {
							o[j] = {width: szSmall};
						}
					});
					fx.start(o);
				});
			});
			$('kwicks').addEvent('mouseleave', function (event) {
				var o = {};
				kwicks.each(function(kwick, j) {
					o[j] = {width: szNormal};
				});
				fx.start(o);
		
			});
			
			var menus = $$('.menu');
			menus.each(function (menu) {
				menu.addEvents({
					'mouseenter': function(event){
						menu.set('morph',{'duration': 200});
						menu.morph({'background-color': stripeBackgroundColor,'color': yellow});
						if ($('projekty').style.top == '420px' && menu == $('projekty_trigger')) mouseOverMenu++;
					},
					'mouseleave': function(event){
						if (menu != activeMenuElement && menu != $('projekty_trigger'))
						{
							menu.set('morph',{'duration': 200});
							menu.morph({'background-color': backgroundColor,'color': gray});
						}
						else if (menu == $('projekty_trigger') && (mouseOverMenu > 0))
						{
							setTimeout("hideMenu()",1000);
							mouseOverMenu--;
						}
						else if (menu == $('projekty_trigger') && (mouseOverMenu <= 0))
						{
							menu.set('morph',{'duration': 200});
							menu.morph({'background-color': backgroundColor,'color': gray});
						}
					}
				});
			});
			$('projekty_trigger').addEvent('click',function(event){
				mouseOverMenu = 1;
				$('projekty').set('tween',{duration: 200, transition: Fx.Transitions.Quint.easeOut});
				$('projekty').tween('top', '420px');
			});

			var submenus = $$('.projekty');
			submenus.each(function (submenu) {
				submenu.addEvents({
					'mouseenter': function(event){
						submenu.set('morph',{'duration': 200});
						submenu.morph({'background-color': stripeBackgroundColor,'color': yellow});
					},
					'mouseleave': function(event){
						submenu.set('morph',{'duration': 200});
						if (submenu != activeMenuElement) submenu.morph({'background-color': projektyBackground,'color': white});
					}
				});
			});
			$('projekty').addEvents({
				'mouseenter': function(event) { mouseOverMenu++; },
				'mouseleave': function(event) { mouseOverMenu--; setTimeout("hideMenu()",1000); }
			});
			$('subprojekty').addEvents({
				'mouseenter': function(event) { mouseOverMenu++; },
				'mouseleave': function(event) { mouseOverMenu--; setTimeout("hideMenu()",1000); }
			});
			$('close').addEvents({
				'mouseenter': function(event){ this.tween('color',yellow); },
				'mouseleave': function(event){ this.tween('color',white); },
				'click': function(event) { hideAll(); }
			});

			$$('.lang').each(function(element) {
				element.addEvents({
					'mouseenter': function(event){ this.tween('color',yellow); },
					'mouseleave': function(event){ this.tween('color',white); }
				})
			});
			
			$('firma_trigger').addEvent('click',function(event){ if (activeMenuElement != this){
				hideAll2();
				activeMenuElement = this;
				fadeMenus();
				closefx.cancel();
				closefx.start('opacity',1);
				setTimeout("langfx.start('opacity',1);",700);
				$('firma').set('tween',{'duration': 200});
				$('firma').tween('top','0px');
				$('slider').set('tween',{'duration': 200});
				$('slider').tween('top','10px');
			}});
			$('kontakt_trigger').addEvent('click',function(event){ if (activeMenuElement != this){
				hideAll2();
				activeMenuElement = this;
				fadeMenus();
				closefx.cancel();
				closefx.start('opacity',1);
				$('kontakt').set('tween',{'duration': 200});
				$('kontakt').tween('top','0px');
			}});
			
			slider = new Slider($('slider'),$('knob'), {
				steps: 100,	
				mode: 'vertical',
				wheel: true,
				onChange: function(value){
					// Everytime the value changes, we change the font of an element
					$('firma').scrollTop = value * ($('firma').scrollHeight-$('firma').offsetHeight) /100;
				}
			}).set(0);


			$('kwick_red').addEvent('click',function (event) {
				openProjekt(pointer[0][0],pointer[0][1]);
			});
			$('kwick_orange').addEvent('click',function (event) {
				openProjekt(pointer[1][0],pointer[1][1]);
			});
			$('kwick_yellow').addEvent('click',function (event) {
				openProjekt(pointer[2][0],pointer[2][1]);
			});
			$('kwick_green').addEvent('click',function (event) {
				openProjekt(pointer[3][0],pointer[3][1]);
			});
			$('kwick_blue').addEvent('click',function (event) {
				openProjekt(pointer[4][0],pointer[4][1]);
			});
/*
		#kwick_red { background: url('<?= getFirstImage($array[0]) ?>') center no-repeat;}
		#kwick_orange { background: url('<?= getFirstImage($array[1]) ?>') center no-repeat;}
		#kwick_yellow { background: url('<?= getFirstImage($array[2]) ?>') center no-repeat;}
		#kwick_green { background: url('<?= getFirstImage($array[3]) ?>') center no-repeat;}
		#kwick_blue { background: url('<?= getFirstImage($array[4]) ?>') center no-repeat;}
*/
		});
		
		
		function hideMenu()
		{
			if (mouseOverMenu == 0)
			{
				$('projekty_trigger').set('morph',{'duration': 200});
				$('projekty_trigger').morph({'background-color': backgroundColor,'color': gray});
				$('projekty').set('tween',{'duration': 200});
				$('projekty').tween('top', '450px');
				$('subprojekty').set('tween',{'duration': 200});
				$('subprojekty').tween('top', '450px');
				$$('.projekty').each(function (submenu) {
					submenu.set('morph',{'duration': 200});
					submenu.morph({'background-color': projektyBackground,'color': white});
				});
				$('projekt').set('tween',{'duration': 200});
				$('projekt').tween('top', '450px');

			}
		}
		function submenutoggle(pointer,el)
		{
			activeMenuElement = el;
			var submenus = $$('.projekty');
			submenus.each(function (submenu) {
				submenu.set('morph',{'duration': 200});
				if (submenu != activeMenuElement) submenu.morph({'background-color': projektyBackground,'color': white});
			});
//			deactivateProjekty(el);
//			loadMenu(projekty[pointer]);
			var fx = new Fx.Morph('subprojekty',{ duration: 200, transition: Fx.Transitions.Quint.easeOut});
			if ($('subprojekty').style.top != '390px') { loadSubmenu(pointer); fx.start({'top': '390px'});}
			else fx.start({'top': '420px'}).chain(function(){ loadSubmenu(pointer); this.start({'top': '390px'}); });
//			$('subprojekty').tween('top', '390px');
		}

		function loadSubmenu(pointer)
		{
			$('subprojekty').innerHTML = '';
			projekty[pointer].each(function (el,i) {
								var node = document.createElement('span');
								node.className = 'subprojekty';
								node.style.cursor = 'pointer';
								node.innerHTML = el['name'];
								var fx = new Fx.Tween('projekt',{ duration: 100, transition: Fx.Transitions.Quint.easeInOut});
								node.onclick= function() { openProjekt(pointer,i); };
								node.onmouseover = function() {
													if ($('projekt').style.top != '450px') { fx.cancel(); fx.start('top','450px').chain(function() {
														empty('projekt');
														//$('projekt').innerHTML = '';
														var newimg = makeImg('image.php?params=500x400x40&src='+el['images'][0],'projekt');
														if (imgOnList) fx.start('top','0px');
														else if (ie) newimg.onload = function () { if ($('subprojekty').style.top != '450px') fx.start('top','0px'); };
														else newimg.addEvent('load', function () { if ($('subprojekty').style.top != '450px') fx.start('top','0px'); });
														
													});}
													else {
														empty('projekt');
														//$('projekt').innerHTML = '';
														var newimg = makeImg('image.php?params=500x400x40&src='+el['images'][0],'projekt');
														if (imgOnList) fx.start('top','0px');
														if (ie) newimg.onload = function () { if ($('subprojekty').style.top != '450px') fx.start('top','0px'); };
														else newimg.addEvent('load', function () { if ($('subprojekty').style.top != '450px') fx.start('top','0px'); });
													}
													// $('projekt').set('tween',{duration: 50, transition: Fx.Transitions.Quint.easeOut});
													// $('projekt').tween('top','0px');
													new Fx.Morph(node, {duration:200, wait:false}).start({
														'background-color': '#271e2c',
														color: '#ff8'
													});
												};
								node.onmouseout = function() {
													//$('projekt').set('tween',{duration: 200, transition: Fx.Transitions.Quint.easeOut});
													// if ($('projekt').style.top != '450px') $('projekt').tween('top','450px');
													fx.cancel();fx.start('top','450px');
													new Fx.Morph(node, {duration:200, wait:false}).start({
														'background-color': '#302936',
														color: '#fff'
													});
												};
/*								node.addEvents({
											   'mouseenter': function() {
													if ($('projekt').style.top != '450px') { fx.cancel(); fx.start('top','450px').chain(function() { $('projekt_image').src = el['images'][0];this.start('top','0px');}); }
													else { $('projekt_image').src = el['images'][0]; fx.start('top','0px'); }
													// $('projekt').set('tween',{duration: 50, transition: Fx.Transitions.Quint.easeOut});
													// $('projekt').tween('top','0px');
													new Fx.Morph(node, {duration:200, wait:false}).start({
														'background-color': '#271e2c',
														color: '#ff8'
													});
												},
											   'mouseleave': function() {
													//$('projekt').set('tween',{duration: 200, transition: Fx.Transitions.Quint.easeOut});
													// if ($('projekt').style.top != '450px') $('projekt').tween('top','450px');
													fx.cancel();fx.start('top','450px');
													new Fx.Morph(node, {duration:200, wait:false}).start({
														'background-color': '#302936',
														color: '#fff'
													});
												},
												'click': function() {
													openProjekt(pointer,i);
												}
								});
*/
								$('subprojekty').appendChild(node);
								//out += '<span class="subprojekty" onmouseover="$('projekt').style.visibility = 'visible'" onmouseout="$('projekt').style.visibility = 'hidden';">Projekt 1</span>'; 
							});
		}
		
		function openProjekt(pointer,index)
		{
			mouseOverMenu = 0;
			hideAll();
			$('description_space').innerHTML = projekty[pointer][index]['text'];
			$('title_space').innerHTML = projekty[pointer][index]['name'];
			thumbnailDiv(projekty[pointer][index]['images']);
			imageDiv(projekty[pointer][index]['images'][0])
			$('projekt_viewer').tween('top','0px');
			closefx.cancel();
			closefx.start('opacity',1);
			
		}
		function thumbnailDiv(images)
		{
			//$('thumbnails_space').empty();
			empty('thumbnails_space');
			images.each(function(image)
			{
				var node = makeImg('image.php?params=50x50x80&src='+image,'thumbnails_space');
				node.setAttribute('height','50px');
				node.setAttribute('width','50px');
				node.style.border = '0px';
				node.style.cursor = 'pointer';
				var fx = new Fx.Morph(node);
				fx.set({'opacity': 0});
				if (imgOnList) fx.start({'opacity': 0.5});
				else if (ie) node.onload = function(){ fx.start({'opacity': 0.5}); };
				else node.addEvent('load',function(){ fx.start({'opacity': 0.5}); });
/*				node.addEvent('click', function(event){
					var fx = new Fx.Tween('image_space',{duration: 100});
					fx.start('opacity',0).chain(function() { imageDiv(image); this.start('opacity',1); });
				});
*/
				node.onclick = function(){
					imageDiv(image);
						//this.start('opacity',1);
				};
				node.onmouseover = function() {
					node.fade(1);
				};
				node.onmouseout = function () {
					node.fade(0.5);
				};
			});
		}
		function imageDiv(image)
		{
			var fx = new Fx.Tween('image_space',{duration: 100});

			fx.start('opacity',0).chain(function() {
				empty('image_space');
				//$('image_space').innerHTML = '';
				var url = 'image.php?params=600x400x90&src='+image;
				var newimg = makeImg(url,'image_space');
				if (imgOnList) fx.start('opacity',1);
				else if (ie) newimg.onload = function() {fx.start('opacity',1);};
				else newimg.addEvent('load',function() {fx.start('opacity',1);});
				//imageDiv(image);
				//this.start('opacity',1);
			});
		}
		
		function hideAll()
		{
			hideAll2();
			activeMenuElement = null;
			fadeMenus();
		}
		function hideAll2()
		{
			hideMenu();
			closefx.cancel();
			closefx.start('opacity',0).chain(function(){ $('close').setStyle('opacity',0);});
			langfx.cancel();
			langfx.start('opacity',0).chain(function(){ $('close').setStyle('opacity',0);});
			$('projekt_viewer').tween('top','450px');
			$('firma').tween('top','450px');
			$('slider').tween('top','460px');
			$('kontakt').tween('top','450px');
		}

		function fadeMenus()
		{
			var f = $('firma_trigger');
			var k = $('kontakt_trigger')
			if (f != activeMenuElement) f.morph({'background-color': backgroundColor,'color': gray});
			if (k != activeMenuElement) k.morph({'background-color': backgroundColor,'color': gray});
		}
		
		function makeImg(url,divid)
		{
			var out = null; imgOnList = false;
			loadedImages.each(function (image) { if ('http://www.archidea.pl/'+url == image.src) { out = image; imgOnList = true; } });
			if (out == null) 
			{
				out = new Element('img',{
					'src': url
				});
				loadedImages[loadedImages.length] = out;
				$(divid).appendChild(out);
			}
			//out.inject(divid);
			else $(divid).appendChild(out);
			return out;
		}
		function empty(divid)
		{
			$(divid).getElements('img').each(function (img) { $('storage').appendChild(img); });
			return;
		}
		
		function firmaLanguage(lang)
		{
			firmafx.cancel();
			firmafx.start('opacity',0).chain(function () { slider.set(0); $('firma_spanner').innerHTML = firma[lang]; firmafx.start('opacity',1); });
		}

