function rememberPatternChanged() {
	if (document.getElementById("rememberPattern").checked) {
		setCookies = true;
		writeCookie(stateName, state, 60);
	}
	else {
		setCookies = false;
		deleteCookie(stateName);
	}
}

function rememberPatternChangedMP() {
	if (document.getElementById("rememberPattern").checked) {
		setCookies = true;
		writeCookie(stateName, state, 60);
		writeCookie('braid', stateName, 60);
	}
	else {
		setCookies = false;
		deleteCookie(stateName);
		deleteCookie('braid');
	}
}

function deleteCookie(key) {
	var expiry = new Date();
	expiry.setDate(expiry.getDate() - 1);
	document.cookie = key + "=;expires=" + expiry.toUTCString();
}

function writeCookie(key, value, days) {
	var expiry = new Date();
	expiry.setDate(expiry.getDate() + days);
	document.cookie = key + "=" + value + ";expires=" + expiry.toUTCString();
}

function stateChangeHandler(newState) {
	state=newState;
	if (setCookies) { writeCookie(stateName, state, 60);}
	// want to find sendPattern id, and update all links inside this id
	
	var submitPhoto = document.getElementById("submitPhoto");
	var links = submitPhoto.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) {
		links[i].href = "/submitphoto.php?" + stateName + "=" + state;
	}

	var getLink = document.getElementById("getLink");
	var links = getLink.getElementsByTagName("a");
	//var urlParts = 
	for (var i = 0; i < links.length; i++) {
		links[i].href = document.URL.split('?')[0] + "?a=" + stateName + "&s=" + state;
	}

}

function heightChangeHandler(height) {
	document.getElementById("applet").height = height;
}

function patternChangeHandler(patternID) {
	if (setCookies) { writeCookie("mp_patt", patternID, 60);}
	
	// Hide all pattern instructions, and display current one
	var patterns = document.getElementById("mp_pattDescr").getElementsByTagName("div"); // check - may need to split into more vars
	for (var i = 0; i < patterns.length; i++) {
		patterns[i].style.display="none";
	}
	document.getElementById("mp_"+patternID).style.display="block";
}

function setPatternLinks(locn) {
	if (hiddenCookie) $("#hiddenCookie").append("<p>Were you expecting your own saved design? <a href='/" + locn + "/?a='" + stateName + "'>Find it here.</a></p>");
	$("#submitPhoto").append("<a href='/submitphoto.php?" + stateName + "=" + state + "'><img src='/i/photo.png' width='20' height='20' /></a><a href='/submitphoto.php?" + stateName + "=" + state + "'>Send us your design</a> with a photo of your finished braid.");
	$("#getLink").append("<a href='/'" + locn + "/?a=" + stateName + "&amp;s=" + state + "'><img src='/i/link.png' width='20' height='20' /></a><a href='/" + locn + "/?a=" + stateName + "&amp;s=" + state + "'>Link to your design</a> - share your pattern with your friends");
	$("#allowCookies").append("<input type='checkbox' onClick='rememberPatternChanged();' id='rememberPattern'" + cookieString + " />Remember pattern (requires cookies)");
}

function loadLHGallery() {
	var height = $('#content').innerHeight();
	var lhcolumn = $('#lhcolumn');
	var headerSet = false;
	while (height > 0 && photoArray.length > 0) {
		var index = Math.floor(Math.random()*photoArray.length);
		var nxtPhoto = photoArray.splice(index,1);
		if (nxtPhoto[0].thWidth > nxtPhoto[0].thHeight) {
			if (nxtPhoto[0].thWidth > 150) {
				nxtPhoto[0].thHeight = 	nxtPhoto[0].thHeight * (150 / nxtPhoto[0].thWidth);
				nxtPhoto[0].thWidth = 150 
			}
		} else {
			if (nxtPhoto[0].thHeight > 150) {
				nxtPhoto[0].thWidth = 	nxtPhoto[0].thWidth * (150 / nxtPhoto[0].thHeight);
				nxtPhoto[0].thHeight = 150 
			}
		}
		if (nxtPhoto[0].thHeight + lhcolumn.innerHeight() > height) {
			continue;
		}
		if (!headerSet) {
			$('#lhcolumn h3').append('From Our Gallery');
			headerSet = true;
		}
		var imgTag = $('<img src="/gallery/thumb/' + nxtPhoto[0].file + '" width="' + nxtPhoto[0].thWidth + '" height="' + nxtPhoto[0].thHeight + '" />');
		var patternText = "";
		
		//imgTag.appendTo(lhcolumn).slimbox('/gallery/large/' + nxtPhoto[0].file);
		$('<a href="/gallery/large/' + nxtPhoto[0].file + '" rel="lightbox" title="' + nxtPhoto[0].descr + '"></a>').append(imgTag).add('').appendTo(lhcolumn).slimbox();
	}
}
