var lsstImage = 1;
var lastBook;
var lastPage="nav1";
var curPage=1;
var theCat="";
var theSubcat="";
var theSlices = new Array();
var loadCount;
var photoCount;
var problemString;
var loading=0;
var imgCount=24;
var theGals = 4;
var galCount = 1;
var curGal = 1;
var theImage = new Array();
var theImages = new Array();
var theImgs1 = new Array();
var theImgs2 = new Array();
var theImgs3 = new Array();
var theImgs4 = new Array();
var meta1 = new Array();
var meta2 = new Array();
var meta3 = new Array();
var meta4 = new Array();

var gals = new Array();
var imgs1 = new Array();
var imgs2 = new Array();
var imgs3 = new Array();
var imgs4 = new Array();
var path1 = "images/paintings/01/";
var path2 = "images/paintings/02/";
var path3 = "images/paintings/03/";
var path4 = "images/paintings/04/";
imgs1[0] = "1_C_Hayes_060828a_Set_for_S.jpg||&quot;Set for Seven, Normandy&quot;, 18&quot; x 18&quot;, oil on canvas";
imgs1[1] = "2_C_Hayes_060611a_Henrietta.jpg||&quot;Henrietta Hen, Normandy&quot;, 8&quot; x 8&quot;, pastel on paper";
imgs1[2] = "3_C_Hayes_060605c_Apricots_.jpg||&quot;Apricots, Normandy&quot;, 8&quot; x 8&quot;, pastel on paper";
imgs1[3] = "4_C_Hayes_060601c_Roses_Nor.jpg||&quot;Roses, Normandy&quot;, 8&quot; x 8&quot;, pastel on paper";
imgs1[4] = "5_C_Hayes_060605d_Green_Wav.jpg||&quot;Green Waves, Normandy&quot;, 8&quot; x 8&quot;, pastel on paper";
imgs1[5] = "6_C_Hayes_Red_Room_Yell.jpg||&quot;Red Room, Yellow Light, Normandy&quot;, 16&quot; x 16&quot;, oil on canvas";

imgs2[0] = "1_C_Hayes_Its_All_Going_to_be_fine.jpg||&quot;It's All Going to Be Fine, Vinalhaven&quot;, 30&quot; x 40&quot;, oil on canvas";
imgs2[1] = "2_C_Hayes_071212b_The_Yello.jpg||&quot;The Yellows of Stonington #2&quot;, 36&quot; x 36&quot;, oil on canvas";
imgs2[2] = "3_C_Hayes_Red_Roof_Red.jpg||&quot;Red Roof, Red Truck, Gasp?&quot;, 10&quot; x 20&quot;, oil on canvas";
imgs2[3] = "4_C_Hayes_060216a_Simmer_Vi.jpg||&quot;Simmer, Vinalhaven&quot;, 36&quot; x 36&quot;, oil on canvas";
imgs2[4] = "5_C_Hayes_041006a_Small_Red.jpg||&quot;Small Red Car, Vinalhaven&quot;, 24&quot; x 24&quot;, oil on canvas";
imgs2[5] = "6_C_Hayes_020424a_Route_129.jpg||&quot;Route 129 At the Bridge #2&quot;, 36&quot; x 36&quot;, oil on canvas";

imgs3[0] = "1_Both_Warm.jpg||&quot;Both Warm and Cool&quot;, 12&quot; x 12&quot;, oil on canvas";
imgs3[1] = "2_Lost_and_Found.jpg||&quot;Lost and Found&quot;, 12&quot; x 12&quot;, oil on canvas";
imgs3[2] = "3_Scruffy_hr.jpg||&quot;Scruffy&quot;, 12&quot; x 12&quot;, oil on canvas";
imgs3[3] = "4_Red_Threa.jpg||&quot;Red Threads&quot;, 12&quot; x 12&quot;, oil on canvas";
imgs3[4] = "5_Pink_and_Lifting.jpg||&quot;Pink and Lifting&quot;, 12&quot; x 12&quot;, oil on canvas";
imgs3[5] = "6_Hush.jpg||&quot;Hush&quot;, 12&quot; x 12&quot;, oil on canvas";

imgs4[0] = "1_C_Hayes_070605a_Stay_Aler.jpg||&quot;Stay Alert, NYC&quot;, 10&quot; x 10&quot;, oil on muslin on panel";
imgs4[1] = "2_C_Hayes_070502a_Cabs_at_t.jpg||&quot;Cabs at the Corner, NYC&quot;, 14&quot; x 14&quot;, oil on muslin on panel";
imgs4[2] = "3_C_Hayes_020530a_Where_to_.jpg||&quot;Where to Sit?&quot;, 20&quot; x 20&quot;, oil on canvas";
imgs4[3] = "4_C_Hayes_060111a_Bring_To_.jpg||&quot;Bring To Light, Vinalhaven&quot;, 20&quot; x 20&quot;, oil on canvas";
imgs4[4] = "5_C_Hayes_070810a_Cyclamen_.jpg||&quot;Cyclamen&quot;, 12&quot; x 12&quot;, oil on canvas";
imgs4[5] = "6_C_Hayes_050312a_Bed_At_Ch.jpg||&quot;Bed At Christmas Cove&quot;, 20&quot; x 20&quot;, oil on canvas";


gals[0] = imgs1;
gals[1] = imgs2;
gals[2] = imgs3;
gals[3] = imgs4;

function GetXmlHttpObject() {

	var objXMLHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		objXMLHttp=new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");	
			}
			catch (e) {
		      	alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	/*
	var objXMLHttp=null

	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	*/
	return objXMLHttp;

}

function stateChanged4() {
	if (xmlHttp4.readyState==4 || xmlHttp4.readyState=="complete") {
		theImages = xmlHttp4.responseText.split("|||");
		//theImages = theImages.split("|||")
		k=0;
		for (i=1; i<=4; i++) {
			for (j=1; j<=6; j++) {
				//eval("imgs"+i+"["+(j-1)+"] = theImages["+((i*j)-1)+"]");
				theImage = theImages[k].split("||");
				theSrc = theImage[0];
				theMeta = theImage[1];
				
				eval("theImgs"+i+"["+(j-1)+"] = theSrc");

				eval("meta"+i+"["+(j-1)+"] = theMeta");
				k++;
			}
		}
		loadTheImg(1,0);
	}
}

function getImages() {
	xmlHttp4 = new GetXmlHttpObject();

	if (xmlHttp4===null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	url="getImages.php";
	url+="?sid="+Math.random();
	xmlHttp4.onreadystatechange=stateChanged4;
	xmlHttp4.open("GET",url,true);
	xmlHttp4.send(null);
}

function makeImage(theString) {
	theArray = theString.split(",^ ");
	theUrl = theArray[0];
	theWidth = parseInt(theArray[1], 10);
	theHeight = parseInt(theArray[2], 10);
	theTitle = theArray[3];
	theYear = theArray[4];
	theMedium = theArray[5];
	//no dates on page
	/*
	if (theYear=="0000") {
		theYear = ""
	} else {
		theYear = ", "+theYear
	}
	*/
	
	document.getElementById("imageWell").style.backgroundImage = "url("+theUrl+")";
	document.getElementById("imgMeta").innerHTML = "&quot;"+theTitle+"&quot;, "+theWidth+"&quot; x "+theHeight+"&quot;, "+theMedium;
}

function stateChanged2() {
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") {
		makeImage(xmlHttp2.responseText);
	}
}

function imageLoader(imgID) {
	xmlHttp2 = new GetXmlHttpObject();

	if (xmlHttp2===null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	url="CHImageLoader.php";
	url+="?imgID="+imgID;
	url+="&sid="+Math.random();
	xmlHttp2.onreadystatechange=stateChanged2;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
}

function bigProblem(theString) {
	var bigString = "";
	theArray = theString.split(",^ ");
	theDivs = document.getElementsByTagName("div");
	j=0;
	for (i=0; i < (theDivs.length); i++) {
		if (theDivs[i].className == "thumbSquare") {
			if (j==0) {
				theDivs[i].id = "chosen";
			} else {	
				theDivs[i].id = parseInt(theArray[j]);
			}
			j++;
		}
	}
	lsstImage = parseInt(theArray[0]);
	imageLoader(parseInt(theArray[0]));
}

function stateChanged3() {
	if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete") {
		lsstImage = parseInt(xmlHttp3.responseText);
		imageLoader(lsstImage);
	}
}

function firstLoader(cat, subcat) {
	xmlHttp3 = new GetXmlHttpObject();

	if (xmlHttp3===null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	url="CHFirstLoader.php";
	url+="?cat="+cat;
	url+="&subcat="+subcat.substr(3);
	url+="&sid="+Math.random();
	xmlHttp3.onreadystatechange=stateChanged3;
	xmlHttp3.open("GET",url,true);
	xmlHttp3.send(null);
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		bigProblem(xmlHttp.responseText);
	}
}

function thumbLoader(cat, subcat) {
	xmlHttp = new GetXmlHttpObject();

	if (xmlHttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	url="CHThumbLoader.php";
	url+="?cat="+cat;
	url+="&subcat="+subcat.substr(3);
	url+="&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function reRun(cat, subCat) {
	if (subCat == "navChosen") {
		return;
	} else {
		theCat = cat;
		theSubcat = subCat;
		curGal = subCat.substring(3);
		document.getElementById("navChosen").id = lastPage;
		document.getElementById("chosen").id = lsstImage;
		lastPage = theSubcat;
		lsstImage = 1;
		document.getElementById(lastPage).id = "navChosen";
		document.getElementById(lsstImage).id = "chosen";
		//thumbLoader(theCat, theSubcat);
		loadTheImg(curGal, 0);
	}
}

function firstRun(cat, subCat) {
	theCat = cat;
	theSubcat = subCat;
	document.getElementById("navChosen").id = lastPage;
	lastPage = theSubcat;
	document.getElementById(lastPage).id = "navChosen";
	//firstLoader(theCat, theSubcat);
}

function loadImage(theID) {
	/*
	if (loading == 0) {
		//theID = event.target.id;
		*/
		if (theID == "chosen") {
			//alert("chosen");
			return;
		} else {
			document.getElementById("chosen").id=lsstImage;
			document.getElementById(theID).id="chosen";

			lsstImage=theID;
			//imageLoader(theID);
			loadTheImg(curGal, theID-1);
		}
	//}
}

function prePreLoader() {
	getImages();
}

//non AJAX Scripts
function preLoader() {
	for (j=1; j<=theGals; j++) {
		for (i=0; i<eval("imgs"+j+".length"); i++) {
			theImg = eval("imgs"+j+"["+i+"]").split("||");
			theSrc = eval("path"+j) + theImg[0];
			//theSrc = theImg[0];
			theMeta = theImg[1];
			
			//eval("theImgs"+j+"["+i+"]") = new Image();
			eval("theImgs"+j+"["+i+"] = theSrc");
			
			eval("meta"+j+"["+i+"] = theMeta");
		}
	}
	//isLoaded();
	loadTheImg(1, 0);
}

function isLoaded() {
	t = setTimeout("isLoaded()", 100)
	if (loadCount == imgCount) {
		clearTimeout(t)
		loadCount = 1
		galCount = 1
		
		loadTheImg(1, 0);
	} else {
		if (eval("theImgs"+galCount+"["+(loadCount-1)+"]").height != 0) {
			loadCount++;
		}
	}
}

function loadTheImg(galID, imgID) {
	document.getElementById("imgMeta").innerHTML = eval("meta"+galID+"["+imgID+"]");
	document.getElementById("imageWell").style.backgroundImage = "url("+eval("theImgs"+galID+"["+imgID+"]")+")";
}
