$(document).ready(function(){
  
  var doc = $(document), 
      win = $(window), 
      sidebar = $('.sidebar'), 
      content = $('#content');
  
  var stickyPos = function() {

		var horizontalScroll = doc.width() - win.width() ;
		var newLeft = content.position().left + content.outerWidth(true);
		var isios = navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad)/);
		
		if ( horizontalScroll > 0 && !(isios) ) {
			newLeft = newLeft - win.scrollLeft() ;
		} 
		
		sidebar.css({"left" : + newLeft + "px"});

	  if ( win.height() > sidebar.outerHeight(true) ) {
	    sidebar.css("position","fixed");
			sidebar.css("top", sidebar.parent().position().top - sidebar.parent().css("padding-top") );
		} else {
	  	sidebar.css("position","inherit");
		}
  };

	$('.thumb:nth-child(4)').css('margin-right','0');

	$('.grid').masonry({
	  itemSelector : '.thumb',
	  columnWidth : 160,
		gutterWidth : 20
	});
	
	//stickyPos(); // when would you need to run this out of the gates like this?
	win.bind('resize', function() { stickyPos(); });
	win.bind('scroll', function() { stickyPos(); });
	
});
