var latest_values = new Array();
var timer;

$(function() {
	$("#slider-bet").slider({
		range: true,
		min: 0,
		max: 100,
		values: [0, 100],
		slide: function(event, ui)
		{
			clearTimeout(timer);
			latest_values.push(new Array(getSliderValue(ui.values[0], min_bet, max_bet), getSliderValue(ui.values[1], min_bet, max_bet)));
			changeBetValues(latest_values.length);
		},
		change: function(event, ui)
		{
			latest_values = [new Array(getSliderValue($("#slider-bet").slider("values", 0), min_bet, max_bet), getSliderValue($("#slider-bet").slider("values", 1), min_bet, max_bet))];
			changeBetValues(1, true);
		}
	})
	
	$('.filter_categories').children('input').click(function(){
		filterGames();
	});
	$('#filter_bonus, #filter_jackpot').click(function(){
		filterGames();
	});
	$('#selectAllCategories').click(function(){
		$('.filter_categories').children('input').attr('checked', true);
		filterGames();
	});
	$('#deselectAllCategories').click(function(){
		$('.filter_categories').children('input').attr('checked', false);
		filterGames();
	});
	latest_values[0] = new Array(min_bet, max_bet);
	changeBetValues(1, show_all_games_from_start);
});

function changeBetValues(index, filter_games)
{
	if(index != latest_values.length)
	{
		return;
	}
	index = index-1;
	$("#slider-bet")
	.children(':eq(1)').html('<div id="minVisualAmount" class="sliderValue searchBoxGrad"><img src="/1274447117/images/black_arrow.gif" class="arrow">$'+latest_values[index][0]+'</div>')
	.parent().children(':eq(2)').html('<div id="maxVisualAmount" class="sliderValue searchBoxGrad"><img src="/1274447117/images/black_arrow.gif" class="arrow">$'+latest_values[index][1]+'</div>');
	
	if(filter_games != null && filter_games)
	{
		filterGames(false);
	}
}

function filterGames(doFilter)
{
	if(all_games == null)
	{
		return;
	}
	if(doFilter == null)
	{
		doFilter = true;
	}
	
	var games = all_games;
	var available_games = new Array();
	
	var user_categories = new Array();
	$('.filter_categories').children('input:checked').each(function(){
		user_categories.push($(this).val());
	});
	
	user_min_bet = getSliderValue($("#slider-bet").slider("values", 0), min_bet, max_bet);
	user_max_bet = getSliderValue($("#slider-bet").slider("values", 1), min_bet, max_bet);
	
	user_bonus = $('#filter_bonus').is(':checked');
	user_jackpot = $('#filter_jackpot').is(':checked');
	
	games_with_jackpot = games_with_bonus = 0;
	games_with_category = new Array();
	
	for(var i in games)
	{
		game_added = game_category = game_bonus = game_jackpot = game_bet = false;
		game = games[i]; 
		game['use_children'] = false;
		if(hasGameValidCategories(game, user_categories))
		{
			game_category = true;
		}
		if((user_bonus && game['bonus'] != null) || !user_bonus)
		{
			game_bonus = true;
		}
		if((user_jackpot && game['jackpot'] != null) || !user_jackpot)
		{
			game_jackpot = true;
		}
		if(user_min_bet <= game['maxbet'] && game['minbet'] <= user_max_bet)
		{
			game_bet = true;
			if(game_category && game_bonus && game_jackpot)
			{
				available_games.push(game);
				added_game = true;
			}
		}
		else if(game['children'] != null)
		{
			for(var ii in game['children'])
			{
				if(user_min_bet <= game['children'][ii]['maxbet'] && game['children'][ii]['minbet'] <= user_max_bet)
				{
					game_bet = true;
					if(game_category && game_bonus && game_jackpot)
					{
						game['use_children'] = game['children'][ii];
						available_games.push(game);
						added_game = true;
						break;
					}
				}
			}
		}
		if(game_bet && game_category)
		{
			if(game_bonus && game['jackpot'])
			{
				games_with_jackpot++;
			}
			if(game_jackpot && game['bonus'])
			{
				games_with_bonus++;
			}
		}
		if(game_bet && game_bonus && game_jackpot && game['game_groups'].length > 0)
		{
			for(ii in game['game_groups'])
			{
				if(!games_with_category[game['game_groups'][ii]['name']])
				{							
					games_with_category[game['game_groups'][ii]['name']] = 1;
				}
				else
				{
					games_with_category[game['game_groups'][ii]['name']]++;
				}
			}
		}
	}
	
	$('#filterBox').find('.numberOf').text(0);
	for(i in games_with_category)
	{
		$('#category_'+i).find('.numberOf').text(games_with_category[i]);
	}
	
	$('#numberOfBonusGames').text(games_with_bonus);
	$('#numberOfJackpotGames').text(games_with_jackpot);
	
	if(doFilter)
	{
		$('#searchList').html('');
		game_template = game_list_template;
		for(var i in available_games)
		{
			game = available_games[i];
			template = game_template;
			template = template.replace(/IMAGE_NAME/gi, game['cr_code']);
			if(game['use_children']['image_url'] != null || game['image_url'] != null)
			{
				template = template.replace(/IMAGE_URL/gi, game['use_children'] ? game['use_children']['image_url'] : game['image_url']);
			}
			template = template.replace(/URL/gi, ( game['use_children'] ? (game['use_children']['seo_url'] ? ly_language+game['use_children']['seo_url'] : ly_language+'game/'+game['use_children']['cr_code']+'/') : (game['seo_url'] ? ly_language+game['seo_url'] : ly_language+'game/'+game['cr_code']+'/') ) );
			template = template.replace(/GAME_NAME_SHORT/gi, game['use_children'] ? game['use_children']['translated_title_short'] : game['translated_title_short']);
			template = template.replace(/GAME_NAME/gi, game['use_children'] ? game['use_children']['translated_title'] : game['translated_title']);
			template = template.replace(/GAME_TEXT/gi, game['use_children'] ? game['use_children']['translated_text'] : game['translated_text']);
			$(template).removeClass('hide');
			$('#searchList').append(template);
		}
	}
	tooltip();
}

function hasGameValidCategories(game, categories)
{
	if(game['game_groups'] == null && game['game_groups'].length <= 0)
	{
		return false;
	}
	for(i in game['game_groups'])
	{
		if(game['game_groups'][i] == null)
		{
			continue;
		}
		if($.inArray(game['game_groups'][i]['name'], categories) > -1)
		{
			return true;
		}
	}
	return false;
}

function getSliderValue(percent, min, max)
{
	pow = 2.7;
	value = Math.pow(percent, pow) / Math.pow(100, pow) * max;
	
	if(value < min) {
		return min;
	} else if(value < 0.1) {
		return value.toString().substr(0,4);
	} else if(value < 1) {
		return value.toString().substr(0,3);
	} else if(value < 10) {
		return Math.round(value)
	} else if(value < 30) {
		return value - (value % 5);
	} else if(value < 100) {
		return value - (value % 10);
	} else if(value < 500) {
		return value - (value % 50);
	} else if(value < 1000) {
		return value - (value % 100);
	} else if(value < 3000) {
		return value - (value % 500);
	} else {
		return value - (value % 1000);
	}
}

