/* Add To My Game */

var cookieGame = "mygames";
var msg_noplace = "There is no space to add in My Games";

// Function to extract game information from cookie
function mygame_list() {

	var mg = document.getElementById( 'my_games' );
	
	if( mg == null ) { return; }
	
	var thecook = cookie_read( cookieGame );
	var output = "";
	
	if( thecook == null || thecook == "" ) {
	
	} else {
	
		thecook.toString();
		
		var cookArray = thecook.split( "||" );
		
		// For update list after there are five games
		if (cookArray.length > 10)
			mygame_delete(cookArray);
		else 
		{
		 for( var ig = 0; ig < 10; ig = ig + 2 ) {
			
			if ( ig < cookArray.length ) {

					gname = cookArray[ig].replace("|","")
					gurl =  cookArray[ig+1].replace("|","")
					
					if (gname.length > 16){
						gname_new = gname.substr(0,14)
						gname_new = gname_new+".."
					}
					else 
						gname_new = gname
					output = output + '<div style="height:25px" title="'+gname+'"><a href="'+gurl+'" target="_self" class="gamename_inner">'+ gname_new +'</a></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  mygame_add(refresh_list ) {
	

	var thecook = cookie_read( cookieGame );
	thecook.toString();
	
	var  tilte;
	tilte = document.title;
	part = tilte.split("-");
	
	if (part.length > 3){
			gameName = part[0]+"-"+part[1]
			
	}
	else gameName  = part[0];
	
	//alert(gameName)
	// if game already exists...
	if ( thecook.indexOf( "|" + gameName + "|" ) >= 0 ){
			
		alert( gameName + "is already in the My Games list." );
		return;
									
	}
		
	var theline = "";
	
	if ( thecook.length < 5 ) {
	
		theline += "|" + gameName + "|";
		theline += "|" + document.location + "|";
		
	} else {
	
		theline += "|" + gameName + "|";
		theline += "|" + document.location + "|";
		theline += thecook;

	}
	
	cookie_create( cookieGame, theline, 365 );
	
	if( refresh_list == true ) { mygame_list(); }
	
}

function mygame_delete(cookArray)
{
	
	var mg1 = document.getElementById( 'my_games' );
	var theline = "";
	thegames = cookArray;
	for( i = 0; i < thegames.length-2; i = i + 2 ) {

			gname = thegames[i].replace("|","")
			gurl =  thegames[i+1].replace("|","")
			
			theline += "|" + gname + "|";
		    theline += "|" + gurl + "|";
		
		}
	
	cookie_create( cookieGame, theline, 365 );
	var thecook1 = cookie_read( cookieGame );
	var output1 = "";
	thecook1.toString();
	var cook_Array = thecook1.split( "||" );
		
	for( var y = 0; y < 10; y = y + 2 ) {
			
			if ( y < cook_Array.length ) {

					gname1 = cook_Array[y].replace("|","")
					gurl1 =  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 style="height:25px" title="'+gname1+'"><a href="'+ gurl1 +'" target="_self" class="gamename_inner">'+ gname_new1 +'</a></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 ) {
	createCookie( name, "", -1 );
}
