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


$(document).ready(function() {
  
  $('ul#home-banner').cycle({ 
      timeout:  8000, 
      speed:  1000
  });
  
  $("#home-tweet").tweet({
      username: "ashscotland",
      join_text: "auto",
      count: 1,
      auto_join_text_default: "",
      auto_join_text_ed: "",
      auto_join_text_ing: "",
      auto_join_text_reply: "",
      auto_join_text_url: "",
      loading_text: "loading tweets..."
  });


   

  $('label.accesslabel').each(function(){
    var lblText = $(this).text();
    var lblInput = 'input#' + $(this).attr('for');
    $(this).hide();
    $(lblInput)
        .attr({title: lblText})
        .focus(function(){
            if($(this).val() === lblText){
                $(this).val('');
            }
        })
        .blur(function(){
            if($(this).val() === ''){
                $(this).val(lblText);
            }
        })
        .val(lblText);
  });

  $("table.sm_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;
    
  });
  
  $('div.article h2, div.article h3, div.article h4').each(function() {  
      var thisText = $(this).text();
      $(this).empty().text(thisText);
      $(this).addClass('typeface-js');
  });  
  _typeface_js.renderDocument();

});





