(function($)
{
	window.addEvent('domready', function()
	{
		var previousPromoIndex = 0;
		var promoIndex = 0;
		var depthIndex = 1;
		var maxDimension =
		{
			x: 0,
			y: 0
		};
		var totalChildren = ($('promoInnerContainer').getChildren().length);
		var animate_lookbook_morph = new Fx.Morph('promoInnerContainer', {
			duration: 1000,
			transition: Fx.Transitions.Quint.easeOut,
			link: 'cancel'
		});
		var promoTimerDelay = 7000;
		var promoTimer = setInterval(advancePromo, promoTimerDelay);
		function resetTimer()
		{
			clearInterval(promoTimer);
			promoTimer = setInterval(advancePromo, promoTimerDelay);
		}
		function advancePromo()
		{
			previousPromoIndex = promoIndex;
			if (totalChildren < 2)
			{
				clearInterval(promoTimer);
				updatePromo();
			}
			else if (Math.min(promoIndex, totalChildren) <= promoLoaded)
			{
				resetTimer();
				if (++promoIndex == totalChildren) promoIndex = 0;
				updatePromo();
			}
			else
			{
				isWaiting = true;
				clearInterval(promoTimer);
			}
		}
		function updatePromo()
		{
			resizeImages();
			$$('.promo').setStyles(
			{
				zIndex: 1
			});
			$('promo' + previousPromoIndex).setStyles(
			{
				zIndex: 2
			});
			$('promo' + promoIndex).setStyles(
			{
				zIndex: 3,
				opacity: 0
			});
			new Fx.Morph($('promo' + promoIndex), {
				duration: 500,
				onComplete: function(passes_tween_element)
				{
					//updatePromoNavigation();
				},
				onStart: function()
				{
				}
			}).start(
			{
				'opacity': 1
			});
		}
		var promoLoaded = 0;
		var isWaiting = true;
		function loadImagesConsecutively(srcs, complete, error)
		{
			var imgs = [];

			function loadImage(i)
			{
				if ("undefined" !== typeof(srcs[i]))
				{
					$('promo' + i).setStyles(
					{
						opacity: 0
					});
					imgs[i] = new Asset.image(srcs[i], {
						onload: function()
						{
							promoLoaded = i;
							//console.log(this);
							this.set('src_width', this.get('width'));
							this.set('src_height', this.get('height'));
							this.addClass('image');
							this.setStyles(
							{
								position: "absolute"
							});
							$('promo' + i).adopt(this);
							resizeImages();
							if (isWaiting)
							{
								isWaiting = false;
								advancePromo();
							}
							loadImage(i + 1);
						}
					});
				}
			}
			loadImage(0);
		}
		function resizeImages()
		{
			$$('.promo').getElement('.image').each(function(el)
			{
				if (el)
				{
					var targetDimensions =
					{
						width: window.getSize().x,
						height: window.getSize().y
					};
					var sourceDimensions =
					{
						width: el.get('src_width'),
						height: el.get('src_height')
					};
					var sourceAspectRatio = sourceDimensions.width / sourceDimensions.height;
					var targetAspectRatio = targetDimensions.width / targetDimensions.height;
					if (sourceAspectRatio && targetAspectRatio)
					{
						if (targetAspectRatio > sourceAspectRatio)
						{
							sourceDimensions.height = targetDimensions.width / sourceAspectRatio;
							sourceDimensions.width = targetDimensions.width;
						}
						else
						{
							sourceDimensions.width = targetDimensions.height * sourceAspectRatio;
							sourceDimensions.height = targetDimensions.height;
						}
						sourceDimensions.x = targetDimensions.width / 2 - sourceDimensions.width / 2;
						sourceDimensions.y = targetDimensions.height / 2 - sourceDimensions.height / 2;
						el.setStyles(
						{
							top: sourceDimensions.y,
							left: sourceDimensions.x,
							width: sourceDimensions.width,
							height: sourceDimensions.height
						});
					}
				}
			});
		}
		window.addEvent('resize', resizeImages);
		loadImagesConsecutively(promoImages);
		$$('.promo').addEvents(
		{
			click: function()
			{
				if(this.get('href')) document.location = this.get('href');
			}
		});
		var hasVisited;
		hasVisited = Cookie.read('hasVisited');
		isMessagehasVisited = null;
		$('introMessage').setStyles({display: "block"});
		var introMessageSlide = new Fx.Slide('introMessage').hide();
		//if (hasVisited)
		if (isMessagehasVisited)
		{
			var myCookie = Cookie.write('hasVisited', 'true', {duration: 365});
			introMessageSlide.slideIn();
			$('introMessageCloser').addEvents({
				click: function(el) {
					introMessageSlide.slideOut();
				}
			});
		}
		$$('.fade').each(function(el)
		{
			var original = el.getStyle('opacity');
			var morph = new Fx.Morph(el, {
				'duration': '300',
				link: 'cancel'
			});
			el.addEvents(
			{
				'mouseenter': function()
				{
					morph.start(
					{
						'opacity': '.7'
					})
				},
				'mouseleave': function()
				{
					morph.start(
					{
						'opacity': '1'
					})
				}
			});
		});
	});
})(document.id);

