var request = null;

function createRequest() {
	try {
		request = new XMLHttpRequest();
		}
	catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (failed) {
				request = null;
				}
			}
		}
	if (request == null)
		alert("Error creating request object!");
		}

var whichfunction = 0;
var alreadydone = 0;

function gettheimages(wfunction) {
	whichfunction = wfunction;
	createRequest();
	var url = "get_images.php";
	request.open("GET", url, true);
	request.onreadystatechange = updateRandomImages;
	request.send(null);
	}
	
function updateRandomImages() {
if (request.readyState == 4) {
	var newContent = request.responseText;
	//splits the servers response at each space, therefore giving array of filenames
	picparts = newContent.split("\n");
	//this removes the last element in the array as it always seems to be a blank space of nothingness.
	picparts.splice((picparts.length - 1), 1);
	if (document.getElementById('full_gallery').style.display == 'inline') {
		whichfunction = 1;
		}
	if (whichfunction == 1) {
		document.getElementById("view_entire").style.color = "#E0E6B2";
		document.getElementById("full_gallery").style.display = 'none';
		document.getElementById("random_pics").style.display = 'inline';
	/*The following code generates an array of 4 random value elements between 0 and the number of elements
	in the picparts array. It is from this array that the random thumbnail generating script will pick it's
	values. */
		var h = 0;
		//generates new 4 element array
		ran = new Array(4);
			//generates first unique random number
			var rannum = Math.ceil(Math.random() * (picparts.length)) - 1;
			ran[0] = rannum
			h++;
		//generates second unique random number	
			do {
				rannum = Math.ceil(Math.random() * (picparts.length)) - 1;
			}
			while (rannum == ran[0])
			ran[1] = rannum;
			h++;
			//generates third unique random number
			do {
				rannum = Math.ceil(Math.random() * (picparts.length)) - 1;
			}
			while ((rannum == ran[0]) || (rannum == ran[1]))
			ran[2] = rannum;
			h++;
			//generates last unique random number
			do {
				rannum = Math.ceil(Math.random() * (picparts.length)) - 1;
			}
			while ((rannum == ran[0]) || (rannum == ran[1]) || (rannum == ran[2]))
			ran[3] = rannum;
			h++
	
		//Generates 4 variables to be used in the next section
		var first = ran[0];
		var second = ran[1];
		var third = ran[2];
		var fourth = ran[3];
	
		//This generates and applies the 4 new random images and adjusts the parent node a tag's href value
		var replacethis = document.getElementById("randomimg0");
		replacethis.src = ("gallery/thumbnails/" + picparts[first]);
		replacethis.parentNode.href = ("gallery/images/" + picparts[first]);
		replacethis.parentNode.accesskey = ('z');
	
		replacethis = document.getElementById("randomimg1");
		replacethis.src = ("gallery/thumbnails/" + picparts[second]);
		replacethis.parentNode.href = ("gallery/images/" + picparts[second]);
		replacethis.parentNode.accesskey = ('x');
	
		replacethis = document.getElementById("randomimg2");
		replacethis.src = ("gallery/thumbnails/" + picparts[third]);
		replacethis.parentNode.href = ("gallery/images/" + picparts[third]);
		replacethis.parentNode.accesskey = ('c');
	
		replacethis = document.getElementById("randomimg3");
		replacethis.src = ("gallery/thumbnails/" + picparts[fourth]);
		replacethis.parentNode.href = ("gallery/images/" + picparts[fourth]);
		replacethis.parentNode.accesskey = ('v');
		}
	
	if (whichfunction == 2) {
		if (alreadydone == 0) {
			var h = 0;
			do {
				var full_a = document.createElement('a');
				full_a.href = ("gallery/images/" + picparts[h]);
				full_a.className = ("thickbox");
				full_a.title = ("Tasty Morsels Image Gallery - image no. " + (h+1));
				full_a.rel = ("group2");
				full_a.target = ("_blank");
		
				var full_img = document.createElement('img');
				full_img.src = ("gallery/thumbnails/" + picparts[h]);
				full_img.id = ("tm_image_" + (h + 1));
				full_img.alt = ("Tasty Morsels Image no. " + (h + 1));
		
				full_a.appendChild(full_img);
				document.getElementById("full_gallery").appendChild(full_a);
				h++; 
				}
				while (h < (picparts.length))
			alreadydone++;
			
		}
		document.getElementById("view_entire").style.color = "#44CADA";
		document.getElementById("random_pics").style.display = 'none';
		document.getElementById("full_gallery").style.display = 'inline';
		/*<!--The next two lines call the thickbox function that registers the new link/images with 
			classname "thickbox" that have been created -->*/
			if (alreadydone == 1) {
			$(document.getElementById("view_entire")).click(TB_init()
  			);
			alreadydone++; }
		}
	
	}
}


