/*
* Cache van javascripts.script.js
* Created: 2010-06-11 13:56:18
*/

/*
$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 = '';
if (document.location.href.indexOf('gids.nl') > 0) {
	google = 'UA-4544761-1';
} else if (document.location.href.indexOf('gids.be') > 0) {
	google = 'UA-4544761-2';
}
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) {

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

	$$('a.newwindow').each(function(node) {
		node.addEvent('click', opennewwin);
	});
	$$('a.externlink').each(function(node) {
		node.addEvent('click', opennewwin);
	});

	$$('div.thumbnails').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);
		}
	});

	$$('ul.imagefader').each(function(el) {
		var fader = new imageFader(
			$(el.get('id')+'_images').getElements('img'), {
				duration: 500,
				auto: false,
				link: 'cancel'
			}
		);
		el.getElements('a').addEvent('mouseenter', function(e) {
			fader.display($(this.get('id')+'_img'));
		});
		el.addEvent('mouseleave', function(e) {
			fader.display($(el.get('id')+'_images').getElement('img'));
		});
		el.fireEvent('mouseleave');
	});

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

	if (Browser.Engine.trident) {
		if (Browser.Engine.version <= 4) {
			$$('ul#menu li.current').each(function(el) {
				el.addClass(el.get('class').replace(' ', '_'));
				el.removeClass('current');
			});
		}
	}

	if ($('searchstudio')) {
		var fields = ['country', 'province', 'city', 'name'];
		fields.each(function(fld, i) {
			if ($(fld)) {
				$(fld).addEvent('change', function(e) {
					for (var k=1; fields[i+k]; k++) {
						var values = '';
						fields.each(function(fld) {
							if ($(fld)) {
								values += '&' + fld + '=' + encodeURIComponent($(fld).get('value'));
							}
						});
						
						new Request({
							'url': $('searchstudio').get('action'),
							'method': 'post',
							'autoCancel': true,
							'onSuccess': function(respText, respXML) {
								var optionflds = respXML.getElementsByTagName('option');
								var numresponses = optionflds.length;
								var updatefield = $(respXML.getElementsByTagName('field')[0].getAttribute('value'));
								updatefield.set('html', '');
								for (var j=0; j<numresponses; j++) {
									new Element('option', {
										'value': optionflds[j].getAttribute('value')
									})
										.set('html', optionflds[j].getAttribute('name'))
										.inject(updatefield);
								}
							}
						}).send('field=' + fields[i+k] + values);
					}
				});
			}
		});

		$('searchstudio').addEvent('submit', function(e) {
			if (!$('searchextra') || $('searchextra').getStyle('height').toInt() == 0) {
				new Event(e).stop();
				var url = $('searchstudio').get('action');
				fields.each(function(fld, i) {
					if ($(fld)) {
						url += '/' + encodeURIComponent($(fld).get('value'));
					}
				});
				window.location.href = url;
			}
		});
	}
	if ($('searchextra')) {
		var sefx = new Fx.Tween($('searchextra'));
		sefx.set('height', 0);
		$('searchextra_button').addEvent('click', function(e) {
			if ($('searchextra').getStyle('height').toInt() == 0) {
				sefx.start('height', $('searchextra').getScrollSize().y);
				this.set('value', 'Eenvoudig zoeken');
			} else {
				sefx.start('height', 0);
				this.set('value', 'Uitgebreid zoeken');
			} 
		});
	}

	$$('div.program').each(function(el) {
		new Element('a', {
			'href': '#'
		})
			.set('html', '&gt; Bekijk het programma van deze kookstudio &lt;')
			.injectBefore(el)
			.addEvent('click', function(e) {
				var clone = el.clone().setStyle('display', 'block');
				new Event(e).stop();

				if (!$('bodyoverlay')) {
					new Element('div', {
						'id': 'bodyoverlay',
						'styles': {
							'opacity': 0.8,
							'width': window.getScrollWidth()+'px',
							'height': window.getScrollHeight()+'px'
						}
					}).injectInside(document.body);
				} else {
					$('bodyoverlay').setStyles({
						'width': window.getScrollWidth()+'px',
						'height': window.getScrollHeight()+'px',
						'display': 'block'
					});
				}
				clone.addClass('popup');
				clone.injectInside(document.body);
				clone.setStyle('top', window.getScroll().y + 160);

				clone.getElements('a').each(function(node) {
					node.addEvent('click', opennewwin);
				});

				var aclose = new Element('a', {
					'class': 'close',
					'href': '#'
				}).injectInside(clone);
				aclose.addEvent('click', function(e) {
					new Event(e).stop();
					clone.setStyle('display', 'none');
					$('bodyoverlay').setStyle('display', 'none');
				});
				aclose.innerHTML = 'sluiten';
			});
		el.setStyle('display', 'none');
	});

	if ($('kbwdeelnemers')) {
		//var details = $('details');
		new Accordion('ul#kbwdeelnemers li', 'div.deelnemers_prov');
		$$('ul#kbwdeelnemers li').each(function(prov) {
			prov.addEvent('click', function(e) {new Event(e).stop();});
		});
		$$('div.deelnemers_prov li').each(function(dln) {
			dln.addEvent('mouseenter', function() {
				dln.addClass('hover');
			});
			dln.addEvent('mouseleave', function() {
				dln.removeClass('hover');
			});
		});
	}

	$$('.expandinglist').each(function(list) {
		var defopen = -1;

		var togglers = list.getElements('a.toggler');
		togglers.each(function(a, i) {
			a.addEvent('click', function(e) {
				if (e) new Event(e).stop();
				if (a.getParent('div.faqblock').hasClass('faqblock-open')) {
					list.getElements('div.faqblock-open').removeClass('faqblock-open');
				} else {
					list.getElements('div.faqblock-open').removeClass('faqblock-open');
					a.getParent('div.faqblock').addClass('faqblock-open');
				}
			});
			if (defopen == -1 && document.location.href.substr(document.location.href.indexOf('#')) == a.get('href')) {
				defopen = i;
			}
		});
		list.getElements('a.toggler2').each(function(a, i) {
			a.addEvent('click', function(e) {
				new Event(e).stop();
				togglers[i].fireEvent('click');
			});
		});

		var listacc = new Accordion(togglers, list.getElements('div.details'), {
			'display': defopen,
			'show': defopen,
			'alwaysHide': true,
			duration: 300
		});

		list.getElements('a.close').each(function(a, i) {
			a.addEvent('click', function(e) {
				new Event(e).stop();
				togglers[i].fireEvent('click');
			});
		});
	});

	if ($('studiovdweek')) {
		$('studiovdweek').removeClass('studiovdweek').addClass('studiovdweek-close');
		$('studiovdweek').getElement('a.close').addEvent('click', function(e) {
			new Event(e).stop();
			$('studiovdweek').removeClass('studiovdweek-open').addClass('studiovdweek-close');
		});
		$('studiovdweek').getElement('a.open').addEvent('click', function(e) {
			new Event(e).stop();
			$('studiovdweek').removeClass('studiovdweek-close').addClass('studiovdweek-open');
		});
	}

	$$('a.mov360').each(function(movlink) {
		movlink.addEvent('click', function(e) {
			new Event(e).stop();
			if (!$('bodyoverlay')) {
				new Element('div', {
					'id': 'bodyoverlay',
					'styles': {
						'opacity': 0.9,
						'width': window.getScrollWidth()+'px',
						'height': window.getScrollHeight()+'px'
					}
				})
				.injectInside(document.body)
			}

			var appletTag = '<applet\n' +
 'codebase = "/mov360/39" \n ' +
 'code     = "com.easypano.tourweaver.TWViewerApplet.class"\n ' + 
 'archive  = "tw2.jar"\n ' + 
 'name     = "TWViewer" \n ' + 
 'width    = "540"\n ' + 
 'height   = "350"\n ' + 
 'hspace   = "0"\n ' + 
 'vspace   = "0"\n ' + 
 'align    = "middle"\n ' + 
 'mayscript = "true"\n ' + 
 '> \n' + 
'<PARAM name = lwImg value = "wait.jpg">\n ' + 
 '<PARAM name = lwBgColor value = "255,255,255,255">\n ' + 
 '<PARAM name = lwBarBgColor value = "255,255,255,255">\n ' + 
 '<PARAM name = lwBarColor value = "255,122,192,67">\n  ' + 
 '<PARAM name = lwBarBounds value = "51,292,460,7">\n ' + 
 '<PARAM name = iniFile value = "config.bin">\n ' + 
 '<PARAM name = skin.archive value = "images.zip">\n ' + 
 '<PARAM name = defaultMovie value = "SlideShow">\n ' + 
 '<PARAM name = isInitFullScreen value = "false">\n ' + 
 '<PARAM name = isShowToolbar value = "true">\n ' + 
 '<PARAM name = isAutoRunMovie value = "true">\n ' + 
 '<PARAM name = hasBgSound value = "false">\n ' + 
 '<PARAM name = hasSceneSound value = "false">\n ' + 
 '<PARAM name = hasMovieSound value = "false">\n ' + 
 '<PARAM name = hasActionSound value = "false">\n ' + 
 '</applet>\n<p>Wacht tot het plaatje in beeld komt. Houd de muisknop ingedrukt en beweeg over het plaatje.</p>';
			new Element('div', {
				'id': 'appletcontainer',
				'styles': {
					'position': 'absolute',
					'top': '50%',
					'left': '50%',
					'margin-top': '-175px',
					'margin-left': '-270px',
					'border': '1px solid #0050f0',
					'background-color': '#ffffff'
				}
			}).injectInside(document.body);
			$('appletcontainer').innerHTML = appletTag;
			new Element('a', {
				'id': 'closeappcont',
				'class': 'fakebutton readmore',
				'href': '#',
				'events': {
					'click': function(e) {
						new Event(e).stop();
						$('bodyoverlay').dispose();
						$('appletcontainer').dispose();
					}
				}
			}).injectInside($('appletcontainer'));
			$('closeappcont').innerHTML = 'Sluiten';
		});
	});

	if ($('persons') && $('giftPackage') && $('total') && $('price') && $('priceGift')) {
		var updatePrice = function() {
			var price = $('price').get('value') * $('persons').get('value').toInt();
			price = price + $('priceGift').get('value') * ($('giftPackage').get('checked')?1:0);
			price = price.toFixed(2).replace('.', ',');
			$('total').set('html', price);
		};
		$('persons').addEvent('keyup', updatePrice.bindWithEvent());
		$('giftPackage').addEvent('change', updatePrice.bindWithEvent());
		updatePrice();
	}
});

function opennewwin(e) {
	var el = this.tagName.toLowerCase() == 'a' ? this : this.getParent('a');
	if (el) {
		new Event(e).stop();
		window.open(el.href, '_blank');
	}
}

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

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

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