// Dealing with local storage of data.

function setCookie(name, value, days)
{
	var cookieValues = [];

	cookieValues.push(name + "=" + value);

	if (days)
	{
		var expireDate = new Date();
		expireDate.setTime(expireDate.getTime() + (days*24*60*60*1000));
		cookieValues.push("expires=" + expireDate.toGMTString());
	}

	cookieValues.push("path=/");

	document.cookie = cookieValues.join("; ");
}

function getCookie(name)
{
	var result = null;
	var matchKey = name + "=";
	var allCookies = document.cookie.split("; ");

	for (var i in allCookies)
	{
		if (!allCookies[i].indexOf(matchKey))
		{
			result = allCookies[i].substring(matchKey.length);
			break;
		}
	}

	return result;
}

function updateHourMark()
{
	var watchCookie = getCookie("watch");
	var watchList = watchCookie ? unescape(watchCookie).split(",") : [];
	var lastRelease = unescape(getCookie("lastRelease"));
	var newRelease = null;
	var allTDs = document.getElementsByTagName("td");

	for (var i = 0; i < allTDs.length; i++)
	{
		var iTD = allTDs[i];

		if (iTD.className == "info")
		{
			if (!newRelease)
			{
				newRelease = iTD.innerHTML;
			}
			if (iTD.innerHTML == lastRelease)
			{
				iTD.parentNode.style.backgroundColor = "#9cf";
				break;
			}
			
			for (var watchIndex in watchList)
			{
				if (iTD.innerHTML.match(watchList[watchIndex]))
				{
					iTD.style.border = "2px black solid";
					iTD.title = watchList[watchIndex];
				}
			}
		}
	}
	setCookie("lastRelease", escape(newRelease), 1);

	// A few additional tricks for people on the internal site.  We really need a proper onload.
	if (location.hostname.substr(-11) == "subsume.net")
	{
		var allIMGs = document.getElementsByTagName("img");
	
		//window.console.log("We usually run Safari!  :-)")
		for (var e = 0; e < document.images.length; e++)
		{
			var imgElement = document.images[e];
			var internalURL = "http://www.subsume.net/";
	
			if (imgElement.src.substr(0, internalURL.length) == internalURL)
			{
				imgElement.src = imgElement.src.substr(internalURL.length);
			}
		}
	}
}
