
// creating the array for how many images are being loaded.
// this information is automatically loaded with a php script
// the scrip reads how many files are loaded into the directory
var galleryTotal = new Array
	
// names of each callery being set here
var galleryNames = new Array
	
var galleryNumber
var number
var h

function pictureHolder(){
	
	// creating cells to hold the picture cells
	var table = document.getElementById(galleryNames[h] + 'Gallery-' + galleryNumber)
	for(var i=1; i<=3; i++){ // creating table row
		var lastRow = table.rows.length
		var tableAdd = table.insertRow(lastRow)
		
		for(var j=1; j<=6; j++){ // creating table cell
			var cell = new Array
			cell[j] = tableAdd.insertCell(j-1)
			if(number == galleryTotal[h]){
				if(j==6){}else{cell[j].style.width = '106px'} // checking if the last cell is being processed and if it is stopping the style from being applied
			}
			else{
				number = number + 1
				cell[j].id = 'cell-' + h + '-' + number
				if(j==6){}else{cell[j].style.width = '106px'} // checking if the last cell is being processed and if it is stopping the style from being applied
			}
			if(number == galleryTotal[h]){i=3} // checking to see if the last image has been reached and canceling the loop if it has
		}
	}
	
	// checking to see if the last image has been reached
	// if the last image has not been reached loading a new gallery
	if(number == galleryTotal[h]){
		// checking to see if the end of the current gallery (children, family...) has been reached and moving onto the next
		if(h < 4){ // not at the end of the total gallery going onto the next
			h = h  + 1
			galleryNumber = 1
			number = 0
			pictureHolder()
		}
		else{ // at the end of the total galleries and moving onto the next function to build the picture tables
			galleryNumber = 1
			number = 0
			h = 1
			pictureTable()
		}
	}
	else{
		galleryNumber = galleryNumber + 1
		pictureHolder()
	}
}

function pictureTable(){
	
	for(var h=1; h<=4; h++){ // controlling the gallery selection (children, family...)
		for(var i=1; i<=galleryTotal[h]; i++){
			var myCell = document.getElementById('cell-' + h + '-' + i) // getting the cell that was created to hold the picture table
			var myTable = document.createElement('table') // creating the table for the picture
				myTable.id = h + '-' + i
				myTable.border = '0'
				myTable.cellPadding = '0'
				myTable.cellSpacing = '0'
				myTable.bgColor = '#3e271a'
				myTable.align = 'center'
			var tbody = document.createElement('tbody')
				myTable.appendChild(tbody)
				myCell.appendChild(myTable)
			
			var pictureTable = document.getElementById(h + '-' + i) // selecting the table that was just created for building the picture cells
			
			// creating new row for the bottom
			var lastRow = pictureTable.rows.length
			var rowAdd = pictureTable.insertRow(lastRow)
			
			var topCell1 = rowAdd.insertCell(0)
			var leftCorner = document.createElement('img')
				leftCorner.src = 'gallery/img/cornerLT.png'
				topCell1.appendChild(leftCorner)
				
			var topCell2 = rowAdd.insertCell(1)
			var blank = document.createTextNode('')
				topCell2.appendChild(blank)
				
			var topCell3 = rowAdd.insertCell(2)
			var rightCorner = document.createElement('img')
				rightCorner.src = 'gallery/img/cornerRT.png'
				topCell3.appendChild(rightCorner)
				
			// creating new row for the image
			lastRow = pictureTable.rows.length
			rowAdd = pictureTable.insertRow(lastRow)
			
			var topCell1 = rowAdd.insertCell(0)
			var blank = document.createTextNode('')
				topCell1.appendChild(blank)
			
			var topCell2 = rowAdd.insertCell(1)
			var pictureLink = document.createElement('a') // creating link
				pictureLink.rel = galleryNames[h]
				pictureLink.href = 'gallery/' + galleryNames[h] + '/slides/' + i + '.jpg'
				pictureLink.style.border = 'none'
				pictureLink.title = i
			var picture = document.createElement('img') // creating picture
				picture.src = 'gallery/' + galleryNames[h] + '/thumbs/' + i + '.jpg'
				picture.border = '0'
				picture.id = 'image-' + h + '-' + i
				pictureLink.appendChild(picture) // putting picture into link tag
				topCell2.appendChild(pictureLink) // putting link into cell tag
				
			var topCell3 = rowAdd.insertCell(2)
			var blank = document.createTextNode('')
				topCell3.appendChild(blank)
			
			// creating new row for the bottom
			var lastRow = pictureTable.rows.length
			var rowAdd = pictureTable.insertRow(lastRow)
		
			var topCell1 = rowAdd.insertCell(0)
			var leftCorner = document.createElement('img')
				leftCorner.src = 'gallery/img/cornerLB.png'
				topCell1.appendChild(leftCorner)
				
			var topCell2 = rowAdd.insertCell(1)
			var blank = document.createTextNode('')
				topCell2.appendChild(blank)
				
			var topCell3 = rowAdd.insertCell(2)
			var rightCorner = document.createElement('img')
				rightCorner.src = 'gallery/img/cornerRB.png'
				topCell3.appendChild(rightCorner)
		}
	}
	
	//controls()
	
	// assigning colorbox even to elements
	$(document).ready(function(){
		$("a[rel='children']").colorbox();
		$("a[rel='family']").colorbox();
		$("a[rel='senior']").colorbox();
		$("a[rel='maternity']").colorbox();
	});
}
