/* Cookie Specific Area */
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

/* Grid Specific Area */
var selectedColor = 'black';
var imgBasePath = "colors/";
var pageName;

// Rotate the grid square through blank, X, o
function shift(myObject) {
        firstHalf = myObject.src.substring(0, myObject.src.length - 4);
        imageId = firstHalf.charAt(firstHalf.length - 1);
        firstHalf = firstHalf.substring(0, firstHalf.length - 2);
        imageId = (imageId + 1) % 3;
        myObject.src = imgBasePath + selectedColor + imageId + ".png";
}

function changeColor(myColor) {
	selectedColor = myColor;
}

// Store the grid in a cookie
function storeData() {
	var dataString = getCookie("puzzle");
	if (dataString != null) {
		var data = dataString.split("_");
		if (data[0] != pageName) {
			//if(!confirm("A different puzzle (" + data[0] + ") has been saved.\nReplace?")) return;
			// NOTE: If we get here, we are saving over a different puzzle.
		}
	}

	dataString = new String(pageName + "_");
	var myObject;
	for (var i = entries.length - 1; i > 0; i--) {
		for (var j = 0; j < entries[i].length; j++) {
			for (var k = 0; k < i; k++) {
				for (var m = 0; m < entries[k].length; m++) {
					elementId = "b"+i+j+k+m;
					myObject = document.getElementById(elementId);

					if (myObject.src == null) {
						alert("Null error at " + elementId);
						return false;
					}

					index = myObject.src.lastIndexOf(imgBasePath);
					imageName = myObject.src.substring(index + imgBasePath.length, myObject.src.length - 4);
					dataString += imageName + "_";
				}
			}
		}
	}
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	setCookie("puzzle", dataString, now);
}

function alertSolution() {
	var dataString = new String();
	var i = entries.length - 1;
	for (var j = 0; j < entries[i].length; j++) {
		dataString += entries[i][j] + ", ";
		for (var k = 0; k < i; k++) {
			for (var m = 0; m < entries[k].length; m++) {
				elementId = "b"+i+j+k+m;
				myObject = document.getElementById(elementId);

				if (myObject == null) {
					alert("Null error at " + elementId);
					return false;
				}
				index = myObject.src.lastIndexOf(imgBasePath);
				imageName = myObject.src.substring(index + imgBasePath.length, myObject.src.length - 4);
        			imageId = imageName.charAt(imageName.length - 1);
				if (imageId == '2') {	
					//dataString += elementId + ",";
					dataString += entries[k][m] + ", ";
				}
			}
		}
		dataString += "<br/>\r\n";
	}
 top.consoleRef=window.open('','myconsole',
  'width=450,height=250'
   +',menubar=0'
   +',toolbar=0'
   +',status=0'
   +',scrollbars=1'
   +',resizable=1')
 top.consoleRef.document.writeln(
  '<html><head><title>My Solution</title></head>'
   +'<body bgcolor=white onLoad="self.focus()">'
   +dataString
   +'</body></html>'
 )
 top.consoleRef.document.close()
}

function retrieveData() {
	var dataString = getCookie("puzzle");
	if (dataString == null) {
		alert("Cookie not found.");
		return;
	}
	var data = dataString.split("_");
	if (data[0] != pageName) {
		alert("Saved data is not for this puzzle.\nSaved puzzle: " + data[0]);
		return;
	}
	var count = 1;
	for (var i = entries.length - 1; i > 0; i--) {
		for (var j = 0; j < entries[i].length; j++) {
			for (var k = 0; k < i; k++) {
				for (var m = 0; m < entries[k].length; m++) {
					elementId = "b"+i+j+k+m;
					myObject = document.getElementById(elementId);
					if (myObject != null && data[count] != null && data[count] != "") {
						myObject.src = imgBasePath + data[count] + ".png";
					}
					else
						return;
					count++;
				}
			}
		}
	}
}

var clueClasses = new Array("clue", "clueUnderlined", "clueCrossed");
function toggleClue(obj) {
	if (obj.style.color != selectedColor && !(obj.style.color == "" && selectedColor == "black")) {
		obj.style.color = selectedColor;
		return;
	}
	for(i = 0; i < clueClasses.length; i++) {
		if(obj.className == clueClasses[i]) {
			obj.className = clueClasses[(i+1) % clueClasses.length];
			break;
		}
	}
	obj.style.color = selectedColor;
}
