var hStreet, hNumber, hCity, hPostal;
Behaviour.register({
	'#detail_right .more_info a': function(el) {
		var t = el.up('.more_info').down('.info_target');
		el.onclick = function() {

			if (el.open) {
				t.update('');
				el.open = false;
				return false;;
			}

			el.open = true;

			new Ajax.Request(el.href, {
				'method': 'get',
				onComplete: function(req) {
					var info = eval('(' + req.responseText + ')');
					if (info.status) {
						t.update(info.content);
						$A(t.getElementsByTagName('form')).each(function(f) {
							f.onsubmit = function() {
								try {
									ajaxSubmitForm(f, t);
								} catch (e) {
									debug(e.message + ' ' + e.line);
								}
								return false;
							};
						})
					} else {
						alert(info.content);
					}
				}
			});
			return false;

		};
	},
	'a.print': function(el) {
		el.onclick = function() {
			window.print();
			return false;
		}
	},
	'#nieuwsbrief form': function(el) {
		
		var inp = el.down('input[name=email]');
		if (inp.value == "") {
			inp.value = 'Uw email adres hier';
			inp.addClassName('placeholder');
			inp.onfocus = function() {
				inp.removeClassName('placeholder');
				inp.value 	= '';
				inp.onfocus = null;
			}
		}

		el.onsubmit = function() {

			var para = el.serialize(true);
			el.disable();
			new Ajax.Request(el.action, {
				method: el.method,
				parameters: para,
				onComplete: function(req) {
					var info = eval('(' + req.responseText + ')');
					if (info.status) {
						el.up('#nieuwsbrief').update(info.content);
						$('nieuwsbrief').style.backgroundImage = 'none';
					} else {
						el.enable();
						inp.focus();
						alert(info.content);
					}
				}
			});

			return false;
		}


	},
	'.threestate': function(el) {
		setThreeStateButton(el);
	},
	'a': function(el) {
		el.onfocus = function() {
			el.blur();
		}
	},
	'#sub_menu_content li div.name': function(el) {
		setHover(el, 'hover');
	},
	'.img img': function(el) {
		var min 		= 0.7;
		var duration 	= 0.2;
		el.observe('mouseover', function() {
			new Effect.Fade(el, { 
				from: 1.0,
				to: min,
				duration: duration
			});
		});
		el.observe('mouseout', function() {
			new Effect.Fade(el, { 
				from: min,
				to: 1.0,
				duration: duration
			});
		});
	},
	'#menu li': function(el) {
		setHover(el, 'hover');
		el.onclick = function() {
			document.location.href = el.down('a').href;
		}
	},
	'div.status_banner': function(el) {
		el.style.cursor = 'pointer';
		el.observe('click', function() {
			var url = el.parentNode.down('a').href;
			//document.location.href = url;
		});
        if (/MSIE/.test(navigator.userAgent) && !window.opera) {

			var img = el.getStyle('background-image');
			el.style.backgroundImage = 'none';
            el.style.backgroundPosition = '-900px';
            var img     = img.replace(/(url\("?|"?\))/g, '');
            var style   = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"',sizingMethod='crop');";
            el.style.filter = style;

        } else {
			el.style.backgroundPosition = '0px';
		}

	},
	'div.content_list div.blocks': function(el) {

		var totalWidth 	= 0;
		var maxWidth	= '620'; //el.getWidth();
		el.getElementsBySelector('div.block').each(function(b) {
			totalWidth += (b.getWidth() + 3);
			if (totalWidth >= maxWidth) {
				new Insertion.After(b, '<div style="clear: both; height: 5px;">&nbsp;</div>');
				totalWidth = 0;
			}
		});
	},
	'img': function(el) {
		setTimeout(function() {
			el.title = el.alt;
		}, 10);
		//if (typeof(el.title) == "undefined") el.title = el.alt;
	}
});

function setThreeStateButton(el) {
	
	var origImage 	= el.src;
	var hover		= el.src.replace(/.jpg$/, '_over.jpg');
	var down		= el.src.replace(/.jpg$/, '_down.jpg');

	el.observe('mouseover', function() {
		el.src = hover;
	});
	el.observe('mouseout', function() {
		el.src = origImage;
	});
	el.observe('mousedown', function() {
		el.src = down;
	});
	el.observe('mouseup', function() {
		el.src = hover;
	});

}
function setHover(el, className) {
	if (!$(el)) return false;
	$(el).observe('mouseover', function() {
		$(el).addClassName(className);
	});
	$(el).observe('mouseout', function() {
		$(el).removeClassName(className);
	});
}
var debug = function() {
	// do nothing
}
if (typeof(console) != "undefined") {
	debug = function(str) {
		console.log(str);
	};
}

function refreshStylesheets(loop) {
	var rnd = Math.round(Math.random()*500);
	$$('link').each(function(l) {
		try {
			l.href = l.href.replace(/\?.*$/, '') + '?rnd = ' + rnd;
		} catch(e) { };
	});

	if (loop > 1000) {
		setInterval(refreshStylesheets, loop);
	}
}
function ajaxSubmitForm(form, container) {
	debug(form);
	var para = Form.serialize(form, true);// form.serialize(true);
	debug($H(para).inspect());
	form.disable();
	new Ajax.Request(form.action, {
		method: form.method,
		parameters: para,
		onComplete: function(req) {
			form.enable();
			var info = eval('(' + req.responseText + ')');
			if (info.status) {
				container.update(info.content);
				$A(container.getElementsByTagName('form')).each(function(f) {
					f.onsubmit = function() {
						ajaxSubmitForm(f, container);
						return false;
					};
				})
			} else {
				alert(info.status);
			}
		}
	});
}
function showGoogleMap(target) {
    if ($(target)) return _showGoogleMap(target);

    Behaviour.addLoadEvent(function() {
        _showGoogleMap(target, address);
    });
}
function _showGoogleMap(target) {

    var map         = null;
    var geocoder    = null;
    var center      = null;
	var address		= $(target).innerHTML;
	if (!GBrowserIsCompatible()) return false;

	geocoder 	= new GClientGeocoder();
	geocoder.getLatLng(address.stripTags() + ', The Netherlands', function(point) {

		if (!point) {
			$(target).update('Adres niet gevonden');
			return false;
		};

		map = new GMap2(target);
		map.addMapType(G_SATELLITE_MAP);
		map.addMapType(G_HYBRID_MAP);

		map.setCenter(point, 28);

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());

		map.setMapType(G_HYBRID_MAP);

		var marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml('<div class="gmarker">' + address +  '</div>');
	})
}
