var showItem;
var isCurItem;

window.addEvent('domready', function() {
	if ($$('#trustBox').length > 0) {
		var trustBox = $('trustBox');
		var items = trustBox.getElements('.item');
		var itemCount = items.length;
		var blockCount = itemCount / 3 + (itemCount % 3 == 0 ? 0 : 1);
		
		var curBlock = 0;
		var animating = false;
		
		items.each(function(el, it) {
			el.fx = new Fx.Morph(el, {duration: 400, transition: Fx.Transitions.Quart.easeInOut}).set({'opacity': (it < 3 ? 0.6 : 0)});
			var elColor = el.getElement('.color');
			elColor.fx = new Fx.Morph(elColor, {duration: 400, transition: Fx.Transitions.Quart.easeInOut}).set({'opacity': 0});
			el.addEvent('mouseenter', function() {
				if (animating) return false;
				el.fx.pause();
				el.fx.start({'opacity': 1});
				elColor.fx.pause();
				elColor.fx.start({'opacity': 1});
			});
			el.addEvent('mouseleave', function() {
				if (animating) return false;
				el.fx.pause();
				el.fx.start({'opacity': 0.6});
				elColor.fx.pause();
				elColor.fx.start({'opacity': 0});
			});
		});
		
		/*(function(){
			animating = true;
			var nextBlock = curBlock + 1;

			if (nextBlock == blockCount) nextBlock = 0;
			for (var i = 0; i < 3; i++) {
				(function(ii){
					var it = curBlock * 3 + ii;
					items[it].fx.pause();
					items[it].fx.start({'opacity': 0});
				}).delay(i * 200, false, i);
			}
			(function(){
				for (var it = curBlock * 3; it <= curBlock * 3 + 2; it++) {
					items[it].setStyle('display', 'none');
				}
				for (var it = nextBlock * 3; it <= nextBlock * 3 + 2; it++) {
					items[it].setStyle('display', 'block');
				}
				
				for (var i = 0; i < 3; i++) {
					(function(ii){
						var it = nextBlock * 3 + ii;
						items[it].fx.pause();
						items[it].fx.start({'opacity': 0.6});
					}).delay(i * 200, false, i);
				}
			}).delay(800);
			
			(function(){
				animating = false;
				curBlock = nextBlock;
			}).delay(1800);
		}).periodical(8000);*/
	}

	$$('#content .blog .item a.more').each(function(el) {
		el.fx = new Fx.Morph(el, {duration: 400, transition: Fx.Transitions.Quart.easeInOut}).set({'width': 0});
		el.getParent().addEvent('mouseenter', function() {
			el.fx.pause();
			el.fx.start({'width': '40px'});
		});
		el.getParent().addEvent('mouseleave', function() {
			el.fx.pause();
			el.fx.start({'width': 0});
		});
	});
	
	if ($$('#indexShowcase').length > 0) {
		var showcase = $('indexShowcase');
		var titleRoll = showcase.getElement('.titleRoll .wrapper').getChildren();
		var contentRoll = showcase.getElement('.contentRoll .wrapper').getChildren();
		//var circleRoll = showcase.getElement('.circleRoll');
		var bigCircle = showcase.getElement('.bigCircle .wrapper').getChildren('.item');
		var mediumCircle = showcase.getElement('.mediumCircle .wrapper').getChildren('.item');
		var smallCircle = showcase.getElement('.smallCircle .wrapper').getChildren('.item');
		var showcaseLegend = $('showcaseLegend').getChildren('.item');
		var itemCount = titleRoll.getChildren('.item').length;
		var itemWidth = 435;
		var mouseOn = false;
		var curItem = 0;
		
		showcase.addEvent('mouseenter', function() {
			mouseOn = true;
		});
		
		showcase.addEvent('mouseleave', function() {
			mouseOn = false;
		});
		
		if ($('spaceScene')) $('spaceScene').addEvent('mouseenter', function() {
			mouseOn = true;
		});
		
		if ($('spaceScene')) $('spaceScene').addEvent('mouseleave', function() {
			mouseOn = false;
		});
		
		//titleRoll.fx = new Fx.Morph(titleRoll, {duration: 600, transition: Fx.Transitions.Quart.easeInOut}).set({'left': 0});
		titleRoll.each(function(el, it) {
			el.fx = new Fx.Morph(el, {duration: 600, transition: Fx.Transitions.Quart.easeInOut}).set({'left': (it > 0) ? '435px' : 0});
		});
		//contentRoll.fx = new Fx.Morph(contentRoll, {duration: 600, transition: Fx.Transitions.Quart.easeInOut}).set({'left': 0});
		contentRoll.each(function(el, it) {
			el.fx = new Fx.Morph(el, {duration: 600, transition: Fx.Transitions.Quart.easeInOut}).set({'left': (it > 0) ? '435px' : 0});
		});
		
		//circleRoll.fx = new Fx.Morph(circleRoll, {duration: 600, transition: Fx.Transitions.Quart.easeInOut}).set({'left': 0, 'opacity': 1});
		
		bigCircle.each(function(el, it) {
			el.fx = new Fx.Morph(el, {duration: 300, transition: Fx.Transitions.Quart.easeInOut}).set({'opacity': (it == 0 ? 1 : 0)});
			el.setStyle('background-position', 'left top');
		});
		
		mediumCircle.each(function(el, it) {
			el.fx = new Fx.Morph(el, {duration: 200, transition: Fx.Transitions.Quart.easeInOut}).set({'opacity': (it == 0 ? 1 : 0)});
			el.setStyle('background-position', 'left top');
		});
		
		smallCircle.each(function(el, it) {
			el.fx = new Fx.Morph(el, {duration: 200, transition: Fx.Transitions.Quart.easeInOut}).set({'opacity': (it == 0 ? 1 : 0)});
			el.setStyle('background-position', 'right top');
		});

		function autoChange() {
			if (mouseOn) return false;
			nextItem();
		}

		(function(){
			showItem(0);
			autoChange.periodical(5000);
		}).delay(1000);
		
		function prevItem() {
			var toShow = curItem - 1;
			if (toShow < 0) toShow = itemCount - 1;
			showItem(toShow);
		}
		
		function nextItem() {
			var toShow = curItem + 1;
			if (toShow == itemCount) toShow = 0;
			showItem(toShow);
		}
		
		showItem = function (it) {
			if ($$('#cubeElement' + (it + 1)).length > 0) {
				//$('cubeElement' + (curItem + 1)).set('src', $('cubeElement' + (curItem + 1)).type);
				$('cubeElement' + (it + 1)).set('src', $('cubeElement' + (it + 1)).activeType);
				(function(){
						if (isCurItem != it) {
							$('cubeElement' + (it + 1)).set('src', $('cubeElement' + (it + 1)).type);
						}
					}).delay(7000);
				//(function(){
				//	if (!$('cubeElement' + (it + 1)).mouseOn) {
				//		$('cubeElement' + (it + 1)).set('src', $('cubeElement' + (it + 1)).type);
				//	}
				//}).delay(1000);
				
			}
			if (it == curItem) return true;
			
			bigCircle[curItem].fx.pause();
			bigCircle[curItem].fx.start({'opacity': 0});
			if (bigCircle[curItem].curDelay) $clear(bigCircle[curItem].curDelay);
			
			if (bigCircle[it].curDelay) $clear(bigCircle[it].curDelay);
			bigCircle[it].curDelay = (function(){
				bigCircle[it].fx.pause();
				bigCircle[it].fx.start({'opacity': 1});
			}).delay(300);
			
			//circleRoll.fx.pause();
			//circleRoll.fx.start({'left': (-it * itemWidth) + 'px', 'opacity': 0});
			
			//if (typeof(circleRoll.curDelay) !== 'undefined') $clear(circleRoll.curDelay);
			//circleRoll.curDelay = (function(){
			//	circleRoll.fx.pause();
			//	circleRoll.fx.start({'opacity': 1});
			//}).delay(300);
			
			/*if (typeof(titleRoll.curDelay) !== 'undefined') $clear(titleRoll.curDelay);
			titleRoll.curDelay = (function(){
				titleRoll.fx.pause();
				titleRoll.fx.start({'left': (-it * itemWidth) + 'px'});
				
				if (typeof(contentRoll.curDelay) !== 'undefined') $clear(contentRoll.curDelay);
				contentRoll.curDelay = (function(){
					contentRoll.fx.pause();
					contentRoll.fx.start({'left': (-it * itemWidth) + 'px'});
				}).delay(300);
			}).delay(200);*/
			
			var curBuffer = curItem;
			
			if (typeof(titleRoll[curBuffer].curDelay) !== 'undefined') $clear(titleRoll[curBuffer].curDelay);
			titleRoll[curBuffer].curDelay = (function(){
				titleRoll[curBuffer].fx.pause();
				titleRoll[curBuffer].fx.start({'left': '-435px'});
			}).delay(200);
			
			if (typeof(contentRoll[curBuffer].curDelay) !== 'undefined') $clear(contentRoll[curBuffer].curDelay);
			contentRoll[curBuffer].curDelay = (function(){
				contentRoll[curBuffer].fx.pause();
				contentRoll[curBuffer].fx.start({'left': '-435px'});
			}).delay(500);
			
			if (typeof(titleRoll[it].curDelay) !== 'undefined') $clear(titleRoll[it].curDelay);
			titleRoll[it].curDelay = (function(){
				titleRoll[it].fx.pause();
				titleRoll[it].fx.set({'left': '435px'});
				titleRoll[it].fx.start({'left': 0});
			}).delay(200);
			
			if (typeof(contentRoll[it].curDelay) !== 'undefined') $clear(contentRoll[it].curDelay);
			contentRoll[it].curDelay = (function(){
				contentRoll[it].fx.pause();
				contentRoll[it].fx.set({'left': '435px'});
				contentRoll[it].fx.start({'left': 0});
			}).delay(500);
			
			
			
			
			
			smallCircle[curBuffer].fx.pause();
			smallCircle[curBuffer].fx.start({'opacity': 0});
			if (typeof(smallCircle[curBuffer].curDelay) !== 'undefined') $clear(smallCircle[curBuffer].curDelay);
			
			if (typeof(mediumCircle[curBuffer].curDelay) !== 'undefined') $clear(mediumCircle[curBuffer].curDelay);
			mediumCircle[curBuffer].curDelay = (function(){
				mediumCircle[curBuffer].fx.pause();
				mediumCircle[curBuffer].fx.start({'opacity': 0});
			}).delay(200);
			
			if (typeof(mediumCircle[it].curDelay) !== 'undefined') $clear(mediumCircle[it].curDelay);
			mediumCircle[it].curDelay = (function(){
				mediumCircle[it].fx.pause();
				mediumCircle[it].fx.start({'opacity': 1});
				
				if (typeof(smallCircle[it].curDelay) !== 'undefined') $clear(smallCircle[it].curDelay);
				smallCircle[it].curDelay = (function(){
					smallCircle[it].fx.pause();
					smallCircle[it].fx.start({'opacity': 1});
				}).delay(200);
			}).delay(400);
			
			showcaseLegend[curItem].removeClass('active');
			showcaseLegend[it].addClass('active');
			
			curItem = it;
			isCurItem = curItem;
		}
		
		showcaseLegend.each(function(el, it) {
			el.addEvent('click', function(ev) {
				if (ev) ev.stop();
				showItem(it);
			});
		});

	}
});

window.addEvent('domready', function() {
	if ($$('#spaceScene').length > 0) {
		var size = 140;
		var pointsArray = [
			{ type: 'grey', crd: new Array(0,size,-size), activeType: 'drukarnia', id: 1 },
			{ type: 'grey', crd: new Array(size,size,0), activeType: 'projekty_graficzne', id: 2 },
			{ type: 'grey', crd: new Array(0,size,size), activeType: 'strony_www', id: 3 },
			{ type: 'grey', crd: new Array(-size,size,0), activeType: 'doradztwo', id: 4 },
			{ type: 'grey', crd: new Array(0,-size,-size), activeType: 'reklama_w_windach', id: 5 },
			{ type: 'grey', crd: new Array(size,-size,0), activeType: 'reklama_zewnetrzna', id: 6 },
			{ type: 'grey', crd: new Array(0,-size,size), activeType: 'fotografia', id: 7 },
			{ type: 'grey', crd: new Array(-size,-size,0), activeType: 'prezentacje', id: 8 },
			{ type: 'grey', crd: new Array(-size,0,-size), activeType: 'filmy', id: 9 },
			{ type: 'grey', crd: new Array(size,0,-size), activeType: 'biuro_ogloszen', id: 10 },
			{ type: 'grey', crd: new Array(size,0,size) },
			{ type: 'grey', crd: new Array(-size,0,size) },
			{ type: 'b', crd: new Array(0,size,0), animation: { id: 0, left: 10, top: -10, width: 50, height: 50 } },
			{ type: 'd', crd: new Array(size,0,0), animation: { id: 0, left: -40, top: -10, width: 50, height: 50 } },
			{ type: 'plus', crd: new Array(0,0,size), animation: { id: 0, left: -15, top: -6, width: 50, height: 50 } },
			{ type: 'a', crd: new Array(-size,0,0), animation: { id: 0, left: -65, top: -1, width: 50, height: 50 } },
			{ type: 'x', crd: new Array(0,-size,0), animation: { id: 0, left: 60, top: -1, width: 50, height: 50 } },
			{ type: 'o', crd: new Array(0,0,-size), animation: { id: 0, left: 35, top: -1, width: 50, height: 50 } }
		];
		
		var toPreload = Array();
		pointsArray.each(function(image) {
			if (!$defined(image.activeType)) return;
			toPreload.push(staticPath + 'css/default/img/cube-' + image.activeType + '.png');
		});
		var myImages = new Asset.images(toPreload);

		//coordinates of the container for the scene
		var container_coords = $('spaceScene').getCoordinates();
		var speed = 40000;
		//var justClicked = false;
		
		/*
		 declare the Moo3D scene:
		 - the rotation axis is given a slight angle to make it look like we're looking from higher
		 - the origins will simply help put our images at the right place on the page
		 - you could also use camera:{x,y,z,focalLength} to position the camera and/or change its focal length
		*/
		var scene = new Moo3D({rotationAxis: {x: 0, y: 0, z: 0}, origins: {x: container_coords.left + container_coords.width / 2 - 20, y: container_coords.top + container_coords.height / 2 - 130}, camera: {x: 0, y: 0, z: 0, focalLength: 500}});
		
		//array to contain 3D points
		var line = [];
		
		//number of images
		var points_nb = pointsArray.length;
		
		var userBrowsing = false;

		//go through all points
		pointsArray.each(function(image, i) {
			if (!image.type) return false;
		
			var image_width = 50;
			var image_height = 50;
			
			var cubeElement = new Element('img', { src: staticPath + 'css/default/img/cube-' + image.type + '.png' });
			cubeElement.mouseOn = false;
			
			if (image.activeType) {
				cubeElement.activeType = staticPath + 'css/default/img/cube-' + image.activeType + '.png';
				cubeElement.type = staticPath + 'css/default/img/cube-' + image.type + '.png';
				cubeElement.addClass('active');
				cubeElement.set('id', 'cubeElement' + image.id);
				
				cubeElement.addEvent('mouseenter', function() {
					//if (justClicked) return false;
					cubeElement.mouseOn = true;
					if (userBrowsing) $clear(userBrowsing);
					cubeElement.set('src', staticPath + 'css/default/img/cube-' + image.activeType + '.png');
					if (image.id) {
						showItem(image.id - 1);
					}
					(function(){
						if (isCurItem != image.id) {
							cubeElement.set('src', staticPath + 'css/default/img/cube-' + image.type + '.png');
						}
					}).delay(7000);
					speed = 150000;
					
					scene.objects.each(function(obj) {
						obj.each(function(point) {
							if (!point.animation) return;
							point.animating = true;
							var tmpFx = new Fx.Morph(point.element, {duration: 500, transition: Fx.Transitions.Quart.easeInOut}).start({
								'left': scene.options.origins.x + point.animation.left,
								'top': scene.options.origins.y + point.animation.top,
								'width': point.animation.width,
								'height': point.animation.height,
								'z-index': 1000100
							});
						});
					});
				});
				cubeElement.addEvent('mouseleave', function() {
					//if (justClicked) return false;
					cubeElement.mouseOn = false;
					userBrowsing = (function(){
						scene.objects.each(function(obj) {
							obj.each(function(point) {
								if (!point.animation) return;
								var tmpFx = new Fx.Morph(point.element, {duration: 300, transition: Fx.Transitions.Quart.easeInOut}).start({
									'left': point.projection.x + scene.options.origins.x,
									'top': point.projection.y + scene.options.origins.y,
									'width': point.projection.scale * point.animation.width,
									'height': point.projection.scale * point.animation.height,
									'z-index': Math.round(1000001 + point.projection.scale * 100)
								});
								speed = 150000;
								(function(){
									speed = 30000;
									point.animating = false;
								}).delay(300);
							});
						});
					}).delay(1600);
					//cubeElement.set('src', staticPath + 'css/default/img/cube-' + image.type + '.png');
					//speed = 30000;
					
					
				});
				/*cubeElement.addEvent('click', function() {
					if (justClicked) return false;
					justClicked = true;
					speed = 1000000;
					(function(){
						justClicked = false;
						speed = 40000;
					}).delay(2000);
				});*/
			}
			
			cubeElement.inject($('spaceScene'));
			
			/*
			 adding each image as a 3D point to line
			 - the element to be transformed
			 - x, y and z define the position in the scene
			 - the CSS modifiers accept a function that has 'this' referring to the point itself, or a fixed value
			*/
			line.push(
			{
				element: cubeElement,
				x: image.crd[0],
				y: image.crd[1],
				z: image.crd[2],
				animation: (image.animation ? image.animation : false),
				modifiers:
				{
					'left': function(){return this.projection.x + scene.options.origins.x;},
					'top': function(){return this.projection.y + scene.options.origins.y;},
					'width': function(){return this.projection.scale * image_width},
					'height': function(){return this.projection.scale * image_height},
					'opacity': function(){return 1},
					'z-index': function(){return Math.round(1000001 + this.projection.scale * 100);}
				}
			});
		});
		
		//add line to the scene
		scene.add(line);
		
		var mouseX = 300, mouseY = 300;
		
		var magicX = 0.3, magicY = 0.008;
		
		var animating = true;
		(function() {
			animating = false;
		}).delay(1000);

		var animateIt = function(){
			if (animating) {
				scene.options.rotationAxis.x += magicX;
				scene.options.rotationAxis.y += magicY;
			} else {
				if (mouseX != undefined){
					scene.options.rotationAxis.x += mouseX / speed;
				}
				if (mouseY != undefined){
					scene.options.rotationAxis.y += mouseY / speed;
				}
			}

			scene.render();
		};


		animateIt.periodical(32);
		
		
		$('spaceScene').addEvents({
			//change rotation axis depending on mouse position
			'mousemove': function(e) {
				e = new Event(e);
				//scene.options.rotationAxis.x = 0.1 + (container_coords.height / 2 - e.page.y + container_coords.top) / 800;
				//scene.options.rotationAxis.y = (0 - (container_coords.width / 2 - e.page.x + container_coords.left)) / 1000;
				mouseX = e.page.x - (container_coords.width / 2);
				mouseY = e.page.y - (container_coords.height / 2);
				
			}
		});
		
		//render the entire scene - could also do "scene.render(line);" which would only render the one object
		scene.render();
		
		
		(function(){
			
			
			(function(){
				
			}).delay(2000);
		}).periodical(8000);
	}
});

