/********************************************************************************
*	Copyright (c)2007, Ryan Provost, Inc.
*	http://www.ryanprovost.com
*   
*	This file is part of [RPI]Ajax_Core.
*	
*	[RPI]Ajax_Core is free software: you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation, either version 3 of the License, or
*	(at your option) any later version.
*	
*	Foobar is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	
*	You should have received a copy of the GNU General Public License
*	along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/

/***
*	AJAX Initialization
**/
var ajax				= new Array();
var history_array		= new Array();

/***
*	AJAX Custom Variables (Feel Free to Modify)
**/
var history_count		= 0;
var loading_msg			= "Loading...";

ajax['path']			= "pages/";
ajax['append']			= ".html";
ajax['default_div']		= "copy";
ajax['default_page']	= "home";
ajax['default_method']	= "GET";
ajax['iframe_id']		= "rpi_ajax_frame";


/***
*	AJAX Core Function
*
*	Note: You should not need to edit beyond here
**/
function load_ajax(ajax_page, ajax_div, ajax_method) {
	// Trigger Any Proprietary Code
	ajax_external_triggers ( ajax_page );

	// Set AJAX Variables
	var ajax_referer	= (ajax_page)	? ajax_page : ajax['default_page'];
	var ajax_page		= (ajax_page)	? ajax['path']+ajax_page+ajax['append'] : ajax['path']+ajax['default_page']+ajax['append'];
	var ajax_div		= (ajax_div)	? ajax_div		: ajax['default_div'];
	var ajax_method		= (ajax_method)	? ajax_method	: ajax['default_method'];

	// AJAX Loader Begin
	var xmlHttp;
	try	{ xmlHttp=new XMLHttpRequest(); }
	catch (e) {
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e) { alert("Your browser does not support AJAX!"); return false; }
		}
	}

	xmlHttp.onreadystatechange = function() {
		if ( xmlHttp.readyState == 4 )
			document.getElementById(ajax_div).innerHTML	= loading_msg;
	
		if ( xmlHttp.readyState == 4 ) {
			// Increment the history counter
			history_count++;

			// Set array variables for history
			history_array[history_count] = new Array();
			history_array[history_count]['content']	= xmlHttp.responseText;
			history_array[history_count]['div']		= ajax_div;
			history_array[history_count]['page']	= ajax_referer;

			// Display requested content
			document.getElementById(ajax_div).innerHTML	= xmlHttp.responseText;

			// Move "blank.html" forward
			document.getElementById(ajax['iframe_id']).src = "blank.html?"+history_count;
		}
	}

	// Load requested document
	xmlHttp.open(ajax_method, ajax_page, true);
	xmlHttp.send("");
}


/***
*	Handles Back/Forward Calls
*	
*	Note: This function is called from "blank.html"
**/
function set_history ( history_id ) {
	var div = history_array[history_id]['div'];

	ajax_external_triggers ( history_array[history_id]['page'] );
	document.getElementById(div).innerHTML = history_array[history_id]['content'];
}

