(function($){$.belowthefold=function(element,settings){var fold=$(window).height()+$(window).scrollTop();return fold<=$(element).offset().top-settings.threshold;};$.abovethetop=function(element,settings){var top=$(window).scrollTop();return top>=$(element).offset().top+$(element).height()-settings.threshold;};$.rightofscreen=function(element,settings){var fold=$(window).width()+$(window).scrollLeft();return fold<=$(element).offset().left-settings.threshold;};$.leftofscreen=function(element,settings){var left=$(window).scrollLeft();return left>=$(element).offset().left+$(element).width()-settings.threshold;};$.inviewport=function(element,settings){return!$.rightofscreen(element,settings)&&!$.leftofscreen(element,settings)&&!$.belowthefold(element,settings)&&!$.abovethetop(element,settings);};$.extend($.expr[':'],{"below-the-fold":function(a,i,m){return $.belowthefold(a,{threshold:0});},"above-the-top":function(a,i,m){return $.abovethetop(a,{threshold:0});},"left-of-screen":function(a,i,m){return $.leftofscreen(a,{threshold:0});},"right-of-screen":function(a,i,m){return $.rightofscreen(a,{threshold:0});},"in-viewport":function(a,i,m){return $.inviewport(a,{threshold:0});}});})(jQuery);	

$(document).ready(function(){

	$('a[rel=prev]').addClass('image-overlay-left');
	$('a[rel=next]').addClass('image-overlay-right');
	
	if (navigator.userAgent.match(/like Mac OS X/i)) {
		
		if( $('article#mophoblog-detail').length ){
			window.location.hash = '#mophoblog-detail';
		}
		else if( $('article#blog-detail').length ){
			window.location.hash = '#blog-detail';
		}
		else if( $('article#photo-detail').length ){
			window.location.hash = '#photo-detail';
		};
		
	};

	$(window).bind("scroll", function(event) {
			
		if( $('a#top').is(':in-viewport') ){
			$('#backUp').hide();
		}
		else{
			
			if ( $('#backUp').length == 0 ){
				$('body').prepend('<a id="backUp" href="#top"></a>');
			}
			else{
				$('#backUp').show();
			};
			
		};
	
		// load more when the div is in the viewport
		if( $('div#more').is(':in-viewport') ){
					
			// set whether it's loading or not
			var isLoading = $('div#more').hasClass('loading');
			
			// if it is not loading, run the function to load it
			if (!isLoading){
				
				// run function to load content
				loadContent();
				
			};
					
		};	
		
		// load more when the div is in the viewport
		if( $('div#more-tumblr').is(':in-viewport') ){
					
			// set whether it's loading or not
			var isLoading = $('div#more-tumblr').hasClass('loading');
			
			// if it is not loading, run the function to load it
			if (!isLoading){
				
				// run function to load content
				loadContentTumblr();
				
			};
					
		};				
	
	});
	
	// trigger a scroll on load
	$(window).trigger('scroll');
	
	// load content
	function loadContent(){
	
		// show loading
		$('div#more').addClass('loading');
		
		// get the kind of content to load
		var type = $('body').attr('id');
		var page = $('div#more').attr('data-page');
		var limit = 20;
		
		// set the page to get
		var page = parseInt(page) + 1;				
						
		$.getJSON("/api/get_category_posts/?dev=1", { slug: type, page: page } ,function(response){
			
			// drill into the posts array and setup arrPosts which we'll fill
			var postArr = response.posts;												
			var arrPosts = [];
			
			// loop through each post
			for(var i=0; i<postArr.length; i++){
				
				// get the stuffs
				var id = postArr[i].id;		
				var title = postArr[i].title;		
				var slug = postArr[i].slug;		
				var content = postArr[i].content;		
				var excerpt = postArr[i].excerpt;		
				var url = postArr[i].url;	
				var date = postArr[i].date;
				
				if(type == 'photos' || type == 'mophoblog'){
					var thumbnail = postArr[i].attachments[0].images.thumbnail.url;	
				};
			
				// build the post based on the slug				
				if( type == 'photos' ){
					var thePost = '<article><a href="'+url+'"><img src="'+thumbnail+'" /></a></article>';
				}
				else if( type == 'mophoblog' ){
					var thePost = '<article><a href="'+url+'"><img src="'+thumbnail+'" /></a></article>';
				}
				else if( type == 'blog' ){
					var thePost = '<article><h1><a href="'+url+'">'+title+'</a></h1><p>'+excerpt+'</p></article>';
				};
								
				arrPosts.push(thePost);
							
			};
			
			var arrPosts = arrPosts.join('');
			
			// if the array has something
			if( arrPosts.length ){

				// append to dom
				$('div#more').before(arrPosts);
				
				// change start attribute and remove loading
				$('div#more').attr('data-page',page).removeClass('loading');
						
			}
			else{
				
				// remove more div
				$('div#more').remove();
				
			};
			
			$(window).trigger('scroll');
			
		});		
		
	};	
	
	
	// hide old school paging
	$('#paging').hide();
	
	















































	// load tumblr content
	function loadContentTumblr(){

		$('div#more-tumblr').addClass('loading');
	
		var start = $('div#more-tumblr').attr('data-start');
		var start = parseInt(start);
		var end = start + 15;	
	
		// get posts
		$.getJSON('http://jyoseph.tumblr.com/api/read/json?&num=15&start='+start+'&callback=?', function (data) {

			var arrPosts = [];
			
			$.each(data.posts, function(i,posts){
			
			    var id = this.id;
			    var type = this.type;
			    var url = this.url;
			    var urlSlug = this["url-with-slug"];         
				
				//text, quote, photo, link, chat, video, or audio
				
				switch(type)
				{
				case 'text':

					var regularTitle = this["regular-title"];			            
					var regularBody = this["regular-body"];					
					
					arrPosts.push('<article><h1>'+regularTitle+'</h1><p>'+regularBody+'</p></article>')   
					
					break;
					
				case 'quote':
				
					var quoteSource = this["quote-source"];
					var quoteText = this["quote-text"];
								
					arrPosts.push('<article><div class="quote">'+quoteText+'</div><p>'+quoteSource+'</p></article>')   
				
					break;
					
				case 'photo':
				
					var photoURL500 = this["photo-url-500"];
					var photoCaption = this["photo-caption"];
					
					arrPosts.push('<article><a href="'+urlSlug+'"><img src="'+photoURL500+'" /></a><p>'+photoCaption+'</p></article>')   
					
					break;
					
				case 'link':

					var linkText = this["link-text"];
					var linkURL = this["link-url"];
					var linkDesc = this["link-description"];

					arrPosts.push('<article><a href="'+linkURL+'">'+linkText+'</a><p>'+linkDesc+'</p></article>');
				
					break;
					
				case 'chat':
				
					
				
					break;
				case 'video':
				
					var videoSource = this["video-source"];
					var videoPlayer = this["video-player"];
					var videoCaption = this["video-caption"];
					var video500 = this["video-500"];
				
					arrPosts.push('<article><div class="media">'+videoPlayer+'</div><p>'+videoCaption+'</p></article>');
	
					break;
					
				case 'audio':
				
					var audioCaption = this["audio-caption"];
					var audioPlayer = this["audio-player"];

					arrPosts.push('<article><div class="audio">'+audioPlayer+'</div><p>'+audioCaption+'</p></article>');
				
					break;
					
				};
					
			});
			
			var arrPosts = arrPosts.join('');
			
			// if the array has something
			if( arrPosts.length ){

				// append to dom
				$('div#more-tumblr').before(arrPosts);

				// change start attribute and remove loading
				$('div#more-tumblr').attr('data-start',end).removeClass('loading');
										
			}
			else{
				
				// remove more div
				$('div#more-tumblr').remove();
				
			};
		
		
		});
		
	};	
	
	$('body#tumblr #navigation').hide();
	
	//$(".colorbox,.thickbox").colorbox();














	$('section:not(.index) article img').addClass('scale-with-grid');
	






















	
	
	
	
	
	
	
	
	
	
	
	
	
	

});
