/**\
 ||
 ||  init(): initial script to load up images, text and what have you.
 ||
\**/
function init(page) {
	if (!page) {
	loadhomeimages();
	loadimagegallery();
	slideshow();
	loadprojects();
	}
	if (page == 'testimonials') {
		loadTestimonials();
	}
	if (page == 'about') {
		var maintext = gettext('/content/about/text1.txt');
		maintext = maintext.replace(/\n/g,'<br>');	
		parent.document.getElementById('TestInsert').innerHTML = maintext;

	}
	if (page == 'contact') {
		var maintext = gettext('/content/contact/text1.txt');
		parent.document.getElementById('TestInsert').innerHTML = maintext;
	}
}

/**\
 ||
 ||  clearUsername(): clears the username box
 ||
\**/
function clearUsername() {
	var input = parent.document.getElementById('inputtext1');
	input.value = '';	
}

/**\
 ||
 ||  addUsername(): puts the word "username" back in the box
 ||
\**/
function addUsername() {
	var input = parent.document.getElementById('inputtext1');
	if (input.value == '') {
		input.value = 'Username';
	}
}

/**\
 ||
 ||  addPassword(): puts the word "password" back in the box
 ||
\**/
function addPassword() {
	var input = parent.document.getElementById('inputtext2');
	if (input.value == '') {
		input.type = 'text';	
		input.value = 'Password';
	}
}

/**\
 ||
 ||  clearPassword(): clears the username box
 ||
\**/
function clearPassword() {
	var input = parent.document.getElementById('inputtext2');
	input.value = '';	
	input.type = 'password';	
}

/**\
 ||
 ||  loadprojects(): loads up a random assortment of projects, something different everytime how cool is that? to a max of 5 entries
 ||
\**/
function loadprojects() {
	// get list of recent projects
	var projectarray = projarray();
	var projects = new Array();
	// pic a few at random, up to 3

	// how many projects?
	var projectqty = projectarray.length / 3;
	var projectName = new Array();
	var projectDiv = new Array();
	var projectID = new Array();
	var randomNums = uniqueRandoms(projectqty);

	if (projectqty > 3) {
		for (i = 1; i <= 3; i++) {
			var fileLocation1 = '/content/projects/text' + randomNums[i] + 'title1.txt';
			var fileLocation2 = '/content/projects/text' + randomNums[i] + 'main.txt';
			projectName[i] = gettext(fileLocation1);
			projectDiv[i] = gettext(fileLocation2);
			projectID[i] = "Proj" + randomNums[i];
		}
	} else {
		for (i = 1; i <= projectqty; i++) {
			var fileLocation1 = '/content/projects/text' + i + 'title1.txt';
			var fileLocation2 = '/content/projects/text' + i + 'main.txt';
			projectName[i] = gettext(fileLocation1);
			projectDiv[i] = gettext(fileLocation2);
			projectID[i] = "Proj" + i;
		}
		for (i = i; i <= 3; i++) {
			projectName[i] = "";
			projectDiv[i] = "";
		}		
	}
	// display
	parent.document.getElementById('projmenuitem1').innerHTML = projectName[1] + "<br>&nbsp;" + projectDiv[1];
	parent.document.getElementById('projmenuitem2').innerHTML = projectName[2] + "<br>&nbsp;" + projectDiv[2];
	parent.document.getElementById('projmenuitem3').innerHTML = projectName[3] + "<br>&nbsp;" + projectDiv[3];

	// some prep work for the below
//	var func1 = 'newcontent("';
//	var func2 = '");';
	
//	parent.document.getElementById('projmenuitem1').setAttribute('onclick',func1 + projectID[1] + func2);
//	parent.document.getElementById('projmenuitem2').setAttribute('onclick',func1 + projectID[2] + func2);
//	parent.document.getElementById('projmenuitem3').setAttribute('onclick',func1 + projectID[3] + func2);

}
/**\
 ||
 ||  loadhomeimages(): loads up a couple of images on the home page to get started.
 ||
\**/
function loadhomeimages() {
	var imgarray = imagearray();
	var image = new Array();
	j = 0;
	for (i = 0; i < 2; i++) {
		if (!imgarray[j]) {
			j = 0;
		}
		image[i] = "/content/homepagepics/" + imgarray[j];
		j++;
	}
	parent.document.getElementById('homeimgtop').src = image[0];
	parent.document.getElementById('homeimgbum').src = image[1];
}

/**\
 ||
 ||  changepic(): changes current picture to the thumbnail moused over
 ||
\**/
function changepic(id) {
	var source = parent.document.getElementById(id).src;
	parent.document.getElementById('homeimgtop').src = source;
	parent.document.getElementById('homeimgbum').src = source;
}

/**\
 ||
 ||  loadimagegallery(): loads up 9 images in to the image gallery
 ||
\**/
function loadimagegallery() {
	var imgarray = imagearray();
	var image = new Array();
	j = 0;
	for (i = 0; i < 9; i++) {
		if (!imgarray[j]) {
			j = 0;
		}
		image[i] = "/content/homepagepics/" + imgarray[j];
		j++;
	}
	parent.document.getElementById('imggal1').src = image[0];
	parent.document.getElementById('imggal2').src = image[1];
	parent.document.getElementById('imggal3').src = image[2];
	parent.document.getElementById('imggal4').src = image[3];
	parent.document.getElementById('imggal5').src = image[4];
	parent.document.getElementById('imggal6').src = image[5];
	parent.document.getElementById('imggal7').src = image[6];
	parent.document.getElementById('imggal8').src = image[7];
	parent.document.getElementById('imggal9').src = image[8];
}
/**\
 ||
 ||  clicked(id): when clicked, a link becomes active
 ||
\**/
function clicked(id) {
	// clear out those testimonials
	dropTestimonialDivision();

	// split link id out
	var link = id.split('_')[1];
	var header_link = 'header_' + link;
	var footer_link = 'footer_' + link;

	// clear the 'active' class from the other links
	parent.document.getElementById('header_Home').removeAttribute('class');
	parent.document.getElementById('header_Testimonials').removeAttribute('class');
	parent.document.getElementById('header_About').removeAttribute('class');
	parent.document.getElementById('header_Contact').removeAttribute('class');
	parent.document.getElementById('footer_Home').removeAttribute('class');
	parent.document.getElementById('footer_Testimonials').removeAttribute('class');
	parent.document.getElementById('footer_About').removeAttribute('class');
	parent.document.getElementById('footer_Contact').removeAttribute('class');

	// set the 'active' class for the selected link
	parent.document.getElementById(header_link).setAttribute('class','active');
	parent.document.getElementById(footer_link).setAttribute('class','active');
	
	// carry out any special functions
	if (link == 'Contact') {
		newcontent('contact');
	} else if (link == 'About') {
		newcontent('about');
	} else if (link == 'Testimonials') {
		newcontent('testimonials');
	}
}



/**\
 ||
 ||  newcontent(content): when clicked, the main page area is updated with new content
 ||
\**/
function newcontent(content) {
	if (content == 'interiors') {
		// get title of content (in the form of a string)
		var title = "Electrical Interiors";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/divisions/interiors/text.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/divisions/interiors/picture.gif';
	} else if (content == 'bopelectrical') {
		// get title of content (in the form of a string)
		var title = "Electrical - Bay of Plenty";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/divisions/bopelectrical/text.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/divisions/bopelectrical/picture.gif';
	}else if (content == 'bopdatatech') {
		// get title of content (in the form of a string)
		var title = "Bay of Plenty";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/divisions/bopdatatech/text.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/divisions/bopdatatech/picture.gif';
	}else if (content == 'data') {
		// get title of content (in the form of a string)
		var title = "Data-tech Services";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/divisions/datatech/text.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/divisions/datatech/picture.gif';
	} else if (content == 'er') {
		// get title of content (in the form of a string)
		var title = "Electrical Response";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/divisions/er/text.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/divisions/er/picture.gif';
	} else if (content == 'contracts') {
		// get title of content (in the form of a string)
		var title = "Contracts Division";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/divisions/contracts/text.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/divisions/contracts/picture.gif';
	} else if (content == 'contact') {
		// get title of content (in the form of a string)
		var title = "Contact Us";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/contact/text1.txt');
		var people = gettext('/content/contact/text2.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/contact/picture.gif';
	} else if (content == 'about') {
		// get title of content (in the form of a string)
		var title = "About Us";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/about/text1.txt');
		var people = gettext('/content/about/text2.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/about/picture.gif';
	} else if (content.indexOf("Proj") != -1) {
		var projIndex = content.replace("Proj", "")
		var fileLocation1 = '/content/projects/text' + projIndex + 'title1.txt';
		var fileLocation2 = '/content/projects/text' + projIndex + 'title2.txt';
		var fileLocation3 = '/content/projects/text' + projIndex + 'main.txt';
		var divisionname = gettext(fileLocation2);	
		if (divisionname == 'electrical interiors') {
			var piclocation = '/content/divisions/interiors/picture.gif';		
		} else if (divisionname == 'data-tech services') {
			var piclocation = '/content/divisions/datatech/picture.gif';			
		} else if (divisionname == 'electrical response') {
			var piclocation = '/content/divisions/er/picture.gif';			
		} else if (divisionname == 'contracts division') {
			var piclocation = '/content/divisions/contracts/picture.gif';			
		} else if (divisionname == 'electrical - bay of plenty') {
			var piclocation = '/content/divisions/bopelectrical/picture.gif';			
		} else if (divisionname == 'data-tech services - bay of plenty') {
			var piclocation = '/content/divisions/bopdatatech/picture.gif';			
		}		
		var title = "About Us";		
		var title = gettext(fileLocation1);				
		var maintext = gettext(fileLocation3);		
	} else if (content == 'testimonials') {
		// get title of content (in the form of a string)
		var title = "Testimonials";
		// get actual content (in the form of a string)
		var maintext = gettext('/content/testimonials/text.txt') + '<div id="TestInsert"></div>';
		var people = gettext('/content/about/text2.txt');
		// get related picture (or at least, the location of the picture file
		var piclocation = '/content/about/picture.gif';
	}
	// prep our content to ensure proper formatting (line breaks etc)
	maintext = maintext.replace(/\n/g,'<br>');	
	
	// update page with new info
	parent.document.getElementById('title').innerHTML = title;
	parent.document.getElementById('maintext1').innerHTML = maintext;
	parent.document.getElementById('maintext2').innerHTML = '';
	if (parent.document.getElementById('homeimgbum')) {
		parent.document.getElementById('homeimgbum').id = 'divimg';
		parent.document.getElementById('divimg').src = piclocation;
		parent.document.getElementById('divimg').width = 141;
		parent.document.getElementById('divimg').height = 60;	
	} else {
		parent.document.getElementById('divimg').src = piclocation;
		parent.document.getElementById('divimg').width = 141;
		parent.document.getElementById('divimg').height = 60;	
	}	
		// any extra work
//	alert('testii');
	if (content == 'testimonials') {
		// load up the testimonials
		loadTestimonials();
	}
	// removes the old image out of the mix
	var d = document.getElementById('slideshow');
  	var olddiv = document.getElementById('homeimgtop');
  	d.removeChild(olddiv);
  	


	// done :-)
}

/**\
 ||
 ||  gettext(location): returns content of relevant text file
 ||
\**/
function gettext(location) {
	var content = ajaxTextGrab(location);
	return content;
}

/*\
 |
 |   returns a text page in the form of a variable, using the get method
 |
\*/

function ajaxTextGrab(file) {
//	var file = '/content/divisions/contracts/text.txt';
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	ajaxRequest.open("GET", file, false);
	ajaxRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	ajaxRequest.send();
	return ajaxRequest.responseText;
}

/*\
 |
 |   returns a php page in the form of a variable
 |
\*/

function ajaxFunction(phpfile){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}

	ajaxRequest.open("GET", phpfile, false);
	ajaxRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	ajaxRequest.send();
	return ajaxRequest.responseText;
}
/**\
 ||
 ||  swapimagearraybum(): generates a list of file names from the /content/homepagepics/ folders. only jpegs are included.
 ||               				
\**/

function projarray()
{
 var phpfile = "/content/projects/projectlist.php"
 var projlist = ajaxFunction(phpfile);
 var projpre = projlist.split(',');
 var projpst = new Array();
 var j = 0;
 for (i = 0; i < projpre.length; i++) {
	if (projpre[i] != "") {
 		projpst[j] = projpre[i];
 		j++
	}
 }
 return projpst;
}

/**\
 ||
 ||  uniqueRandoms(): generates an array of unique random integers, index of array begins at 1. the second functions was downloaded of the net
 ||               				
\**/

function uniqueRandoms(leng)
{
 var randoms = new Array();
 for (i = 1; i <= leng; i++) {
	randoms[i] = i;
 }
 return randoms.sort(randOrd);
}

function randOrd()
{
	return (Math.round(Math.random())-0.5); 
} 

/**\
 ||
 ||  loadTestimonials(): checks content folder for testimonials, displays them in order
 ||               				
\**/

function loadTestimonials()
{
	// get list of testimonials
	var testarray = testimonialarray();
	var testimonialText = new Array();
	var testimonialPic = new Array();

	// how many testimonials?
	var testimonialqty = (testarray.length - 2) / 2;

	for (i = 1; i <= testimonialqty; i++) {
		var fileLocation1 = '/content/testimonials/text' + i + '.txt';
		
		// insert new content
		// define new elements
		var newTestImg = document.createElement("img");
		var newTestText = document.createElement("p");
	
		// get division
		var newDiv = parent.document.getElementById('TestInsert');
	
		// set picture content location
		var piclocation = '/content/testimonials/picture' + i + '.gif';
	
		// set text
		var text = gettext(fileLocation1);

		// add picture and text to elements
		newTestImg.src = piclocation;
		newTestText.innerHTML = text;
	
		// insert said picture
		newDiv.appendChild(newTestImg); 	

		// insert text
		newDiv.appendChild(newTestText); 
	}
}
/**\
 ||
 ||  testarray(): returns array containing list of testimonials
 ||               				
\**/

function testimonialarray()
{
 var phpfile = "/content/testimonials/testimonialslist.php"
 var testlist = ajaxFunction(phpfile);
 var testpre = testlist.split(',');
 var testpst = new Array();
 var j = 0;
 for (i = 0; i < testpre.length; i++) {
	if (testpre[i] != "") {
 		testpst[j] = testpre[i];
 		j++
	}
 }
 return testpst;
}

/**\
 ||
 ||  dropTestimonialDivision(): clear up all that mess made by the testimonials page
 ||               				
\**/

function dropTestimonialDivision()
{
	if (document.getElementById('TestInsert')) {
		var d = document.getElementById('maintext1');
		var olddiv = document.getElementById('TestInsert');
	//	d.removeChild(olddiv);
	}
}
