$(function() {
  
  if($('#gmap').size() > 0)
  {
    $('#gmap').gMap({
      latitude : '43.57525',
      longitude : '-116.7910',
      zoom : 5,
      markers : [
        {latitude : '43.57525', longitude : '-116.7910'}
      ],
      icon:
          {
              image:              "/images/maps_icon.png",
              shadow:             "/images/maps_icon_shadow.png",
              iconsize:           [32, 32],
              shadowsize:         [52, 32],
              iconanchor:         [16, 32],
              infowindowanchor:   [8, 2]
          }
    });
    
  }
  
  
  // Add an event handler to the fake submit buttons in the forms
  $('[rel=submit]').live('click', function(evt) {
    evt.preventDefault();
    $(this).closest('form').submit();
  });
  
  // Catch submission of the google maps form and handle it asynchronously
  $('#route-box form').submit(function(evt) {
    evt.preventDefault();
    
    var source = trim($('#address').val());

    if( ! source)
    {
      alert('You haven\'t given us an address');
      return;
    }
    
    // plan route
    getDirections(source);
  });
  
  
  $('#btn-calendar').click(function(evt) {
    evt.preventDefault();
    var url = $(this).attr('href');
    $.colorbox({
	'href' : url,
	opacity : .35,
    preloading : true,
    transition : 'elastic'
	});  
  });
  
  // Gallery code
  $('.gallery ul a').colorbox({
    opacity : .35,
    preloading : true,
    transition : 'elastic',
    slideshow : false
  }); 
   
  $('.gallery .next').click(galleryNext);
  $('.gallery .prev').click(galleryPrev);
  $('.gallery li:first').addClass('current');
  $('.gallery li').each(function(){
    $(this).data('offset', $(this).offset().left);
  });
  
  $('.gallery li:first').addClass('current');
  
  
});

function galleryNext(evt)
{
  evt.preventDefault();
   
  var $gallery = $(this).closest('.gallery');
  var $ul = $gallery.find('ul');
  var index = $ul.find('.current').index();
  
  (index === 0) ? index += 4 : index += 5;
  
  galleryMove($ul, index);
    
}

function galleryPrev(evt)
{
  evt.preventDefault();
   
  var $gallery = $(this).closest('.gallery');
  var $ul = $gallery.find('ul');
  var index = $ul.find('.current').index() - 5;
  
  galleryMove($ul, index);  
}

function galleryMove(elm, curr)
{
  var total = elm.find('li').size();
  
  if (curr < 0)
    curr = 0;
    
  if (curr + 6 >= total)
    curr = total-6;

  var $next = elm.find('li:eq('+curr+')');
  
  if ($next.hasClass('current'))
    return false;
    
  var x = $next.data('offset')-90;
  
  if (curr === 0)
    x = 0;
  
  $next.addClass('current').siblings('.current').removeClass('current');
  
  elm.animate({
    'left' : -x
  }, {
    duration : 500
  });

}

function getDirections(from)
{
  window.open("http://maps.google.com/maps?saddr=" + from + "&daddr=43.57525,-116.7910", "GoogleWin", "menubar=1,resizable=1,scrollbars=1,width=750,height=500,left=10,top=10");
}

function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}













