/**
 * @author denny1987
 */

//prevent errors in browsers that do not support console. 

if(typeof window.loadFirebugConsole == "undefined" || typeof window.console == 'undefined' ) {   var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];   window.console = {};   for (var i = 0; i < names.length; ++i) {     window.console[names[i]] = function(){};   } }

function setCurrentSide(page)
{
	$("#higscore-naviagtion a").each(function(i) {
		$(this).removeClass("highscore-active-page")
	});
	
	$(".ajaxLink"+page).addClass("highscore-active-page");
	
	console.info('Akutelle Seite '+page);
}

function setHighscoreLoder()
{
	/**
	 * height of the loder image in px
	 */
	
	var ajax_loader_image_height = 19;
	
	/**
	 * set the ajax loder middle
	 */
	
	var cssObj = {"margin-top":(($("#highscore-table").height() - ajax_loader_image_height) / 2)+"px"};
	
	$("#highscore-ajax-loader").css(cssObj).show();
	$("#highscore-table").toggleClass("table-opacity-low");
}

function setHighscoreData(data, side)
{		   
	var side = side;
	
	var place_start = 1;
	
	/**
	 * count of rows per side
	 */
	
	var COUNT_PER_SIDE = 15;
	
	/**
	 * count of rows on the page
	 */
	
	var existing_rows = $("tbody tr").length;
	
	/**
	 * count of the database result
	 */
	
	var count_results = data.highscore_result.length;
	
	/**
	 * remove the table opacity class / hide the ajax loader  
	 */
	
	$("#highscore-table").toggleClass("table-opacity-low");
	$("#highscore-ajax-loader").hide();
	
	/**
	 * logic
	 */

	if(side > 0)
	{
		place_start = (side * COUNT_PER_SIDE) + 1;
	}

	if(count_results < COUNT_PER_SIDE) {
		$("tbody tr").each(function(i) {
			if(i >= count_results) {
				$(this).fadeOut("slow");
				
				if($(this).hasClass("user-highlighted")) {
					$(this).removeClass("user-highlighted");
				}
			}
		});
	} else {
		$("tbody tr").each(function(i) {
			$(this).fadeIn("slow");
		});		
	}
	
	$.each(data.highscore_result, function(i,item) {
		var temp_data = $($("tbody tr")[i]);
		
		/*
		 * set the class if the user on the page
		 */
		
		if(item.highlight_user == true) {
			temp_data.toggleClass("user-highlighted", true);
		}
		
		var temp_data = $("td", $("tbody tr")[i]);
		
		$(temp_data[0]).html(place_start + i);
		$(temp_data[1]).html(item.username);
		$(temp_data[2]).html(item.game_score);   
	});
}
