var sayings = Array()
sayings[1] = {'from' : 'Hsi-Tang, 1952-2002',         'saying' : 'Alhoewel goudstof kostbaar is, kan het, als het in je ogen komt, je gezichtsvermogen hinderen.' };
sayings[2] = {'from' : 'Marie Curie, 1867',           'saying' : 'Nothing in life is to be feared. It is only to be understood' };
sayings[3] = {'from' : 'Nelson Mandela, 1918',        'saying' : 'Education is the most powerful weapon which you can use to change the world.' };
sayings[4] = {'from' : 'Albert Einsten, 1879 - 1955', 'saying' : 'Logica brengt je van A naar B, verbeelding brengt je overal.' };

function shortLink(link) {
	if (link == undefined) {
		return '/Home';
	}
	if (link.indexOf('http://') != -1) {
		var parts = link.split('/');
		link = '';
		for (var i = 3 ; i < parts.length ; i++) {
			link += '/'+parts[i];
		}
	}
	return link;
}

var eTrainersClass = Class.create();
eTrainersClass.prototype = {
	initialize: function(url) {
		var agent       = navigator.userAgent.toLowerCase();
		if ((agent.indexOf("opera") != -1) || (agent.indexOf("safari") != -1)) {
			$('home_face').setStyle({top:'120px'});
		}
		this.agent          = agent;
		this.url            = url;
		this.homeImage      = 1;
		this.homeTabs       = [];
		this.homeTimer      = false;
		this.tabTimer       = false;
		this.originalHeight = false;
		this.clickEvent = this.onClick.bindAsEventListener(this);
		if (this.url == '/Home' || this.url == '/' || this.url == '') {
			this.showHome();
		} else {
			this.showDefault();
		}
		var onderwerp       = 0;
		this.convertLinks();
	},

	convertLinks: function() {
		var click = this.clickEvent;
		$$('a').each(function(element) {
			if (element.getAttribute('destination') == undefined && (element.href.indexOf('etrainers') != -1 || element.href.indexOf('e-trainers') != -1)) {
				element.setAttribute('destination', element.href);
				element.href = '#';
				Event.observe(element, 'click', click);
			} else if (element.href != '#' && element.href.indexOf('etrainers') == -1 && element.href.indexOf('e-trainers') == -1) {
				element.setAttribute('target','_blank');
			}
		});
	},

	highlightActive: function(url) {
		$$('a').each(function(element) {
			if (element.getAttribute('destination') != undefined) {
				var link = shortLink(element.getAttribute('destination'));
				if (link == url) {
					element.addClassName('active');
				} else {
					element.removeClassName('active');
				}
			}
		});
	},

	clearTimers: function() {
		if (this.tabTimer) {
			clearTimeout(this.tabTimer);
			this.tabTimer = false;
		}
		if (this.homeTimer) {
			clearTimeout(this.homeTimer);
			this.homeTimer = false;
		}
	},

	tabNext: function() {
		var prev = this.homeImage - 1;
		if (prev == 0) prev = 4;
		this.homeTabs[prev].removeClassName('onderwerp_active');
		this.homeTabs[this.homeImage].addClassName('onderwerp_active');
	},

	homeNext: function() {
		this.tabTimer  = false;
		this.homeTimer = false;
		var prev       = 'face'+this.homeImage;
		this.homeImage = this.homeImage < 4 ? this.homeImage + 1 : 1;
		var next       = 'face'+this.homeImage;
		new Effect.Fade($(prev),   { duration : 1.0 });
		new Effect.Appear($(next), { duration : 1.0, delay : 1.0 });
		new Effect.Fade($('home_from'), { duration : 1.0, afterFinish : function() {
			$('home_from').innerHTML = sayings[eTrainers.homeImage].from;
			new Effect.Appear($('home_from'), { duration : 1.0 });
		}});
		new Effect.Fade($('home_line'), { duration : 1.0, afterFinish : function() {
			$('home_line').innerHTML = sayings[eTrainers.homeImage].saying;
			new Effect.Appear($('home_line'), { duration : 1.0 });
		}});
		this.tabTimer  = setTimeout("eTrainers.tabNext()", 1100);
		this.homeTimer = setTimeout("eTrainers.homeNext()", 8000);
	},

	showHome: function() {
		this.clearTimers();
		$('home_face').setStyle({'display' : 'block'});
		var onderwerp  = 0;
		this.homeImage = 1;
		var tabs       = [];
		$$('#onderwerpen .onderwerp').each(function(element) {
			element.removeClassName('onderwerp_active');
			onderwerp++;
			tabs[onderwerp] = element;
		});
		this.homeTabs = tabs;
		$$('#home_face img').each(function(element) {
			element.setStyle({'display' : 'none'});
		});
		$('home_from').innerHTML = sayings[1].from;
		$('home_line').innerHTML = sayings[1].saying;
		$('face1').setOpacity(0.1);
		$('face1').setStyle({display:'block'});
		new Effect.Appear($('face1'), { duration : 1.0 });
		this.homeTabs[1].addClassName('onderwerp_active');
		this.homeTimer = setTimeout("eTrainers.homeNext()", 6000);
	},

	showDefault: function() {
		this.clearTimers();
		$('home_face').setStyle({'display' : 'none'});
	},

	onClick: function(event, url) {
		Event.stop(event);
		if (url == undefined && event) {
			var url = Event.element(event).getAttribute('destination');
		}
		YAHOO.util.History.navigate('page', shortLink(url));
	},

	loadPage: function() {
		if (eTrainers.url == '/Home' || eTrainers.url == '' || eTrainers.url == '/') {
			eTrainers.highlightActive('/Home');
			new Effect.Morph($('content'), {style:'height:383px', duration: 0.3, afterFinish : function(effect) {
				$('content').undoClipping();
				eTrainers.showHome();
				new Effect.Appear($('home_content'), { duration : 0.3});
				new Effect.Appear($('disclaimer'),   { duration : 0.3});

			}});
		} else {
			eTrainers.showDefault();
			eTrainers.highlightActive(eTrainers.url);
			new Ajax.Request('/Ajax?module=Json&method=get_page&path='+encodeURIComponent(eTrainers.url), { onComplete: eTrainers.onLoaded.bindAsEventListener(eTrainers), method : 'get'});
		}
	},

	onLoaded: function(transport) {
		var json = transport.responseText.evalJSON();
		$('page_title').update(json.title || '');
		$('page_text').update(json.content || '');
		$('page_intro').update(json.intro || '');
		$('page_module').update(json.module || '');
		$('related').setOpacity(0);
		var newHeight = $('page_content').getHeight() + 60;
		$('content').makeClipping();
		$('related_links').innerHTML = '';
		if (typeof(json.see_also) != 'undefined') {
			for (var i = 0 ; i < json.see_also.length ; i++) {
				var link  = json.see_also[i];
				var title = typeof(link.title) == 'undefined' ? ''  : link.title;
				var url   = typeof(link.url)   == 'undefined' ? '#' : link.url;
				new Insertion.Bottom($('related_links'), '<div><img src="/images/lees_meer.gif" alt="Lees meer"/><a href="'+url+'">'+title+'</a></div>');
			}
		}
		new Effect.Morph($('content'), {style:'height:'+newHeight+'px', duration: 0.3, afterFinish : function(effect) {
			$('page_content').setOpacity(0);
			$('page_content').setStyle({left:'0px'});
			new Effect.Appear($('page_content'), {duration: 0.3});
			new Effect.Appear($('disclaimer'), {duration: 0.3});
			if ($('related_links').innerHTML != '') {
				new Effect.Appear($('related'), {duration: 0.3, delay : 0.4});
			}
		}});
		this.convertLinks();
	},

	gotoPage: function(url) {
		if (typeof(url) != 'undefined' && url != null && url != '#') {
			this.animating = true;
			this.url       = shortLink(url);
			if (url == undefined) {
				YAHOO.util.History.navigate("etrainers", url);
			}
			this.originalHeight = $('content').getHeight();
			new Effect.Fade($('disclaimer'), { duration : 0.3 });
			if ($('page_content').visible()) {
				new Effect.Fade($('page_content'), {duration:0.3, afterFinish: function() {
					$('related').setOpacity(0);
					eTrainers.loadPage();
				}});
			} else {
				var current_face = 'face'+this.homeImage;
				new Effect.Fade($(current_face),   { duration : 0.3 });
				new Effect.Fade($('home_content'), { duration : 0.3, afterFinish: function() {
					eTrainers.loadPage();
				}});
			}
		}
	}
}
