/**
 * Team
 * 
 * Please check the javascript code after changes with JSlin
 * (http://jslint.com/). 
 * 
 * @author Chris Müller <mueller@cyperfection.de> 
 * @version $Id: team.js 375 2009-11-30 16:20:32Z christian $
 * @package typo3
 * @subpackage cyz_team
 */ 

/*jslint
	bitwise: true,
	browser: true,
	eqeqeq: true,
	immed: true,
	newcap: true,
	nomen: true,
	onevar: true,
	plusplus: true,
	regexp: true,
	undef: true
*/
/*global
	window,

	$,
	$$,
	$F,
	Ajax
*/



/***** Constants **************************************************************/

var CyzTeamConfig = {
	// Selector id prefix for the form tag which holds the pids
	formIdPrefix: 'cyz-team-pids-',

	// Selector id of the input field
	inputId: 'cyz-team-search',

	// Selector id of the suggest box
	suggestId: 'cyz-team-suggest',

	// minimum length for suggesting
	minLength: 3,

	// basic url for the ajax request
	url: '/index.php?eID=tx_cyzteam',

	// selector class for the email address
	emailClass: 'cyzteam-vcard-email'
};



/***** Processing *************************************************************/

function CyzTeam() {
	/**
	 * Needed page ids to search in
	 * @type String
	 */
	var pids;



	/**
	 * Transform email address from "email [at] example [...] com"
	 * to "email@example.com"	 
	 */
	function transformEmail() {
		/**
		 * found selector classes
		 * @type Element
		 */
		var classes,

		/**
		 * Old and new value of the email address
		 * @type String
		 */
		oldValue, newValue,

		/**
		 * Loop variables
		 * @type Number
		 */
		i,

		/**
		 * Helper: length
		 * @type Number
		 */
		len;


		classes = $$('.' + CyzTeamConfig.emailClass);
		len = classes.length;

		for (i = 0; i < len; i += 1) {
			oldValue = $(classes[i]).innerHTML;

			// get correct email address
			newValue = oldValue.replace(/ \[at\] /, '@');
			newValue = newValue.replace(/ \[[a-z]+\] /g, '.');

			// substitute email address
			$(classes[i]).update('<a href="mailto:' + newValue + '">' +
			                         newValue +
								  '</a>');
		}

	}



	/**
	 * Callback function on event "key up" of the input field
	 * @param {Event}
	 * @private
	 */
	function inputKeyUp(e) {
		/**
		 * value of input box
		 * @type String
		 */
		var value,

		/**
		 * url for ajax request
		 * @type String
		 */
		url;


		value = $F(CyzTeamConfig.inputId);

		if (value.length >= CyzTeamConfig.minLength) {
			// assemble url
			url = CyzTeamConfig.url +
			      '&q=' + value +
			      '&pids=' + pids;

			// fire ajax request
			(new Ajax.Request(
				url,
				{
					method: 'get',
					onSuccess: function(transport) {
						if (transport.responseText) {
							$(CyzTeamConfig.suggestId).update(transport.responseText);

							if (!$(CyzTeamConfig.suggestId).visible()) {
								$(CyzTeamConfig.suggestId).show();
							}
						} else {
							$(CyzTeamConfig.suggestId).update();
							if ($(CyzTeamConfig.suggestId).visible()) {
								$(CyzTeamConfig.suggestId).hide();
							}
						}
					},
					onFailure: function() {
						if ($(CyzTeamConfig.suggestId).visible()) {
							$(CyzTeamConfig.suggestId).hide();
						}
					}
				}
			));
		} else {
			$(CyzTeamConfig.suggestId).hide();
		}
	}


	/**
	 * Callback function on click on a member
	 * @param {Event}
	 * @private
	 */
	function clickMember(e) {
		/**
		 * Help variable for splitting selector id
		 * @type Number
		 */
		var idSplit,

		/**
		 * uid of the clicked members
		 * @type String
		 */
		uid,
		
		/**
		 * url to the new page
		 * @type String
		 */
		url;



		// stop event processing
		e.stop();

		// take the clicked member and put in into the input field
		$(CyzTeamConfig.inputId).value = e.element().innerHTML;

		// get selector id of the clicked item
		// at the end of the id is the uid of the member (separated with -)
		idSplit = e.element().identify().split('-');
		if (idSplit.length > 0) {
			// get member uid
			uid = idSplit[idSplit.length - 1];

			// get recent url without query parameter
			url = window.location.pathname +
			      '?tx_cyzteam_pi1[uid]=' + parseInt(uid, 10);

			location.href = url;
		}

	}



	/**
	 * Initializing
	 * This function will be called when the html code is loaded
	 * @public
	 */
	this.init = function () {
		/**
		 * selector id of the form
		 * @type String
		 */
		var formId;


		if ($(CyzTeamConfig.inputId) && $(CyzTeamConfig.suggestId)) {
			// get the id of the form tag: there are the page ids
			formId = $(CyzTeamConfig.inputId).up().identify();
			if (formId.startsWith(CyzTeamConfig.formIdPrefix)) {
				pids = formId.substr(CyzTeamConfig.formIdPrefix.length);
			}

			// ... and observe
			$(CyzTeamConfig.inputId).observe('keyup', inputKeyUp);
			$(CyzTeamConfig.suggestId).observe('click', clickMember);
		}

		transformEmail();
	};
}



/***** Start ******************************************************************/
document.observe('dom:loaded', function() {
	var cyzTeam = new CyzTeam();
	cyzTeam.init();
});

