
function check_screen_size() {
  /*
   * if the user's window size is below 1024, we apply a "small width" class
   * this removes some sneaky CSS rules from .left_clm
   */
  var windowwidth = $('body').width();
  if (windowwidth >= 1024) {
    if ($("body").hasClass("small_width")) {
      $("body").removeClass("small_width");      
    }
  } else  {
    $("body").addClass("small_width");
  }
}

function check_left_column_scroll() {
  /*
   * if the user's window size is below 1024 AND they're scrolling horizontally,
   * we calculate the position of .left_clm here
   */
  if ($("body").hasClass("small_width") && $("#wrap").hasClass("fixed")) {
    $(".left_clm")
            .stop()
            .css("left",(-$(window).scrollLeft()) + "px");      
    }
}

$(document).ready(function() {
  
  /*
   * activates "see more" hovers on images by sliding up a SPAN
   */
  $(".image a").hover(function() {
    $(this).children("span").stop(false,true).slideDown("fast");
  }, function() {
    $(this).children("span").stop(true,true).slideUp("fast");
  });
  
  
  if ($("body").hasClass("waypoint_scroll")) {
    
    var detail = false;
    if ($("body").hasClass("detail")) {
      var detail = true;
    }
    
    $("#wrap").addClass("fixed_scroll_enabled");
    
    /* affects the polling interval for scrolling */
  	$.waypoints.settings.scrollThrottle = 10;

    /* run these functions at page load */
    check_screen_size();
    check_left_column_scroll();
    
    /* run same functions when appropriate according to user actions */
  	$(window).resize(function() {
      check_screen_size();
    });
    $(window).scroll(function(){
      check_left_column_scroll();
    });
    
    /* "content_end" is a hidden waypoint that tells the browser when to remove fixed position from .left_clm */
    if (detail == true) {
      $(".content_end").height($(".content_block:first .left_clm").height()+90);
    } else {
      $(".content_end").height($(".content_block:last .left_clm").height()+90);
    }
    
    /* hides content / dims images for all but the first content block */
    $(".content_block").not(":first").each(function() {
      if (detail == false) {
        $(this).children(".left_clm").hide();
	    $(this).find("img").stop(false).fadeTo("fast",.3);
      }

    });
    
    /* first waypoint: adds/removes fixed position of left_clm */
    $(".content_block:first").waypoint(function(event, direction) {
       if (direction === 'down') {
         $("#wrap").addClass("fixed");
       }
       else {
         $("#wrap").removeClass("fixed");
         if (detail == false) {
           $(".left_clm").hide();
           $(this).children(".left_clm").show();
		   $(this).find("img").stop(false).fadeTo(500,1);
         }
    
       }
     });

     /* other waypoints: swap content, fade images */
    $(".content_block").not(":first").waypoint(function(event, direction) {
      if (direction === 'down') {
        if (detail == false) {
          $(".left_clm").hide();
          $(this).children(".left_clm").show();
		          
        $(this).prev().find("img").stop(false).fadeTo(500,.3);
        $(this).find("img").stop(false).fadeTo(500,1);
        }

      }
      else {
        if (detail == false) {
          $(".left_clm").hide();
          $(this).prev().children(".left_clm").show();
		  $(this).find("img").stop(false).fadeTo(500,.3);
          $(this).prev().find("img").stop(false).fadeTo(500,1);
        }

      }
       } , {
         offset: 240
    });

    /* last waypoint: remove fixed position from left_clm */
    $(".content_end").waypoint(function(event, direction) {
       if (direction === 'down') {
          $("#wrap").removeClass("fixed");
          $("#wrap").addClass("fixed_low");
       }
       else {
          $("#wrap").addClass("fixed");
           $("#wrap").removeClass("fixed_low");
       }
    });

    
  }

});
