// ajax dynamic content file
// created from tutorial document at http://dhtmlgoodies.com/index.html?whichTutorial=ajax-basics
// NOTE: code in this file requires ajax_sack.js to be included prior to loading this file

// create a new global variable for the ajax SACK object
var dynamicContent_ajaxObjects = new Array();

// add some caching funcionality for multiple requests and to prevent duplicate requests
var jsCache = new Array();

// enable/disable caching
var enableCache = true;

// main page ajax function that takes the div id where the loaded content should go as well as the url to retrieve
function ajax_loadContent(divId,pathToFile) {
	// if cache is enabled and an entry is found in the cache array for this url (path), output the content from cache
	if(enableCache && jsCache[pathToFile]){
		// send the cached content to the div
		document.getElementById(divId).innerHTML = jsCache[pathToFile];
		
		// all done, break out of the function
  	return; 
	}
	// no cached content for this path, open and fetch the url/path
	else {
		// get the index of our new array element
		var ajaxIndex = dynamicContent_ajaxObjects.length;
		
		// output a temporary message in the output div
		// NOTE: THIS LINE BREAKS IE SCRIPTS SO REMOVING (IE 6 & 7)
		//document.getElementById(divId).innerHTML = "<p><em>Loading content...</em></p>";
		//document.getElementById('neighbourhood_dropdown').innerHTML = "<p><em>Loading content...</em></p>";
		
		// Create a sack object:
		dynamicContent_ajaxObjects[ajaxIndex] = new sack();
		
		// Define requestFile:
		dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;
		
		// Define onCompletion method:
		dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ ajax_showContent(divId,ajaxIndex,pathToFile); };
		/* from the tutorial: We have defined onCompletion as a dynamic function. 
		When the Ajax request is completed, it will call a function named 
		ajax_showContent. We need to create this function in order to show 
		the response from Ajax inside our div. */
		
		// execute the ajax call
		dynamicContent_ajaxObjects[ajaxIndex].runAJAX();
	}
}

// This is the function that would be executed by our Ajax objects when it is finished returning content from the server. 
// The purpose of this function is to put content into our divs and to update our Javascript cache array.
function ajax_showContent(divId,ajaxIndex,pathToFile) {
	// populate the div with our returned content
	document.getElementById(divId).innerHTML = dynamicContent_ajaxObjects[ajaxIndex].response;
	
	// if caching enabled, update our cache array
	if(enableCache) {
  	jsCache[pathToFile] = dynamicContent_ajaxObjects[ajaxIndex].response;
	}
	
	// clean the ajax object from memory since we're done with it
	dynamicContent_ajaxObjects[ajaxIndex] = false;
	
	
}