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

  var empty = '', almostEmpty = ' ', init = {
          map: null, 
          gdir: null,
          geocoder: null,

          load: function() {
                  // Find it..
                  var right = $( '#content > div.right.auto-height' );
                  if( right.length ) {
                          right.css({
                                'height': ( right.parent().height() < 800 ? 800 : right.parent().height() ) + 'px'
                          });
                  }
                  
                  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 = $( 'li#misc > ul > li.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 prints = $( '.print_view' );
                  if( prints.length ) {
                          prints.click(function(){
                                  var cp = $( '.middle' ).clone();
                                  $( 'body' ).empty();
                                  $( 'body' ).css( 'padding', '10px' ).css({
                                      'font': 'normal 12px Verdana'
                                  }).append(cp);
                                  $( 'body' ).find( '.print_view' ).remove();
                                  $( 'body' ).find( '.block' ).prev().remove(); 
                                  $( 'body' ).find( '.block' ).remove(); 
                                  setTimeout(function(){
                                        window.location.reload();
                                  }, 100);
                          });
                  }
                  
                  var persons = $( 'ul.persons' );
                  if( persons.length ) {
                          var maxHeight = 0;
                          persons.find( 'li' ).each(function(){
                                  myHeight = $(this).height();
                                  if( myHeight > maxHeight )
                                         maxHeight = myHeight;
                          });
                          
                          // Update all li's
                          persons.find( 'li' ).css( 'height', maxHeight + 'px' );
                  }
                  
                  var langs = $( 'li.lang > label' );
                  if( langs.length ) {
                          langs.click(function(){
                               $(this).next().css( 'visibility', 'visible' );
                          });
                  }
                  
                  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' ).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/print.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
                                          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": lang + '_' + lang.toUpperCase() });
                                           }
                                   }).next().bind( 'click', function(){
                                           init.gdir.load("from: " + $(this).prev().val() + " to: " + $( '#vest' ).val(), { "locale": lang + '_' + lang.toUpperCase() });
                                   });
                                   
                                   $( '#vest' ).bind( 'change', function(){
                                           init.gdir.load("from: " + $( 'input#zipcode' ).val() + " to: " + $( '#vest' ).val(), { "locale": lang + '_' + lang.toUpperCase() });
                                   });
                          } 
                  }
                  
                  // Find required fields
                  $( 'input[required], select[required], textarea[required]' ).each(function(){
                          $(this).prev().html( $(this).prev().html() + ' *' );
                  });
          }
  }, markInvalid = function(isSelect, isFirst, form){
          // Set A link after input
          var key = this.attr( 'name' );
          if( isFirst ) {
                  //$( '<a />' ).attr( 'id', key ).attr( 'name', key ).attr( 'href', '#' + key ).insertBefore( this );
                  $( '<div />' ).addClass( 'form-message' ).append(
                          $( '<h3 />' ).append(
                                 $( '<a />' ).attr( 'id', 'foutopgetreden' ).attr( 'name', 'foutopgetreden' ).attr( 'href', '#foutopgetreden' )
                          ).append( 'Er is een fout opgetreden' )
                  ).append(
                          'U heeft niet alle verplichte velden ingevuld.'
                  ).append(
                          '<br />'
                  ).append(
                          '<br />'
                  ).append(
                          '<br />'
                  ).insertBefore( form );
                  window.location = window.location + '#foutopgetreden';
                  this.select().focus();
          }
          
          // Set it red
          this.css( 'margin-top', '-2px' ).css( 'outline', '0px' ).css( 'margin-left', '-2px' ).css( 'border', '2px solid red' );
          this.unbind();
          if( !isSelect ) {
                  this.bind( 'blur change', function(){
                          if( $(this).val() != empty
                           && $(this).val() != almostEmpty ) {
                                  $(this).css( 'border', '' ).css( 'margin-top', '0px' ).css( 'margin-left', '0px' ).unbind();
                          }
                  });
          }
          else {
                  this.bind( 'blur', function(){
                          if( $(this).val() != 0
                           && $(this).val() != empty ) {
                                  $(this).css( 'border', '' ).css( 'margin-top', '0px' ).css( 'margin-left', '0px' ).unbind();
                          }
                  });
          }
          
  }, checkForm = function(){
          // Search for inputs
          var valid = true, first = true, form = this, inputs = $(this).find( 'input[required], textarea[required]' );
          if( inputs.length ) {
                  inputs.each(function(){
                          if( ( $(this).val() == empty
                             || $(this).val() == almostEmpty
                           && valid ) ) {
                                  valid = false;
                                  markInvalid.apply($(this), [false, first, form]);
                                  first = false;
                          }
                  });
          }
          
          var selects = $(this).find( 'select[required]' );
          if( selects.length
           && valid ) {
                  inputs.each(function(){
                          if( ( $(this).val() == 0
                             || $(this).val() == empty
                           && valid ) ) {
                                  valid = false;
                                  markInvalid.apply($(this), [true, first, form]);
                                  first = false;
                          }
                  });
          }
          
          return valid;
  };
  
  $( window ).bind( 'load', init.load );

