var mainContent = null;
var loading = null;
var module = "Core";
var page = "Core";
var elementID = 0;
var anchor = null;
var fadeDuration = 1;
var addToHistory = false;

function navigate(m, p, e, a, history) {
	if(history == null) {
		addToHistory = false;
	} else {
		addToHistory = history;
	}
	
	if(mainContent == null) {
		mainContent = document.getElementById('mainContent');
	}
	
	if(loading == null) {
		loading = document.getElementById('loading');
	}
	
	module = m;
	page = p;
	elementID = e;
	anchor = a;
	
	var anim = new YAHOO.util.Anim('mainContent', { opacity: { to: 0 } }, fadeDuration, YAHOO.util.Easing.easeOut);
	anim.onComplete.subscribe(fadeOutComplete);
	anim.animate();

	return false;
}

function generateLink(module, page, elementID, text, anchor) {
	var link = '<a href="' + site_link + '/' + module + '/' + page + '/' + elementID + '/';
	if(anchor != null && anchor != "") {
		link += '#' + anchor;
	}
	link += '" onclick="return navigate(\'' + module + '\',\'' + page + '\',\'' + elementID + '\', \'' + anchor + '\');">' + text + '</a>';
	return link;
}

function fadeOutComplete() {
	if(mainContent == null) {
		mainContent = document.getElementById('mainContent');
	}
	if(loading == null) {
		loading = document.getElementById('loading');
	}
	mainContent.style.display = "none";
	loading.style.display = "block";
	AjaxNavigationObject.startRequest(module, page, elementID, site_root, anchor);
}

function fadeIn() {
	if(loading == null) {
		loading = document.getElementById('loading');
	}
	if(mainContent == null) {
		mainContent = document.getElementById('mainContent');
	}
	loading.style.display = "none";
	mainContent.style.display = "block";
	var anim = new YAHOO.util.Anim('mainContent', { opacity: { to: 100 } }, fadeDuration, YAHOO.util.Easing.easeIn);
	anim.animate();
	
	if(!addToHistory) {
		// Add to AJAX navigation history
		var ajaxNavigationObject = new Object();
		ajaxNavigationObject.module = module;
		ajaxNavigationObject.page = page;
		ajaxNavigationObject.elementID = elementID;
		ajaxNavigationObject.anchor = anchor;
		var key = "";
		if(anchor != "" && anchor != undefined && anchor != "undefined") {
			key = module + ':' + page + ':' + elementID + ':' + anchor;
		} else {
			key = module + ':' + page + ':' + elementID + ':top';
		}
		dhtmlHistory.add(key, ajaxNavigationObject);
	}
}

/*
 * AjaxObject is a hypothetical object that encapsulates the transaction
 *     request and callback logic.
 *
 * handleSuccess( ) provides success case logic
 * handleFailure( ) provides failure case logic
 * processResult( ) displays the results of the response from both the
 * success and failure handlers
 * call( ) calling this member starts the transaction request.
 */

var AjaxNavigationObject = {
	anchor:"",
	handleSuccess:function(o){
		// This member handles the success response
		// and passes the response object o to AjaxObject's
		// processResult member.
		var mainContent = document.getElementById('mainContent');
		var html = '<a name="' + module + ':' + page + ':' + elementID + ':top"></a>';
		html += o.responseText;
		mainContent.innerHTML = html;
		fadeIn();
		/*if(this.anchor != "" && this.anchor != undefined && this.anchor != "undefined") {
			location.hash = "#" + this.anchor;
		} else {
			location.hash = "#top";
		}*/
		this.callAlert(o);
	},

	handleFailure:function(o){
		// Failure handler
		var mainContent = document.getElementById('mainContent');
		mainContent.innerHTML = "There was an error retrieving that content. Please try again.";
		fadeIn();
	},

	processResult:function(o){
		// This member is called by handleSuccess
	},

	startRequest:function(module, page, elementID, site_root, anchor) {
		this.anchor = anchor;
		YAHOO.util.Connect.asyncRequest('POST', site_root + 'modules/' + module + '/' + page + '.php', ajaxNavigationCallback, 'elementID=' + elementID);
	}

};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var ajaxNavigationCallback =
{
	success:AjaxNavigationObject.handleSuccess,
	failure:AjaxNavigationObject.handleFailure,
	scope: AjaxNavigationObject
};



var AjaxLoginObject = {
	resultContainer:null,
	handleSuccess:function(o){
		var response = o.responseText;
		if(response.indexOf('error') >= 0) {
			if(this.resultContainer != null) {
				this.resultContainer.innerHTML = response.replace('error:', '');
			}
			document.getElementById('loginForm').style.display = "block";
			document.getElementById('loginInfo').style.display = "none";
		} else {
			if(this.resultContainer != null) {
				this.resultContainer.innerHTML = '';
			}
			response = response.replace('success:', '');
			var userID = response.substr(0, response.indexOf(":"));
			var username = response.substr(response.indexOf(":") + 1, response.length - response.indexOf(":") + 1);
			document.getElementById('loginForm').style.display = "none";
			document.getElementById('loginInfo').innerHTML = 'You are logged in as ' + username + '<br />(' + generateLink("Users", "ViewBio", userID, "profile", "") + ') (<a href="' + site_root + 'modules/Users/logout.php" onclick="return ajaxLogout();">logout</a>)<br><br>';
			document.getElementById('loginInfo').style.display = "block";
		}
		this.callAlert(o);
	},

	handleFailure:function(o){
		if(this.resultContainer != null) {
			this.resultContainer.innerHTML = "An error occured while attempting to login";
		}
		document.getElementById('loginForm').style.display = "block";
		document.getElementById('loginInfo').style.display = "none";
	},

	processResult:function(o){
		// This member is called by handleSuccess
	},

	startRequest:function(login, password, autoLogin, resultContainerID) {
		if(resultContainerID != null && resultContainerID != "") {
			this.resultContainer = document.getElementById(resultContainerID);
		}
		var params = 'login=' + login + '&password=' + password + '&autoLogin=';
		if(autoLogin) {
			params += "true";
		} else {
			params += "false";
		}
		params += "&ajaxLogin=true";
		YAHOO.util.Connect.asyncRequest('POST', site_root + 'modules/Users/login.php', ajaxLoginCallback, params);
	}

};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var ajaxLoginCallback =
{
	success:AjaxLoginObject.handleSuccess,
	failure:AjaxLoginObject.handleFailure,
	scope: AjaxLoginObject
};

function ajaxLogout() {
	YAHOO.util.Connect.asyncRequest('POST', site_root + 'modules/Users/logout.php', null, "noRedirect=true");
	document.getElementById('loginForm').style.display = "block";
	document.getElementById('loginInfo').style.display = "none";
	return false;
}

function initializeAjaxNavigation() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(handleHistoryChange);
}
YAHOO.util.Event.addListener(window, "load", initializeAjaxNavigation); 
 
/** Our callback to receive history 
change events. */
function handleHistoryChange(newLocation, historyData) {
	var historyMsg = historyData;
	
	if (newLocation == "") {
		navigate("Core", "Core", "0", null, true);
	} else {
		navigate(historyData.module, historyData.page, historyData.elementID, historyData.anchor, true);
	}
}