/*
* Cache van javascripts.script.js
* Created: 2010-02-10 12:05:20
*/

/*
$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-7735185-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"));
}

var cookieopts = {
	'domain': 'www.beenhakker.nl',
	'path': ''
};

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

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

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

	if ($('textsize')) {
		[0, 1, 2].each(function(size) {
			new Element('a', {
				'href': 'Zoom',
				'class': 'zoom'+size,
				'html': 'A'
			}).addEvent('click', function(e) {
				new Event(e).stop();
				if ($('zoomstyle')) $('zoomstyle').dispose();
				new Asset.css('/zoom'+size+'.style.css', {'id': 'zoomstyle'});
				Cookie.write('zoom', size, cookieopts);
			}).inject($('textsize'));
		});
		var zoom = Cookie.read('zoom');
		if (zoom && zoom.toInt() > 0 && zoom.toInt() <= 2) {
			new Asset.css('/zoom'+zoom.toInt()+'.style.css', {'id': 'zoomstyle'});
		}
	}
	
	$$('ul#menu ul').each(function(ul) {
		var parent = ul.getParent('li');
		parent.setStyle('cursor', 'pointer');

		if (parent.hasClass('current')) {
			ul.setStyles({
				'overflow': 'hidden'
			});
		} else {
			ul.setStyles({
				'overflow': 'hidden',
				'height': 0
			});
		}

		parent.addEvent('click', function(e) {
			new Event(e).stop();
			var curheight = ul.getSize().y;
			if (curheight == 0) {
				ul.tween('height', curheight, ul.getScrollSize().y);
			} else {
				ul.tween('height', curheight, 0);
			}
		});
	});
	$$('ul#menu ul a').each(function(a) {
		a.addEvent('click', function(e) {
			new Event(e).stopPropagation();
		});
	});

	new mouseOvers();

	$$('ul#supernav 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');

			if (el.get('href').substring(0, 7) == 'http://' || el.get('href').substring(0, 8) == 'https://') {
				newwin = newwin || el.get('href').indexOf('http://www.beenhakker.nl');
			}
			
			var downloadsurl = '/downloads/';
			newwin = newwin || el.get('href').substring(0, downloadsurl.length) == downloadsurl;
		}
		if (newwin) {
			e.stop();
			window.open(el.href, '_blank');
		}
	});

	$$('div.thumbs').each(function(div) {
		var imglarge = new Element('div').setStyle('width', '3000px').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 ($('establishments')) {
		var defopen = -1;

		if ($('showestablishments')) {
			$('showestablishments').setStyle('cursor', 'pointer');
			$('showestablishments').addEvent('click', function(e) {
				$('establishments').setStyle('display', 'block');
				this.setStyle('display', 'none');
			});
		}

		var establishments = [];
		$$('#establishments a.toggler').each(function(a, i) {
			a.addEvent('click', function(e) {
				new Event(e).stop();
			});
			if (defopen == -1 && document.location.href.substr(document.location.href.indexOf('#')) == a.get('href')) {
				defopen = i;
			}

			establishments[i] = a.getParent('li').clone();
		});

		var accests = new Accordion($$('#establishments a.toggler'), $$('#establishments ul'), {
			'display': defopen,
			'show': defopen,
			'alwaysHide': true
		});

		if ($('map_nl_map')) {
			if (Browser.Engine.trident4) {
				$('map_nl').set('src', $('map_nl').get('src').replace('.png', '.gif'));
			}
			
			var defimage = $('map_nl').get('src');

			var map_nls = [];
			$$('#map_nl_map area').each(function(area) {
				map_nls.push(defimage.replace(/\/([a-z]+)\.([a-z]{3})$/, '/'+area.get('class')+'.$2'));
			});
			new Asset.images(map_nls);

			$$('#map_nl_map area').addEvent('mouseenter', function() {
				$('map_nl').set('src', defimage.replace(/\/([a-z]+)\.([a-z]{3})$/, '/'+this.get('class')+'.$2'));
			});
			$$('#map_nl_map area').addEvent('mouseleave', function() {
				$('map_nl').set('src', defimage);
			});
			$$('#map_nl_map area').addEvent('click', function(e) {
				new Event(e).stop();
				var classname = this.get('class');
				$$('#establishments a.toggler').each(function(a, i) {
					if (a.get('id') == classname) {
						accests.display(i);

						var establs = $$('#establishments ul');
						if ($('textbox')) {
							$('textbox').set('html', establishments[i].get('html'));
							$$('#textbox h6').each(function(h) {
								new Element('h6')
									.set('html', h.get('text'))
									.replaces(h);
							});
						}
					}
				});
			});
		}
	}

});

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