/**
 *	Script for Entix template animations
 *		copyright 2008-2011 Entix, http://www.entix.nl/
 */
var animTime = 1000;

function closePageAnimation() {
	var baseHref = $$('base')['0'].get('href');
	var page = $('main');
	var logotop = $('logo');
	if (page) {
		var fxPage = new Fx.Morph(page, {'transition': Fx.Transitions.Back.easeInOut, 'duration': animTime});
		if (baseHref && logotop) { // back to front
			$$('a[href]').each(function(el) {
				var url = el.get('href');
				if (url != null) {
					if (url == baseHref) {
						el.addEvent('click', function(e) {
							e.stop();
							logotop.fade('out');
							fxPage.start({'margin-top': '395px', 'opacity': [1,0]});
							(function() {
								Cookie.write('EntixReferrer', window.location, {domain: URI.base.get('host')});
								window.location = e.target.href;
							}).delay(animTime*.75);
						});
					}
				}
			});
		}
		// to other page
		$$('nav .menublock li a[href]').each(function(el) {
			var url = el.get('href');
			if (url != null) {
				if (url != baseHref) {
					el.addEvent('click', function(e) {
						e.stop();
						fxPage.start({'margin-top': '395px', 'opacity': [1,0]});
						(function() {
							Cookie.write('EntixReferrer', window.location, {domain: URI.base.get('host')});
							window.location = e.target.href;
						}).delay(animTime*.75);
					});
				}
			}
		});
	}
}

function enterPageAnimation() {
	var page = $('main');
	var logobig = $('logo_big');
	var logotop = $('logo');
	if (page && logobig && logotop) {
		var fxPage = new Fx.Morph(page, {'transition': Fx.Transitions.Quad.easeOut, 'duration': animTime});
		var fxLogob = new Fx.Morph(logobig, {'transition': Fx.Transitions.Cubic.easeIn, 'duration': animTime*.9});
		var fxLogot = new Fx.Tween(logotop, {'transition': Fx.Transitions.Sine.easeInOut, 'duration': animTime});
		fxPage.start({'margin-top': '30px', 'opacity': [.2,1]});
		fxLogob.start({'top': '-125px', 'opacity': [1,0]});
		(function() {
			logotop.fade('hide'); // opacity 0
			logotop.setStyle('display', 'block'); // display on
			fxLogot.start('opacity', 1); // fade in
		}).delay(animTime*.75);
	}
}

function switchPageAnimation() {
	var page = $('main');
	if (page) {
		var fxPage = new Fx.Morph(page, {'transition': Fx.Transitions.Quad.easeOut, 'duration': animTime});
		fxPage.start({'margin-top': '30px', 'opacity': [.2,1]});
	}
}

function introFpAnimation() {
	var animTrans = Fx.Transitions.Quad.easeIn;
	var logobig = $('logo_big');
	var menu = $$('nav')[0];
	var footer = $$('footer')[0];
	if (logobig && menu && footer) {
		var fxLogob = new Fx.Tween(logobig, {'transition': animTrans, 'duration': animTime});
		var fxMenu = new Fx.Tween(menu, {'transition': Fx.Transitions.Quad.easeOut, 'duration': animTime});
		var fxFooter = new Fx.Tween(footer, {'transition': animTrans, 'duration': animTime});
		/* logo anim */
		logobig.fade('hide');
		logobig.setStyle('display', 'block');
		fxLogob.start('opacity', 1);
		/* menu anim */
		var menuSlides = [];
		menu.getElements('li').each(function(el,i) {
			if (el.getStyle('display') == 'none') {
				el.show();
				menuSlides[i] = new Fx.Slide(el, {mode: 'horizontal'}).hide();
			}
		});
		(function() {
			fxMenu.start('left', '255px').chain(
				function() {
					menu.getElements('.line')[0].show();
					menuSlides.each(function(slide, i) {
						(function() { slide.slideIn(); }).delay((i-1)*animTime*.45);
					});
				}
			);
		}).delay(animTime*.9);
		/* footer anim */
		footer.fade('hide'); // opacity 0
		footer.setStyle('display', 'block'); // display on
		(function() {
			fxFooter.start('opacity', 1); // fade in
		}).delay(animTime*4);
	}
}

function enterFpAnimation() {
	var animTrans = Fx.Transitions.Quad.easeOut;
	var logobig = $('logo_big');
	var menu = $$('nav')[0];
	if (logobig && menu) {
		var fxLogob = new Fx.Morph(logobig, {'transition': animTrans, 'duration': animTime});
		var fxMenu = new Fx.Tween(menu, {'transition': animTrans, 'duration': animTime});
		fxLogob.start({'top': '85px', 'opacity': [0,1]});
		fxMenu.start('top', '250px');
	}
}

function fpAnimation() {
	var animTrans = Fx.Transitions.Back.easeInOut;
	var menu = $$('nav')[0];
	if (menu) {
		var fxMenu = new Fx.Tween(menu, {'transition': animTrans, 'duration': animTime});
		menu.getElements('a').addEvent('click',function(e){
			e.stop();
			fxMenu.start('top', '495px');
			(function() {
				Cookie.write('EntixReferrer', window.location, {domain: URI.base.get('host')});
				window.location = e.target.href;
			}).delay(animTime*.75);
		});
	}
}

/* domready event */
window.addEvent('domready',function(){
	if ($$('body')[0].hasClass('frontpage')) {
		fpAnimation();
	} else if ($('container').hasClass('refhome')) {
		enterPageAnimation();
	} else if ($('container').hasClass('refint')) {
		switchPageAnimation();
	}
	
	if (!$$('body')[0].hasClass('frontpage')) {
		closePageAnimation();
	} else if ($('container').hasClass('refint')) {
		enterFpAnimation();
	} else {
		introFpAnimation();
	}
});
/* onload event */
window.addEvent('load',function(){
	Cookie.dispose('EntixReferrer', {domain: URI.base.get('host')});
});

