  $(document).ready(function(){

    var i=1; // All view fields start with one, set variable to iterate
    var newImg = new Image(); // Initialize new image    
    
    // Resize the images to thumbnail size 
    $("div > .imagefield").each(function(){
    
      var fullImg = document.getElementById('imagefield-field_image_' + i).src; // Get Image sources, all fields go from 1 - ...
      newImg.src = fullImg; // Set new image to image source
      var imgHeight = document.getElementById('imagefield-field_image_' + i).height; // Get image height
      var imgWidth =  document.getElementById('imagefield-field_image_' + i).width; // Get image width
      var newWidth = 215; // Set static width of new thumbnail
      var newHeight = (imgHeight/imgWidth) * newWidth; // Scale height based on width setting
    
      $("img#imagefield-field_image_" + i).attr("height", newHeight); // Resize original image which is already loaded through views
      $("img#imagefield-field_image_" + i).attr("width", newWidth); // Resize original image which is already loaded through views
      
      if(i > 3){
        var rightImage = '#field-image-' + i + '-fid';
        var rImg = '#imagefield-field_image_' + i;
        rightImage = $(rightImage).html();
        $("img#imagefield-field_image_" + i).remove();
        $("#featured-right").append(rightImage);
      }
    
      i++;
    });

    $(".imagefield").hover(function() {$(this).css("cursor", "pointer")},
        function() {$(this).css("cursor", "normal");
        });
    
    
    // Resize the sub nav image to thumbnail size 
    $("a > .imagefield").each(function(){
      var imgHeight = $(this).attr("height"); // Get image height
      var imgWidth = $(this).attr("width"); // Get image height
      var newWidth = 330; // Set static width of new thumbnail
      var newHeight = (imgHeight/imgWidth) * newWidth; // Scale height based on width setting
      $(this).attr("height", newHeight); // Resize original image which is already loaded through views
      $(this).attr("width", newWidth); // Resize original image which is already loaded through views
    });    
    
     
    // Set the middle image
    var firstImage = document.getElementById('imagefield-field_image_1').src
        firstImage = '<img class="bImg" src="' + firstImage + '" width="560px" />'
        $("#bigImage").append(firstImage);
        $(".bImg").fadeIn("slow");
    
    // Wrap the middle img with A tag, set A tag href to node title link, remove node title link
    $(".bImg").wrap("<a></a>");
    $("#bigImage > a").attr("id","bigImageA")
    var nodeTitle = $(".views-field-title > .field-content > a").attr("href");
    $("#bigImageA").attr("href", nodeTitle);
    $("#content-area * .views-field-title").remove();
    
    
    // Set the clicked image to the middle image
    $("div > .imagefield").click(function () {
      var index = $(".imagefield").index(this);
      index++;
      var bigImage = document.getElementById('imagefield-field_image_' + index).src;
      if($(".bImg").is(":hidden")){
        bigImage = '<img class="bImg" src="' + bigImage + '" width="560px" />'
        $("#bigImage").append(bigImage);
        $(".bImg").fadeIn("slow");
        // Wrap the middle img with A tag, set A tag href to node title link, remove node title link
        $("#bigImageA").remove();
        $(".bImg").wrap("<a></a>");
        $("#bigImage > a").attr("id","bigImageA")
        $("#bigImageA").attr("href", nodeTitle);
      } else {
        bigImage = '<img class="bImg" src="' + bigImage + '" width="560px" />'
        $(".bImg").fadeOut("slow");
        $("img.bImg").remove();
        $("#bigImage").append(bigImage);
        $(".bImg").fadeIn("slow");        
        // Wrap the middle img with A tag, set A tag href to node title link, remove node title link
        $("#bigImageA").remove();
        $(".bImg").wrap("<a></a>");
        $("#bigImage > a").attr("id","bigImageA")
        $("#bigImageA").attr("href", nodeTitle);
      }
      
    });

  
    // Set the width and height for the Flash Video
    $("object").attr("width", 330);
    $("object").attr("height", 207);
    $("embed").attr("width", 330);
    $("embed").attr("height", 207);
    
    // Remove divs with no content. removeMe ID set in views-view-fields.tpl.php
    $(".removeMe").parent().remove();
    
    var sidebar = 0;        
    $("a#hide").click(function(event){
      if(sidebar == 0){
        $("#sidebar-left").animate({marginLeft:'-150px'}, 1500);
        $("body").removeClass("logged-in")
        sidebar = 1;
      } else {
        $("#sidebar-left").animate({marginLeft:'0px'}, 1500);
        sidebar = 0;
        $("body").addClass("logged-in")        
        }
    });

    
});
