$(function() {
	selectGame(0);
});

function setState(stars) {
	var counter = 1;
	
	do {
		var theStar = document.getElementById("star" + counter);
		theStar.src = '/images/icons/star.png';
		counter++;
	} while (counter <= stars)
}

function returnState(stars) {
	var counter = 1;
	
	do {
		var theStar = document.getElementById("star" + counter);
		theStar.src = '/images/icons/star_grey.png';
		counter++;
	} while (counter <= stars)
}

function clearTime() {
	clearTimeout(timeout);
}

function featuredGames(count) {	
	
	
	if(!document.getElementById('featured_' + count)) {
		count = 1;
	}	
	
	for(var i = 1; i <= 10; i++) {
		var thisVar = document.getElementById('featured_' + i);
		
		if(thisVar) {
			thisVar.style.display = 'none';
		}
	}
	
	document.getElementById('featured_' + count).style.display = 'block';
	
	count++;
	
	timeout = setTimeout("featuredGames(" + count + ")", 10000);
}

function submitVote(rating, gid, check) {	
	$.ajax({
		type: "POST",
		url: "/ajax/rate.php",
		data: "rating=" + rating + "&gid=" + gid + "&check=" + check,
		success: function(msg){
			document.getElementById('your_rating').innerHTML = '<span class="grey">Takk for stemmen!</span>';
			document.getElementById('rating_holder').innerHTML = msg;
		}
	});
}

function comments_save(gid, comment, check, check2) {
	if(comment != '') {		
		$.ajax({
			type: "POST",
			url: "/ajax/comments_save.php",
			data: "gid=" + gid + "&comment=" + comment + "&check=" + check,
			
			success: function(msg){
				comments_load(gid, 1, check2);
				document.comment_form.comment_text.value = '';
			}
		});
	}
}

function comments_load(gid, page, check) {
	$.ajax({
		type: "POST",
		url: "/ajax/comments_load.php",
		data: "gid=" + gid + "&p=" + page + "&check=" + check,
		success: function(msg){
			document.getElementById('comments_holder').innerHTML = msg;
		}
	});
}

function report(cid, check) {
	$.ajax({
		type: "POST",
		url: "/ajax/report.php",
		data: "cid=" + cid + "&check=" + check,
		success: function(msg){
			document.getElementById('comment_' + cid).innerHTML = msg;
		}
	});
}

function favorite(gid, check) {
	$.ajax({
		type: "POST",
		url: "/ajax/favorite.php",
		data: "gid=" + gid + "&check=" + check,
		success: function(msg){
			document.getElementById('fav_holder_' + gid).innerHTML = msg;
		}
	});
}

function delete_comment(cid, check, gid, check2) {
	$.ajax({
		type: "POST",
		url: "/ajax/comment_delete.php",
		data: "cid=" + cid + "&check=" + check,
		success: function(msg){
			comments_load(gid, 1, check2);
		}
	});
}

function highscores_load(gid, mode, check) {
	$.ajax({
		type: "POST",
		url: "/ajax/highscores_load.php",
		data: "gid=" + gid + "&mode=" + mode + "&check=" + check,
		success: function(msg){
			document.getElementById("highscores_holder").innerHTML = msg;
		}
	});	
}

function highscores_delay(gid, mode, check) {
	t = setTimeout('highscores_load(\'' + gid + '\', \'' + mode + '\', \'' + check + '\');', 2000);
}

/* Slideshow */
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if($active.length == 0) 
		$active = $('#slideshow IMG:last');

    var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
	});
}

function selectGame(index)
{
	if($("#slideshow #holder a:eq(" + index + ")").length == 0)
		index = 0;
	
	$("#slideshow #holder").animate({left: "-" + (index * 408) + "px"}, {duration: 1000});	
	$("#feature_title a").text($("#slideshow #holder a:eq(" + index + ")").attr("title"));
	$("#feature_title a").attr({href: $("#slideshow #holder a:eq(" + index + ")").attr("href")});

	$("#feature_bar span a").removeClass('active');
	$("#feature_bar span a:eq(" + index + ")").addClass('active');
	
	index++;
	
	timeout = setTimeout("selectGame(" + index + ")", 5000);
}

function openTab(id, div) 
{
	$(".section-tab a").removeClass('active');
	$(".section-tab a:eq(" + id + ")").addClass('active');

	$("#leaderboard-week, #leaderboard-top").css({'display' : 'none'});
	$("#" + div).css({'display' : 'block'});
}

function removeFromFeed(uuid, check)
{
	$.ajax({
		type: "POST",
		url: "/ajax/remove_from_feed.php",
		data: "uuid=" + uuid + "&check=" + check,
		success: function(msg){
			$("#game-" + uuid).html("");
			$("#game-" + uuid).css({'margin' : '0', 'padding' : '0'});
		}
	});
}
