var cType = "";
var months = "";
var year = "";

function rollInit() {
	
	$(".enable-selection").hover(function(){
		$(this).parent().find("ul.filter").show();	
	},
	function(){
		$(this).parent().find("ul.filter").hide();	
	});
	$("ul.filter").hover(function(){$(this).show();}, function(){ $(this).hide(); })
	$("#ctype-selection").html($("#ctype-filter li:first-child").html());
	
	$("#ctype-filter li").click(function(){
		
		cType = evaluateFilterValue("news", this);
		filter(this, "ctype-selection");
		
		$("#ctype-filter li").removeClass("active");
		$(this).addClass("active");
	})
	
	$("#months-filter li").click(function(){
		
		months = evaluateFilterValue("all", this);
		filter(this, "months-selection");
		
		$("#months-filter li").removeClass("active");
		$(this).addClass("active");
	})
	
	$("#years-filter li").click(function(){
		
		year = evaluateFilterValue("all", this);
		filter(this, "years-selection");
		
		$("#years-filter li").removeClass("active");
		$(this).addClass("active");
	})
	
	reLayout(".roll", new Array("h1", ".padded"), 2);
}

function filter(obj, selection) {
	
	$("#"+selection).html($(obj).html());
			
	// combine the items thats need filtering
	targetClass = cType+""+months+""+year;
	
	if(targetClass != "") {
		$(".roll").css("opacity",".2");
		$(targetClass).css("opacity","1");
	} else {
		$(".roll").css("opacity","1");
	}
		
}	
	
	/**
	 * It evaluates the filter value base on the item selected.
	 * It retrieves the title from the filter.
	 *
	 * @param string	Text value of the selected item
	 * @param object	The target filter item
	 * @return string	The resulting class to enable the filter
	 */
	function evaluateFilterValue(defaultVal, obj) {
	
		if($(obj).attr("title") == defaultVal) 
			return "";
			
		return "."+$(obj).attr("title")
	}
	
	
function reLayout(listClass, targetAdjustments, itemsPerRow) {

	// get the last row
	var totalItems = $(listClass).length;
	
	// How many items to align per row
	var itemsPerRow = itemsPerRow;
	var numberOfRows = Math.floor(totalItems/itemsPerRow);
	var extraItems = totalItems%itemsPerRow;
	
	var targets = targetAdjustments
	
	
	////////////////////////////////////////////////////////////
	// Handle extra items on a given row.
	// If there are 7 items with 2 per row: the number 3 with 1 extra (7%2 = 1)
	
	////////////////////////////////////////////////////////////
	// Handle exact items on a given row. 
	// If there are 6 items with 2 per row: the number is 3 (6%2 = 0)
	// If the extra items is 0 then use the last items on that given row
	if(extraItems == 0) extraItems = itemsPerRow;
	
	// Loop through the last items
	for(var a=1; a<=extraItems; a++) {
		$($(listClass)[totalItems-a]).addClass("last");
	}
	
	var flag = 0;
	var obj = new Array();
	$(listClass).each(function(index){
		
		obj[flag] = $(this);
		
		// increament the flag to store the position of the object
		// that will be compared
		flag++;
		
		if(flag == itemsPerRow) {
			flag = 0;
			
			// Loop thru the objects collected for comparison
			for(var i=0; i<obj.length; i++) {
				// Evaluate the items now.
				//console.log(obj);
			}
			layoutObject(obj, targets);
			// Reset the array
			obj = new Array();
		}
		
		// there are more than 1 row and there are extra items to be considered
		if(numberOfRows >= 1 && extraItems > 0) {
			if(index == totalItems-1) {
				// Loop thru the objects collected for comparison
				for(var i=0; i<obj.length; i++) {
				//	console.log(obj);
				}
				layoutObject(obj, targets);
				// Reset the array
				obj = new Array();
			}
		}
			
	});
}
	
	function layoutObject(obj, targets) {
	
		// after all targets has been configured for height
		// get the container height and compare it.
		for(var j=0; j<targets.length; j++) {
			var max = 0;
			// Retrieve the max value to be used
			for(var i=0; i<obj.length; i++) {
				if(j < targets.length - 1) {
					// Get the max between the target objects
					max = getMax(max, $(obj[i]).find(targets[j]).height());
				} else {
					// The last item on the target is the outer container
					// it uses the outerHeight for any padding considerations.
					max = getMax(max, $(obj[i]).find(targets[j]).height());
				}
			}
			
			// Apply the max to all the objects in the group
			for(var i=0; i<obj.length; i++) {
				$(obj[i]).find(targets[j]).height(max);
			}
		}
	}
	
	/**
	 * Get max between two numbers
	 */
	function getMax(num, num2) {
		if(num2 > num)
			return num2;
			
		return num;
	}
