$(document).ready(function() {
  locationSettings = {
    gsy: {coords: {jsy: '101px 37px', iom: '101px 37px'},
          timeout: {jsy: 980, iom: 1650}},
    jsy: {coords: {gsy: '101px 37px', iom: '296px 42px'},
          timeout: {gsy: 980, iom: 1120}},
    iom: {coords: {gsy: '101px 37px', jsy: '296px 42px'},
          timeout: {gsy: 1650, jsy: 1120}}
  };
  var islands = ['gsy', 'jsy', 'iom'];
  var islandsLength = islands.length;
  var dottedLineImages = [];
  for(var i=0; i<islandsLength; i++) {
    dottedLineImages[i] = [];
    for(var j=0; j<islandsLength; j++) {
      if(i!=j) {
        dottedLineImages[i][j] = new Image();
        dottedLineImages[i][j].src = '/locn/images/landing-page/dotted_line_'
                                     islands[i]+'-'+islands[j]+'.gif';
      }
    }
  }
  redPulseImage = new Image();
  redPulseImage.src = '/locn/images/landing-page/red_pulse.gif';

  currentLocation = undefined;
  currentTimeout = undefined;
  $('#locations>li>a').css('background-image', 'none');


  $('#locations>li').mouseover(function() {
    clearTimeout(currentTimeout);
    var newLocation = this.id.substring(this.id.length-3);
    if(currentLocation) {
      $('#locations').css({
        backgroundImage: 'url(/locn/images/landing-page/dotted_line_'
                         +currentLocation+'-'+newLocation+'.gif)',
        backgroundPosition: locationSettings[currentLocation]
                            .coords[newLocation]
      });
      currentTimeout = setTimeout(
        "$('#locations').css('background-image', 'none');",
        locationSettings[currentLocation].timeout[newLocation]
      );
      $('#locations-'+currentLocation+'>a').css('background-image', 'none');
    }
    $('#locations-'+newLocation+'>a').css(
      'background-image', 'url(/locn/images/landing-page/red_pulse.gif)'
    );
    currentLocation = newLocation;
  });


  $(document).keypress(function(event) {
    if(currentLocation && (event.which==13 || event.keyCode==13)) {
      var url = $('#locations-'+currentLocation+'>a').attr('href');
      window.location = url;
      event.preventDefault();
      event.stopPropogation();
      return false;
    }
  });
});
