var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var pageTracker = _gat._getTracker("UA-4240998-1");
pageTracker._initData();
pageTracker._trackPageview();

function create_xml_http()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}
function preview()
{
	var gender = document.getElementById('gender').value;
	var faction = document.getElementById('faction').options[document.getElementById('faction').selectedIndex].value;
	var eye_color = document.getElementById('eye_color').value;

	var eye_color_red = eye_color.split(",")[0];
	var eye_color_green = eye_color.split(",")[1];
	var eye_color_blue = eye_color.split(",")[2];

	var eye_color_gamma_list = new Array();
	eye_color_gamma_list[0] = 'dark';
	eye_color_gamma_list[1] = 'normal';
	eye_color_gamma_list[2] = 'light';

	var eye_color_gamma = eye_color.split(",")[3];

	if(eye_color)
	{
		var eyes = "&eyes=" + eye_color_gamma_list[eye_color_gamma];
	}
	else
	{
		var eyes = "&eyes=0";
	}

	var hair_type = document.getElementById('hair_type').options[document.getElementById('hair_type').selectedIndex].value;
	var hair_color = document.getElementById('hair_color').value;

	var hair_color_red = hair_color.split(",")[0];
	var hair_color_green = hair_color.split(",")[1];
	var hair_color_blue = hair_color.split(",")[2];

	var hair_color_gamma_list = new Array();
	hair_color_gamma_list[0] = 'dark';
	hair_color_gamma_list[1] = 'normal';
	hair_color_gamma_list[2] = 'light';

	var hair_color_gamma = hair_color.split(",")[3];

	if(hair_type > 0)
	{
		var hair = "&hair=" + hair_type + "_" + hair_color_gamma_list[hair_color_gamma];
	}
	else
	{
		var hair = "&hair=";
	}

	var beard_type = document.getElementById('beard_type').options[document.getElementById('beard_type').selectedIndex].value;
	var beard_color = document.getElementById('beard_color').value;

	var beard_color_red = beard_color.split(",")[0];
	var beard_color_green = beard_color.split(",")[1];
	var beard_color_blue = beard_color.split(",")[2];

	var beard_color_gamma_list = new Array();
	beard_color_gamma_list[0] = 'dark';
	beard_color_gamma_list[1] = 'normal';
	beard_color_gamma_list[2] = 'light';

	var beard_color_gamma = beard_color.split(",")[3];

	if(beard_type > 0)
	{
		var beard = "&beard=" + beard_type + "_" + beard_color_gamma_list[beard_color_gamma];
	}
	else
	{
		var beard = "&beard=";
	}

	document.getElementById("face").src = "face.php?faction_id=" + faction + "&gender_id=" + gender + hair + "&hair_red=" + parseInt(hair_color_red) + "&hair_green=" + parseInt(hair_color_green) + "&hair_blue=" + parseInt(hair_color_blue) + beard + "&beard_red=" + parseInt(beard_color_red) + "&beard_green=" + parseInt(beard_color_green) + "&beard_blue=" + parseInt(beard_color_blue) + eyes + "&eye_red=" + parseInt(eye_color_red) + "&eye_green=" + parseInt(eye_color_green) + "&eye_blue=" + parseInt(eye_color_blue);
}
var event_count = 0;

function notify_event()
{
	xmlHttp = create_xml_http();
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			if(xmlHttp.responseText)
			{
				if(Math.floor(xmlHttp.responseText) > event_count || document.getElementById("game_link").className != "notify")
				{
					/* Store number of events. */
					event_count = Math.floor(xmlHttp.responseText);

					/* Ask for attention in the task bar. */
					// window.focus();

					/* Update the game link with notify status. */
					document.getElementById("game_link").className = "notify";

					/* Update the player profile. */
					player_update();
				}
			}
			else
			{
				document.getElementById("game_link").className = "";
			}
		}
	}

	xmlHttp.open("GET","/src/ajax/event_count.php", true);
	xmlHttp.send(null);
}var animating = Array();

animating["health"] = 0;
animating["mana"] = 0;
animating["actions"] = 0;
animating["experience"] = 0;


function player_update()
{
	if(document.getElementById("player_profile"))
	{
		xmlHttp = create_xml_http();
		xmlHttp.onreadystatechange = function()
		{
			if(xmlHttp.readyState == 4)
			{
				if(xmlHttp.responseText)
				{
					// Store the response in the player object.
					eval("player = " + xmlHttp.responseText);

					/* Update the players health. */
					if(animating["health"] == 0)
					{
						delay = Math.round(1000 / Math.abs(player["health"] - Math.floor(document.getElementById("player_health_text").innerHTML)));
						animate_profile_bar("health", player["health"], Math.floor(document.getElementById("player_health_text").innerHTML), player["max_health"], delay)
					}

					/* Update the players health. */
					if(animating["mana"] == 0)
					{
						delay = Math.round(1000 / Math.abs(player["mana"] - Math.floor(document.getElementById("player_mana_text").innerHTML)));
						animate_profile_bar("mana", player["mana"], Math.floor(document.getElementById("player_mana_text").innerHTML), player["max_mana"], delay)
					}

					/* Update the players health. */
					if(animating["actions"] == 0)
					{
						delay = Math.round(1000 / Math.abs(player["actions"] - Math.floor(document.getElementById("player_actions_text").innerHTML)));
						animate_profile_bar("actions", player["actions"], Math.floor(document.getElementById("player_actions_text").innerHTML), player["max_actions"], delay)
					}

					/* Update the players health. */
					if(animating["experience"] == 0)
					{
						delay = Math.round(1000 / Math.abs(player["experience"] - Math.floor(document.getElementById("player_experience_text").innerHTML)));
						animate_profile_bar("experience", player["experience"], Math.floor(document.getElementById("player_experience_text").innerHTML), player["max_experience"], delay)
					}
				}
			}
		}
	}

	xmlHttp.open("GET","/src/ajax/player_status.php", true);
	xmlHttp.send(null);
}

function set_value(target_type, target_value, max_value)
{
	document.getElementById("player_" + target_type).style.width = Math.floor((target_value / max_value) * 122) + "px";
	document.getElementById("player_" + target_type + "_text").innerHTML = Math.floor(target_value);
}

function animate_profile_bar(target_type, target_value, current_value, max_value, delay)
{
	animating[target_type] = 1;

	document.getElementById("player_" + target_type).style.width = Math.floor((Math.floor(current_value) / max_value) * 122) + "px";
	document.getElementById("player_" + target_type + "_text").innerHTML = Math.floor(current_value);

	if(current_value > target_value)
	{
		setTimeout("animate_profile_bar(\"" + target_type + "\", " + target_value + ", " + Math.floor(current_value - 1) + ", " + max_value + ", " + delay + ")", delay);
	}

	else if(current_value < target_value)
	{
		setTimeout("animate_profile_bar(\"" + target_type + "\", " +  target_value + ", " + Math.floor(current_value + 1) + ", " + max_value + ", " + delay + ")", delay);
	}

	else if(current_value == target_value)
	{
		animating[target_type] = 0;
	}
}
