currentHeaderImage = 1;
var slideshowTimeout = null;
var slideshowTransition = 2000;
var slideshowInterval = 4000;
$(document).ready(function() {
  
  // open links with class 'popup' in new windows
  $('a.popup, li.print a').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
  
  $('#register-email').focus(function(){
    if (this.value == 'enter your email address here') this.value = '';
  }).blur(function(){
    if (this.value == '') this.value = 'enter your email address here';
  });
	$('#register-email').parents('form').submit(function(){
		if ($('#register-email').val() == 'enter your email address here')
			$('#register-email').val('');
		return true;
	});
  
  $('#register-submit').mouseover(function(){
    $(this).attr('src','/images/go_hover.png');
  }).mouseout(function(){
    $(this).attr('src','/images/go.png');
  });
  
  if ($('.header-images .header-image').length > 1)
  {
    $('.header-numbers a').click(function(){
      if (parseInt(this.innerHTML) != currentHeaderImage)
      {
        $('.header-numbers a.current').removeClass('current');
        $(this).addClass('current');
        $('.header-images .header-image:eq(' + (parseInt(this.innerHTML) - 1) + ')').stop().css({'z-index': 50, 'opacity': 1}).show();
        $('.header-images .header-image:eq(' + (currentHeaderImage - 1) + ')').css('z-index',51).fadeOut(slideshowTransition);
        currentHeaderImage = parseInt(this.innerHTML);
        slideshowTransition = 2000;
      }
      return false;
    });
    $('.header-numbers').fadeIn();
    
    if ($('.editable').length == 0)
    {
      slideshowTimeout = setTimeout('autoprogressSlideshow()', slideshowInterval);
      $('.header-numbers a').mouseover(function(){ slideshowTransition = 300; clearTimeout(slideshowTimeout); })
      $('.header-numbers a').mouseout(function(){ slideshowTransition = 2000; slideshowTimeout = setTimeout('autoprogressSlideshow()', slideshowInterval); })
    }
  }
  
});

function autoprogressSlideshow()
{
  $('.header-numbers a:eq(' + ((currentHeaderImage == $('.header-numbers a').length) ? 0 : currentHeaderImage) + ')').click();
  slideshowTimeout = setTimeout('autoprogressSlideshow()', slideshowInterval);
}

if (typeof(google) != 'undefined')
{
  var clPopup = document.createElement('DIV');
  clPopup.className = 'clinfowindow';
  clPopup.innerHTML = '<div class="clinfowindow-inner"><div class="clinfowindow-region">No Region</div></div>';
  
  var clPopupContent = [];
  function drawMarkers(regionName)
  {
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(clPopup);
    
    for (var i=0; i<destinations.length; i++)
    {
      if (parseFloat(destinations[i]['longitude']) != 0)
      {
        var pos = new google.maps.LatLng(destinations[i]['latitude'],destinations[i]['longitude']);
        var content = '<div class="clinfowindow-inner">';
        content += '<div class="clinfowindow-region">' + regionName + '</div>';
        content += '<div class="clinfowindow-title">' + destinations[i]['name'] + '</div>';
        content += destinations[i]['description'];
        content += '<a href="' + destinations[i]['link'] + '" class="clinfowindow-link">More Information</a>'
        content += '<a href="#" class="clinfowindow-close"></a>'
        content += '</div>';
      
        destinations[i]['marker'] = new google.maps.Marker({
           position: pos,
           map: map,
           title:destinations[i]['name'],
           icon: iconUrl
        });
        destinations[i]['marker']['destination'] = i;
        clPopupContent[i] = content;

        google.maps.event.addListener(destinations[i]['marker'], 'click', function(){
          posTmp = new google.maps.LatLng(this.position.lat() + 5,this.position.lng());
          map.panTo(posTmp);
        });
        
        google.maps.event.addListener(destinations[i]['marker'], 'mouseover', function(){
          $(clPopup).html(clPopupContent[this['destination']]).show().find('.clinfowindow-close').click(function(){
            $(clPopup).hide();
            return false;
          });
        });
      }
    }
  }
}