// homepage animation stuff
var anim_current = 1;
var anim_info = new Array( 3 );
var anim_data = new Array( 3 );
var anim_link = new Array( 3 );
var anim_file = new Array( 3 );
var anim_path = new Array( 3 );
	
// initialize animation data
function anim_init() {

	// only do this if the animation is available
	if ( document.getElementById( 'animList' ) == null ) { return false; }

	var animations	= document.getElementById( 'animList' ).getElementsByTagName( 'li' );
	for( var y = 0; y < animations.length; y++ ) { anim_info[ y + 1 ] = animations[ y ]; }

	animations	= document.getElementById( 'animData' ).getElementsByTagName( 'li' );	
	for( var y = 0; y < animations.length; y++ ) { anim_data[ y + 1 ] = animations[ y ]; }
	
	animations	= document.getElementById( 'animData' ).getElementsByTagName( 'a' );	
	for( var y = 0; y < animations.length; y++ ) { anim_path[ y + 1 ] = animations[ y ].href; }
		
	animations	= document.getElementById( 'animNumber' ).getElementsByTagName( 'a' );
	for( var y = 0; y < animations.length; y++ ) {
		anim_link[ y + 1 ] = animations[ y ];
		filename = animations[ y ].href.split( "#" );
		anim_file[ y + 1 ] = filename[ 1 ]; 
		animations[ y ].href = "#";
	}
	
}

// switch to next animation
function anim_next() {

	// make sure the animation can jump forward
	if ( document.getElementById( 'animList' ) == null ) { return; }
	
	anim_current ++;
				
	if ( anim_current > 3 ) { anim_current = 1;	}
	anim_set();
}
	
// switch to previous animation
function anim_prev() {
	anim_current --;
		
	if ( anim_current < 1 ) { anim_current = 3; }
	anim_set();
}
	
function anim_set( anim_new ) {
	
	// if anim_new is set then change current anim
	if ( anim_new != null ) { anim_current = anim_new; }
	
	// check animation has loaded ok
	if( anim_info[ anim_current ] == null ) { return; }
		
	// loop through icon holders and set selected value 
	for( var y = 1; y <= 3; y++ ) {
				
		// if selected set class name else remove
		if ( y == anim_current ) {
				anim_info[ y ].className = "selected";
				anim_data[ y ].className = "selected";
				anim_link[ y ].className = "selected";
		} else {
				anim_info[ y ].className = "";
				anim_data[ y ].className = "";
				anim_link[ y ].className = "";
		}
	}
	
	document.getElementById( "animHolder" ).innerHTML = AC_FL_GetContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0',
		'width', '290',
		'height', '200',
		'quality', 'high',
		'bgcolor', '#ffffff',
		'menu', 'false',
		'allowscriptaccess', 'sameDomain',
		'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		'movie', '/swfcontent/animations/' + anim_file[ anim_current ] + '?url=' + anim_path[ anim_current ],
		'wmode', 'transparent'
	);
	
	return false;

}
	
/* LIST ALL GAMES */

function list_init() {

	// list all games menu
	var games	= document.getElementById( 'gameList' ).getElementsByTagName( 'a' );

	document.forms[ 'jumpList' ].jumpSelect.options[ 0 ] = new Option(  language[ 9 ], "" );

	for( var y = 0; y < games.length; y++ ) {
		document.forms[ 'jumpList' ].jumpSelect.options[ y + 1 ] = new Option( games[ y ].innerHTML, games[ y ].href );
	}
	
	document.forms[ 'jumpList' ].jumpSelect.onchange = function() {
		list_jump();
	}
		
	// grab cookies
	var list_display = cookie_read( 'list_display' );
		
	// no cookie so set default
	if ( list_display == null ) {
		
		cookie_create( "list_display", "1", 150 );
		list_display = 1;
			
	// hidden so hide stuff
	} else if( list_display == "0" ) {
	
		document.getElementById( 'WrapperComplete' ).className = "hidden";
		document.getElementById( 'listToggle' ).innerHTML = language[ 7 ];
	}	

}
	
// show/ hide the list
function list_toggle() {

	// grab cookies
	var list_display = cookie_read( 'list_display' );
	
	// currently hidden so show
	if( list_display == "0" ) {
			
		document.getElementById( 'WrapperComplete' ).className = "shown";
		document.getElementById( 'listToggle' ).innerHTML = "Hide all Games Sections and Full Games List";
		cookie_create( "list_display", "1", 150 );
		
	// else hide	
	} else {

		document.getElementById( 'WrapperComplete' ).className = "hidden";
		document.getElementById( 'listToggle' ).innerHTML = "Show all Games Sections and Full Games List";		
		cookie_create( "list_display", "0", 150 );
		
	}
		
}

function list_jump( list ) {

	if( list == null ) { list = "jumpList"; }

	var newIndex = document.forms[ list ].jumpSelect.selectedIndex;
	
	if ( newIndex != 0 ) { 

		url = document.forms[ list ].jumpSelect.options[ newIndex ].value;
		top.location = url; 

	} 

}

/* MORE MENU */

var more_displayed = false;

function more_display() {

	if ( more_displayed == true ) {
		document.getElementById( 'moreNav' ).className = "hidden";
		more_displayed = false;
	} else {
		document.getElementById( 'moreNav' ).className = "shown";
		more_displayed = true;	
	}
	
}

/* TOOLBAR */

var cookName = "minicliptoolbar_id";
var cookieGame = "minicliptoolbarsession";
var msg_noplace = "There is no space to add more Miniclips, use the \"Edit My Miniclips...\" function to make space for new games.";

var gameurl = new Array();
var gametit = new Array();

var Maxgame = ( gametit.length - 1 );

// Function to extract game information from cookie
function toolbar_list() {

	var mg = document.getElementById( 'mg_miniclips' );
	
	if( mg == null ) { return; }
	
	var thecook = cookie_read( cookieGame );
	var output = "";
	
	if( thecook == null || thecook == "" ) {
	
		output = language[ 0 ];	
	
	} else {
	
		thecook.toString();
		
		var cookArray = thecook.split( "~" );

		for( var ig = 1; ig < 12; ig = ig + 2 ) {
			
			if ( ig < cookArray.length ) {

				var num = cookArray[ ig ].split( "^" );
				
				detail1 = document.getElementById( 'tid_' + num[ 1 ] ).href;
				detail2 = document.getElementById( 'tid_' + num[ 1 ] ).innerHTML;
				
				output = output + "<li><a href='javascript:toolbar_delete( " + num[ 1 ] + " );' class='toolbar_delete'><img src='http://images-vip.napmia.miniclip.com/images/button_delete.gif' alt='delete this miniclip' /></a><a href='" + detail1 + "'>" + detail2 +"</a></li>";
				//output = output + "<li><a href='" + detail1 + "'>" + detail2 +"</a></li>";
			
			}
		}

	}

	mg.innerHTML = output;

}

function toolbar_update(){

	//var checkSystemForUpdate = checkSystem();
	
	var url = "http://www.donotchangeme.com/?bar=minicliptoolbar&command=reloadbar";
	document.getElementById( "iToolbar" ).src = url;
	
}

function toolbar_refresh( full, addOrRemove, gameName ){

	var thecook = cookie_read( cookieGame );
	thecook = thecook.toString();
	
	if ( thecook.length > 5 ){
	
		toolbar_update();
		
		if ( full == 1 ){
		
			alert( msg_noplace );
			
		} else if( addOrRemove == "add" ){
		
			//alert( gameName + " has been added to My Miniclips. Click \"My Miniclips\" in the toolbar to use it!" );
			
		} else if( addOrRemove == "remove" ) {
		
			//alert( gameName + " " + language[ 1 ] );
				
		}
	
	}
	
}

function toolbar_add( thegameid, refresh_list ) {

	toolbar_update();

	var thecook = cookie_read( cookieGame );
	thecook.toString();
	
	gameName = document.getElementById( "tid_" + thegameid ).innerHTML;
	
	// if game already exists...
	if ( thecook.indexOf( "^" + thegameid + "~" ) >= 0 ){
			
		alert( language[ 3 ] + " " + gameName + " " + language[ 2 ] );
		return;
									
	}
	
	var theline = "";
	
	if ( thecook.length < 5 ) {
	
		theline += "gm1^" + gameName + "~";
		theline += "ur1^" + thegameid + "~";
		
	} else {
	
		theline += "gm1^" + gameName + "~";
		theline += "ur1^" + thegameid + "~";
		theline += thecook;

	}
	
	countTheCook = thecook.split( "~" );
	toolbarcook = cookie_read( cookName );
	/*
	// too many miniclips and not enough toolbar space
	if ( ( countTheCook.length - 1 ) / 2 >= 6 && toolbarcook.length < 5 ) {
	
		language[ 10 ] = "test";
		
		if ( confirm( language[ 10 ] ) ) {
		
			//alert( "install toolbar ");
		
		}
		
	}
	*/
	
	cookie_create( cookieGame, theline, 20000 );
	
	toolbar_refresh( 0, "add", gameName );

	if( refresh_list == true ) { toolbar_list(); }
	
}

function toolbar_delete( gameID ) {

	var thecook = cookie_read( cookieGame );
	var theline = "";
	
	gameName = document.getElementById( "tid_" + gameID ).innerHTML;
	
	thegames = thecook.split( "~" );
	
	for( i = 0; i < thegames.length - 2; i = i + 2 ) {
	
		number = thegames[ i + 1 ].split( "^" );
		
		if( thegames[ i + 1 ].indexOf( gameID ) > -1 ) {
		
		} else {
		
			theline += thegames[ i ] + "~" + thegames[ i + 1 ] + "~";
		
		}
	
	}

	cookie_create( cookieGame, theline, 20000 );
	toolbar_refresh( 0, "remove", gameName );
	toolbar_list();

}

/* clear current page links */

function clearCurrentLink() {

    var a = document.getElementsByTagName( "A" );
    
    for( var i = 0; i < a.length; i++ ) {
        if( a[ i ].href == window.location.href.split( "#" )[ 0 ] ) {
		    a[ i ].className = a[ i ].className + " current";
		}
    }
}

/* RECENTLY PLAYED */
function recent_save() {

	// load existing recent games
	var recentgame = t_gameId;
	var recentlist = recent_load();
	var savedgames = 0;
	
	if( recentlist != null ) {
		var recentArray = recentlist.split( "|" );
		
		// loop through array
		for( i = 0; i < recentArray.length; i++ ) {
		
			// remove duplicates and crop list to recent 6
			if ( recentArray[ i ] != t_gameId && savedgames < 5 ) {
				recentgame = recentgame + "|" + recentArray[ i ];
				savedgames ++;			
			}
		}	
	}
	
	// save new list
	cookie_create( "recent_games", recentgame, 28 );
	
}

function recent_load() {

	// load recent games into array
	recentlist = cookie_read( "recent_games" );
	
	return recentlist;
	
}

function recent_write() {

	var mg_latest = document.getElementById( "mg_latest" );
	
	if( mg_latest == null ) { return; }

	var recentlist = recent_load();
	var outputHTML = "<ul>";
	
	if( recentlist != null ) {
	
		var recentArray = recentlist.split( "|" );
		
		// loop through array
		for( i = 0; i <= recentArray.length; i++ ) {
		
			var gamelist = document.getElementById( "tid_" + recentArray[ i ] );
			
			if ( recentArray[ i ] != 0 && gamelist != null ) {
			
				gamename = gamelist.innerHTML;
				gameurl = gamelist.href;

				outputHTML = outputHTML + "<li><a href=\"" + gameurl + "\">" + gamename + "</a></li>\n";
				
			}
					
		}
		
		outputHTML = outputHTML + "</ul>";
		
	} else {
		
		outputHTML = language[ 4 ];
		
	}
	
	mg_latest.innerHTML = outputHTML;
	
}

/* set homepage for IE */
function SetHomepage() {

	if ( document.all ){
		document.write( '<a href="javascript:history.go(0);" id="icon_blog" onClick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.Miniclip.com/\');">' );
		document.write( language[ 5 ] + '</a>');
	}
	
}

function popProduct( productID ) {

	var url		= "http://www.miniclip.com/metacharge/www/index.php?id=" + productID;
	var width	= 440;
	var height	= 580;

	var args	= "width = " + width + ","
	    + "height=" + height + ","
	    + "resizable=0,"
	    + "scrollbars=0,"
	    + "statusbar=0,"
	    + "menubar=0,"
	    + "hotkeys=0,"
    	+ "titlebar=0";

   	popupWin = window.open( url, 'popupWin', args );
		
}

/* preview the games room */
function previewGamesRoom() {

	title = "Miniclip Games Room preview";
	widthVal = 600;
	heightVal = 280;
	       
    args="width = " + widthVal + ","
	+ "height=" + heightVal + ","
	+ "resizable=1,"
	+ "scrollbars=1,"
	+ "statusbar=1,"
	+ "menubar=1,"
	+ "hotkeys=0,"
	+ "titlebar=1";
    
	popupWin = window.open( '/games/gamesroom.htm', 'popupWin', args );
	popupWin.document.write( page_data );
	popupWin.document.close( );
	
	return false;
								
}
/* 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 );
}

/* CLIPBOARD ( IE only naturally ).. naturally?*/

function cb_init() {

	// check we're not in MSIE
	if ( navigator.appName != "Microsoft Internet Explorer" ) {
	
		// grab all the divs
		var divs = document.getElementsByTagName( "div" );
	
		// if a copy div replace content
	  	for( var i = 0; i < divs.length; i++ ) {
			var div = divs[ i ];
			
			// hide unusable buttons
			if ( div.className == "copyButton" ) {
	
				div.innerHTML = language[ 6 ];
				
			}
			
		}	
	}

}

function cb_copy( item ) {

	copied = item.createTextRange();
	item.focus();
	item.select();
	copied.execCommand("Copy");
	
}

/* GENERIC STUFF */
	
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function writeEmail( name ) {
 
	address = name + "&#64" + "miniclip.com";
	
	document.write( "<a href='mailto:" + address + "'>" + address + "</a>" );
  
}

function prerollCreateGame( pr_gamename, pr_gamewidth, pr_gameheight ) {

	// --------------------------------------------------------------------
	// create the code for a flash game, simple function that can be reused
	// --------------------------------------------------------------------

	str_return = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"" + pr_gamewidth + "\" height=\"" + pr_gameheight + "\">";
	str_return = str_return + "<param name=\"movie\" value=\"" + pr_gamename + "\">";
	str_return = str_return + "<param name=\"quality\" value=\"high\">";
	str_return = str_return + "<param name=\"menu\" value=\"false\">";
	str_return = str_return + "<embed src=\"" + pr_gamename + "\" quality=\"high\" menu=\"false\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + pr_gamewidth + "\" height=\"" + pr_gameheight + "\"></embed>";
	str_return = str_return + "</object>";

	document.getElementById( "video_ad" ).innerHTML = str_return;

}



// XML stuff..
function xml_getXMLHttpRequest() {
	var req = false;

    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	return req;
}

function xml_debug(text) {
	return;
	var output = document.getElementById('miniPanel');
	output.innerHTML = text;
}
function xml_onReadyStateChange(e) {
	xml_debug('readystatechange: ' + xml_request.readyState);
    if (xml_request.readyState == 4) {
		xml_debug('done: ' + xml_url);
    }
}

function xml_loadXMLDoc(url) {
	xml_request = xml_getXMLHttpRequest();
	if(xml_request) {
		xml_debug('loading: ' + url);
		xml_request.onreadystatechange = xml_onReadyStateChange;
		try {
			xml_request.open("GET", url, true);
			xml_request.send(null);
		} catch(e) {
			xml_debug('error: ' + e);
		}
	}
}

function xml_tracker(id) {
	xml_url = 'http://www.miniclip.com/php/tracker/tracker.php?p=' + id + "&rand=" + Math.round(Math.random()*10000);
	xml_loadXMLDoc(xml_url);
}
var xml_request = null;
var xml_url = "";
