// (c) 2005-2007 funkypool.com

function generateGameWindowLink(page, loggedIn, loggedInTitle, loggedOutTitle, large, initialGame) {
	if (loggedIn) {
		page += "?loggedIn=true";
		if (initialGame != "") {
			page += "&initialGame=" + initialGame;
		}
		title = loggedInTitle;
	} else {
		if (initialGame != "") {
			page += "?initialGame=" + initialGame;
		}
		title = loggedOutTitle;
	}
	document.write('<a href="javascript:openGameWindow(\'' + page + '\',' + large + ');" title="Open game window">');
	document.write(title);
	document.write('</a>');
}

function openGameWindow(page, large) {
	try {
		var w = 800, h = 600; // default sizes
		if (window.screen) {
			w = window.screen.availWidth;
			h = window.screen.availHeight;

			if (w < 800) {
				var confirmed = confirm('Your screen resolution is set too low to play funkypool.com\nRequired minimum resolution is 800 * 600.\nDo you wish to continue anyway?');
				if (!confirmed) {
					return false;
				}
			}
		}

		// safe initial size for 800*600
		var windowwidth = 625;
		var windowheight = 503;

		var usingIE7 = false;
		if (navigator) {
			if (navigator.appVersion.indexOf("MSIE 7.0") != -1) {
				//usingIE7 = true;
			}
		}

		if (usingIE7) {
			// or bigger window if room
			if (w > 900) {
				windowwidth = 878;
				windowheight = 650;
			}
		} else {
			var windowwidth = 625;
			var windowheight = 523;

			// or bigger window if room
			if (w > 900) {
				windowwidth = 870;
				windowheight = 680;
			}
		}

		// if large add the increase size
		if (large != undefined && large) {
			windowheight += 110;
		}

		// center it
		var top = (h - windowheight) / 2;
		var left = (w - windowwidth) / 2;

		// open the window
		var windowObj = window.open(page,'_blank','top='+top+',left='+left+',width='+windowwidth+',height='+windowheight+',scrollbars=no,resizable=yes,status=no');

		if (windowObj == null || windowObj.closed) {
			alert("Your computer seems to be running some sort of pop-up stopper.\n" +
				"To open a funkypool.com game window please allow pop-ups from this\n" +
				"site. funkypool.com uses pop-ups to open a custom game window,\n" +
				"display player information and other helpful functionality.\n" +
				"We do NOT display advertising pop-ups.");
		}
	} catch (e) {
		alert("An error ocurred when attempting to open the game window.\n" +
				"Details: " + e);
	}

}

function checkWebPage() {
	var requiredWebPage = "www.funkypool.com";

	// 1. check that the page is not framed, if framed unframe page
	try {
		if (top != self) {
			top.location.href = self.location.href;
		}
	} catch (e) {}

	// 2. check domain name is correct
	try {
		var host = top.location.hostname;
		if (host != "localhost" && host != "127.0.0.1" && host != requiredWebPage) {
			top.location.href = "http://" + requiredWebPage + location.pathname;
		}
	} catch (e) {}
}

function toggle(divID) {
	try {
		var div = document.getElementById(divID);
		if (div.style.display == "none") {
			div.style.display = "";
		} else {
			div.style.display = "none";
		}
	} catch (e) {}

	return false;
}

var MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

function formatDate(dateString, fs) {

	var date = new Date(dateString);
	if (isNaN(date)) {
		document.write(dateString);
		return;
	}

	var year = date.getYear();
	var yearShort = year.toString().substring(2,4);
	if (yearShort.length < 2) yearShort = "0" + yearShort;
	var month = date.getMonth() + 1;
  	var dayOfMonth = date.getDate();
	var hours = date.getHours();
	var minutes = date.getMinutes();
	var day = date.getDay();

	fs = fs.replace(/yyyy/, year);
	fs = fs.replace(/yy/, yearShort);
	fs = fs.replace(/MMM/, MONTHS[month - 1]);
	fs = fs.replace(/MM/, month < 10 ? '0' + month : month);
	fs = fs.replace(/dd/, dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth);
	fs = fs.replace(/d/, dayOfMonth);
	fs = fs.replace(/HH/, hours < 10 ? '0' + hours : hours);
	fs = fs.replace(/mm/, minutes < 10 ? '0' + minutes : minutes);
	fs = fs.replace(/EE/, DAYS[day]);
	fs = fs.replace(/E/, DAYS[day].substring(0,3));
	fs = fs.replace(/\(z\)/, '');
	fs = fs.replace(/z/, '');

	document.write(fs);
}
function changeStyleClass(tableRow, style) {
	tableRow.className = style;
}
function navTo(theUrl) {
	document.location.href = theUrl;
}