
  /*
  * Init object
  * Fixing minor things on the website
  */

  var init = {
          map: null, 
          gdir: null,
          geocoder: null,

          load: function() {
                  var imageCollection = $( '.image' );
                  if( imageCollection.children().length >= 2 ) {
                          $( '.image' ).cycle({
                                  fx: 'fade',
                                  slideExpr: 'img',
                                  timeout: 3000
                          });
                  } 
                  
                  var imageCollection = $( '#images' );
                  if( imageCollection.children().length >= 2 ) {
                          $( '#images' ).cycle({
                                  fx: 'fade',
                                  slideExpr: 'img',
                                  timeout: 3000
                          });
                  }
                  
                  var imageCollection = $( '.image_collection' );
                  if( imageCollection.children().length >= 2 ) {
                          $( '.image_collection' ).cycle({
                                  fx: 'fade',
                                  slideExpr: 'img',
                                  timeout: 3000
                          });
                  }
                  
                  var imageCollection = $( '#atmospheres' );
                  if( imageCollection.children().length >= 2 ) {
                          $( '#atmospheres' ).cycle({
                                  fx: 'fade',
                                  slideExpr: 'img',
                                  timeout: 3000
                          });
                  }
                  
                  
                  // Find it..
                  var right = $( '#content > div.right.auto-height' );
                  if( right.length ) {
                          right.css({
                                'height': right.parent().height()
                          });
                  }
                  
                  var search = $( '.search input' );
                  if( search.length ) {
                          search.bind( 'keyup', function( event ){
                                  if( event.keyCode == 13 )
                                          window.location = url + '/' + lang + '/search/' + encodeURIComponent( $(this).val() );
                          });
                  }  
                  
                  var homeDiv = $( 'div.isHome' );
                  if( homeDiv.length ) {
                          // Set mouseover of the images
                          homeDiv.find( 'img' ).each(function(){
                                  $( this ).bind( 'mouseover', function(){
                                          $(this).attr( 'src', $(this).attr( 'src' ).replace( /.png/g, '_hover.png' ) ).fadeIn();
                                  }).bind( 'mouseout', function(){
                                          $(this).attr( 'src', $(this).attr( 'src' ).replace( /_hover.png/g, '.png' ) ).fadeIn();
                                  });
                          });
                  }
                  
                  var gmap = $( 'div#gmaps' );
                  if( gmap.length ) {
                          if( GBrowserIsCompatible() ) {
                                  init.map = new GMap2(document.getElementById("gmaps"));
                                  init.map.setUIToDefault();
                                  
                                  // Add directions set to the end of the map
                                  var dr = $( '<div />' ).css( 'display', 'none' ).attr( 'id', 'dirs' );
                                  gmap.after(dr);
                                  $( '<h3 />' ).text( 'Routebeschrijving' ).appendTo(dr);
                                  $( '<div />' ).attr( 'id', 'directions' ).appendTo(dr);
                                  
                                  init.gdir = new GDirections(init.map, document.getElementById("directions"));
                                  GEvent.addListener(init.gdir, "load", function(){
                                         $( '#dirs' ).show();
                                  });
                                  GEvent.addListener(init.gdir, "error", function(er){  });
                                  
                                  init.geocoder = new GClientGeocoder();
                                  if ( init.geocoder ) {
                                          // Get addresses
                                          var addr1 = $( '#footer .address > dl:eq(0) > dd' ).text();
                                          init.geocoder.getLatLng(
                                                  addr1,
                                                  function(point) {
                                                          if (!point) {
                                                                  alert(address);
                                                          } else {
                                                                  init.map.setCenter(point, 9);
                                                                  var marker = new GMarker(point, {});
                                                                  GEvent.addListener(marker, "click", function(){
                                                                          marker.openInfoWindowHtml(addr1  + '<br /><br /><br /><a href="javascript:void(0);" onclick="$(\'#zipcode\').focus().select();">Routebeschrijving</a>');
                                                                  });
                                                                  init.map.addOverlay(marker);
                                                          }
                                          });
                                          var addr2 = $( '#footer .address > dl:eq(1) > dd' ).text();
                                          init.geocoder.getLatLng(
                                                  addr2,
                                                  function(point) {
                                                          if (!point) {
                                                                  alert(address);
                                                          } else {
                                                                  var marker = new GMarker(point, {});
                                                                  GEvent.addListener(marker, "click", function(){
                                                                          marker.openInfoWindowHtml(addr2 + '<br /><br /><br /><a href="javascript:void(0);" onclick="$(\'#zipcode\').focus().select();">Routebeschrijving</a>');
                                                                  });
                                                                  init.map.addOverlay(marker);
                                                          }
                                          });
                                  }

                          }
                          
                          // Get zipcode
                          var zp = $( 'input#zipcode' );
                          if( zp.length ) {
                                   zp.bind( 'keyup blur', function(event){
                                           if( event.keyCode == 13 ) {
                                                   init.gdir.load("from: " + $(this).val() + " to: " + $( '#vest' ).val(), { "locale": 'nl_NL' });
                                           }
                                   });
                                   
                                   $( '#vest' ).bind( 'change', function(){
                                           init.gdir.load("from: " + $( 'input#zipcode' ).val() + " to: " + $( '#vest' ).val(), { "locale": 'nl_NL' });
                                   });
                          } 
                  }
          }
  };
  
  $( window ).bind( 'load', init.load );

