(function($) {

   var geocoder = null, map = null;
   function initialize(map_div) {
     if (GBrowserIsCompatible()) {
       geocoder = new GClientGeocoder();
       map = new GMap2(map_div);
       map.setUIToDefault() ;
     }
     else {
       alert('Uw browser ondersteunt deze functie niet.') ;
     }
   }

   $( function() {
	var page = $('body').attr('rel'),
	submenu = $('#submenu') ;

	$('#folder div > img').parent().addClass('plaatje').after('<div>', {'class':  'clearboth'}) ;

	// all external links in posters and news start in new frame
	$('#folder a[href^=http], #news a[href^=http]').each( function () { $(this).attr('target', '_blank') ; } ) ;

	submenu
	  .delegate('a.collapse', 'click',
		    function(e) {
		      $(this).parent().toggleClass('open') ;
		    } )
	  .delegate('.subsubmenu', 'click',
		    function(e) {
		      if ( $(e.target).is('li') ) {
			$(this).find('a:first').trigger('click') ;
			return false ;
		      }
		      return true ;
		    }) ;
	
	$('a[href=' + page + ']', submenu).eq(0)
	  .parent().addClass('current')
	  .closest('div').prev().click() ;

	$('#zoek_kno-arts button').live('click',
					function() {
					  window.location.href = '/publiek/vind' ;
					  return false ;
					}) ;

	var vind = $('.page_vind') ;
	if ( vind.length ) {
	  $(window).unload(function() { GUnload() ; });

	  var map_container = $('#map_container', vind),
	  map_div = map_container.find('.map_canvas').get(0) ;

	  $(window).load( function() {
		  initialize(map_div) ; 
		  map_container
		      .hide()
		      .css({ position: 'fixed',
				  top: '50%',
				  left: '50%',
				  'margin-top': '-270px', // half the height 
				  'margin-left': '-270px' // half the width
				  }) ;
	      } ) ;

	  $.each(['by_distance', 'by_name', 'by_hospital'],
		 function(i, name) {
		   vind.delegate('a.' + name, 'click', function() { 
				   vind
				     .find('a.toggle.active').removeClass('active').end()
				     .find('a.' + name).addClass('active').end()
				     .find('form.vind_form').hide().end()
				     .find('form.' + name).show()
				     .find('input').focus().end() ;
				   return false ;
				 }) ;
		 } ) ;
	  $('a.by_distance').click() ;
	  vind
	    .find('form').bind('submit',
		  function() {
		    var input = $(':text:first', this),
		    name = input.attr('name'),
		    value = input.val(),
		    display = function(text) {
		      $('#vind_list', vind).hide().html(text).fadeIn('slow') ;
		    } ;
		    
		    if ( name === 'postcode' ) {
		      var match = value.match(/(\d\d\d\d)\W*([A-z]{2})?/) ;
		      if (!match)
			alert('Vul een Nederlandse postcode in.') ;
		      else {
			var postcode = match[1] + ( match[2] ? ' ' + match[2].toUpperCase() : '') ;
			geocoder.getLatLng(
			  postcode + ', netherlands',
			  function(point) {
			    $.post('vind_arts',
				   { postcode: postcode, x: point.x, y: point.y },
				   display ) ;
			  }) ;
		      }
		    }
		    else {
		      var post_data = {} ;
		      post_data[name] = value ;
		      $.post('vind_arts', post_data, display ) ;
		    }
		    return false ;
		  }).end()
	    .delegate('a.show_map', 'click',
		      function() {
			var meta = $(this).metadata(),
			point = new GLatLng( meta.lat, meta.lng ) ;	
			map_container.show() ;
			map.setCenter(point, 14);
			map.addOverlay(new GMarker(point)) ;
			return false ;
		    } )
	    .delegate('a.close_map', 'click',
		      function() { map_container.hide() ; return false ; } ) ;
	}
      }) ;
 })(jQuery) ;

