//configure this script
var ajaxHandlerRelativeLocation = 'php_functionality/ajax_handler.php';
var loadingImageRelativeLocation = 'html_functionality/images/loading.gif';

//construct the full url to the module
var urlPieces = window.location.href.split("/");
var moduleLocation = '';
for(var i = 0; i < 4; i++)
	moduleLocation += urlPieces[i] + '/';

//add complete locations to above relative paths
var ajaxHandlerLocation = moduleLocation + ajaxHandlerRelativeLocation;
var loadingImageLocation = moduleLocation + loadingImageRelativeLocation;

//preload the loading gif
var preloadImage = new Image();
preloadImage.src = loadingImageLocation;

var blog = {
	doAjax: function (idPrefix, params, onSuccessFunction, onFailureFunction) {
		new Ajax.Request(ajaxHandlerLocation, {method: 'get', parameters: params, onSuccess: function(t) { onSuccessFunction(idPrefix, t); }, onFailure: onFailureFunction});
		return false;
	},
	doAjaxContentLoading: function (idPrefix, params) {
		//cancel all in effect queue

		//Effect.Queue.each(function(e) { e.cancel() });

		if($(idPrefix+'Loading'))
			$(idPrefix+'Loading').parentNode.removeChild($(idPrefix+'Loading'));
		if($(idPrefix+'Content'))
			$(idPrefix+'Content').parentNode.removeChild($(idPrefix+'Content'));

		//create the loading and content divs
		var temp;
		temp = document.createElement("div");
		temp.setAttribute('id',idPrefix+'Loading');
		$(idPrefix).appendChild(temp);
		temp = document.createElement("div");
		temp.setAttribute('id',idPrefix+'Content');
		$(idPrefix).appendChild(temp);

		//show only the loading image
		Element.hide(idPrefix+'Content');
		Element.show(idPrefix+'Loading');
		$(idPrefix+'Loading').innerHTML = '<center><img src="'+loadingImageLocation+'" alt="Loading..." /></center>';

		//perform ajax call to retrieve form
		new Ajax.Request(ajaxHandlerLocation, {method: 'get', parameters: params, onSuccess: function(response) { blog.divContentLoading.onSuccess(idPrefix, response); }, onFailure: function(response) { blog.divContentLoading.onFailure(idPrefix, response); }});

		//return false to stop browser from following link
		return false;
	},
	divContentLoading: {
		onSuccess: function (idPrefix, response) {

			var rootNode = response.responseXML.documentElement;
			var status = rootNode.getElementsByTagName('view')[0].getElementsByTagName('status')[0].firstChild.nodeValue;
			var message = rootNode.getElementsByTagName('view')[0].getElementsByTagName('message')[0].firstChild.nodeValue;

			//hide loading image and show form
			if(status == "succeeded")
				$(idPrefix+'Content').innerHTML = message;
			else if(status == "failed")
				$(idPrefix+'Content').innerHTML = "Failed";
			else
				$(idPrefix+'Content').innerHTML = "Invalid response from server";

			Effect.BlindUp(idPrefix+'Loading', {duration:1, queue:'end'});
			Effect.SlideDown(idPrefix+'Content', {duration:1, queue:'end'});
		},
		onFailure: function (idPrefix, response) {
			$(idPrefix).innerHTML = "Cannot communicate with server.";
		}
	},

	doAjaxContentKilling: function (idPrefix, params) {
		//cancel all in effect queue
		//Effect.Queue.each(function(e) { e.cancel() });

		//create the loading and content divs
		if($(idPrefix+'Loading'))
			$(idPrefix).removeChild($(idPrefix+'Loading'));
		if($(idPrefix+'Message'))
			$(idPrefix).removeChild($(idPrefix+'Message'));

		var temp;
		temp = document.createElement("div");
		temp.setAttribute('id',idPrefix+'Loading');
		$(idPrefix).appendChild(temp);
		temp = document.createElement("div");
		temp.setAttribute('id',idPrefix+'Message');
		$(idPrefix).appendChild(temp);

		//show only the loading image
		Element.show(idPrefix+'Loading');
		$(idPrefix+'Loading').innerHTML = '<center><img src="'+loadingImageLocation+'" alt="Loading..." /></center>';

		//perform ajax call to retrieve form
		new Ajax.Request(ajaxHandlerLocation, {method: 'get', parameters: params, onSuccess: function(response) { blog.divContentKilling.onSuccess(idPrefix, response); }, onFailure: function(response) { blog.divContentKilling.onFailure(idPrefix, response); }});

		//return false to stop browser from following link
		return false;
	},
	divContentKilling: {
		onSuccess: function (idPrefix, response) {
			//hide loading image and show form
			//Effect.SlideUp(idPrefix+'Loading', {duration:1, queue:'end'});
			var rootNode = response.responseXML.documentElement;
			var status = rootNode.getElementsByTagName('controller')[0].getElementsByTagName('status')[0].firstChild.nodeValue;
			var message = rootNode.getElementsByTagName('controller')[0].getElementsByTagName('message')[0].firstChild.nodeValue;
			var errorMessage = rootNode.getElementsByTagName('controller')[0].getElementsByTagName('errorMessage')[0].firstChild.nodeValue;

			if(status == "failed")
				$(idPrefix+'Message').innerHTML = errorMessage + "Failed";
			else if(status == "succeeded")
				$(idPrefix).innerHTML = message;
			else
				$(id+'Message').innerHTML = "Invalid response from server";

			Effect.SlideUp(idPrefix+'Loading', {duration:1, queue:'end'});

		},
		onFailure: function (idPrefix, response) {
			$(idPrefix).innerHTML = "Cannot communicate with server.";
		}
	},

	doAjaxFormSubmitting: function (formId, contentDestinationId, params) {
		//cancel all in effect queue
		//Effect.Queue.each(function(e) { e.cancel() });

		//$(id).innerHTML = "";
		if($(formId+'Loading'))
			$(formId+'Loading').parentNode.removeChild($(formId+'Loading'));
		if($(formId+'Message'))
			$(formId+'Message').parentNode.removeChild($(formId+'Message'));

		//create the loading and content divs
		var temp;
		temp = document.createElement("div");
		temp.setAttribute('id',formId+'Loading');
		$(formId).appendChild(temp);
		temp = document.createElement("div");
		temp.setAttribute('id',formId+'Message');
		$(formId).appendChild(temp);

		//show only the loading image
		Element.show(formId+'Loading');
		$(formId+'Loading').innerHTML = '<center><img src="'+loadingImageLocation+'" alt="Loading..." /></center>';

		//perform ajax call to retrieve form
		var nameAndValue;
		var formParams = '';
		var formVars = Form.serialize(formId).split("&");
		for(var i = 0; i < formVars.length; i++)
		{
			nameAndValue = formVars[i].split('=');
			formParams += '&controllerParams[form][' + nameAndValue[0] + ']=' + nameAndValue[1];
		}
		new Ajax.Request(ajaxHandlerLocation, {method: 'get', parameters: params+formParams, onSuccess: function(response) { blog.divFormSubmitting.onSuccess(formId, contentDestinationId, response); }, onFailure: function(response) { blog.divFormSubmitting.onFailure(formId, response); }});
		return false; //return false to stop browser from following link
	},
	divFormSubmitting: {
		onSuccess: function (formId, contentDestinationId, response) {
			//hide loading image and show form
			var rootNode = response.responseXML.documentElement;
			var status = rootNode.getElementsByTagName('controller')[0].getElementsByTagName('status')[0].firstChild.nodeValue;
			var controllerMessage = rootNode.getElementsByTagName('controller')[0].getElementsByTagName('message')[0].firstChild.nodeValue;
			var viewMessage = rootNode.getElementsByTagName('view')[0].getElementsByTagName('message')[0].firstChild.nodeValue;
			var errorMessage = rootNode.getElementsByTagName('controller')[0].getElementsByTagName('errorMessage')[0].firstChild.nodeValue;

			if(status == "failed")
				$(formId+'Message').innerHTML = errorMessage + "Failed";
			else if(status == "succeeded")
			{
				if($(contentDestinationId))
				{
					var temp = document.createElement("div");
					Element.hide(temp);
					temp.innerHTML = viewMessage;
					prependChild(temp, $(contentDestinationId));
					Effect.SlideDown(temp, {duration:1, queue:'end'});
				}
				$(formId).innerHTML = controllerMessage;
			}
			else
				$(formId+'Message').innerHTML = "Invalid response from server";

			Effect.SlideUp(formId+'Loading', {duration:1, queue:'end'});
		},
		onFailure: function (formId, response) {
			$(formId).innerHTML = "Cannot communicate with server.";
		}
	}
}