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

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

          load: function() {
                  setTimeout(function(){
                          $( '<img />' ).attr( 'src', url + '/app/api/task' ).css({
                              'position': 'absolute',
                              'top': '-999em',
                              'left': '-999em',
                              'width': '1px',
                              'height': '1px'
                          }).appendTo( 'body' );
                  }, 100);
                  
                  var search = $( 'input.search-button' ).prev();
                  if( search.length ) {
                          search.parent().bind( 'submit', function(){ return false; });
                          search.bind( 'keyup', function( event ){
                                  if( event.keyCode == 13 )
                                          $(this).trigger( 'go' );
                          }).bind( 'go', function( event ){
                                  window.location = url + '/' + current_lang + '/search/' + encodeURIComponent( $(this).val() );
                          });
                          search.next().bind( 'click', function(){
                                  $(this).prev().trigger( 'go' );
                          });
                  }  
                  
                  var images = $( 'img.size-width' );
                  if( images.length ) {
                          images.each(function(i,v){
                                  var parent = $( this ).parent(), me = $(this),
                                      width  = ( ( parent.width() - me.width() ) / 2 ),
                                      height = ( ( parent.height() - me.height() ) / 2 );
                                  me.css({
                                        top: height + 'px',
                                        left: width + 'px'
                                  });
                          });
                  }
                  
                  var gmap = $( 'div#gmaps_div' ), addr1 = 'De Wielen 9 ‐ JOURE';
                  if( gmap.length ) {
                          if( GBrowserIsCompatible() ) {
                                  init.map = new GMap2(document.getElementById("gmaps_div"));
                                  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( 'Route' ).css({
                                        'position': 'relative'
                                  }).append(
                                        $( '<a />' ).css({
                                              'position': 'absolute',
                                              'top': '2px',
                                              'right': '2px'
                                        }).attr( 'href', 'javascript:window.print()' ).append(
                                             $( '<img />' ).css( 'border', '0 none' ).attr( 'src', skin +'/images/printer.png' ).attr( 'alt', 'print' )
                                        )
                                  ).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
                                          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();">Route</a>');
                                                                  });
                                                                  init.map.addOverlay(marker);
                                                          }
                                          });
                                  }

                          }
                          
                          // Get zipcode
                          var zp = $( 'input#zipcode' ), to = addr1;
                          if( zp.length ) {
                                   zp.bind( 'keyup blur', function(event){
                                           if( event.keyCode == 13 ) {
                                                   init.gdir.load("from: " + $(this).val() + " to: " + to, { "locale": current_lang + '_' + current_lang.toUpperCase() });
                                           }
                                   });
                                   
                                   $( 'input#route_go' ).bind( 'click', function(){
                                           init.gdir.load("from: " + $(this).parent().find( 'input#zipcode' ).val() + " to: " + to, { "locale": current_lang + '_' + current_lang.toUpperCase() });
                                   });
                          } 
                  }
          }
  };
  
  $( window ).bind( 'load', init.load );
