var RelatedModule = function(mobile) {

	var searchString;
	var resultArray;
	var currentId;
	
	var getEventTarget = function(e) {  
		e = e || window.event;  
		return e.target || e.srcElement;
	}
	
	var showRelatedFunctionality = function() {
	
		var related = new Array();
		
		$("#content").children().each(function(){
       		var div = $(this);
			var classes = div.attr('class').split(" ");
			
			for ( var id in classes ) {
				var _class = classes[id];
				if ( _class.search('category') > -1 ) {
					if ( related[_class] == undefined ) {
						related[_class] = 1;
					} else {
						related[_class]++;
					}
				}
			}
       	});
       	
       	for ( var id in related ) {
       		if ( related[id] < 2 ) 
       			$("." + id).addClass("alone");
       	}	
	}

	var searchDom = function(value, id) {
	
		if ( mobile ) return;
				
		if ( id == undefined ) {
			id = currentId;
		} else {
			currentId = id;
		}
		
		resultArray = new Array();
	
		$("#content").children().each(function(){
       		var div = $(this);
       		var title;
       		if ( div.attr('id') != id ) {
	       		if ( div.attr('id') != "favorites") {
    	   			if( div.attr('class').search(value) > -1 ) {
	       				div.children().children().each(function(){
	       					_div = $(this);
   	    					if ( _div.attr('class') == 'post-title' ) {
							    title = _div.html();
							}
   	    				});
   	    				resultArray.push({id:div.attr('id') ,offset:div.offset().top, title:title});       			
   	    			}    		
   	    		}
       		}
       	});
       	
       	searchString = value;	       	
       	showResults();
       	
       	return (resultArray > 0 ) ? false : true;
	}
	 
	var showResults = function() {
	
		if ( mobile ) return;
	
		$("#tagIndicator").empty();
		var percentage = ($(window).height() - 90) / $(document).height();
		
		var lastOffset = 0;
	
		for ( var id in resultArray ) {
			var div = resultArray[id];
			
			resultArray[id]['ref'] = id;
			
			newOffset = Math.floor((div['offset'] * percentage) + 90);		
			
			if ( newOffset - lastOffset < 40 ) {
				newOffset = lastOffset + 40;
			}			
	
			var html = "<div id='tag-" + id + "' class='tagOverlay' style='top: " + newOffset + "px'>" +
						"   <div class='tagTitle'>" + div['title'] + "</div>" +		
						"	<div class='tagArrow'></div>" +	
						"</div>";						
			
			$("#tagIndicator").append(html);
			
			var target = $('#tag-' + id + ' .tagTitle');
			
			target.css('left', -(target.textWidth() + 31));
			target.css('width', (target.textWidth()));
			
			lastOffset = newOffset;
		}
		
		active = true;
		$("#tagIndicator").fadeIn('fast', function() {
        	$("#tagIndicator").show();
        });
		
		setViewHeight();
	}
	
	var tagHandler = function (event) {
		if ( mobile ) return;
		
		var target = getEventTarget(event);
		
		target = target.parentNode;
			
		var id = target.getAttribute('id').substr(4)[0];
			
		var time = 0;
		if ( $(window).scrollTop() > resultArray[id]['offset'] ) {
			time = calculateTime($(window).scrollTop() - resultArray[id]['offset']);
		} else {
			time = calculateTime(resultArray[id]['offset'] - $(window).scrollTop());
		}
			
		$.scrollTo({top:(resultArray[id]['offset'] - 90), left:0}, time);
		
		setTimeout(function() {
			$("#tagIndicator").fadeOut('fast', function() {
	        	$("#tagIndicator").hide();
	        	$("#tagIndicator").empty();
	        	resultArray = new Array();
	        });
		}, time);		
	}
	
	var setViewHeight = function () {
		
		if ( mobile ) return;
		
		for ( var id in resultArray ) {
			var div = resultArray[id];
			
			if ( ( div['offset'] - $(window).scrollTop() - 90 ) < 100 && ( div['offset'] - $(window).scrollTop() - 90 ) > -150 ) {				
				$("#tag-" + div['ref']).addClass('hover');
			} else {
				$("#tag-" + div['ref']).removeClass('hover');
			}
		}
	}
	
	var close = function () {
		if ( resultArray != undefined && resultArray.length > 0) {
			$("#tagIndicator").fadeOut('fast', function() {
	        	$("#tagIndicator").hide();
	        	$("#tagIndicator").empty();
	    	 	resultArray = new Array();
	       	});
		}
	}

	var toReturn = {
		init: function() {
			$('#tagIndicator').bind('click', function(event) {
				tagHandler(event.originalEvent);
			});
			
			$('#wrapper').bind('click', function(event) {
				if ( $(getEventTarget(event)).attr('class').search('post') > -1 )
					close();
			});
			
			$(window).scroll(function() {
				if ( resultArray != undefined ) {
					setViewHeight();
				}
			});
			
			showRelatedFunctionality();
		},
		viewCategory: function(value, id){
			searchString = value;
			searchDom(value, id);
		},
		resize: function() {
			if ( resultArray != undefined && resultArray.length > 0 ) {
				showResults();
			}
		},
		rePosition: function() {
			if ( resultArray != undefined && resultArray.length > 0) {
				searchDom(searchString);
			}
		},
		close: function() {
			close();
		}
	}
	
	return toReturn;
}
