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

  var 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() < 600 ? 600 : right.parent().height() ) + 'px'
                          });
                  }
                  
                  var search = $( 'li#misc > ul > li.search input' );
                  if( search.length ) {
                          search.bind( 'keyup', function( event ){
                                  if( event.keyCode == 13 )
                                          window.location = url + '/insolvency/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 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( 'Routedescription' ).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();">Routedescription</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();">Routedescription</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": 'en_EN' });
                                           }
                                   }).next().bind( 'click', function(){
                                           init.gdir.load("from: " + $(this).prev().val() + " to: " + $( '#vest' ).val(), { "locale": 'en_EN' });
                                   });
                                   
                                   $( '#vest' ).bind( 'change', function(){
                                           init.gdir.load("from: " + $( 'input#zipcode' ).val() + " to: " + $( '#vest' ).val(), { "locale": 'en_EN' });
                                   });
                          } 
                  }
          }
  };
  
  $( window ).bind( 'load', init.load );

