var TopicPaths, RelatedPages; 

// --------------------------------------------------------------------------------------
// General Helpers

$.fn.orParent = function(selector){
	if (this.is(selector)) { return this }
	return $(this.parent(selector))
}; 

// returns true if empty
$.fn.isEmpty = function() {
	return this.size() == 0
};

// returns false if empty
$.fn.isNotEmpty = function() {
	return this.size() > 0
};

// return any links to referring page inside element 
$.fn.referringLink = function() {
	var referringPage = document.referrer.split('/').pop();
	return this.find('a[href=' + referringPage +']');
};

// --------------------------------------------------------------------------------------
// Page objects

$.fn.topicPath = function(){
	return this.extend({
 		
		markCurrent: function(){  
			if (!(this.hasClass('current'))) {
				TopicPaths.removeCurrent();
				this.addClass('current'); 
			}
		}
	});
}  

// p in #related-pages
$.fn.relatedPage = function() {
	return this.extend({
		moveToTop: function(){
			RelatedPages.prepend(this);
		}		                        
	});
};    


$(document).ready(function(){  
	TopicPaths = $('#topic-paths');
	RelatedPages = $('#related-pages')	
	
	// --------------------------------------------------------------------------------------
	
	TopicPaths.extend({
		init: function(){ 
			this.personalize();
		},
		
	 	personalize: function(){     
			// first check if any particular link was last page visited
			var referring = this.referringPath();
		  if (referring) { return referring.markCurrent(); }
		  // otherwise show most visited path
    	var mostVisited = this.mostVisitedPath();
			if (mostVisited) { mostVisited.markCurrent(); }
	 	},
		
		// returns path containing referring link
		referringPath: function(){
			var referrer = this.referringLink();  	
			if (referrer.isNotEmpty()) { return referrer.parent().topicPath() } else {return false}
		},
		
		// returns path with most visited links
		mostVisitedPath: function(){
			var mostVisited, mostVisitedCount, count, list;
			mostVisited = false;
			mostVisitedCount = 0;
	
			this.find('.topic-path').each(function(i) {
				path = $(this).topicPath();
				count = path.find('a').visited().size();
				if (count > mostVisitedCount) { mostVisited = path; mostVisitedCount = count; }
			});
			return mostVisited
		}, 
		
		removeCurrent: function(){
			this.find('.current').removeClass('current');
		}
	
	});   
	
	RelatedPages.extend({
		init: function(){
			if (this.all().size() > 1) { this.personalize(); }
		},
		
		all: function(){
			return this.find('p')
		},
		
		// reorders parents depending on referrer
		personalize: function(){
			var referringLink = this.referringLink();			
			if (referringLink.isNotEmpty()) { referringLink.parent().relatedPage().moveToTop(); } 
		}
		
	});
	
	// --------------------------------------------------------------------------------------

	TopicPaths.init();
	RelatedPages.init();
}); 


                   
                                 
	
