// ajaxEngine
// Revision 0.0.3
// Author: Chris Cowdery-Corvan (cjc5976)

// Initialize the AJAX request.
// First try Firefox/Safari, then the two IE AJAX ActiveX Objects.
var request = false;

// Initialize global variables, these are set by the site-specific getter methods
var DOMid = null; // The id of the specific element in the page/DOM to update
var delay = null; // The amount of time to delay the response (in milliseconds) - useful for timing transitions
var response = null; // The (raw) response from the server
var useLoadingGraphic = null; // The option to use a loading graphic or not while
var ajaxDSLhandler = null; // The specific handler to use in the AJAX Data Services Layer library

var DOMid2 = null; // The id of the specific element in the page/DOM to update
var delay2 = null; // The amount of time to delay the response (in milliseconds) - useful for timing transitions
var response2 = null; // The (raw) response from the server
var useLoadingGraphic2 = null; // The option to use a loading graphic or not while
var ajaxDSLhandler2 = null; // The specific handler to use in the AJAX Data Services Layer library

try {
	request = new XMLHttpRequest();
} catch (msie) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (msie2) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (nosupport) {
			request = false;
		}  
	}
}

// Alert the user if they don't have an AJAX compatible browser
if (!request) {
	alert("Warning: AJAX Engine was not able to instantiate properly. Site functionality may be limited.");
}

// Method to update the infomation on the page
// Recieves the DOMid and delay from an individual getter method
function updateData() {
			
	// Wait until the information is retreived successfully
	if (request.readyState == 4) {
		if (request.status == 200) {
			
			// Set the content of the specified DOM id to be the results of the output from the site-specific getter methods
			response = request.responseText;
			setTimeout("document.getElementById(DOMid).innerHTML = response;", delay);
				
		}
	} else {
		
		// Display a loading graphic if desired
		if (useLoadingGraphic) {
			document.getElementById(DOMid).innerHTML = "<img src=\"images/site/ajaxactivity.gif\">";	
		}
	}
}

function startRequest(id, writing) {
	
	// Open this url via GET
	var url = "scripts/ajaxDSL.php?id=" + escape(id) + "&handler=" + ajaxDSLhandler;
	if (writing){
		url += "&writing";
	}
	request.open("GET", url, true);
	
	// Tell the script to update the id (contactPane) with the new data when data is ready
	request.onreadystatechange = updateData;
	
	// Send the request to the DSL
	request.send(null);
}

/* ------------- SITE SPECIFIC METHODS ------------- */

// Grab the output from getContactInfo for the specified contact ID 
function getContactInfo(id, writing) {
	
	// Set the update parameters
	DOMid = "contactPane";
	delay = 300;
	useLoadingGraphic = true;
	
	ajaxDSLhandler = "contactInfo";
	startRequest(id, writing);
	
}

// WARNING: Asynchronous rquests don't work very well with ajaxEngine yet, so this is an interim solution
function changeContacts(departmentId) {
	
	// Set the update parameters
	DOMid = "contacts";
	delay = 300;
	useLoadingGraphic = false;
	
	// We'll be accessing the contactInfo method in the DSL, and sending the ID as it's parameter
	ajaxDSLhandler = "departmentContacts";
	
	startRequest(departmentId);
	
	// Update contact title
	if (departmentId == 1) {
		setTimeout("document.getElementById('departmentTitle').innerHTML = \" ASC Staff\";", delay);
	} else if (departmentId == 2) {
		setTimeout("document.getElementById('departmentTitle').innerHTML = \" The Writing Center\";", delay);
	} else if (departmentId == 3) {
		setTimeout("document.getElementById('departmentTitle').innerHTML = \" LSS Learning Specialists\";", delay);
	}
	
	// Clear the content on the right
	//new Effect.Opacity('contactPane', { from: 1.0, to: 0.0, duration: 0.3 });
	//setTimeout("document.getElementById('contactPane').innerHTML = \"\"", delay);
	
	new Effect.Opacity('contactPane', { from: 1.0, to: 0, duration: 0.3 });
	setTimeout("new Effect.Opacity('contactPane', { from: 0, to: 1.0, duration: 0.3 });", delay+200);
	setTimeout("document.getElementById('contactPane').innerHTML = \"<h6>Click a contact on the left to learn more.</h6>\";", delay);
	
	//setTimeout("document.getElementById('contactPane').innerHTML = \"Waiting\"", delay);
	//document.getElementById('contactPane').innerHTML = "<p>Hello</p>";
}


