//-
//- Copyright (c) 2008-2009 Lionel P. Castro
//- GWUPS!
//- 2008.11.22

GWUPS = {
	sGWUPSDomain : "www.gwups.com",

	iWinWdt  : 640,
	iWinHgt  : 480,
	iWinScrollX : 0,
	iWinScrollY : 0,
	iMouseX  : 0,
	iMouseY  : 0,

	aLoadFunc : new Array(),
	aResizeFunc : new Array(),
	aMouseMoveFunc : new Array(),

	aImgSrc  : new Array(),
	aImg     : new Array(),

	dDate    : new Date(),

	//aColorHi : new Array("#fff", "#d00", "#d90", "#dd0", "#9d0", "#0d0", "#0d9", "#0dd", "#09d", "#00d", "#90d", "#d0d", "#d09", "#000", "#999"),
	//aColorHi : new Array("#fff", "#fbb", "#fdb", "#ffb", "#dfb", "#bfb", "#bfd", "#bff", "#bdf", "#bbf", "#dbf", "#fbf", "#fbd", "#888", "#ddd"),
	//aColorHi : new Array("#fff", "#fdd", "#fed", "#ffd", "#efd", "#dfd", "#dfe", "#dff", "#def", "#ddf", "#edf", "#fdf", "#fde", "#ccc", "#eee"),
	aColorHi : new Array("#fff", "#e55", "#b85", "#bb5", "#8b5", "#5b5", "#5b8", "#5bb", "#58b", "#55b", "#85b", "#b5b", "#b58", "#333", "#888"),
	//aColor   : new Array("#fff", "#900", "#960", "#990", "#690", "#090", "#096", "#099", "#069", "#009", "#609", "#909", "#906", "#000", "#666"),
	aColor   : new Array("#fff", "#c33", "#963", "#993", "#693", "#393", "#396", "#399", "#369", "#339", "#639", "#939", "#936", "#000", "#666"),
	iColor   : 13,
	iChangeColor : 13,

	sBodyId  : "idBody",  oBody  : null,
	sHeadId  : "idHead",  oHead  : null,
	sLogoId  : "idLogo",  oLogo  : null,
	sDetlId  : "idDetl",  oDetl  : null,
	sInfoId  : "idInfo",  oInfo  : null, iInfoXOff : 10, iInfoYOff : 10,

	aCardNameInfo : new Array(
		new Array( "b", "Back"),
		new Array( "hem", "Straight-Male"),
		new Array( "hef", "Straight-Female"),
		new Array( "hom", "Gay-Male"),
		new Array( "hof", "Gay-Female"),
		new Array( "bim", "Bi-Male"),
		new Array( "bif", "Bi-Female"),
		new Array( "cem", "Celibate-Female"),
		new Array( "cef", "Celibate-Female"),
		new Array( "wic",  "Wild-Card" ),
		new Array( "oc", "Orientation-Change" ),
		new Array( "sc", "Sex-Change" ),
		new Array( "sm", "Soul-Mate" ),
		new Array( "d", "Death" ),
		new Array( "t", "Time" )
	),

	aInfo : new Array(
		"Card representing a person",	//- 0 Play Card
		"Card representing a life chance",	//- 1 Chance Card
		"Location where unused cards are placed",	//- 2 Draw Pile
		"Location where used cards are kept",	//- 3 Discard/Chance Pile
		"Player's pile where collected Couples are kept",	//- 4 Couples Pile
		"Player's pile where collected Exes are kept",	//- 5 Exes Pile
		"Collected matching cards",	//- 6 Couples
		"Collected matched cards",	//- 7 Exes
		"First Player who collected the most Couples",	//- 8 The Match Maker
		"First Player who collected the most Exes",	//- 9 The Player
		"Disclosing player's card to picked card's source or <br>to one other player if card was drawn",	//- 10 Private Disclosure
		"Disclosing player's card to more than one player",	//- 11 Public Disclosure
		"Used a Death card in a match",	//- 12 Suicide
		"Used or picked two Death cards in a match",	//- 13 Tragedy
		"Used or picked two Soul-Mate cards in a match",	//- 14 Fairy Tale
		"A Tragedy and a Fairy Tale",	//- 15 Romeo & Juliet
		"Play card taken from Chance Pile", //- 16 Chance PC
		""	//- xyz
	),

	xyz : null
}

GWUPS.isNav = function() { return (navigator.appName.indexOf("etscape") != -1); }
GWUPS.isIE = function() { return (navigator.appName.indexOf("icrosoft") != -1); }
GWUPS.isOpera = function() { return (navigator.appName.indexOf("pera") != -1); }
GWUPS.isKonq = function() { return (navigator.appName.indexOf("onqueror") != -1); }
GWUPS.getBrowVer = function() { return parseInt(navigator.appVersion); }
GWUPS.makeHTTP = function(sFile) { if (document.location.protocol == "https:") { GWUPS.loadPage("http://"  + this.sGWUPSDomain + "/" + sFile); } }
GWUPS.makeHTTPS = function(sFile) { if (document.location.protocol == "http:") { GWUPS.loadPage("https://" + this.sGWUPSDomain + "/" + sFile); } }
GWUPS.getWinDim = function () {
	var oD = document.documentElement; //?
	var oB = document.body;
	if (oB && (oB.clientWidth || oB.clientHeight)) { this.iWinWdt = oB.clientWidth; this.iWinHgt = oB.clientHeight; } //- Mac&MSFirefox, MacSafari, MacOpera, MSIE.
	else if (window.innerWidth || window.innerHeight) { this.iWinWdt = window.innerWidth; this.iWinHgt = window.innerHeight; }
	else if (oD && (oD.clientWidth || oD.clientHeight)) { this.iWinWdt = oD.clientWidth; this.iWinHgt = oD.clientHeight; }
}
GWUPS.getMouseCoords = function (evt) {
	this.iMouseX = 0;
	this.iMouseY = 0;
	if (this.isIE()) {
		var oD = document.documentElement;
		var oB = document.body;
		if (oD && (oD.scrollLeft || oD.scrollTop)) { this.iWinScrollX = oD.scrollLeft; this.iWinScrollY = oD.scrollTop; }
		else if (oB && (oB.scrollLeft || oB.scrollTop)) { this.iWinScrollX = oB.scrollLeft; this.iWinScrollY = oB.scrollTop; }
		else if ((typeof window.pageXOffset == "number") ||
		         (typeof window.pageYOffset == "number")) { this.iWinScrollX = window.pageXOffset; this.iWinScrollY = window.pageYOffset; }
		else if (window.scrollX || window.scrollY) { this.iWinScrollX = window.scrollX; this.iWinScrollY = window.scrollY; }
		this.iMouseX = window.event.clientX + this.iWinScrollX;
		this.iMouseY = window.event.clientY + this.iWinScrollY;
	} else if (this.isKonq()) {
		this.iMouseX = evt.clientX; this.iMouseY = evt.clientY; //- Konqueror.
	} else {
		this.iMouseX = evt.pageX; this.iMouseY = evt.pageY; //- MacFirefox, MacSafari, MacOpera.
	}
	if (this.oInfo && this.oInfo.style.display == "block") {
		this.oInfo.style.left = (this.iMouseX + this.iInfoXOff) + "px";
		this.oInfo.style.top  = (this.iMouseY + this.iInfoYOff) + "px";
	}
}
GWUPS.getElementX = function (oElement) {
	var iX = 0;
	if (oElement.offsetParent) {
		while (true) {
			iX += oElement.offsetLeft;
			if (!oElement.offsetParent) { break; }
			oElement = oElement.offsetParent;
		}
	} else if (oElement.x) { iX = oElement.x; }
	return iX;
}

GWUPS.getElementY = function (oElement) {
	var iY = 0;
	if (oElement.offsetParent) {
		while (true) {
			iY += oElement.offsetTop;
			if (!oElement.offsetParent) { break; }
			oElement = oElement.offsetParent;
		}
	} else if (oElement.y) { iY = oElement.y; }
	return iY;
}

GWUPS.addLoadFunc = function (sFuncCall) {
	this.aLoadFunc.push(sFuncCall);
}
GWUPS.addResizeFunc = function (sFuncCall) {
	this.aResizeFunc.push(sFuncCall);
}
GWUPS.addMouseMoveFunc = function (sFuncCall) {
	this.aMouseMoveFunc.push(sFuncCall);
}

GWUPS.loadImages = function () {
	var oImage;
	this.addCornerImgs();
	for (var i = 0; this.aImgSrc && (i < this.aImgSrc.length); i++) {
		oImage = new Image;
		oImage.src = this.aImgSrc[i];
		this.aImg.push(oImage);
	}
}
GWUPS.addImage = function (sImgSrc) {
	this.aImgSrc.push(sImgSrc);
}
GWUPS.addImages = function (aImgSrc) {
	for (var i = 0; aImgSrc && (i < aImgSrc.length); i++) {
		this.addImage(aImgSrc[i]);
	}
}
GWUPS.loadPage = function (sHTML) {
	document.location = sHTML;
}

GWUPS.createIds = function () {
	var aCreateId = new Array(this.sInfoId);
	var oDiv;
	for (var i = 0; i < aCreateId.length; i++) {
		if (! document.getElementById(aCreateId[i])) {
			oDiv = document.createElementNS ? document.createElementNS("http://www.w3.org/1999/xhtml", "div") : document.createElement("div");
			oDiv.setAttribute("id", aCreateId[i]);
			document.body.appendChild(oDiv);
		}
	}
	this.oInfo  = document.getElementById(this.sInfoId);

	this.oBody  = document.getElementById(this.sBodyId);  //- Exception.
	this.oHead  = document.getElementById(this.sHeadId);  //- Exception.
	this.oLogo  = document.getElementById(this.sLogoId);  //- Exception.
	this.oDetl  = document.getElementById(this.sDetlId);  //- Exception.
}
GWUPS.attachFuncs = function () {
	var aElement, oElement, sId;

	if (this.oHead) {
		this.oHead.onmouseover = function () { GWUPS.cycleColors(true); }
	}
	if (this.oLogo) {
		this.oLogo.onclick     = function () { GWUPS.onCycleColors(); }
	}

	if (aElement = document.body.getElementsByTagName("*")) {
		for (i = 0; i < aElement.length; i++) {
			oElement = aElement[i];
			sId      = oElement.getAttribute("id");
			//if (sId && sId.substring(0,2) == "pg") {
			//	oElement.onclick     = function () { GWUPS.loadPage(this.getAttribute("id").substring(2) + ".html"); }
			//} else
			if (oElement.getAttribute("info")) {
				oElement.onmouseover = function () { GWUPS.showInfo(this.getAttribute('info')); }
				oElement.onmouseout  = function () { GWUPS.hideInfo(); }
			}
		}
	}
}
GWUPS.showInfo = function (sHTML) {
	if (this.oInfo) {
		this.oInfo.innerHTML     = (sHTML.substring(0,1) == "=") ? eval(sHTML.substring(1)) : sHTML;
		this.oInfo.style.display = "block";
		this.oInfo.style.visibility = "visible";
		this.oInfo.style.left    = (this.iMouseX + 10) + "px";
		this.oInfo.style.top     = (this.iMouseY + 10) + "px";
	}
}
GWUPS.hideInfo = function () {
	if (this.oInfo) {
		this.oInfo.innerHTML     = "";
		this.oInfo.style.display = "none";
		this.oInfo.style.visibility = "hidden";
	}
}
GWUPS.showElement = function (sId) { document.getElementById(sId).style.display = "block"; }
GWUPS.hideElement = function (sId) { document.getElementById(sId).style.display = "none"; }

GWUPS.load = function () {
	this.getWinDim();
	this.createIds();
	this.attachFuncs();
	this.getCookies();
	this.cycleColors(true);
	for (var i = 0; i < this.aLoadFunc.length; i++) { eval(this.aLoadFunc[i]); }
	this.loadImages();
}
GWUPS.resize = function () {
	this.getWinDim();
	for (var i = 0; i < this.aResizeFunc.length; i++) { eval(this.aResizeFunc[i]); }
}

GWUPS.getAnInt = function (nRange) {
	var m, n;
	n = (Math.random() * this.dDate.getSeconds()) % nRange;
	m = n.toString(); n = parseInt(m);
	return n;
}
GWUPS.cycleColors = function (bInitial) {
	if (bInitial) {
	} else if (this.iChangeColor == 0) {
		return;
	} else {
		this.iColor++;
		if (this.iColor == this.aColor.length) { this.iColor = 1; }
		this.setCookies();
	}
	this.updateColors();
}
GWUPS.onCycleColors = function () {
	if (this.iChangeColor == 0) {
		this.iChangeColor = 1;
		this.setCookies();
/* FireFox only
		if (sessionStorage) {
			sessionStorage.setItem("changecolor", this.iChangeColor);
		}
*/
	}
	this.cycleColors(false);
}
GWUPS.updateColors = function() {
	var aElement, oElement;
	if (this.oBody) {
		this.oBody.style.backgroundColor = this.aColor[this.iColor];
	}
	if (this.oDetl) {
		this.oDetl.style.backgroundColor = this.aColorHi[this.iColor];
	}

	if (aElement = document.body.getElementsByTagName("*")) {
		for (var i = 0; i < aElement.length; i++) {
			oElement = aElement[i];
			sId      = oElement.getAttribute("id");
			if (sId && sId.substring(0,2) == "fg") {
				oElement.style.color = this.aColor[this.iColor];
			} else if (sId && sId.substring(0,2) == "bg") {
				oElement.style.backgroundColor = this.aColor[this.iColor];
			} else if (sId && sId.substring(0,2) == "bd") {
				oElement.style.borderColor = this.aColor[this.iColor];
			} else if (sId && (sId.substring(0,2) == "fb" || sId.substring(0,4) == "ctrl")) {
				oElement.style.color = this.aColor[this.iColor];
				oElement.style.borderColor = this.aColor[this.iColor];
			}
		}
	}
	if (this.oInfo) {
		this.oInfo.style.color = this.aColor[this.iColor];
		this.oInfo.style.borderColor = this.aColor[this.iColor];
	}
}
GWUPS.changeColors = function (iNewColor) {
	if (this.iChangeColor == 1) {
		this.iChangeColor = 0;
		this.setCookies();
/* FireFox only
		if (sessionStorage) {
			sessionStorage.setItem("changecolor", this.iChangeColor);
		}
*/
	}
	if (this.iColor != iNewColor) {
		this.iColor = iNewColor;
		if (this.iColor == this.aColor.length) { this.iColor = 1; }
		this.setCookies();
		this.updateColors();
	}
}

GWUPS.shuffleCards = function (aCard, iLft, iRgt, iWdt, iHgt, bCreate, bSnglSd) {
	var x, y, iCardNum, oElement;
	for (var i = 0; i < aCard.length; i++) {
		x = iLft + Math.random() * iWdt;
		y = iRgt + Math.random() * iHgt;
		if (bCreate) {
			iCardNum = aCard[i];
			document.writeln("<img id='idCard"          + (i+1) + (bSnglSd ? "" : "ds") + "' src='GRAPIK/fwai" + this.aCardNameInfo[iCardNum][0] + (bSnglSd ? "" : "ds") + "flat.png' style='position:absolute; left:" + x + "px; top:" + y + "px;' info='" + this.aCardNameInfo[iCardNum][1] + "'/>");
		} else {
			oElement = document.getElementById("idCard" + (i+1) + (bSnglSd ? "" : "ds"));
			oElement.style.left = x + "px";
			oElement.style.top  = y + "px";
		}
	}
}
GWUPS.getCardImgSrc = function (iCard, bSnglSd) {
	return "GRAPIK/fwai" + this.aCardNameInfo[iCard][0] + (bSnglSd ? "" : "ds") + "flat.png";
}
GWUPS.getCardHImgSrc = function (iCard, bSnglSd) {
	return "GRAPIK/fwai" + this.aCardNameInfo[iCard][0] + (bSnglSd ? "" : "ds") + "hflat.png";
}
GWUPS.getCardImg = function (iCard, bSnglSd) {
	return "<img class='imgspc' src='GRAPIK/fwai" + this.aCardNameInfo[iCard][0] + (bSnglSd ? "" : "ds") + "flat.png'/>";
}
GWUPS.getOtherImg = function (sImgName) {
	return "<img class='imgspc' src='GRAPIK/" + sImgName + "'/>";
}
GWUPS.getInfo = function (iInfo) {
	return this.aInfo[iInfo];
}

GWUPS.addCornerImgs = function () {
	var i;
	var aCornerSuffix = new Array("ul", "ur", "ll", "lr");
	var aSideSuffix   = new Array("l", "t", "r", "b");
	for (i = 0; i < aCornerSuffix.length; i++) {
		GWUPS.addImage("GRAPIK/fwFFFCornerOut" + aCornerSuffix[i] + ".gif");
		GWUPS.addImage("GRAPIK/fwFFFCornerIn"  + aCornerSuffix[i] + ".gif");
		//- GWUPS.addImage("GRAPIK/fwTrns???"  + aCornerSuffix[i] + ".gif");
	}
	//- for (i = 0; i < aSideSuffix.length; i++) {
	//- 	//- GWUPS.addImage("GRAPIK/fwTrns???"  + aSideSuffix[i] + ".gif");
	//- }
}
GWUPS.addCardImgs = function (bSnglSd) {
	for (var i = 0; i < this.aCardNameInfo.length; i++) { GWUPS.addImage("GRAPIK/fwai" + this.aCardNameInfo[i][0] + (bSnglSd ? "" : "ds") + "flat.png"); }
}
GWUPS.addCardHImgs = function (bSnglSd) {
	for (var i = 0; i < this.aCardNameInfo.length; i++) { GWUPS.addImage("GRAPIK/fwai" + this.aCardNameInfo[i][0] + (bSnglSd ? "" : "ds") + "hflat.png"); }
}

GWUPS.setCookies = function() {
	document.cookie = "color=" + escape(this.iColor) + "&changecolor=" + escape(this.iChangeColor) + "&";
}
GWUPS.getCookies = function() {
	if (document.cookie) {
		this.iColor       = this.getCookie("color");
		if (this.iColor == null || this.iColor == "") { this.iColor = 14; }
		this.iChangeColor = this.getCookie("changecolor");
		if (this.iChangeColor == null || this.iChangeColor == "") { this.iChangeColor = 14; }
	} else {
		this.setCookies();
	}
}
GWUPS.getCookie = function(sName) {
	var sValue = "", iBeg, iEnd;
	if (document.cookie.length > 0) {
		iBeg = document.cookie.indexOf(sName + "=");
		if (iBeg != -1) {
			iBeg = iBeg + sName.length + 1;
			iEnd = document.cookie.indexOf("&", iBeg);
			if (iEnd == -1) iEnd = document.cookie.length;
			sValue = unescape(document.cookie.substring(iBeg, iEnd));
		}
	}
	return sValue;
}

GWUPS.printContents = function(sId, sW) {
	var sContent1 = document.getElementById(sId).innerHTML;
	var sContent2;
	var s = "<span class='winChoice' style='float:right; color:" + this.aColor[this.iColor] + "; border-color:" + this.aColor[this.iColor] + "; margin-top:5pt;'";
	var w = window.open('','GWUPS_Print_Contents','toolbar=0,status=0,scrollbars=1');
	var wd = w.document;
	var t = '<html><head><title>GWUPS ' + sId + '</title>';
	wd.write(t);

	sContent2 = sContent1.replace(/rgb\(238, 238, 238\)|#eee/g, "#fff");
	sContent1 = sContent2.replace(/width="240"/g, 'width="120"');
	sContent2 = sContent1.replace(/height="180"/g, 'height="90"');
	sContent1 = sContent2.replace(/width:\s*240px;\s*height:\s*180px/g, "width:120px; height:90px");

	wd.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>');
	wd.write('<link rel="stylesheet" href="GAMIT/GWUPS.css" type="text/css"/>');
	if (!this.isIE()) {
		wd.write('<script src="GAMIT/GWUPS.js" type="text/javascript"></script>');
	}
	t = "</head><body style='background-color:#fff; width:" + sW + "; ' onload='self.focus();'>";
	wd.write(t);

	wd.write('<img src="GRAPIK/gwups.jpg" width="150" height="45" info="Beautiful Person!" alt="Beautiful Person!"/>');
	t = s + " onclick='javascript:window.close();'>&nbsp;close&nbsp;</span>";
	wd.write(t);
	t = s + " onclick='javascript:window.print();'>&nbsp;print&nbsp;</span>";
	wd.write(t);
	wd.write("<br style='clear:both;'/>");
	wd.write(sContent1);
	t = "<div style='color:" + this.aColor[this.iColor] + "; font-size:10pt; text-align:center;'>" + document.getElementById('idCopyright').innerHTML + "</div>";
	wd.write(t);
	wd.write("</body></html>");
	wd.close();
	if (!this.isIE()) {
		w.focus();
	}
}

GWUPS.toggleDisplay = function (sId) {
	var o = document.getElementById(sId);
	if (o.style.display == "none") {
		o.style.display = "block";
	} else {
		o.style.display = "none";
	}
}

GWUPS.toggleMstrDtl = function(sTId, sDId, sMinStr, sMaxStr) {
	var oT = document.getElementById(sTId);
	var oD = document.getElementById(sDId);
	if (!sMinStr) { sMinStr = "-"; }
	if (!sMaxStr) { sMaxStr = "+"; }
	if (oD.style.display == "none") {
		oT.innerHTML = sMinStr;
		oD.style.display = "block";
	} else {
		oT.innerHTML = sMaxStr;
		oD.style.display = "none";
	}
}

GWUPS.googleTrackA = function () {
	var gaJsHost = (("https:" == document.location.protocol) ? "https://sl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
}
GWUPS.googleTrackB = function (sId) {
	var pageTracker = _gat._getTracker(sId);
	pageTracker._trackPageview();
}

window.onload = function () { GWUPS.load(); }
window.onresize = function () { GWUPS.resize(); }

document.onmousemove = function (evnt) {
	GWUPS.getMouseCoords(evnt);
	for (var i = 0; this.aMouseMoveFunc && i < this.aMouseMoveFunc.length; i++) { eval(this.aMouseMoveFunc[i]); }
}


