var submittedData
var submittedNumber
var submittedGallery
var submittedDirection
var tblWidth = 700
var tblHeight = 360
var remove = 20
var removeVertical = 15
var intervalDelayV = 30
var intervalDelayH = 5
var intervalUp
var intervalDown
var scrollFinishF
var scrollFinishR
var divNumber
var gallerySelected
var galleryClose
var galleryHiding
var active

// horizontal scrolling
var delay = 5
var count = 0
var intervalR
var intervalL

function scrolling(data, number, gallery, direction){
	
	// recording the data that was submitted if the function needs to run again
	submittedData = data
	submittedNumber = number
	submittedGallery = gallery
	submittedDirection = direction

	divNumber = number
	gallerySelected = 'gallery' + gallery

	// ADD NEW GALLERIES TO THIS ARRAY
	var galleryName = new Array()
	galleryName[0] = 'galleryChildren'
	galleryName[1] = 'galleryFamily'
	galleryName[2] = 'gallerySenior'
	galleryName[3] = 'galleryMaternity'
	
	// setting the display status of each gallery
	var galleryDisplayStatus = new Array()
	for(var i=0; i<=10; i++){
		if(galleryName[i] == undefined){i=10}
		else{
			galleryDisplayStatus[i] = document.getElementById(galleryName[i]).style.display
		}
	}
	
	// checking to see if gallerys are displayed and closing them if they are
	if(data == 'back' || data == 'forward'){}
	else{
		for(var j=0; j<=10; j++){
			if(galleryName[j] == undefined){j=10} // checking to see if the variable contains data and if not stopping the loop
			else{
				if(galleryDisplayStatus[j] == ''){ // checing to see if the gallery is being displayed
					if(gallerySelected == galleryName[j]){return false} // if gallery is already open this will stop the function so it doesn't reopen
					else{
						galleryHiding = galleryName[j]
						active = 'true'
						intervalUp = setInterval('scrollUp()',intervalDelayV)
						j=10
					}
				}
			}
		}
	}
	
	// checking if it is scrolling vertically or horizontally
	if(direction == 'vertical'){}
	else{
		scrollFinishF = document.getElementById('scrollDiv-' + divNumber).scrollLeft + tblWidth
		scrollFinishB = document.getElementById('scrollDiv-' + divNumber).scrollLeft - tblWidth
	}
	
	if(data == 'down'){intervalDown = setInterval(function(){scrollDown(number)},intervalDelayV)}
}

function scrollDown(data){
	if(active == 'true'){clearInterval(intervalDown)} // checking to see if the the window is scrolling up and if it is bypassing so a loop doesn't start
	else{
		document.getElementById(gallerySelected).style.display = ''
		var obj = document.getElementById('verticalDiv')

		// internet explorer resets the scroll left when the table is switched and messes up the buttons, this sets the scroll left back to where it should be
		
		var tableWidth = document.getElementById('totalWidth-' + divNumber).style.width
		tableWidth = tableWidth.split('px')
		tableWidth = tableWidth[0] * 1
		
		// setting horizontal scrolling back to what it was if needed
		// ie will start the scroll back at the beginning each time when all other browsers don't
		document.getElementById('scrollDiv-' + divNumber).scrollLeft = document.getElementById('scrollPossition-' + data).value
		
		obj.scrollTop = obj.scrollTop + removeVertical
		if(obj.scrollTop == tblHeight){
			clearInterval(intervalDown)
			controls(data)
			document.getElementById('imgStarting').style.display = 'none' // removing the starting image so it's not seen everytime a gallery is switched
			return false
		}
	}
}

function scrollUp(){
	var obj = document.getElementById('verticalDiv')
	obj.scrollTop = obj.scrollTop - removeVertical
	if(obj.scrollTop == 0){
		document.getElementById(galleryHiding).style.display = 'none'
		active = ''
		clearInterval(intervalUp)
		scrolling(submittedData, submittedNumber, submittedGallery, submittedDirection) // running the function again to make sure everything is closed and resetting the variables
		return false
	}
}

function horizontalScroll(direction, data){
	if(document.getElementById('btn' + direction + 'Arrow-' + data).className == 'arrow' + direction + 'Deactivated'){ // checking to see if the right button is deactivated
	}
	else{
		if(direction == 'Right'){count = 0; intervalR = setInterval(function(){scrollingRight(data)},10)}
		if(direction == 'Left'){count = 0; intervalL = setInterval(function(){scrollingLeft(data)},10)}
	}
}

function controls(data){
	
	// stopping the activation of scroll buttons if there is no second page to load
	if(galleryTotal[data] <= 18){
		document.getElementById('scrollNumbers-' + data).innerHTML = '1 of 1'
		return false
	}
	
	var currentScroll = scrollCurrent(data)
	var possitionsScroll = scrollPossitions(data)
	
	document.getElementById('scrollNumbers-' + data).innerHTML = currentScroll + ' of ' + possitionsScroll
	
	if(currentScroll == possitionsScroll){ // if we are at the end of the gallery scrolling
		document.getElementById('btnLeftArrow-' + data).className = 'arrowLeftActivated'
		document.getElementById('btnRightArrow-' + data).className = 'arrowRightDeactivated'
	}
	else{
		if(currentScroll == 1){ // if we are at the start of the gallery scrolling
		document.getElementById('btnLeftArrow-' + data).className = 'arrowLeftDeactivated'
		document.getElementById('btnRightArrow-' + data).className = 'arrowRightActivated'
		}
		else{ // if we are in the middle of the gallery scrolling
			document.getElementById('btnLeftArrow-' + data).className = 'arrowLeftActivated'
			document.getElementById('btnRightArrow-' + data).className = 'arrowRightActivated'
		}
	}
}

function scrollingRight(data){
	if(document.getElementById('btnRightArrow-' + data).className == 'arrowRightDeactivated'){clearInterval(intervalR); return false}
	var scrolling = document.getElementById('scrollDiv-' + data)
	if(scrollPossitions(data) == scrollCurrent(data)){
		clearInterval(intervalR)
		document.getElementById('scrollPossition-' + data).value = scrolling.scrollLeft
		controls(data)
	}
	else{
		if(count == 700){
			clearInterval(intervalR)
			document.getElementById('scrollPossition-' + data).value = scrolling.scrollLeft
			controls(data)
		}
		else{
			count = count + 10
			scrolling.scrollLeft = scrolling.scrollLeft + 10
		}
	}
}
function scrollingLeft(data){
	if(document.getElementById('btnLeftArrow-' + data).className == 'arrowLeftDeactivated'){clearInterval(intervalL); return false}
	var scrolling = document.getElementById('scrollDiv-' + data)
	if(scrollPossitions() == 1){
		clearInterval(intervalL)
		document.getElementById('scrollPossition-' + data).value = scrolling.scrollLeft
		controls(data)
	}
	else{
		if(count == 700){
			clearInterval(intervalL)
			document.getElementById('scrollPossition-' + data).value = scrolling.scrollLeft
			controls(data)
		}
		else{
			count = count + 10
			scrolling.scrollLeft = scrolling.scrollLeft - 10
		}
	}
}

function scrollPossitions(data){
	var number = 1
	var possitions = 0
	for(var i=1; i<=8; i++){
		if(document.getElementById('cell-' + data + '-' + number)){
			possitions = possitions + 1
			number = number + 18
		}
		else{i=8}
	}
	return possitions
}
function scrollCurrent(data){
	var possition = document.getElementById('scrollDiv-' + data).scrollLeft + 700
	var current = possition / 700
	return current
}
