window.addEvent('domready', function() {
	// Schuif menu maken
	var myAccordion = new Fx.Accordion($('leftMenu'), 'li span.toggler', 'li div.element', {
		opacity: false,
		show: showMenu,
		display: -1,
		onActive: function(toggler, element){
		},
		onBackground: function(toggler, element){
		}
	});
	
	// Fix voor IE7
	myAccordion.display(showMenu);
	
	// Maak CMS link
	$('footerLeft').addEvent('click', function() {
		window.open('http://cms.depoort.nl/index.php?id=116','CMS','status=no,toolbar=no,width=300,height=120');
	});
	
	// Foto album
	if($('photoalbum')) {
		// Geen logo in achtergrond tonen op producten pagina
		$('contentMiddle').setStyle('background-image', 'none');
		$('footerMiddle').setStyle('background-image', 'none');
		
		// Cache met alle teksten van de foto's. Voorkomt dat er steeds opnieuw een request wordt uitgevoerd
		var photoCommentsCache = {};
		var setPhotoComments = function(photoId) {
			// private functie die de teksten instelt
			var _setPhotoComments = function(id) {
				var data = photoCommentsCache[id];
				$('photoTitle').set('text', data.comment_short);
				$('photoText').set('html', data.comment_long.replace(/\r?\n/,'<br>'));
				
				Cufon.replace('#photoTitle', {fontFamily: 'Arial'});
				Cufon.replace('#photoText', {fontFamily: 'Arial'});
			}
			
			// Kijk of de foto in de cache voorkomt, zo niet dan een request uitvoeren
			// en dan alsnog de gegevens setten
			if(!photoCommentsCache[photoId]) {
				var request = new Request.JSON({
					url: location.pathname,
					onComplete: function(response) {
						photoCommentsCache[photoId] = response;
						_setPhotoComments(photoId);
					}
				}).get({'aktie': 'comments_get', 'foto_id': photoId});
			}
			else {
				_setPhotoComments(photoId);
			}
		}
		
		// Alle foto's selecteren
		var photos = $('photoalbum').getElements('li');
		
		photos.addEvent('mouseover', function(e) {
			var photoId = this.getStyle('background-image').replace(/[^\d]/g, '');
			setPhotoComments(photoId);
			$('contentRight').getElement('div.photo').setStyle('background-image', this.getStyle('background-image').replace('-k', '-g'));
		});
		
		// Eerste foto tonen
		if(photos.length > 0) {
			$('contentRight').getElement('div.photo').setStyles({'height': '244px'});
			photos[0].fireEvent('mouseover');
		}
	}
	
	// Slideshow
	if($('photoSlideshow')) {
		var photos 		= JSON.decode( $('photoSlideshow').get('title') );
		$('photoSlideshow').removeProperty('title');

		if(photos[0].length > 0) {
			var photoOffset = 0;
			var photoTimer	= photos[1];
			$('contentRight').getElement('div.photo').setStyles({'height': '244px'});
						
			$('photoSlideshow').addEvent('changePhoto', function() {
				if(this.getStyle('background-image') == 'none') {
					this.setStyle('background-image', 'url(\'/afbeeldingen/fotomod_'+photos[0][photoOffset]+'-g.jpg\')');
					var el = this;
					(function() {
						el.fireEvent('changePhoto')
					}).delay(photoTimer);
				}
				else {
					var fxIn = new Fx.Morph(this, {
						'duration': 'long',
						'onComplete': function() {
							if(this.element.get('opacity') == 1) {
								var el = this.element;
								(function() {
									el.fireEvent('changePhoto')
								}).delay(photoTimer);
							}
							else {
								photoOffset++;
								if(photoOffset == photos[0].length - 1) {
									photoOffset = 0;
								}
								this.element.setStyle('background-image', 'url(\'/afbeeldingen/fotomod_'+photos[0][photoOffset]+'-g.jpg\')');
								this.start({'opacity': [0, 1]});
							}
						}
					});
					fxIn.start({'opacity': [1, 0]});
				}
			});
			
			$('photoSlideshow').fireEvent('changePhoto');
		}
	}
});
