/*
* Cache van javascripts.script.js
* Created: 2011-11-25 17:19:55
*/

/*
$Author: sjors $
$LastChangedDate: 2006-10-19 14:05:31 +0200 (do, 19 okt 2006) $
$Id: javascripts.js 28 2006-10-19 12:05:31Z sjors $
$Rev: 28 $
*/

var google = 'UA-7316242-1';
if (google) {
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
}

window.addEvent('load', function(e) {
	if (google) {
		var pageTracker = _gat._getTracker(google);
		pageTracker._initData();
		pageTracker._trackPageview();
	}
});

window.addEvent('domready', function(e) {

	$$('body').addClass('js');

	new mouseOvers();
	
	new imageFader($$('div#sfeerimgs img'), {duration: 2500, interval: 10000});

	document.addEvent('click', function(e) {
  	var e = new Event(e);
  	var el = $(e.target)
  	if (el.tagName.toLowerCase() != 'a') {
  		el = el.getParent('a');
  	}
  	var newwin = false;

		if (el) {
			newwin = newwin || el.hasClass('newwindow');
			newwin = newwin || el.hasClass('externlink');
			
			var downloadsurl = '/downloads/';
			newwin = newwin || el.get('href').substring(0, downloadsurl.length) == downloadsurl;
		}
		if (newwin) {
			e.stop();
			window.open(el.href, '_blank');
		}
	});

	if ($('total')) {
		function updateTotal() {
			var total = 0;
			suminputs.each(function(el) {
				if (el.get('value').length > 0) {
					total += el.get('value').toInt();
				}
			});
			$('total').set('value', total);
		}
		
		var suminputs = $$('input.sum');
		suminputs.each(function(inp) {
			inp.addEvent('keyup', updateTotal.bindWithEvent(inp));

			if ($('co_'+inp.get('id'))) {
				$('co_'+inp.get('id')).addEvent('click', function(e) {
					inp.set('disabled', !$('co_'+inp.get('id')).get('checked'));
					if (!$('co_'+inp.get('id')).get('checked')) {
						inp.set('value', 0);
						suminputs[0].fireEvent('keyup');
					} else {
						inp.addEvent('keyup', updateTotal.bindWithEvent(inp));
					}
				});
				if (!$('co_'+inp.get('id')).get('checked')) {
					$('co_'+inp.get('id')).fireEvent('click');
				}
			}
		});
		suminputs[0].fireEvent('keyup');
	}
});

var mouseOvers = new Class({
	initialize: function(elements) {
		elements = elements || $$('img.mouseover');
		elements.each(function(image) {
			if (image.hasClass('specialmo')) {
				image.set('src_mouseover', image.get('class').replace(/.*mouseover=/, ''));
				image.set('src_mouseout',  image.get('src'));
				image.addEvent('mouseenter', this.mouseover);
				image.addEvent('mouseleave', this.mouseout);
				new Asset.image(image.get('src_mouseover'));
			} else {
				var xsrc = image.src;
				if (xsrc.substring(xsrc.length-7, xsrc.length-3) == '_up.')	{
					image.set('src_mouseover', image.get('src').replace(/_up\.([a-z]{3})/, '_ov.$1'));
					image.set('src_mouseout',  image.get('src'));
				
					image.addEvent('mouseenter', this.mouseover);
					image.addEvent('mouseleave', this.mouseout);

					// Preload
					new Asset.image(image.get('src_mouseover'));
				}
			}
		}, this);
	},
	mouseover: function() {
		this.set('src', this.get('src_mouseover'));
	},
	mouseout: function()	{
		this.set('src', this.get('src_mouseout'));
	}
});

var imageFader = new Class({

	Extends: Fx.Elements,

	options: {
		auto: true,
		interval: 10000
	},

	initialize: function(elements, options) {
		this.elements = elements;
		this.setOptions(options);

		this.elements.setStyle('opacity', 0);

		if (this.elements[0]) {
			this.elements[0].setStyle('opacity', 1);
			if (this.elements[0].getParent().getStyle('position') != 'absolute' && this.elements[0].getParent().getStyle('position') != 'relative') {
				this.elements[0].getParent().setStyle('position', 'relative');
			}
		}

		this.curel = 0;
		if (this.options.auto) {
			this.auto.periodical(this.options.interval, this);
			this.auto();
		}
	},

	auto: function() {
		if (!this.elements[this.curel]) {
			this.curel = 0;
		}
		this.display(this.curel);
		
		this.curel += 1;
	},

	display: function(idx) {
		var obj = {};
		this.elements.each(function(el, i) {
			if (el.getStyle('opacity') != 0 && i != idx) {
				obj[i] = {'opacity': 0};
				el.setStyle('z-index', 1);
			} else if (i == idx) {
				obj[i] = {'opacity': 1};
				el.setStyle('z-index', 2);
			}
		});
		return this.start(obj);
	}
});
