// Variables needed for Rollovers
var W3CDOM = (document.createElement && document.getElementsByTagName);
var finished = 'yes';
var over = 'no';
var styleSheetElement;
var oldElement;
var lastDisplayedMenu;
var lastMenu = "";
var beenCalled;


// Find out if we need to serve the live site or the dev site
str = document.location + "";
re = /dev/i; 
if(str.search(re) != -1) {
	var whichEnv = '/dev/';
} else {
	var whichEnv = '/';
}


// Image Objects
aboutOn = new Image();
aboutOn.src = whichEnv + "images/nav/nav_about_on.png";
aboutOff = new Image();
aboutOff.src = whichEnv + "images/nav/nav_about_off.png";

programsOn = new Image();
programsOn.src = whichEnv + "images/nav/nav_programs_on.png";
programsOff = new Image();
programsOff.src = whichEnv + "images/nav/nav_programs_off.png";

communityOn = new Image();
communityOn.src = whichEnv + "images/nav/nav_community_on.png";
communityOff = new Image();
communityOff.src = whichEnv + "images/nav/nav_community_off.png";

contactOn = new Image();
contactOn.src = whichEnv + "images/nav/nav_contact_on.png";
contactOff = new Image();
contactOff.src = whichEnv + "images/nav/nav_contact_off.png";


// function that turns on the main nav graphics as well as the menus
function menuOn(whichMenu) {
	
	// Make sure the page has loaded
	if (finished == 'yes') {
		
		// set over to show that the user is now over a menu
		over = 'yes';

		// This section of code is executed for wc3 css compliant browsers
		// Execute the following if this is not the first time this function has been called
		if (beenCalled == 'yes')  {
									
			// These lines gather the menu element which we want to access
			lastDisplayedMenu = lastMenu + 'Menu';
			oldElement = document.getElementById(lastDisplayedMenu);
			
			// Turn off any currently displayed menu and image
			if((lastMenu != 'quickLinks') && (lastMenu != 'intlSites')) {
				if (currentSection != lastMenu) eval('document.images[lastMenu].src = ' + lastMenu + 'Off.src');
			}
			oldElement.style.visibility = "hidden";	
					
		}
		
		// These lines gather the menu element which we want to access
		layerName = whichMenu + 'Menu';
		styleSheetElement = document.getElementById(layerName);	
		
		// Turn on the menu and image which need now need to be displayed		
		styleSheetElement.style.visibility = "visible";
		if ((whichMenu != 'quickLinks') && (whichMenu != 'intlSites')) {
			if (currentSection != whichMenu) eval('document.images[whichMenu].src = ' + whichMenu + 'On.src');
		}

		// set lastDisplayed Array to hold names of menus turned on for use next time function is called
		lastMenu = whichMenu;	

		beenCalled = 'yes';
	}
}


// Timer function that is run to give the user time to get to the menu from the button
function overChecker(whichMenu) {
	if ((over == 'yes') && (finished == 'yes')) {
	
		//gather the names of all the menus that you want on into the currentMenus Array
		lastMenu = whichMenu;
		over = 'no';
		setTimeout("menuOff()", 900);
	}
}

// function that turns off the main nav graphics as well as the menus
function menuOff() {
	if (over == 'no')  {
		// These lines gather the menu element which we want to access			
		lastDisplayedMenu = lastMenu + 'Menu';
		oldElement = document.getElementById(lastDisplayedMenu);
				
		// Turn off any currently displayed menu and image
		oldElement.style.visibility = "hidden";	
		if ((lastMenu != 'quickLinks') && (lastMenu != 'intlSites')) {
			if (currentSection != lastMenu) eval('document.images[lastMenu].src = ' + lastMenu + 'Off.src');
		}
	}	
}

// Image replace On Function	
function on(pic) {
	if (document.images) { 
		eval('document.images[pic].src = ' + pic + 'On.src');
	}
}
        
// Image replace Off Function	
function off(pic) {     
	if(document.images) {
		eval('document.images[pic].src = ' + pic + 'Off.src');
	}	
}

function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	var fakeInput = document.createElement('input')
	fakeInput.setAttribute('class', 'filebox');
	fakeFileUpload.appendChild(fakeInput);
	var image = document.createElement('img');
	image.src='/dev/images/buttons/btn_form_select.png';
	image.style.verticalAlign = 'middle';
	image.style.marginLeft = '10px';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

// Ajax Variables
var request;
var dest;



// Function to update Database value
function updateDatabase(fieldName, tableName, newValue, fid) {
	var contentElement = document.getElementById('updateDb');
	contentElement.innerHTML = "Saving Change";
	
	if(tableName == "kid") {
		var separator = '-';
		valueArray = fieldName.split(separator);
		fieldName = valueArray[0];
		var kidId = valueArray[1];
	}
	var URL = 'update_database.php?fieldName=' + fieldName + '&tableName=' + tableName + '&newValue=' + newValue + '&fid=' + fid;
	if(tableName == "kid") {
		URL += '&kidId=' + kidId;
	}
	loadHTML(URL, 'updateDb');
}

// Function to process state changes
function processStateChange(){
    if (request.readyState == 4){
        contentDiv = document.getElementById(dest);
        if (request.status == 200){
            response = request.responseText;
            contentDiv.innerHTML = response;
        }  else {
            contentDiv.innerHTML = "Error: Status "+request.status;
        }
    }
}

// Function to load new page with ajax
function loadHTML(URL, destination){
    dest = destination;
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
        request.onreadystatechange = processStateChange;
        request.open("GET", URL, true);
        request.send(null);
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
            request.onreadystatechange = processStateChange;
            request.open("GET", URL, true);
            request.send();
        }
    }
}
function ajaxFileUpload(whichFile, whichId, whichType)	{
		$("#" + whichFile + "loading")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});

		$.ajaxFileUpload
		(
			{
				
				url:'doajaxfileupload.php',
				secureuri:false,
				fileElementId:whichFile,
				idElementId:whichId,
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							var contentElement = document.getElementById(whichFile + 'Photo');
							contentElement.innerHTML = '<img src="' + whichEnv + 'images/' + whichType + '/' + data.msg + '" />';

						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		)
		
		return false;

	}