//Top Articles

var taSelectedSlot = 1;
var taTimeoutID = 0;

function taSetSlot(slot, clicked) {
	taSelectedSlot = slot;
	if (clicked) {
		clearTimeout(taTimeoutID);
		taTimeoutID = setTimeout ("taNextSlot()", 10000);
	}
	if ($("#top-article-"+slot).is(":hidden")) {
		$("#top-articles-content li").fadeOut("slow");
		$("#top-article-"+slot).fadeIn("slow");
		$("#top-articles-nav li").removeClass("selected");
		$(".top-article-nav-"+slot).addClass("selected");
	}
}

function taNextSlot() {
	taSelectedSlot = taSelectedSlot + 1;
	if (taSelectedSlot > 4) {
		taSelectedSlot = 1;
	}
	taSetSlot(taSelectedSlot, false);
	taTimeoutID = setTimeout ("taNextSlot()", 5000);
}

//Navbar
function navBar(tab) {
	var secondaryIsOpen = $("#primary-nav .home").hasClass("home-open");
	var thisTabIsOpen = $("#"+tab).hasClass("is-open");
	
	if (secondaryIsOpen) {
		if (thisTabIsOpen) {
			$("#"+tab).hide();
			$("#"+tab).removeClass("is-open");
			$("#primary-nav .home").removeClass("home-open");
		} else {
			$(".is-open").hide();
			$(".is-open").removeClass("is-open");
			$("#"+tab).show();
			$("#"+tab).addClass("is-open");	
		}
	} else {
		$("#primary-nav .home").addClass("home-open");
			$("#"+tab).show();
			$("#"+tab).addClass("is-open");	
	}
}

function CommentsReplyTo(id) {
	$("#reply"+id).show();
	$("#replyhere").appendTo("#reply"+id);
	$("#replyhere").show();
	$("#replyhere .parent_id").val(id);
}

function clearGameFilter() {
	$(".results-filter input").attr('checked', false);
}

//Game Score Ajax
function setGameScore(type_id, id, score) {
	$.post("/api/user/postRating", { entity_type_id: type_id, entity_id: id, rating: score },			
	function(data) {
	//alert(data);
		var display_avg = Math.round(10*data.avg_rating)/10;
		var display_votes = "<span id='user-score-count'>" + data.num_ratings + "</span> vote";
		
		if (display_avg % 1 == 0) {
			display_avg = display_avg + '.0';
		}
		
		if (data.num_ratings > 1 ) {
			display_votes = display_votes + 's';
		}
	
		$("#user-rating a").removeClass("selected");
		$("#user-rating a.rating-"+score).addClass("selected");
		$("#user-rating-large a").removeClass("selected");
		$("#user-rating-large a.rating-"+score).addClass("selected");
		$(".content .first").hide();

		$(".content .user-score").text(display_avg);
		$(".content .votes").html(display_votes);
		$(".content .votes").show();
		$(".content .avgscore").show();
		$(".content .user-score").show();
}, "json");
	
}

/*jqModal stuff */
function videoModalOpen() {
	var target = $(this).attr("href");
	
	$("#modal-video iframe").replaceWith("<iframe src='" + target + "'></iframe>");
	
}

$(document).ready(function(){	
	//init Top Article carousel timer
	taTimeoutID = setTimeout("taNextSlot()", 3000);

	/*jqModal stuff */
	$('#login-dialog').jqm({
		trigger: '.login-dialog'
	});
	
	//LightBox
	$(".lightbox").lightbox({
		fitToScreen: true,
		imageClickClose: false
	});
	
	//review call to arms
	setTimeout('$(".user-content-cta").fadeIn()', 2000)
	
});