/********************************/
/******* DECLARE CLASSES ********/
/********************************/

/**
 * CLASS: AjaxCpx
 * DESCR: A customized, extended version of the Ajax class, containing some ajax helper functionality
 * USAGE:
 * EXMPL:

		new AjaxCpx('/ajax/collegeCenter/processStudentCollege.jsp', {

			onSuccess : function () {

				this.referenceObj.collegeCenter.pulseElem(this.referenceObj.interestLevelElem);

			},

			// this assumes that this AjaxCpx object is being instantiated in a method inside the collegeCenter object,
			// and that tile.interestLevelElem is available to that method
			referenceObj : {
				collegeCenter : this,
				interestLevelElem : tile.interestLevelElem
			}

		}).request(tile);

 */
var AjaxCpx = Ajax.extend({

	referenceObj : null, // on initialize, you can optionally pass in a object to be referenced in an onSuccess function, for example, (since I can't get .bind(this) to work, this might be useful when attempting to reference a containing class
	indicator : null, // new addition, not used by older code
	hideIndicatorOnSuccess : true,

	/*
	 *
	 */
	initialize : function (url, options) {

		if (options) {
			this.referenceObj = options.referenceObj;
		}
		
		this.parent(url, options);

		// not working for some reason:		this.addEvent('request', this.onRequest); 
	},

	/**
	 * Builds an indicator and inserts it after the specified element
	 * @param elemId {String} The id of the element to which which you'd like to attach the indicator
	 * @param where {String} Where to place the indicator relative to elemID, passed to Element.inject() method - 'before', 'after', or 'top', default is appendChild (inside)
	 * @return The new indicator {Element} object
	 * @type Element
	 */
	buildIndicator : function(elemId, where) {
		var indicatorElem = new Element('span', {
			'id' : elemId + 'Indicator',
			'styles': {
				'display': 'none'
			 },
			 'class': 'ajaxIndicator'
		});
		// part of mootools retrofit to mootools v1.11
		indicatorElem.setAttribute('class', 'ajaxIndicator');
		// part of mootools retrofit to mootools v1.11
		indicatorElem.setStyles({'display': 'none'});
		// part of mootools retrofit to mootools v1.11
		if ($defined($(elemId))) {
			indicatorElem.inject($(elemId), where);
//		} else {
//			global.log('ajax.buildIndicator attempt on an non-existent elem')
		}
		this.indicator = indicatorElem;
		return indicatorElem;
	},

/**
 * Builds an indicator with a complex FX (hence creates an img instead of a span with a background),
 * and inserts it after the specified element.
 * @param elemId {String} The id of the element to which which you'd like to attach the indicator
 * @param where {String} Where to place the indicator relative to elemID, passed to Element.inject() method - 'before', 'after', or 'top', default is appendChild
 * @param src {String} (optional) The url of the image to use. Defaults to bigGreenArrow.gif
 * @return The new indicator {Element} object
 * @type Element
 */
/* switch back to this (if you want) when upgraded to mootools v1.11 (MOOCAP)
	buildIndicatorDone : function(elemId, where, src) {
		if (!src) {
			src = '/img/icons/bigGreenArrow.gif';  // default src
		}
		var indicatorElem = new Element('img', {
			'id' : elemId + 'IndicatorDone',
			'src' : src,
			'styles': {
				'display': 'none'
			},
			'class': 'ajaxIndicatorDone'
		});
*/
	buildIndicatorDone : function(elemId, where, src) {
		if (!src) {
			src = '/img/icons/bigGreenArrow.gif';
			// default src
		}
		var indicatorElem = new Element('img', {
			'id' : elemId + 'IndicatorDone',
			'src' : src,
			'styles': {
				'display' : 'none',
				'position' : 'absolute'
			},
			'class': 'ajaxIndicatorDone'
		});
		// part of mootools retrofit to mootools v1.11
		indicatorElem.setAttribute('class', 'ajaxIndicatorDone');
		// part of mootools retrofit to mootools v1.11
		// $('hsNameIndicatorDone').style.position='absolute'
		indicatorElem.style.position = 'absolute';
		indicatorElem.setStyles({'display': 'none'});
		// part of mootools retrofit to mootools v1.11
		indicatorElem.inject($(elemId), where);
		return indicatorElem;
	},

	// not working for some reason
	onRequest : function() {

		global.trace('AjaxCpx.onRequest');

		if (this.indicator != null) {
			global.trace('AjaxCpx.onRequest -> setStyle()');
			this.indicator.setStyle('display', 'block');
		}
		this.parent();
	},

	onSuccess : function() {

		global.trace('AjaxCpx.onSuccess');

		if (this.indicator != null && this.hideIndicatorOnSuccess) {
			this.indicator.setStyle('display', 'none');
		}
		this.parent();
	},

	// default onFailure behavior
	onFailure : function () {
		switch (this.transport.status) {
			case 401:
				alert("Your session has timed out. Please log back in and try again.")
				break;
			default:

				var a = "We're sorry, but we were unable to process your request.  There is a problem with either your internet connection or our server.  Please check your internet connection and try again. If you continue to have difficulties, please contact cappexsite_cs";
				var b = "@";
				var c = "cappex.com with the following message: Error " + this.transport.status + ": " + this.transport.statusText;
				alert(a + b + c);

				break;
		}
		// this.parent();
	}

});

/********************************/
/***** DECLARE GLOBAL VARS ******/
/********************************/

// probably not a good idea to use this until its request-queuing process has been explored: Global.extend({ajax : new AjaxCpx()});

/********************************/
/******** EVENT HANDLERS ********/
/********************************/

// NOTE: The following is likely redundant with the same statement in global.js. I believe ajax.js SHOULD never be used without first including global.js first.
// However, it's possible that just ajax.js is included somewhere, necessitating the duplicate code here. - KGH 2008-01-15
// browser-specific: adds a body class to target safari
window.addEvent('domready', function() {
	if (global.isSafari2down) {
		$$('body').addClass('safari2');
	}
});

/********************************/
/********** FUNCTIONS ***********/
/********************************/