
	$(document).ready(function() { 

		var $container_posts = $('#post-grid,#comp-grid');
		  $container_posts.masonry({
		    itemSelector : '.post',
		    columnWidth: 275,
		 	gutterWidth: 20,
		 	isAnimated: true
		  });

		var $container_links = $('#links-grid');
		  $container_links.masonry({
		    itemSelector : '.link-post',
		    columnWidth: 275,
		 	gutterWidth: 20
		  });

		tiles = $(".post").fadeTo(0,0);

		$(window).scroll(function(d,h) {
		    checkFades();
		});

		$('a.external').click(function(){
			window.open(this.href);
			return false;
		});

		$('.showcomments').click(function(){
			$(this).parent().parent().parent().siblings('.post-comments').slideToggle('fast',function(){
				  $container_posts.masonry({
				    itemSelector : '.post',
				    columnWidth: 275,
				 	gutterWidth: 20,
				 	isAnimated: true
				  });
			});
			return false;
		});

		$('.toggle-comments').click(function(){
			$('.comments-holder').slideToggle('slow');
			return false;
		});

		$('.showOnClick a').click(function(){
			$('.showOnClickContainer').show();
			$(this).hide();
		});

		$('.magazine-alpha').each(function(){
			
			var ImageCount = $(this).children('img').size();
			
			if (ImageCount > 1) {
				$(this).siblings('.magazine-beta').children('.magazine-beta-inner').children('.magazine-title').children('.pagination').show();
			}
			
			var $this = $(this);
			
			$this.cycle({ 
				speed:  'slow', 
				timeout: 0,
				pager: $this.parent().children('.magazine-beta').children('.magazine-beta-inner').children('.magazine-title').children('.pagination')
			});
			
		});

		$('#product-alpha-wrap').each(function(){
			
			var ImageCount = $(this).children('img').size();
			
			if (ImageCount > 1) {
				$('#product-title').children('#pagination').show();
			}
			
			var $this = $(this);
			
			$this.cycle({ 
				speed:  'slow', 
				timeout: 0,
				pager: $('#product-title').children('#pagination')
			});
			
		});

		$('.post-media').each(function(){
			
			var ImageCount = $(this).children('img').size();
			
			if (ImageCount > 1) {
				$(this).siblings('.post-title').children('.pagination').show();
			}
			
			var $this = $(this);
			
			$this.cycle({ 
				speed:  'slow', 
				timeout: 0,
				pager: $this.siblings('.post-title').children('.pagination')
			});
			
		});	

		$("#features-wrap").cycle({ 
			fx: 'scrollVert',
			speed:  'slow', 
			timeout: 6000,
			pager: $("#features-title-beta"),
			pause: 1,
			before: onBefore, 
    		after: onAfter 
		});
		
		function onBefore(){
			$("#features-title").hide();
		}

		function onAfter(){
			$("#features-title").show();
		}

		$("#promo-inner").cycle({ 
			speed:  'slow', 
			timeout: 6000,
			pager: $('#promo-pagination')
		});

		$('#single-alpha').each(function(){
			
			var ImageCount = $(this).children('.gallery-slide').size();
			
			if (ImageCount > 1) {
				$(this).siblings('#single-beta').children('.pagination').show();
			}
			
			var $this = $(this);
			
			$this.cycle({ 
				speed:  'slow', 
				timeout: 0,
				pager: $this.siblings('#single-beta').children('.pagination')
			});
			
		});

	    $("#Image input,.middleColumn select").uniform();

	    $(".close").click(function(){
			$(this).parent().parent().parent('.post-comments').slideToggle('fast',function(){
				  $container_posts.masonry({
				    itemSelector : '.post',
				    columnWidth: 275,
				 	gutterWidth: 20,
				 	isAnimated: true
				  });
			});
			return false;
	    	
	    });

        $('#post-gallery-inner a').click(function(){
           $('#single-alpha img').remove();
            var href = $(this).attr('href')
            var img = new Image();
            $(img).load(function(){
                $('#single-alpha').append(img);
            }).attr('src',href);
            return false;
        });	

	    checkFades();

	});

	function checkFades(){
		tiles.each(function(i) {
		   // a = $(this).offset().top + $(this).height() - 500;
		    a = $(this).offset().top + 200;
		    b = $(window).scrollTop() + $(window).height();
			if (a < b) $(this).fadeTo(500,1);
		});
	}

