/* Egdom Groep Javascript
	(c) 2006-2009 Omines - www.omines.com
	
	All rights explicitly reserved - unauthorized reproduction strictly prohibited
*/

// Compatibility stuff
Element.implement({
	effect: function(property, options){
		return new Fx.Tween(this, $extend({property: property}, options));
	}
});

// Site class - main active content wrapper
var Site = {
	start: function()
	{
		this.initTabs();
		this.initPaging();
		this.initLightboxes();
		this.initHeaderLinks();
		this.autoCompleteOff();
		if (Browser.Engine.trident4)
			this.LogoIE6();
	},
	LogoIE6: function()
	{
		var logoImage = $('header-logo').getElement('img');
		var logoImageUrl = logoImage.src;
		logoImage.src = logoImageUrl.replace('png', 'gif');
	},	
	initTabs: function()
	{
		$$('.tabs .tab').each(function (el) {
			el.addEvent('click', function(e) {
				el.getParent().getChildren('.tab').each(function(child) {
					child.removeClass('active');
					$(child.id+'cont').removeClass('active');
				});
				el.addClass('active');
				$(el.id+'cont').addClass('active');
				Cookie.write('tab', el.id, {duration:1});
			});
		}, this);
		var el = Cookie.read('tab');
		if(el && $(el)) {
			$(el).fireEvent('click');
			Cookie.dispose('tab');
		}
	},
	initPaging: function()
	{
		this.pagers = [];
		$$('.paging').each(function (el, idx) {
			this.pagers[idx] = new Pager(el);
		}, this);		
	},
	initLightboxes: function()
	{
		this.initLinks('contactlink', '/ajax/contact');
		this.initLinks('applicationlink', '/ajax/application');
		this.initLinks('loginlink', '/ajax/login');
		Lightbox.initialize();
	},
	initLinks: function(className, target)
	{
		$$('a.'+className).each(function(el, idx) {
			el.addEvent('click', function(e) {
				e.stop();
				Lightbox.showPage(target);
			}.bindWithEvent(this));
		}, this);
	},
	initHeaderLinks: function()
	{
		this.headerFx = new Array();
		$$('#header-menu .subitems a').each(function(el, idx) {
			this.headerFx[idx] = new Fx.Morph(el, {
				duration: 150,
				transition: Fx.Transitions.Linear,
				wait: false
			});
			el.addEvent('mouseenter', function(e) {
				this.headerFx[idx].start({'color' : '#FFFFFF', 'padding-left' : '21px'});
			}.bindWithEvent(this));
			el.addEvent('mouseleave', function(e) {
				this.headerFx[idx].start.delay(100, this.headerFx[idx], {'color' : '#D5D5D5', 'padding-left' : '16px'});
			}.bindWithEvent(this));
		}, this);
	},
	contactForm: function()
	{
	},
	autoCompleteOff: function()
	{
		$$('.nocomplete').each(function (el) {
			el.set('autocomplete', 'off');
		});			
	}
};

// Pager class - controls Javascript paged elements
var Pager = new Class({
	initialize: function(element)
	{
		this.root = element;
		this.pages = element.getChildren('.page');
		this.active = 0;
		if(this.pages.length > 1)
			this.createPager();
	},	
	perform: function()
	{
		this.pages.each(function (el, idx) {
			if(idx != this.active)
				el.addClass('hidden');
			else
				el.removeClass('hidden');
		}, this);
		this.pagelinks.getChildren('a').each(function (el, idx) {
			if(idx != this.active)
				el.removeClass('active');
			else
				el.addClass('active');
		}, this);
		if(this.active == 0)
			this.previous.addClass('hidden');
		else
			this.previous.removeClass('hidden');
		if(this.active == this.pages.length-1)
			this.next.addClass('hidden');
		else
			this.next.removeClass('hidden');		
	},
	createPager: function(element)
	{
		this.pager = new Element('div', {'class':'page-selection'}).injectInside(this.root);
		this.previous = new Element('a', {'class':'hidden','href':'#'}).injectInside(new Element('div', {'class':'previous'}).injectInside(this.pager)).set('text','vorige').addEvent('click',this.prevClick.bindWithEvent(this));
		this.next = new Element('a',{'href':'#'}).injectInside(new Element('div', {'class':'next'}).injectInside(this.pager)).set('text','volgende').addEvent('click',this.nextClick.bindWithEvent(this));
		this.pagelinks = new Element('div', {'class':'pages'}).injectInside(this.pager);
		for(var i = 0; i != this.pages.length; i++)
		{
			if(i != 0)
				this.pagelinks.appendText(' | ');
			new Element('a',{'href':'#'}).set('text',i+1).injectInside(this.pagelinks).addEvent('click',this.pageClick.bindWithEvent(this));
		}
		this.perform();
	},
	pageClick: function(e)
	{
		this.active = e.target.get('text')-1;
		this.perform();
		e.stop();
	},
	nextClick: function(e)
	{
		this.active++;
		this.perform();
		e.stop();
	},
	prevClick: function(e)
	{
		this.active--;
		this.perform();
		e.stop();
	}
});

//Lightbox class - allows floating popups with detailed information
var Lightbox = {
	initialize: function()
	{
		var closer = this.close.bind(this);
		this.overlay = new Element('div', {'id':'lb-overlay'}).injectInside(document.body).addEvent('click', closer);
		this.box = new Element('div', {'id':'lb-box'}).injectInside(document.body);
		this.closelink = new Element('a', {'id':'lb-closer'}).injectInside(this.box).addEvent('click', closer);
		this.content = new Element('div', {'id':'lb-content'}).injectInside(this.box);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500,wait:false}),
			box: this.box.effect('opacity', {duration: 500,wait:false})
		};
		this.overlay.setStyle('visibility','hidden');
		this.box.setStyle('visibility','hidden');
		$$('.lightbox').each(function(el, idx) {
			el.addEvent('click', this.clickLink.bindWithEvent(this));		
		}, this);
	},
	position: function()
	{
		this.overlay.setStyles({top:0,left:0,width:window.getScrollWidth(),height:window.getScrollHeight()});
		this.box.setStyles({display: '',top:(window.getScrollTop()+75)+'px'});
	},
	clickLink: function(e)
	{
		this.showPage(e.target.href);
		e.stop();
	},
	showPage: function(url)
	{
		this.position();
		new Request.HTML({
			url: url,
			method: 'get',
			update: $('lb-content'),
			onComplete: function() {
				this.fx.box.start(1);
			}.bind(this)
		}).send();
		this.fx.overlay.start(0, 0.5);
	},
	close: function()
	{
		this.fx.overlay.start(0);
		this.fx.box.start(0);
	}
};

// Validation functions
function validateContactForm()
{
	var errors = new Array();
	var valid = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
	if(!$('fullname').value.length)
		errors.push("- U heeft uw naam niet ingevuld");
	if(!$('email').value.length && !$('phone').value.length)
		errors.push("- Telefoonnummer of emailadres is verplicht");
	if($('email').value.length && !valid.test($('email').value))
		errors.push("- Ongeldig email adres");
	if($('message').value.length < 10)
		errors.push("- Uw bericht dient minimaal 10 karakters te bevatten");
	if(!errors.length)
	{
		new Request.HTML({
			url: '/contacted',
			method: 'post',
			data: $('contactform'),
			update: $('lb-content')
		}).send();
	}
	else
		alert(errors.join("\n"));
	return false;
}
function validateLoginForm()
{
	new Request.HTML({
		url: '/extranet',
		method: 'post',
		data: $('loginform'),
		onSuccess: function(html) {
			$('lb-content').set('text', '');
			$('lb-content').adopt(html);
		},
		onFailure: function() {
			alert("Ongeldige gebruikersnaam en/of wachtwoord");
		}
	}).send();
	return false;
}
function validateApplicationForm()
{
	var errors = new Array();
	var valid = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
	if(!$('fullname').value.length)
		errors.push("- U heeft uw naam niet ingevuld");
	if(!$('email').value.length && !$('phone').value.length)
		errors.push("- Telefoonnummer of emailadres is verplicht");
	if($('email').value.length && !valid.test($('email').value))
		errors.push("- Ongeldig email adres");
	if($('message').value.length < 10)
		errors.push("- Uw bericht dient minimaal 10 karakters te bevatten");
	if(!errors.length)
	{
		new Request.HTML({
			url: '/applied',
			method: 'post',
			data: $('applicationform'),
			update: $('lb-content')
		}).send();
	}
	else
		alert(errors.join("\n"));
	return false;
}

// Set event to start running active content when DOM is loaded
window.addEvent('domready', function() { Site.start(); });

