$(document).ready(function(){
	$('.more ul').hide();
	$('.more').hover(
		function() { $('ul',this).css('display', 'block'); $(this).addClass('highlight'); },
		function() { $('ul',this).fadeOut('fast'); $(this).removeClass('highlight'); }
	);
	$("#slider").easySlider({auto: true, continuous: true, pause: 4000});
	tabson();

	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn("fast");	
		$('#mask').fadeTo("fast",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn("fast"); 
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
	
	favorite();
	comments();

	$('#dialog').jqm({ajax:'@href',modal:true,trigger: 'a.jax'}); 
	
});
function tabson(){
	$('#tabs .hide').hide();
	$('#tabs .hide:first').show();
	$('#tabs .tablinks li a:first').addClass('selected');
	$('#tabs .tablinks li a').click(function(){
		$('#tabs .tablinks li a').removeClass('selected');
		$(this).addClass('selected');
		var currentTab = $(this).attr('href');
		$('#tabs .hide').hide();
		$(currentTab).show();
		return false;
	});
}

function comments(){
	cshow();
	cpost();
}
function cshow(){
	// vote up
	//$("#comment-form").html('<div class="box heading red"><h2>Comments</h2></div><form method="post" action="/"><textarea class="comment-box"></textarea><input type="hidden" name="game_id" value="214" id="game_id" /><a href="/" class="button btn-red post">Post Comment</a><small>Commenting will <strong>NOT</strong> interrupt your game</small></form>');
	$(".error_box").hide();
	$(function(){
		$(".voting a.up").click(function(){
		 the_id = $(this).attr('name');
		 //$("span#votes_count"+the_id).fadeOut("fast");
		  $.ajax({
		   type: "POST",
		   data: "vote=1&id="+the_id,
		   url: "vote.php",
		   success: function(html)
		   {
			$("span#votes_count"+the_id).html(html);
			$("span#votes_count"+the_id).fadeIn();
			$(".cv"+the_id+" a.up").addClass("voted-up");
		   },
		   error: function(html)
		   {
			$("span#votes_count"+the_id).html(html);
			$("span#votes_count"+the_id).fadeIn();
		   }
		  });
		return false;
		});
	});	
	$(function(){
		$(".show-comment").click(function(){
			the_id = $(this).parents('li').attr('id');
			$("#"+the_id+" .lowscore").hide();
			$("#"+the_id+" .thumb").fadeIn("fast");
			$("#"+the_id+" .main").fadeIn("fast");
			$("#"+the_id).removeClass('hidden');
			return false;
		});
	});
}

function cpost(){
	// post comment
	$("#pcomment").click(function() {
		$("#pcomment").removeClass("btn-red").addClass("btn-black").html("<span>Posting...</span>");
		var body = $('#body').attr('value');		
		$.ajax({
		  type: "POST",  
		  url: "comments.php",  
		  data: "id=" + id + "&type=" + type + "&task=post" + "&body=" + body,  
		  success: function(html) { 
			$("#pcomment").removeClass("btn-black").addClass("btn-red").html("<span>Post Comment</span>");
			$(".posted").html("");
			$(".error_box").hide();
	 		$(".comment-list").prepend(html);
	 		$(".nocomments").hide();
		  },
		  error: function() {
		  	 console.log("Failed to submit");
		  }
		});
		return false;
	});
}
function loadComments(page,type,id,pp) {
	$.ajax({
	  type: "GET",
	  url: "comments.php",
	  data: "id=" + id + "&type=" + type + "&pp=" + pp + "&p=" + page,
	  success: function(html){
	    $("#comments").html(html);
	  }
	});
	$().ajaxSend(function(r,s){
		$("#loading").show();
	});
	$().ajaxStop(function(r,s){
		$("#loading").fadeOut("fast");
	});
}

function page_next(curpage) {
	$("#comments").text("");
	loadComments(curpage+1,type,id,pp);
}
function page_prev(curpage) {
	$("#comments").text("");
	loadComments(curpage-1,type,id,pp);
}
function favorite () {
	$(".favorite").click(function() {
		var game = $(this).attr('id');
		$.ajax({
		  type: "POST",  
		  url: "game_fav.php",  
		  data: "game_id=" + game + "&task=add",
		  success: function(html) {
			$("#fav").html("Favorited!");
		  },
		  error: function() {
		  	 console.log("Failed to submit");
		  }
		});
		return false;
	});
}