function logThis(logitem) {
  if (window.console) {
    console.log(logitem);
  }
}

function equalHeights(objColl) {
  var maxHeight = 0;
  objColl.each(function() {
    if ($(this).height() > maxHeight) { maxHeight = $(this).height()};
  })
  objColl.height(maxHeight);
}


$(document).ready(function() {
  
 $('#home-banner').cycle({
      timeout:  8000,
      speed:  1000
  })
  
  
  equalHeights($('ol.branch-results > li'));

  $('.show-hide-content').hide();
  $('.show-hide h4').toggle(function() {
    $('.show-hide h4').removeClass("open");
    $(this).addClass("open");
    $('.show-hide-content').hide();
    $(this).next('.show-hide-content').fadeIn('fast');
  }, function() {
     $(this).removeClass("open").next('.show-hide-content').fadeOut('fast');
  });
  
  /*
  $('.show-hide h4').click(function() {
    $('.show-hide h4').removeClass("open");
    $(this).addClass("open");
    $('.show-hide-content').hide();
    $(this).next('.show-hide-content').fadeIn('slow');
  })
*/
  
  $("table.event-calendar td").hover( function () {
    $(this).children("div.events-today").fadeIn("fast");
    },
    function () {
    $(this).children("div.events-today").fadeOut("fast");
    }
  );


  
 $("div.scrollable").scrollable({size: 5}).circular();
  
  
  //preload gallery images
  $("ul.items li a").each(function() {
    var newImgPath = $(this).attr("href");
    img = new Image();
    img.src=newImgPath;
  });
  
  
  $("ul.items li a").click(function() {
    
    //$(".main_project_image img").remove();
    if( !$(this).parents(".image_gallery").hasClass("opened") ) {
      
      $("<img class='gallery_main_img' src="+$(this).attr('href')+" alt=''></img>")
      .appendTo($(this).parents(".image_gallery").find(".main_image"));
      
      $(this).parents(".image_gallery").find(".main_image").slideDown();
      $(this).parents(".image_gallery").addClass("opened");
      $(this).parents(".image_gallery").find(".open_gallery").text("Click to close gallery");
      
      $.scrollTo($(this).parents(".image_gallery"),800);  
      return false;  
    }
    else {
      $(this).parents(".image_gallery").find(".main_image img").attr("src", $(this).attr('href'));
    }
    
    return false;
    
  });
  
  $("img.gallery_main_img").live("click", function(){
    var currentImg = $(this).parent().parent("div.image_gallery").find("ul.items a[href="+ $(this).attr('src') +"]");
    var currentRel = parseInt(currentImg.attr("rel"));  
    //
    logThis("This Image:"  + currentImg.attr("href") + " - (REL) " + currentRel);
    //
    var api = $(this).parent().parent("div.image_gallery").find("div.scrollable").scrollable({api: true, size:5});
    if(currentImg.hasClass("last")) {
      $(this).parents(".image_gallery").find(".main_image img").attr("src", $(this).parents(".image_gallery").find("ul li a.first").attr("href"));
      logThis("last image?");
    }
    else {
      var nextImage = currentImg.parent("li").next().find("a");
      logThis("Next Image:"  + nextImage.attr("href") + " - (REL) " + nextImage.attr("rel") );
      $(this).parents(".image_gallery").find(".main_image img").attr("src", $(this).parents(".image_gallery").find("ul li a[rel="+(currentRel+1)+"]").attr("href"));
    }
    api.next();
  });

  
  $("a.open_gallery").click(function() {
    
    //$(".main_project_image img").remove();
    
    if( !$(this).parents(".image_gallery").hasClass("opened") ) {
      
      $("<img class='gallery_main_img' src="+$(this).parent('.image_gallery').find('ul.items li:first-child a').attr('href')+" alt=''></img>")
      .appendTo($(this).parent(".image_gallery").find(".main_image"));
      $(this).parent(".image_gallery").find(".main_image").slideDown();
      $(this).text("Click to close gallery");
      
      $(this).parents(".image_gallery").addClass("opened");
      
      $.scrollTo($(this).parents(".image_gallery"),800);  
    }
    else {
        $(this).parent(".image_gallery").find(".main_image").slideUp( 500, function(){
            
            $(this).parents(".image_gallery").find(".main_image img").remove();
          });
        $(this).text("Click to see the full images");

        $(this).parents(".image_gallery").removeClass("opened");

        $.scrollTo($(this).parents(".image_gallery"),800);
    }
    
    return false;
    
  });
  
var fm = {};
fm.maps = new Array();
if (typeof ItemEditing == 'undefined') {
  $('div.map').each(function() {

    $(this).addClass('mapdimensions');
    var mapId = $(this).attr('id');
    
    var value = $(this).html();
    value = $.trim(value);
    
    var point = value.split(',');

    var lat = parseFloat(point[0]);
    var lon = parseFloat(point[1]);
    var zoom = parseFloat(point[2]);
    
    fm.maps[fm.maps.length] = new GMap2(document.getElementById(mapId));
    var m = fm.maps[fm.maps.length-1];
    
    var p = new GLatLng(lat, lon);
    m.setCenter(p, zoom);
      

    var marker = new GMarker(p);
    m.addOverlay(marker);  
  });
}


$("ul.archive li ul").hide();
$("ul.archive li h5,ul.archive li h6").toggle(function() {
  $(this).addClass("open").next("ul").slideDown("fast");
  return false;  
}, function() {
  $(this).removeClass("open").next("ul").slideUp("fast");  
  return false;
})



$("ul.nav > li").hover(
  function () {
    $(this).addClass("hover");
  }, 
  function () {
    $(this).removeClass("hover");
  }
);

  $("#v-ticker").jCarouselLite({
    vertical: true,
    hoverPause:true,
    visible: 2,
    auto:3000,
    speed:1000,
    timeOut: 3000
  });

  
  
});





