/*
* Cache van javascripts.script.js
* Created: 2010-12-10 00:36:11
*/

/*
$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-10297964-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 && _gat) {
		var pageTracker = _gat._getTracker(google);
		pageTracker._initData();
		pageTracker._trackPageview();
	}
});

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

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

	if ($('menu')) {
		var menuel = $('menu');
		var lastel = menuel.getLast('li');
		var firstel = menuel.getFirst('li').getElement('a');
		var steps = [10, -5, 2, -1];
		var padding = 0;
		var maxtries;
		var lastelposok;

		// Add padding to all elements until the menu is too big, then remove padding
		steps.each(function(step) {
			lastelposok = lastel.getPosition().y;
			maxtries = 10;
			do {
				padding += step;
				menuel.getElements('a').setStyles({
					'padding-left': padding,
					'padding-right': padding
				});
				maxtries--;
			}
			while (maxtries > 0 && lastel.getPosition().y == lastelposok)
		});

		// Add padding to the first element until the menu is too big, then remove padding
		steps.each(function(step) {
			lastelposok = lastel.getPosition().y;
			maxtries = 10;
			do {
				padding += step;
				firstel.setStyles({
					'padding-left': padding
				});
				maxtries--;
			}
			while (maxtries > 0 && lastel.getPosition().y == lastelposok)
		});

		new mouseOvers();
	}

	$$('ul.metamenu').each(function(list) {
		list.getElements('li').each(function(el, i) {
			if (i > 0 && el.get('id') != 'english-flag') {
				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');

			if (el.get('href').substring(0, 7) == 'http://' || el.get('href').substring(0, 8) == 'https://') {
				newwin = newwin || el.get('href').indexOf('http://www.netwerkecologischemonitoring.nl');
			}

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

window.addEvent('load',function(){
	new pngSupport().replace();
});

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