$(document).ready(function(){

	/**
	 * Get my favorites
	 */
	$.ajax({
		   type: "POST",
		   url: "/user/collect",
		   success: function(msg){
			   //alert(typeof(msg));
			   if(msg == 2){
				  // alert('aaaa');
				   html = '<span class="textLabel">Please <a href="/login">login</a> first.</span>';
				   $('#collect_games').html(html);
			   }else{
				   if(msg == 1){

				   }else{
					   var myFavorites = eval(msg);
					   var html = '<ul id="myfavorites">';

					   if (myFavorites.length <= 0 )
					   {
						   return;
					   }
					   for (var i =0; i < myFavorites.length;i++) {
						   var currentGame = myFavorites[i];
						   html = html + '<li><a href="' + currentGame.visiturl + '">' + currentGame.subtitle + '</a>';
					   }
					   html = html + '</ul>';

					   if (myFavorites.length > 3){
						   html = html + '<br><div class="moreGames"><a href="/collect/more/">More</a></div>';
					   }

					   //alert(html);
					   $("#collect_games").html(html);
				   }
			   }
		   }
	});

	/**
	 * Get recent played
	 */
	$.ajax({
		   type: "POST",
		   url: "/user/visit",
		   success: function(msg){
			   if(msg != false){
				   var playedGames = eval(msg);
				   var html = '<ul>';
				   for (var i =0; i < playedGames.length;i++) {
					   var currentGame = playedGames[i];
					   html = html + '<li><img src="/images/addtofavorites.gif" width="10" height="10" border="0" alt="Add to my favorites" title="Add to my favorites" onclick="addCollect(' + currentGame.gameid + ', \'' + currentGame.visiturl +'\', \'' + currentGame.subtitle + '\')">&nbsp;&nbsp;';
					   html = html + '<a href="' + currentGame.visiturl + '">' + currentGame.subtitle + '</a></li>';
				   }
				   html = html + '</ul>'
				   html = html + '<div id="confirm_dialog" title="Basic dialog"></div>';

				   if (playedGames.length > 0){
						   html = html + '<br><div id="morePlayedGames"><a href="/visit/more/">More</a></div>';
					   }

				   //alert(html);
				  $("#recent_played_games").html(html);
			   }
		   }
	});

	$.ajax({
		   type: "POST",
		   url: "/user/nick",
		   success: function(nickname){
		      if(nickname != false){
				  login = nickname + ' | <a href="/user/modifyuser">settings</a> | <a href="/login/logout">logout</a>';
				  $("#guide").html(login);
			  }else{
				  login = '';
				  $("#guide").html(login);
			  }

		   }
	});

	$.ajax({
		   type: "POST",
		   url: "/user/haslogin",
		   success: function(msg){
			   if(msg == false){
				   $("#registeritr").html('<td><a href="/register"><img src="/images/register.gif" border="0" alt=""></a></td>');
				   $("#forgettr").html('<td><span class="textLabel forgetText">Forgot your User ID/Password?</span></td>');
			   }
		   }
	});
});

function addCollect(gid, visiturl, subtitle){
	$.ajax({
		   type: "POST",
		   url: "/collect/add/gid/" + gid,
		   success: function(msg){
			   var response = eval("(" + msg + ")");

				if (response.status != '0') {
					addItemToFavorites(gid, visiturl, subtitle);
				}

				$("#confirm_dialog").html(response.msg);

				//$("#confirm_dialog").css('display', 'block');
				showDialog();
				setTimeout("hideDialog()", 3000);
		}
	})
}

function showDialog(){
	$("#confirm_dialog").show();

}
function   hideDialog(){
	$("#confirm_dialog").fadeOut(2000);
}

/*
 * Add the game item from the recent_played_games
 * to collect_games.
 */
function addItemToFavorites(gid, visiturl, subtitle){
	var html = '<li><a href="' + visiturl + '">' + subtitle + '</a>';
	$("#myfavorites").append(html);
}