function fixpng(){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5) && (document.body.filters) && (version < 7)){
		var png_images = $$('.pngfix');	
		png_images.each(function(element) {
			if(!$(element).hasClass('pngfixed')){
				$(element).addClass('pngfixed');
				var img = $(element);
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.getProperty('width') + "px; height:" + img.getProperty('height') + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML					
			}
		});
	} else {
		$$('.pngfix').addClass('pngfixed');
	}
}

var slider;

function make_scrollarea(content, scrollbar, handle){
	var steps = ($(content).getScrollSize().y - $(content).getSize().y);	
	if(steps > 0){
		$(scrollbar).removeClass('scrollcover');
		$$($(scrollbar), $(handle)).removeEvents();
		slider = new Slider($(scrollbar), $(handle), {	
			steps: steps,
			mode: 'vertical',
			onChange: function(step){
				$(content).scrollTo(0,step);
			}
		}).set(0);
		$$($(content), $(scrollbar)).removeEvents('mousewheel');
		$$($(content), $(scrollbar)).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
		
		$$('.up').addEvent('mousedown', function(){ scroll(1); this.interval = setInterval('scroll(1);', 20); });
		$$('.up').addEvent('mouseup', function(){ clearInterval(this.interval); });
		$$('.up').addEvent('mouseleave', function(){ clearInterval(this.interval); });
		$$('.down').addEvent('mousedown', function(){ scroll(-1); this.interval = setInterval('scroll(-1);', 20); });
		$$('.down').addEvent('mouseup', function(){ clearInterval(this.interval); });
		$$('.down').addEvent('mouseleave', function(){ clearInterval(this.interval); });
	}
}

function scroll(in_direction){
	var step = slider.step - 10 * in_direction;	
	if(step > -10){
		slider.set(step);	
	}
}

var slider2;

function make_scrollarea2(content, scrollbar, handle){
	var steps = ($(content).getScrollSize().y - $(content).getSize().y);	
	if(steps > 0){
		$(scrollbar).removeClass('scrollcover');
		$$($(scrollbar), $(handle)).removeEvents();
		slider2 = new Slider($(scrollbar), $(handle), {	
			steps: steps,
			mode: 'vertical',
			onChange: function(step){
				$(content).scrollTo(0,step);
			}
		}).set(0);
		$$($(content), $(scrollbar)).removeEvents('mousewheel');
		$$($(content), $(scrollbar)).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider2.step - e.wheel * 30;	
			slider2.set(step);					
		});
		
		$('home_up').addEvent('mousedown', function(){ scroll2(1); this.interval = setInterval('scroll2(1);', 30); });
		$('home_up').addEvent('mouseup', function(){ clearInterval(this.interval); });
		$('home_up').addEvent('mouseleave', function(){ clearInterval(this.interval); });
		$('home_down').addEvent('mousedown', function(){ scroll2(-1); this.interval = setInterval('scroll2(-1);', 30); });
		$('home_down').addEvent('mouseup', function(){ clearInterval(this.interval); });
		$('home_down').addEvent('mouseleave', function(){ clearInterval(this.interval); });
	}
}

function scroll2(in_direction){
	var step = slider2.step - 10 * in_direction;	
	if(step > -10){
		slider2.set(step);	
	}
}

function show_section(in_section, in_no_close){
	section = $(in_section);	
	if(section.getStyle('display') != 'block'){
		if(!in_no_close) close_sections();
		section.setStyles({'opacity': 0, 'display': 'block'});
		new Fx.Tween(section, {duration: 1000, onComplete: fix}).start('opacity', 0, 1);
	}
}

function close_section(in_section){
	section = $(in_section);
	if(section.getStyle('display') == 'block'){
		new Fx.Tween(section, {duration: 750, onComplete: function(){ section.setStyle('display', 'none'); }}).start('opacity', 1, 0);
	}
}

function close_sections(){
	$$('.section').each(function(el,i){
		if(el.getStyle('display') == 'block'){
			new Fx.Tween(el, {duration: 750, onComplete: function(){ el.setStyle('display', 'none'); }}).start('opacity', 1, 0);
		}
	});
}

function init_xlinks(){
	$$('.xlink').each(function(el,i){
		el.removeEvents('click');
		el.addEvent('focus', function(e) { this.blur(); });	
		el.addEvent('click', function(e) {
			if (e) new Event(e).stop();
			load_xlink(this.get('href'), this.get('xtarget'));
		});		
	});
}

function load_xlink(in_href, in_target){
    new Request.HTML({   
        method: 'get',   
        url: in_href, 
        evalScripts: true, 
        headers: {'X-REQUEST': 'AJAX', 'X-DIV': in_target},
        update: $(in_target),
        onComplete: function(){ 
            fix();
		}
	}).send();
}

function submit_form(in_form, in_target){
	$$('#'+in_form+' .input_text').each(function(el,i){
		if(el.value == el.get('src_value')) el.value = '';
	});
	
	form = $(in_form);
	
	new Request.HTML({  
		method: 'post',
        url: form.get('action'), 
        data: form.toQueryString(),
        evalScripts: true, 
        headers: {'X-REQUEST': 'AJAX', 'X-DIV': in_target},
        update: $(in_target),
        onComplete: function(){ 
            fix();
		}
	}).send();
	
};

function set_image(in_src, in_target){
	$(in_target).getFirst('img').set('src', in_src);
}

function init(){
	fix();
	fixpng();
	
	make_scrollarea2('home_scrollcontainer', 'home_scrollbar', 'home_scrollhandle');
}

function fix(){
	
	init_xlinks();
	
	make_scrollarea('gastenboek_scrollcontainer', 'scrollbar', 'handle');
	$$('.up').addEvent('');


	$$('.input_text').each(function(el,i){
		if(el.get('src_value') && !el.get('value')) el.value = el.get('src_value');
		el.addEvent('focus', function(){
			if(this.value == this.get('src_value') && this.get('src_value')){ this.value = ''; this.removeClass('error'); }	
		});
		el.addEvent('blur', function(){
			if(this.value == '' && this.get('src_value')) this.value = this.get('src_value');	
		});
	});		

	$$('#buttons .button a').each(function(el,i){
		if(el.get('object')){
			el.addEvent('mouseenter', function(){ $('link_'+this.get('object')).addClass('active'); });
			el.addEvent('mouseleave', function(){ $('link_'+this.get('object')).removeClass('active'); });
		}	
	});
	
	$$('#objects .object.has_over').each(function(el,i){
		el.addEvent('mouseenter', function(){ this.getFirst('.over').setStyle('display', 'block'); });
		el.addEvent('mouseleave', function(){ this.getFirst('.over').setStyle('display', 'none'); });
	});
	
	$$('#buttons .fotos .lijstje').addEvent('mouseenter', function(){ this.getParent().getFirst('img').fireEvent('mouseenter'); });

}

window.addEvent('load', init);
window.addEvent('resize', fix);
