var contentActive = true;
var hstyle = 'min-height';

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

	//pngfix voor IE6
	if(Browser.Engine.trident){
		if(Browser.Engine.version < 5){
			pngFix();
			hstyle = 'height';
		}		
	}
	
	//grootte van het contentvlak aanpassen
	sizeContent();
	
	//uitschuivend menuutje
	$$('.menu > li > a').addEvent('click', function(e){
		var list = this.getNext('ul');
		if(list){
			
			var morph = new Fx.Morph(this, {duration: 400});

			if(!this.hasClass('high')){
				morph.start({'top': 	40});
				this.addClass('high');
				if(list)	list.reveal();
			}else{
				if(list)	list.dissolve();
				morph.start({'top': 	87});
				this.removeClass('high');
			}
			
		}
		e.stop();
	});
		
	//ajaxify the crap outta ya
	$$('.menu a').addEvent('click', function(e){
		var link = this.get('href');
		
		$$('.menu a').removeClass('active');
		this.addClass('active');
		
		if(link != '#'){
		
			if(yySlide){
				yySlide.destroy('empty');
				yySlide = null;
				new Element('img', {'src': defaultImage}).inject('slideshow');
			}
		
			var gallery = false;
		
			if(link.contains('gallery')){
				gallery = true;
			}
			
			//achtergrondje aanpassen
			if(link.contains('relax')){
				$(document.body).set('class', '');
				$(document.body).addClass('relax');
			}else if(link.contains('wellness')){
				$(document.body).set('class', '');
				$(document.body).addClass('wellness');
			}else if(link.contains('outdoor')){
				$(document.body).set('class', '');
				$(document.body).addClass('outdoor');
			}else if(link.contains('horeca')){
				$(document.body).set('class', '');
				$(document.body).addClass('horeca');
			}else{
				$(document.body).set('class', '');
			}
		
			if(gallery && contentActive){
				$('content').tween('opacity', 0);
				$('contentdata').empty();
				contentActive = false;
			}else if(!gallery && !contentActive){
				$('content').tween('opacity', 1);
				contentActive = true;
			}
		
			//show loading spinner
			toggleActivity(1);
	
			if(gallery){
			
				new Request({
					'url': 		urlbase +'data/gallery.php',
					'data': 	'data='+ link,
					onComplete: function(){
						toggleActivity(0);
					}
				}).send();
			
			}else{
				//not a gallery link

				new Fx.Reveal($('contentdata'), {
					duration: 300,
					onComplete: 	function(){
						
						new Request.HTML({
							'url': 		urlbase +'data/content.php',
							'data': 	'data='+ link,
							'update': 	'contentdata',
							'evalScripts': 	true,
							onComplete: function(){
								$('contentdata').reveal();
								toggleActivity(0);
								sizeContent();
							}
						}).send();
						
					}
				}).dissolve();
			
			}
			
		}
		
		e.stop();
		
	});
	
});

var toggleActivity = function(show){
	
	$$('a[rel=external]').setProperty('target', '_blank');
	
}

var sizeContent = function(){
	
	var contentdim = $('content').getCoordinates();
	var windowdim = $(window).getSize();
	
	var minheight = windowdim.y - contentdim.top - 85; //padding
	$('content').setStyle(hstyle, minheight);
	
}

var pngFix = function(){
	
	var pngElements = $$('h1');
	
	pngElements.each(function(el){

		var imgURL = el.getStyle('background-image');
		var imgURLLength = imgURL.length;
	
		if(imgURL != 'none' && imgURL.substring(imgURLLength - 5, imgURLLength - 2) == 'png'){
			el.setStyles({
				background: '',
				height: (el.getSize().y.toInt() - el.getStyle('padding-bottom').toInt() - el.getStyle('padding-top').toInt()),
				filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='" + imgURL.substring(5,imgURLLength  - 2) + "')"
			});
		};

		if(el.get('tag') == 'img' && el.get('src').substring(el.get('src').length  - 3) == 'png'){
			var imgReplacer = new Element('input', {
				'styles': {
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='" + el.getProperty('src') + "')",
					'position': 'relative',
					'background': 'transparent'
				},
				'title': el.get('alt')
			});

			imgReplacer.setStyles(el.getStyles('padding','margin','border','height','width'));
			imgReplacer.setProperties(el.getProperties('id','class'));
			imgReplacer.disabled = true;
			imgReplacer.replaces(el);

		};

	});
	
}