(function(){
/**
 * @projectDescription
 * Ajax enabling page
 *  - itinerary buttons
 * @author Derek Fowler
 */

window.NewMind = window.NewMind || {};
var NewMind = window.NewMind;

NewMind.ajax = {
	
	ajaxEnablePage: function()
	{
		NewMind.ajax.ajaxEnableItineraryLinks();
	},
	
	/**
	 * Attach the handler to the add and remove from itinerary links.
	 * @param {Object} oLink
	 */
	ajaxEnableItineraryLinks: function(oLink)
	{
		$('a.ep_Add, a.ep_Remove').click(NewMind.ajax.itinButtonClickHandler);
	},
	
	/**
	 * Determines what action is being performed i.e. add or remove and performs
	 * that action using an AJAX call.
	 * @param {Object} e
	 */
	itinButtonClickHandler: function(e)
	{
		var elmLink = this;
				
		if(elmLink.tagName.toLowerCase() == 'a')
		{
			
			var strAction, intKey;
			var aryActionMatches = elmLink.href.match(/epaction=([a-z]+)/);
			if(aryActionMatches.length > 0)
			{	
				strAction = aryActionMatches[1];
			}
			
			var aryKeyMatches = elmLink.href.match(/epkey=([0-9]+)/);
			if(aryKeyMatches.length > 0)
			{
				intKey = aryKeyMatches[1];
			}
			
			if(strAction && intKey)
			{
				if(strAction == 'add') {
					$(this).removeClass('ep_Add').addClass('ep_Adding');
				}
				
				NewMind.ajax.doItinAjaxAction(strAction, intKey, this);
			}
		}
		return false;
	},
	
	/**
	 * This method actually does the AJAX call using a jQuery get.
	 * @param {Object} action
	 * @param {Object} key
	 * @param {Object} btn
	 */
	doItinAjaxAction: function(action, key, btn)
	{
		$.get(NewMind.env.page.realurl + '/ajax-explanner?epaction=' + action + '&epkey=' + key, function(){
			if(arguments[1] == 'success') {
				NewMind.ajax.itinAjaxCallback(action, key, btn, arguments[0]);
			}
		});
	},
	
	/**
	 * The response from the server for an itinerary action (data) is a blob 
	 * of HTML that contains the updated itinerary basket and some script that 
	 * fires the itinUpdated event below.
	 * @param {Object} action 'add', 'remove' or 'ctrlremove'
	 * @param {Object} key The product key being worked on
	 * @param {Object} btn The link clicked to perform the action
	 * @param {Object} data The data returned from the server
	 */
	itinAjaxCallback: function(action, key, btn, data)
	{
		
		if(action == 'add') {
			$(btn)
				.removeClass('ep_Adding')
				.addClass('ep_Added')
				.attr('href', '#excursionHelp')
				.flyTo('#excursionHelp')
				.end()
				.queue(function(){
					$('#excursionHelp').replaceWith(data);
					$('#excursionHelp a.ep_Remove').click(NewMind.ajax.itinButtonClickHandler);
					$(this).dequeue();
				});
		}
		if (action == 'remove' || action == 'ctrlremove') {
			$('.ep_Added.ep_' + key)
				.removeClass('ep_Added')
				.addClass('ep_Add')
				.attr('href', '?epaction=add&epkey=' + key);
				
			$('#excursionHelp').replaceWith(data);
			$('#excursionHelp a.ep_Remove').click(NewMind.ajax.itinButtonClickHandler);
		}
		
	},
	
	itinUpdated: new NewMind.CustomEvent("itinUpdated", this)
	
};
})();