/* Add To My Game */
var cookieGame = "mygamesv3";
var cookieRecentGame = "recentgames";
var msg_noplace = "There is no space to add in My Games";
var global_mygames_default_text = '';
var global_recentgames_default_text = '';
var isGlobalsSet=false;
// Function to extract game information from cookie
function usergames_list(list_type, el_id, mygames_default_text, recentgames_default_text)  {
	
	if(!isGlobalsSet) {
		//alert("Setting globals");
		global_mygames_default_text=mygames_default_text;
		global_recentgames_default_text = recentgames_default_text;
		isGlobalsSet=true;
	}

	if (list_type=='mygames') {
		cookie_create( 'mygames', "", -1 );
		list_type = 'mygamesv3' ;
	}
	// Get the DOM element containing mygames
	var mg = document.getElementById( el_id );
	
	// If not found, do nothing
	if( mg == null ) { return; }
	
	// Else get the mygames list from the cookies
	var thecook = cookie_read( list_type );
	var output = "";
	//alert(thecook);
	// If nothing in the cookies, do nothing
	if( thecook == null || thecook == "" ) {
		
		if (list_type=='mygamesv3') {
	 		mg.innerHTML = "<p align='center'><br>"+mygames_default_text+"</p>";
		} else if (list_type=='recentgames') {
	 		mg.innerHTML = "<p align='center'><br>"+recentgames_default_text+"</p>";			
		}
	  
	} else {
	
		// Split the cookie list into tokens of strings, separated by ||
		thecook.toString();
		//alert(thecook)
		var cookArray = thecook.split( "||" );
		//alert(thecook);
		// Maximum number of games we store is 3.  There are 3 attributes in the cookie per game
		if (cookArray.length > 9)
			mygame_delete(cookArray);
		else 
		{
		 for( var ig = 0; ig < 9 ; ig = ig + 3 ) {
			
			if ( ig < cookArray.length ) {

					gname = unescape(cookArray[ig].replace("|",""))
					gurl =  cookArray[ig+1].replace("|","")
					gameid =  cookArray[ig+2].replace("|","")
					
					if (gname.length > 16){
						gname_new = gname.substr(0,14)
						gname_new = gname_new+".."
					}
					else 
						gname_new = gname
					gnameWOPlay=gname.substr(4,gname.length);
					output += '<div class="games_thumbnailRight marginLeftTow"><center>'
					if (list_type=='mygamesv3') {
					output += '<a href="javascript:void(0);" onclick="mygame_remove('+ ig +')"><img border="0" src="http://static.gangofgamers.com/imagesv3/deleteCross.gif" title="Remove" alt="Remove" /> </a>' }
					output += '<a class="rThumbLink" href="'+gurl+'" target="_self">'
					output += '<img border="0" title="'+gname+' Game" alt="'+gname+' Game" src="http://static.gangofgamers.com/imagesv3/th/th_'+gameid+'.gif"></a>'
					if(el_id=="recent_games") {
						output += '<h5 class="padTopFive"><a href="'+gurl+'">'+gnameWOPlay+'</a></h5></div>';
					} else {
						output += '<h5 class="padTopFive"><a href="'+gurl+'">'+gname+'</a></h5></div>';
					}
					
			}
		}
		mg.innerHTML = output;
	  }
	  
	}
	

}

function  mygame_refresh(){

	var thecook = cookie_read( cookieGame );
	thecook = thecook.toString();
	
	//For max no of games in Mygame list
	
	max_mygame = thecook.split("http");
	
	//alert(max_mygame.length)
	
	if (max_mygame.length < 5 ){
		
			alert( msg_noplace );
			return;
			
		} 
	
}

function getGameName() // reads the document title and returns the game name
{
	var  title;
	var gameName;
	title = document.title;
	part = title.split("-");
	//console.debug(part)
	if (part.length > 4){
			gameName = part[0]+"-"+part[1]		
	}
	else gameName  = part[0];
	return gameName ;
}

function  recentgame_add(refresh_list, game_id  ) {
	

	var thecook = cookie_read( cookieRecentGame );
	thecook.toString();

	if ( thecook.indexOf( "|" + game_id + "|" ) >= 0 ){			
		return;									
	}
	
	cookArray = thecook.split("||");
	if (cookArray.length >= 9) // If 3 recent games are added then delete the last game in the array to make place for the latest.
	{
		for(i=1;i<=3;i++) cookArray.pop(); 
		thecook = cookArray.join("||") + "|"
	}
	
	//console.debug(cookArray);
	
	gameName = getGameName() ;
	var theline = "";
	
	if ( thecook.length < 5 ) {
	
		theline += "|" + escape(gameName) + "|";
		theline += "|" + document.location + "|";
		theline += "|" + game_id + "|" ;
		
	} else {
	
		theline += "|" + escape(gameName) + "|";
		theline += "|" + document.location + "|";
		theline += "|" + game_id + "|" ;
		theline += thecook;

	}
	//console.debug(theline);
	cookie_create( cookieRecentGame, theline, 365 );
	
	if( refresh_list == true ) {usergames_list('recentgames', 'recent_games',global_mygames_default_text,global_recentgames_default_text); }
	
}

function mygame_remove(arr_index)
{
	var thecook = cookie_read( cookieGame );
	thecook.toString();
	//alert(thecook);
	cookArray = thecook.split("||");

	cookArray.splice(arr_index, 3); 
	for (i=0 ; i<cookArray.length;i++)
	{
		cookArray[i] = cookArray[i].replace("|", "")
	}
	thecook = cookArray.join("||")
	//alert("before:"+thecook);
	if (thecook != "") { thecook = "|" + thecook + "|" }
	//alert(thecook);
	cookie_create( cookieGame, thecook, 365 );
	usergames_list(cookieGame, 'my_games',global_mygames_default_text,global_recentgames_default_text);	
	
}
function  mygame_add(refresh_list, game_id  ) {
	
	
	var thecook = cookie_read( cookieGame );
	thecook.toString();
	
	gameName = getGameName();
	
	if ( thecook.indexOf( "|" + game_id + "|" ) >= 0 ){
			
		alert( gameName + " is already in the My Games list." );
		return;
									
	}
		
	var theline = "";
	
		cookArray = thecook.split("||");
	if (cookArray.length >= 9) // If 3 recent games are added then delete the last game in the array to make place for the latest.
	{
		alert("Please remove a game from My Games list to add this game.");
		return;
	}
	
	if ( thecook.length < 5 ) {
	
		theline += "|" + escape(gameName) + "|";
		theline += "|" + document.location + "|";
		theline += "|" + game_id + "|" ;
		
	} else {
	
		theline += "|" + escape(gameName) + "|";
		theline += "|" + document.location + "|";
		theline += "|" + game_id + "|" ;
		theline += thecook;

	}
	
	cookie_create( cookieGame, theline, 365 );
	
	showMessage('A2MyG_msg','A2MyG_msg_inner', 'Game added successfully')
	
	
	if( refresh_list == true ) {usergames_list(cookieGame, 'my_games',global_mygames_default_text,global_recentgames_default_text); }
	
}
function showMessage(node,node_inner, msg)
{
	$("#"+node_inner).html(msg)
	tooltipShow(node);
	function_name = "tooltipHide('" + node + "')"
	setTimeout ( function_name, 7000 );
}
function mygame_delete(cookArray)
{
	
	var mg1 = document.getElementById( 'my_games' );
	var theline = "";
	thegames = cookArray;
	for( i = 0; i < thegames.length-2; i = i + 3 ) {

			gname = thegames[i].replace("|","")
			gurl =  thegames[i+1].replace("|","")
			gameid = thegames[i+2].replace("|","")
			
			theline += "|" + gname + "|";
		    theline += "|" + gurl + "|";
		    theline += "|" + gameid + "|";
		
		}
	
	cookie_create( cookieGame, theline, 365 );
	var thecook1 = cookie_read( cookieGame );
	var output1 = "";
	thecook1.toString();
	var cook_Array = thecook1.split( "||" );
		
	for( var y = 0; y < 9; y = y + 3 ) {
			
			if ( y < cook_Array.length ) {

					gname1 = cook_Array[y].replace("|","")
					gurl1 =  cook_Array[y+1].replace("|","")
					gameid1 = cook_Array[y+1].replace("|","")
				
					if (gname1.length > 16){
						gname_new1 = gname1.substr(0,14)
						gname_new1 = gname_new1+".."
					}
					else 
						gname_new1 = gname1
						
					//output1 = output1 + "<div style='height:25px' title='"+gname1+"'><a href='" + gurl1 + "' target='_self' class='gamename_inner'>" + gname_new1 +"</a></div>";
					output1 = output1 + 
						'<div class="games_thumbnailRight marginLeftTow"><center>' +
						'<a class="rThumbLink" href="'+gurl1+'" target="_self">'+
						'<img border="0" title="'+gname_new1+'" alt="'+gname_new1+'" src="http://cdn.adoption.games2win.com/imagesv3/th/th_'+gameid1+'.gif"></a>'+
						'<h5 class="padTopFive"><a href="'+gurl1+'">'+gname_new1+'</a></h5></div>';
								}
			
		}
		mg1.innerHTML = "";
		mg1.innerHTML = output1
		
}
/* COOKIES */
	
function cookie_create( name, value, days ) {

	if ( days ) {
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString();
	} else {
		var expires = "";
	}
	
	document.cookie = name + "=" + value + expires +"; path=/";
}
	
function cookie_read( name ) {

	var nameEQ = name + "=";
	var ca = document.cookie.split( ';' );
		
	for( var i = 0; i < ca.length; i++ ) {
		var c = ca[i];
		while ( c.charAt(0) == ' ' ) c = c.substring( 1, c.length );
		if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length );
	}
	
	return "";
	
}
	
function cookie_erase( name ) {
	cookie_create( name, "", -1 );
}