//////// PHOTO PAGE FUNCTIONS - COOKIES / DIRECTION ////////

function goto(arry,nm) { // launched from content pages
	document.cookie = escape("thearray=" + arry +"; ");
	document.cookie += escape("thenum=" + nm);
}

function store() {
var anarray = array;
var anum = num;
goto(anarray,anum);
}

function showphoto() {
array = readcookie("thearray");
var doof = array.charAt(0);
if ( ( doof == "0" ) || ( doof == "1" ) ) { array = "x" + array; }
num = readcookie("thenum");
display(); 
}

function readcookie(varia) { // if there is no ? in URL (i.e. after cookie storage and refresh)
//if ( document.cookie.length > 0 ); 
	if ( document.cookie ) {
		var coookie = unescape(document.cookie);
		var ca = coookie.split(';'); // creates an array
		var nameEQ = varia + "=";
		for(var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
			if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); }
		} // end for
	} // end if
	else { 
		if ( varia == "thearray" ) { return "crops"; }
		else if ( varia == "thenum" ) { return 1; }
	}
}

//////// PHOTO PAGE FUNCTIONS - NAVIGATION / DISPLAY ////////

function nextphoto() {
	num++;
	arrayname = eval(array); 
	if ( num == arrayname.length ) { 
		ArrayNum = getArrayNum(array); 
		ArrayNum++;
		if ( ArrayNum == arrays.length ) { ArrayNum = 0; } 
		if ( ArrayNum == 0 ) { ArrayNum = 1 } //skip community array
		if ( ArrayNum == 5 ) { ArrayNum = 6 } //skip contact array
		array = arrays[ArrayNum];
		num = 1;
	}
	display();
}

function lastphoto() { // awaiting programming
	num--;
	if ( num == 0 ) { 
		ArrayNum = getArrayNum(array); 
		ArrayNum--;
		if ( ArrayNum == 5 ) { ArrayNum = 4 } //skip contact array
		if ( ArrayNum == 0 ) { ArrayNum = -1 } //skip community array
		if ( ArrayNum == -1 ) { ArrayNum = arrays.length -1 } 
		array = arrays[ArrayNum];
		arrayname = eval(array); 
		num = arrayname.length -1; 
	}
	display();
}


function readabout() { 
	var onechar = array.charAt(0); 
	if ( onechar == "x" ) {
		var arrr = array.substring(1,array.length);
		window.location.href = "http://www.willowcreekheirlooms.com/updates.html#" + arrr; 
	}
	else {
		window.location.href = "http://www.willowcreekheirlooms.com/" + array + ".html";
	}
}

//write perl program to add to photoadd.js (w/array name and length)

function getArrayNum(arrayy) { 
	for ( var i=0; i<arrays.length; i++ ) {
		if ( arrays[i] == arrayy ) {
		var arraynm = i;
		return arraynm;
		}
	}
}

function display() {
	doof = array + "[" + num + "]";
	var gnarf = eval(doof); // IE cannot eval to a global variable... for some reason
	var photo = "/photos/" + gnarf;
	document.photopage.src = photo; // brackets are best practice
	var thedate = finddate();
	document.getElementById('photodate').innerHTML = thedate;
	var onechar = array.charAt(0); 
	if ( onechar == "x" ) { var arrrr = array.substring(1,7); }
	else { var arrrr = array; }
	var precapt = "c_" + arrrr + "_" + num;
	caption = findcapt(precapt);
	document.getElementById('photocaption').innerHTML = caption;
}

function finddate() {
	var onechar = array.charAt(0); 
	if ( onechar == "x" ) {
		var theyear = array.substring(1,3);
		var themonth = array.substring(3,5);
		if ( themonth == "01" ) { themonth = "Jan."; }
		else if ( themonth == "02" ) { themonth = "Feb."; }
		else if ( themonth == "03" ) { themonth = "Mar."; }
		else if ( themonth == "04" ) { themonth = "Apr."; }
		else if ( themonth == "05" ) { themonth = "May"; }
		else if ( themonth == "06" ) { themonth = "June"; }
		else if ( themonth == "07" ) { themonth = "July"; }
		else if ( themonth == "08" ) { themonth = "Aug."; }
		else if ( themonth == "09" ) { themonth = "Sept."; }
		else if ( themonth == "10" ) { themonth = "Oct."; }
		else if ( themonth == "11" ) { themonth = "Nov."; }
		else if ( themonth == "12" ) { themonth = "Dec."; }
		var theday = array.substring(5,7);
		onechar = theday.charAt(0);
		if ( onechar == "0" ) { theday = theday.substring(1,2); }
		var thedate	= "added <a href='javascript:readabout();'>" + themonth + " " + theday + " '" + theyear + "</a>";
		}
	else { var thedate = "from <a href='javascript:readabout();'>" + array + ".html</a>"; }
	return thedate;
}

function help() {
	document.getElementById('photodate').innerHTML = "Photo Help Page";
	document.photopage.src = "images/spacer.gif";
	document.getElementById('photocaption').innerHTML = "<br><br>Welcome to Willow Creek's online photo gallery.<br>Windows users can hit F11 for a fullscreen view.<br>You'll need to <a href=\"http://www.google.com/cookies.html\">enable cookies</a> to return to the same photo later.<br>To view a set of thumbnails, return to a picture's page by clicking on it.<br><br><a href=\"javascript:display()\">Exit help</a><br><br><br><br><br><img src=\"images/line.gif\">";
}


/*
function display(image1,text1,hv1) {
image2 = "photos/"+image1+".jpg";
text2 = text1;
hv2 = hv1; 
whandle = window.open( "http://www.willowcreekheirlooms.com/photos.html", "imagewin", "status = 0, scrollbars=yes" );
whandle.focus();
}
*/
/*
function change() {
var image3 = window.opener.image2;
var text3 = window.opener.text2;
var hv3 = window.opener.hv2;
document.mainimage.src = image3;
document.getElementById('imagetext').innerHTML = text3;
if ( hv3 == "h" ) { 
window.outerWidth="675";
window.outerHeight="560";
}
if ( hv3 == "v" ) { 
window.outerWidth="520"; 
window.outerHeight="610";
}

}

*/

//////// OTHER PAGE FUNCTIONS - NAVIGATION / DISPLAY ////////

function basetext() {
document.getElementById('sect').innerHTML = '...click on a picture to enter...';
}

function preload() {
if (document.images)
{ 
prel = new Array(10);
for(i = 0; i < 3; i++) 
{ prel[i] = new Image(); }
prel[0].src = "images/logotop.gif";
prel[1].src = "images/greenframe.gif";
prel[2].src = "images/tallgreenframe.gif";
}
}

function swap(pic,AB) {
if ( AB == "B" )
{ document.getElementById(pic).src = 'images/' + pic + 'B.jpg';}
if ( AB == "A" )
{ document.getElementById(pic).src = 'images/' + pic + '.jpg'; }
}


function varieties(crop) {
var variety = document.getElementById(crop);
if ( variety.className == "showvars" )
{ variety.setAttribute("class", "neutral");
 variety.setAttribute("className", "neutral"); // for IE (duh)
}
if ( variety.className == "varieties" )
{ variety.setAttribute("class", "showvars");
 variety.setAttribute("className", "showvars"); // for IE (duh)
}
if ( variety.className == "neutral" )
{ variety.setAttribute("class", "varieties");
 variety.setAttribute("className", "varieties"); // for IE (duh)
}
return;
}


function showallvars() {
	var gnarl = getElementsByClassName("varieties", "ul", document.getElementById("crops"));
	for ( i=0; i<gnarl.length; i++ ) {
		gnarl[i].setAttribute("class", "showvars"); // relies on inheritance
		gnarl[i].setAttribute("className", "showvars");
	}
}

function hideallvars() {
	var gnarl = getElementsByClassName("showvars", "ul", document.getElementById("crops"));
	for ( i=0; i<gnarl.length; i++ ) {
		gnarl[i].setAttribute("class", "varieties"); // relies on inheritance
		gnarl[i].setAttribute("className", "varieties");
	}
}

//////////////// lengthy borrowed function from Robert Nyman ///////////////////////

var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

///////////////////////////////////////


function toggle(divname,text) {
divname2 = divname;
text2 = text;
innerstuff = document.getElementById(divname2).innerHTML;
if ( innerstuff == text2 )
{ document.getElementById(divname2).innerHTML = ""; 
}
if ( innerstuff == "" )
{ document.getElementById(divname2).innerHTML = text2; 
}
}

//photoadd.pl deletes last line of this file, write another cs array element (i.e. photo caption), and replaces the last line with authorization code.  Do not add a line break after the last line below.

function findcapt(pic) {
var cs = new Array();
cs["c_plans_1"] = "The Back Plot";
cs["c_plans_2"] = "The Green House";
cs["c_plans_3"] = "The Hoop House";
cs["c_plans_4"] = "The Full Back Yard";
cs["c_plans_5"] = "The Front Plot in January";
cs["c_plans_6"] = "The Back in January";
if ( typeof cs[pic] == "undefined" ) { var caption = "..."; } else { var caption = cs[pic]; } return caption; }