window.addEvent('domready', function() {
	
	rotateShow();
	
	if ($chk($$('ul.links-afbeeldingen-woningen'))) {
		var preloads = new Array();
		$$('ul.links-afbeeldingen-woningen').each(function(el){
			el.getChildren('li').each(function(item){
				var ipath  = item.getProperty('alt');
				if (ipath != '' && ipath != null) {
					preloads.extend([ipath]);
				}
			});
		});
		if (preloads.length > 0) {
			var myImages = new Asset.images(preloads);
			if ($chk($$('container-woningen-tmp'))) {
				$('container-woningen-tmp').setStyle('display', 'none');
				$('container-woningen').setStyle('display', 'block');
			}
		}
	}
	
	if ($chk($('accordion'))) {
		var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
			opacity: false,
			alwaysHide: true,
			fixedHeight: false,
			display: -1,
			onActive: function(toggler, element){
				toggler.setStyle('color', '#FFF');
				toggler.setStyle('background-image', 'url(assets/templates/tuyn/images/bg-accordeon-on.jpg)');
				toggler.setStyle('background-repeat', 'no-repeat');
				toggler.setStyle('background-position', 'top');
			},
			onBackground: function(toggler, element){
				toggler.setStyle('color', '#FFF');
				toggler.setStyle('background-image', 'url(assets/templates/tuyn/images/bg-accordeon-off.jpg)');
				toggler.setStyle('background-repeat', 'no-repeat');
				toggler.setStyle('background-position', 'top');
			}
		});
	}

	if ($chk($$('ul.links-afbeeldingen-woningen'))) {
		$$('ul.links-afbeeldingen-woningen').each(function(w) {
			var c = w.getFirst('li');
			if ($chk(c.getProperty('dl'))) {
					$$('div.ad'+c.get('id')).setStyle('display', 'block');
					$$('div.ad'+c.get('id')).getFirst('a').set('href', c.getProperty('dl'));
			}
		});
		if ($chk($$('li.suggestie'))) {
			$$('li.suggestie').addEvent('click', function() {
				$$('img.a'+this.get('id')).set('src', this.get('alt'));
				if ($chk($('t'+this.get('id').substring(1)))) {
					$('t'+this.get('id').substring(1)).removeClass('active');
				}
				if ($chk($('v'+this.get('id').substring(1)))) {
					$('v'+this.get('id').substring(1)).removeClass('active');
				}
				$(this.get('id')).addClass('active');
				if ($chk(this.getProperty('dl'))) {
					$$('div.ad'+this.get('id')).setStyle('display', 'block');
					$$('div.ad'+this.get('id')).getFirst('a').set('href', this.getProperty('dl'));
				} else {
					$$('div.ad'+this.get('id')).setStyle('display', 'none');
				}
			});
		}
		if ($chk($$('li.technisch'))) {
			$$('li.technisch').addEvent('click', function() {
				$$('img.a'+this.get('id')).set('src', this.get('alt'));
				if ($chk($('s'+this.get('id').substring(1)))) {
					$('s'+this.get('id').substring(1)).removeClass('active');
				}
				if ($chk($('v'+this.get('id').substring(1)))) {
					$('v'+this.get('id').substring(1)).removeClass('active');
				}
				$(this.get('id')).addClass('active');
				if ($chk(this.getProperty('dl'))) {
					$$('div.ad'+this.get('id')).setStyle('display', 'block');
					$$('div.ad'+this.get('id')).getFirst('a').set('href', this.getProperty('dl'));
				} else {
					$$('div.ad'+this.get('id')).setStyle('display', 'none');
				}
			});
		}
		if ($chk($$('li.verdieping'))) {
			$$('li.verdieping').addEvent('click', function() {
				$$('img.a'+this.get('id')).set('src', this.get('alt'));
				if ($chk($('s'+this.get('id').substring(1)))) {
					$("s"+this.get('id').substring(1)).removeClass('active');
				}
				if ($chk($('t'+this.get('id').substring(1)))) {
					$('t'+this.get('id').substring(1)).removeClass('active');
				}
				$(this.get('id')).addClass('active');
				if ($chk(this.getProperty('dl'))) {
					$$('div.ad'+this.get('id')).setStyle('display', 'block');
					$$('div.ad'+this.get('id')).getFirst('a').set('href', this.getProperty('dl'));
				} else {
					$$('div.ad'+this.get('id')).setStyle('display', 'none');
				}
			});
		}
	}
	
	if ($chk($('contactForm'))) {
		new FormCheck('contactForm');
	}
	
});

function rotateShow() {
	if ($chk($('hero-images'))) {
		if ($('hero-images').getChildren('<li>').length > 1) {
			var f = new Fader('hero-images');
			f.start(); 
		}
	}
}

var Fader = new Class({
	implements: Options,
	options: {
		pause: 3000,
		duration: 800,
		loop: true
	},
	initialize: function(container, options) {
		this.container = $(container); 
		this.lists = this.container.getChildren('<li>');
		for(var i = 0; i < this.lists.length; i++) {
			this.lists[i].setStyles({
				'z-index': String(this.lists.length - i), 
				'position': 'absolute',
				'opacity' : 0,
				'margin-left' : '2px'
			});
		}
		this.lists[0].setStyle('opacity',1).show();
		this.next = 0;
	},
	start: function() {
		//this.show();
		this.periodical = this.show.bind(this).periodical(this.options.pause);
	},
	stop: function() {
		$clear(this.periodical);
	},
	show: function() {
		if (!this.options.loop && this.next == this.lists.length - 1)
			this.stop();
		this.next = (this.next == this.lists.length-1) ? 0 : this.next + 1;
		var prev = (this.next == 0) ? this.lists.length - 1 : this.next - 1;
		this.lists[this.next].fade('in');
		this.lists[prev].fade('out');
	}
}); 

function load() {
	if (GBrowserIsCompatible()) {
		if (document.getElementById("map")) {
			var map = new GMap2(document.getElementById("map"));
			var geocoder = new GClientGeocoder();
			var gm_html = '';
			map.setUIToDefault();
			map.removeMapType(G_HYBRID_MAP);
			map.removeMapType(G_PHYSICAL_MAP);
			if (gm_addr != '') {
				gm_html += gm_addr+'<br />';
			}
			if (gm_zip != '') {
				gm_html += gm_zip+'<br />';
			}
			if (gm_city != '') {
				gm_html += gm_city+'<br />';
			}
			showAddress(map,geocoder, gm_addr+','+gm_zip+','+gm_city, '<strong>De Hooghe Tuyn</strong><br />'+gm_html);
		}
	}
}

function showAddress(map,geocoder,address,html) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 14);
        var marker = createMarker(point,html);
        map.addOverlay(marker);
      }
    }
  );
}

function createMarker(point,html){
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(decodeURIComponent(html));
	});
	return marker;
}
