currentHeaderImage = 1;
var slideshowTimeout = null;
var slideshowTransition = 2000;
var slideshowInterval = 4000;
var slideshowTestimonialsTransition = 1500;
var slideshowTestimonialsInterval = 8000;

function setHomeMenuWidth()
{
  //alert('done');
  var home_menu_width = 10;
  $('.home-menu-list .home-menu-item').each(function() { home_menu_width += $(this).outerWidth( true ); $(this).css( 'height', '100%' ); });
  if ($.browser.msie && $.browser.version <= 6) { home_menu_width += 100; }
  $('.home-menu-list').css('width', home_menu_width);
  
  /*$('.home-menu-list').children().addClass('test');
  $('.home-menu-list').children().removeClass('test');*/

  //alert(home_menu_width);
}

$(document).ready(function() {
  
  $('.candl-item-link').click(function() {
    id = $(this).parent().attr('id').replace('candl-item-', '');
    $('#candl-item-popup-'+id).show();
    return false;
  });
  
  // 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');
  });
  
  $('.read-toggle').click(function (event) {
    var link = $(this);
    var caption = link.parent();
    var background = caption.siblings('.header-image-caption-background');
    var stored_opacity = link.data('original_height');
    
    if (stored_opacity == undefined)
    {
      link.data('original_height', caption.height());
      link.data('original_opacity', background.css('opacity'));
      
      link.siblings('.full').fadeIn();
      caption.animate({height: '341px'});
      /*background.css('filter', 'alpha(opacity = 100)');*/
      background.fadeTo(500, 1);
      link.html('<span>+</span> click to read less');
    }
    else
    {
      link.siblings('.full').fadeOut();
      caption.animate({height: stored_opacity});
      /*background.css('filter', 'alpha(opacity = ' + (stored_opacity) + ')');*/
      background.fadeTo(500, (stored_opacity / 100));
      link.html('<span>+</span> click to read more');
      
      link.data('original_height', null);
      link.data('original_opacity', null);
    }
    
    return false;
  });
  
  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); })
    }
  }
  
  /* add regiontitle highlight when we hover over the appropriate area */
  $(".regionarea").hover(function () {
                           var classes = this.className.split(' ');
                           //console.log(classes);
                           classes.splice($.inArray('regionarea', classes), 1);
                           for (k in classes)
                           {
                             if (classes[k] != 'default')
                               $('.' + classes[k] + ' .home-menu-title').addClass('regiontitle');
                           }
                         },
                         function () {
                           var classes = this.className.split(' ');
                           //console.log(classes);
                           classes.splice($.inArray('regionarea', classes), 1);
                           for (k in classes)
                           {
                             if (classes[k] != 'default')
                               $('.' + classes[k] + ' .home-menu-title').removeClass('regiontitle');
                           }
                         });
                         
  setTimeout('slideshowTestimonials(1)', slideshowTestimonialsInterval);
});

function slideshowTestimonials(current)
{
  var testimonials = $('#home-content .home-menu-text-container');
  /*console.log(testimonials);*/
  if (current >= testimonials.size())
    current = 0;
    
  $(testimonials).fadeOut(slideshowTestimonialsTransition);
  $(testimonials[current]).fadeIn(slideshowTestimonialsTransition);
  
  setTimeout('slideshowTestimonials(' + (current + 1) + ')', slideshowTestimonialsInterval)
}

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 linklabel = 'Read more on ' + destinations[i]['name'];
        
        if (destinations[i]['linklabel'] != undefined)
          linklabel = destinations[i]['linklabel'];
          
        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">' + linklabel + '</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;
          });
        });
      }
    }
  }
}
