/*
* Cache van javascripts.script.js
* Created: 2011-02-21 15:26:58
*/

/*
$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-6735155-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('domready', function(e) {

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

	if (google && _gat) {
		var pageTracker = _gat._getTracker(google);
		pageTracker._initData();
		pageTracker._trackPageview();
	}

	new mouseOvers();

	$$('ul#mainmenu li').each(function(el, i) {
		if (i > 0) {
			new Element('li').set('html', '|').inject(el, 'before');
		}
	});

	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');
		}
	});

	new pngSupport().replace();

	$$('div.thumbs').each(function(div) {
		var imglarge = new Element('div').setStyle('width', '30000px').inject($(div.get('id')+'_large'));
		var thumbs = div.getElements('a');
		var imgsfx;
		var imgs;
		function showimage(idx) {
 			imgsfx.start(
 				'margin-left',
 				imglarge.getStyle('margin-left'),
 				-1 * idx * imglarge.getParent('div').getStyle('width').toInt()
 			);
			thumbs.removeClass('current');
			thumbs[idx].addClass('current');
		}
		thumbs.each(function(img, i) {
			new Asset.image(img.get('href')).inject(imglarge);
			img.addEvent('click', function(e) {
				new Event(e).stop();
				showimage(i);
			});
		});
		imgsfx = new Fx.Tween(imglarge, {
			duration: 300,
			transition: Fx.Transitions.Sine.easeInOut
		});
		if (thumbs[0]) {
			showimage(0);
		}
	});

	if ($('home_imagerotator')) {
		var images = $$('div#home_imagerotator img');
		var positions = [0, 1, 2];

		var imageinpos = [];
		positions.each(function(pos) {
			new Element('div', {
				'class': 'imgcontainer',
				'id': 'imgcontainer' + pos
			}).inject($('home_imagerotator'));

			if (images[pos]) {
				images[pos].inject($('imgcontainer' + pos));
				imageinpos[pos] = pos;
			}
		});

		if (images.length > positions.length) {

			var fx = new Fx.Elements(images, {
				duration: 1000
			});

			function shownextimage() {
				var imgid = -1;
				while (imgid < 0) {
					imgid = Math.floor(Math.random() * images.length);

					imageinpos.each(function(imgidx) {
						if (imgidx == imgid) {
							imgid = -1;
						}
					});
				}
			
				var pos = Math.floor(Math.random() * positions.length);
				var oldimg = imageinpos[pos];

				imageinpos[pos] = imgid;
				images[imgid].setStyles({
					'z-index': 2,
					'opacity': 0
				}).inject($('imgcontainer' + pos));
				images[oldimg].setStyle('z-index', 1);

				var fxobj = {};
				fxobj[oldimg] = {};
				fxobj[imgid]  = {};
				fxobj[oldimg]['opacity'] = [1, 0];
				fxobj[imgid]['opacity']  = [0, 1];
				fx.start(fxobj);
			}

			shownextimage.periodical(4000);
		}
	}

});

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 pngSupport = new Class({

	Implements: [Options],

	options: {
		transparentImage: '/images/pnghack/transparent.gif'
	},

	initialize: function(options) {
		this.setOptions(options);
		this.needed = Browser.Engine.trident4 && Browser.Platform.win;
	},

	replace: function(parentel) {
		if (!this.needed) return;
		if (!parentel) parentel = document;

		parentel.getElements('img').each(function(img) {
			if (img.src.indexOf(this.options.transparentImage) == -1 && img.src.indexOf("png") >= 0) {
				var oldsrc = img.get('src');
				img.set('src', this.options.transparentImage);
				img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldsrc + "',sizingMethod='scale')";
			}
		}, this);
	}
});
