var currentLocation = (document.location + "").replace("http://" + window.location.hostname + "/email", "");
var getArray = currentLocation.split("/");
var email_md5 = "";
var randomInteger = -1;
loadStats();
if(showWorldwideTable)
{
	var json = '{"at":"Austria","au":"Australia","be-fr":"Belgium (French)","be-nl":"Belgium (Dutch)","br":"Brazil","ca":"Canada (English)","ch-de":"Switzerland (German)","ch-fr":"Switzerland (French)","cn":"China","cz":"Czech Republic","de":"Germany","dk":"Denmark","es":"Spain","fi":"Finland","fr":"France","hk":"Hong Kong (English)","hk-zh":"Hong Kong (Chinese)","hu":"Hungary","id":"Indonesia","ie":"Ireland","it":"Italy","jp":"Japan","kr":"South Korea","lu":"Luxembourg","mx":"Mexico","my":"Malaysia","nl":"Netherlands","no":"Norway","nz":"New Zealand","ph":"Philippines","pl":"Poland","pt":"Portugal","se":"Sweden","sg":"Singapore","th":"Thailand","tw":"Taiwan","uk":"United Kingdom","us":"United States of America","vn":"Vietnam","xf":"Canada (French)"}';
	var array = eval('(' + json + ')');
	var count = 0;
	var output = '';
	for(x in array)
	{
		if(count % 4 == 0)
		{
			output += "</tr><tr>";
		}
		output += "<td>" + x + '</td><td><img src="/images/worldwide/' + x + '.png" alt="Apple Store ' + array[x] + '" /></td>';
		count++;
	}
	document.getElementById("worldwideTable").innerHTML = "<tr>" + output + "</tr>";
}
if(getArray[1] == "manage")
{
	document.form.email.style.visibility = "hidden";
	document.getElementById("submit").value = "Update";
	document.getElementById("info").innerHTML = "Select the categories you want to subscribe to, click Update and we'll update your subscriptions.<br />You can change your subscriptions as often as you want.";
	email_md5 = "email=" + getArray[2] + "&md5=" + getArray[3];
	updateSubscriptions();
}
function loadStats()
{
	statsRequest = new XMLHttpRequest();
	statsRequest.open("GET", "/api/stats", false);
	statsRequest.send(null);
	statsResponse = statsRequest.responseText;
	stats = eval('(' + statsResponse + ')');
	changeInfo();
	loop = window.setInterval("changeInfo();", 7000);
}
function fadeIn(element, fadeIn, fast)
{
	document.getElementById(element).className = "fade" + (fadeIn ? "In" : "Out") + (fast ? "-fast" : "");
}
function changeInfo()
{
	randomInteger = getRandomInt((stats.length - 1), randomInteger);
	document.getElementById("stats").innerHTML = stats[randomInteger];
	fadeIn("stats", true);
	setTimeout(function(){fadeIn("stats", false);}, 6500);
}
function getRandomInt(rangeEnd, exclude)
{
	do
	{
		randomInteger = Math.floor(Math.random() * rangeEnd);
	}
	while(randomInteger == exclude);
	return randomInteger;
}
function subscribe()
{
	if(email_md5 == "")
	{
		email = "email=" + document.form.email.value;
	}
	else
	{
		email = email_md5;
	}
	var params = email + getSubscriptions();
	request = new XMLHttpRequest();
	request.open("POST", "/email/subscribe", true);
	request.onreadystatechange = function(){handleSubscribeReadyStateChange(this)};
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-Length", params.length);
	request.send(params);
}
function handleSubscribeReadyStateChange(object)
{
	if(object.readyState == 4)
	{
		switch(object.responseText)
		{
			case "ok": document.form.style.display = "none"; error("Thank you for your subscription! We just sent out a confirmation email."); break;
			case "updated": document.form.style.display = "none"; error("Your subscriptions were updated."); break;
			case "exists": error("This email address was already submitted before! Please enter another one.<br />If you want to change your subscriptions, please follow the link in any mail from us."); break;
			case "subscriptions": error("Please select at least one category to subscribe to."); break;
			default: error("This email address seems to be not valid. Please enter a correct one.");
		}
	}
}
function updateSubscriptions()
{
	request = new XMLHttpRequest();
	request.open("GET", "/api/getUser/" + getArray[2] + "/" + getArray[3], false);
	request.send(null);
	var response = request.responseText;
	json = eval('(' + response + ')');
	count = 0;
	for(key in json)
	{
		with(document.form.subscriptions[count])
		{
			checked = (eval("json." + key) == 1 ? "checked" : "");
		}
		count++;
	}
}
function error(error)
{
	fadeIn("error", true);
	fadeIn("info", false, true);
	setTimeout(function(){fadeIn("info", true, true); document.getElementById("info").innerHTML = error;}, 60);
	document.form.email.select();
	//shake();
}
function shake()
{
	shakeFreeze(true);
	setTimeout(function(){shakeFreeze();}, 300);
}
function shakeFreeze(s)
{
	document.form.email.className = (s ? "shake" : "");
}
function getSubscriptions()
{
	returnString = "";
	for(i = 0; i != document.form.subscriptions.length; i++)
	{
		with(document.form.subscriptions[i])
		{
			if(checked)
			{
				returnString += "&subscriptions[]=" + value;
			}
		}
	}
	return returnString;
}
