
var siteLiveFieldTimeouts = new Array();

var eContent = new Array();

var eUpdating = new Image( 11, 11 );
eUpdating.src = "/images/animations/updating.gif";
eUpdating.alt = "updating...";

/**
 *  handles calls from ajax_link's
 *
 *  @param sUrl url of resource
 *  @param sUpdate id of element to update
 *  @param sHandler optional name of handler function
 *  @return nothing
 *
 */

function site_ajaxLink( sUrl, sUpdate, sHandler ) {
	site_ajaxCall( sUrl, sUpdate, sHandler, null );
}

/**
 *  handles calls from ajax_form's
 *
 *  @param oForm the form to submit
 *  @param sUrl the url of the resource
 *  @param sUpdate id of element to update
 *  @param sHandler optional custom handler function
 *  @param sParameters request params
 *  @param sFeedback id of feedback element
 *  @return nothing
 *
 */

function site_ajaxForm( oForm, sUrl, sUpdate, sHandler, sParameters, sFeedback ) {
	Element.extend( oForm );
	site_ajaxCall( sUrl, sUpdate, sHandler, oForm.serialize(), sFeedback );
}

/**
 *  makes an ajax call.
 *
 *  @param sUrl the url resource
 *  @param sUpdate the element id to receive the response
 *  @param sHandler optional function to handle the response
 *  @param sParameters any params for the request
 *  @param sFeedback optional id of element for feedback
 *  @return nothing
 *
 */

function site_ajaxCall( sUrl, sUpdate, sHandler, sParameters, sFeedback ) {

	// get a reference to the handler function
	var rHandler = null;

	if ( $( sUpdate ) && !$( sUpdate ).visible() )
	    $( sUpdate ).toggle();

	if ( !sHandler )
	    sHandler = 'site_ajaxUpdate';
	eval( 'rHandler = ' + sHandler + ';' );

	new Ajax.Request(
	    sUrl,
	    {
		//method: 'get',
	        parameters: sParameters,
	        onLoading: function() {
		    if ( sFeedback )
			site_ajaxFeedback( sFeedback, '...' );
	        },
	        onSuccess: function( oRequest ) {
		    if ( sFeedback )
			site_ajaxFeedback( sFeedback, '' );
		    rHandler( oRequest, sUpdate );
	        },
	        onFailure: function( oRequest ) {
		    alert( 'Ajax error' );
	        }
	    }
	);

}

/**
 *  sets a feedback element with some content.  if you want
 *  to clear the feedback then just pass the empty string.
 *
 *  @param sId id of element
 *  @param sContent the contect to set
 *  @return nothing
 *
 */

function site_ajaxFeedback( sId, sContent ) {
	var eElem = $( sId );
	if ( eElem ) {
	    if ( !eContent[sId] ) { 
		eContent[sId] = eElem.innerHTML;
		$$( 'input.submit' ).invoke( 'writeAttribute', 'disabled', 'disabled' );
		eElem.update( eUpdating );
		$$( '.active' ).invoke( 'removeClassName', 'active' );
	    }
	    else {
		$$( 'input.submit' ).invoke( 'writeAttribute', 'disabled' );
		eElem.innerHTML = eContent[sId];
		eElem.addClassName( 'active' );
		eContent[sId] = false;
	    }
	}
}

/**
 *  this is the default handles for ajax queries and just
 *  updates the element id specified by sUpdate with the
 *  response of the query
 *
 *  @param oRequest the request object
 *  @param sUpdate id of element to update
 *  @return nothing
 *
 */

function site_ajaxUpdate( oRequest, sUpdate ) {

	var eElem = $( sUpdate )

	if ( eElem ) {

		var sResult = new String( oRequest.responseText );

		eElem.innerHTML = sResult;
		sResult.evalScripts();

	}

	else alert( 'AjaxError: element "' +sUpdate+ '" does not exist...' );

}

/**
 *  a function that just does nothing.  used as a function when
 *  no action is required (but we can still pass a valid function
 *  to whatever wants it.
 *
 *  @return nothing
 *
 */

function site_doNothing() {
	return true;
}

/**
 *  called when a live field changes
 *
 *  @param sUrl the url to call on change
 *  @param sUpdate the element to update
 *  @param oField the field to watch
 *  @param sFeedback id of feedback element
 *  @return nothing
 *
 */

function site_ajaxLiveFieldChange( sUrl, sUpdate, oField, sFeedback ) {
	// clear any timeouts
	if ( timeout == siteLiveFieldTimeouts[oField.id] )
		clearTimeout( timeout );
	// set a timeout
	siteLiveFieldTimeouts[oField.id] = setTimeout( function() {
		site_ajaxCall( sUrl + '/' + escape(oField.value), sUpdate, null, null, sFeedback );
	}, 1000 );
}
