window.addEvent('domready', function() {
	if (Browser.Engine.gecko) {
		//$$('h3').setStyle('font-size', '16px');
	}

	/* Navigation animation */
	if($$('#nav').length > 0 && $$('#showcase').length > 0) {
		if (Browser.Engine.trident) {
			$$('#nav ul li ul').setStyle('background', '#777');
			$$('#nav ul li ul').setStyle('opacity', '0.9');
		}
	
		var itemCount = $('nav').getElements('li.level0').length;
		
		var mouseOn = false;
		var curItem = 0;
		var allShown = false;
		var mouseOnHeader = false;
		
		var opacity = 0;
		
		var trident = Browser.Engine.trident;
		
		$$('header').addEvent('mouseenter', function() {
			mouseOnHeader = true;
			allShown = true;
			showcaseFx.each(function(el, it) {
				el.image.pause();
				el.image.start({'opacity' : 1});
			});
		});
		
		$$('header').addEvent('mouseleave', function() {
			mouseOnHeader = false;
			allShown = false;
			showcaseFx.each(function(el, it) {
				if (it == curItem) return false;
				el.image.pause();
				el.image.start({'opacity' : opacity});
			});
		});
		
		$('nav').getElement('ul').addEvent('mouseenter', function() {
			mouseOn = true;
		});
		
		$('nav').getElement('ul').addEvent('mouseleave', function() {
			mouseOn = false;
		});
		
		var showcaseFx = new Array();
		$('nav').getElements('li.level0').each(function(el, it) {
			showcaseFx[it] = { 'image': new Fx.Morph(el.getElement('.color.'), {duration: 500, transition: Fx.Transitions.Quart.easeInOut}).set({'opacity' : opacity}),
							   'text': new Fx.Morph(el.getElement('.text'), {duration: 500, transition: Fx.Transitions.Quart.easeInOut}).set({'background-position' : '0 30px'}),
							   'color': new Fx.Morph(el.getElement('.text'), {duration: 500, transition: Fx.Transitions.Quart.easeInOut}).set({'color' : '#444'}) }
			if (el.getElement('.inside-wrapper')) {
				var abc = 0;
				if (trident) abc = 1;
				showcaseFx[it].submenu = new Fx.Morph(el.getElement('.inside-wrapper'), {duration: 500, transition: Fx.Transitions.Quart.easeInOut}).set({'height' : 0, 'opacity': abc});
				showcaseFx[it].subheight = el.getElement('.inside-wrapper').getElement('ul').getDimensions().height;
			}			
		});
		$('showcase').getElements('.item').each(function(el, it) {
			showcaseFx[it].showcase = new Fx.Morph(el, {duration: 500, transition: Fx.Transitions.Quart.easeInOut}).set({'opacity' : 0});
			el.setStyle('display', 'block');
		});
		showcaseFx[0].image.set({'opacity' : 1});
		showcaseFx[0].text.set({'background-position' : '0 0'});
		showcaseFx[0].color.set({'color' : '#444'});
		showcaseFx[0].showcase.set({'opacity' : 1});

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

		autoChange.periodical(5000);
		
		function nextItem() {
			var nextItem = curItem + 1;
			if (nextItem == itemCount) nextItem = 0;
			showItem(nextItem);
		}
		
		function showItem(it) {
			if (allShown) return false;
			showcaseFx[curItem].image.pause();
			showcaseFx[curItem].image.start({'opacity' : opacity});
			showcaseFx[curItem].text.pause();
			showcaseFx[curItem].text.start({'background-position' : '0 30px'});
			showcaseFx[curItem].color.pause();
			showcaseFx[curItem].color.start({'color' : '#444'});
			
			showcaseFx[it].image.pause();
			showcaseFx[it].image.start({'opacity' : 1});
			showcaseFx[it].text.pause();
			showcaseFx[it].text.start({'background-position' : '0 0'});
			showcaseFx[it].color.pause();
			showcaseFx[it].color.start({'color' : '#111'});
			
			if (trident) {
				showcaseFx[curItem].showcase.set({'opacity' : 0});
				showcaseFx[it].showcase.set({'opacity' : 1});
			} else {
				showcaseFx[curItem].showcase.pause();
				showcaseFx[curItem].showcase.start({'opacity' : 0});
				showcaseFx[it].showcase.pause();
				showcaseFx[it].showcase.start({'opacity' : 1});
			}
			
			curItem = it;
		}
		
		$$('#nav ul li.level0').each(function(el) {
			var itemIndex = parseInt(el.getChildren('a')[0].get('rel')) - 1;
			el.addEvent('mouseenter', function() {
				showItem(itemIndex);
				if (showcaseFx[itemIndex].submenu) {
					(function(){
						showcaseFx[itemIndex].submenu.pause();
						showcaseFx[itemIndex].submenu.start({'height': showcaseFx[itemIndex].subheight, 'opacity': 1});
					}).delay(300);
				}
			});
			if (showcaseFx[itemIndex].submenu) {
				el.addEvent('mouseleave', function() {
					(function(){
						var abcd = 0;
						if (trident) abcd = 1;
						showcaseFx[itemIndex].submenu.pause();
						showcaseFx[itemIndex].submenu.start({'height': 0, 'opacity': abcd});
					}).delay(300);
				});
			}
		});
		
		/* SHOW ALL ON START */
		showcaseFx.each(function(el, it) {
			if (it == curItem) return;
			el.image.pause();
			el.image.start({'opacity' : 1});
		});
		(function() {
			showcaseFx.each(function(el, it) {
				if (it == curItem) return;
				el.image.pause();
				el.image.start({'opacity' : opacity});
			})
		}).delay(2000);
		
	}
});
