var HoverModule = function(mobile) {

	var postIndex;
	var minActivationHeight = 200;

	var readoutPosts = function () {
	
		if ( mobile ) return;
	
		postIndex = new Array();
	
		$("#content").children().each(function(){
       		var div = $(this);
       		
       		if ( div.attr('id') != "favorites" && div.attr('id') != "" ) {
       			postIndex.push({id:div.attr('id').substr(5) ,offset:div.offset().top});    		
       		}
       	});
       	
       	setActivePost();
	}
	
	var setActivePost = function() {
		
		if ( mobile ) return;
		
		for ( var id in postIndex ) {
			var div = postIndex[id];
			
			var position = $(window).scrollTop() + 90;
			var windowHeight = $(window).height() - 90;
			var postPos = $('#post-' + div['id']).offset().top;
			var postHeight = $('#post-' + div['id']).height();
			
			var top = position + windowHeight - postPos;
			var bottom = postPos - position + postHeight;
			
			if ( postPos < position || (postPos + postHeight) < bottom ) {
				$('#post-' + div['id']).css('opacity', 1);
				console.log('hovermodule.js 01'); 			
			}
			
			if ( (bottom > minActivationHeight) && (top > minActivationHeight) ) {
				if($('#post-' + div['id']).css('opacity') < 1 )	
					$('#post-' + div['id']).animate({opacity: 1}, 150);
					console.log('hovermodule.js 02'); 
			} else {
				if ( postPos > (position + windowHeight) ) {
					$('#post-' + div['id']).css('opacity', 0.4);
					console.log('hovermodule.js 03'); 
				}
			}
		}
	}
	
	var clearTop = function() {
		for ( var id in postIndex ) {
			var div = postIndex[id];
			
			var position = $(window).scrollTop() + 90;
			var windowHeight = $(window).height() - 90;
			var postPos = $('#post-' + div['id']).offset().top;
			var postHeight = $('#post-' + div['id']).height();
			
			var bottom = postPos - position + postHeight;
			
			if ( postPos < position || (postPos + postHeight) < bottom ) {
				$('#post-' + div['id']).css('opacity', 1);
				console.log('hovermodule.js 04'); 			
			}
		}
	}

	var toReturn = {
		init: function(){
			readoutPosts();
			clearTop();
		},
		hover: function () {
			setActivePost();
		}		
	}
	
	return toReturn;
}

