// -----------------------------------------------------
// Lekane TalkNow - build for Lekane.com frontpage
// Copyright Lekane 2008
// -----------------------------------------------------

/**
 * This is where you define what you want to do when you receive a presence response.
 * The whoIsAvailableCallback expects an array of user id's paired with display names.
 * @param availableUsers[][] id matched with display name array
 */
var myWhoIsAvailableCallback = function whoIsAvailableCallback( availableUsers ) {	
	var dropDown = $('#userDropDown')[0];
	var dropDownSpan = $('#userToTarget');
	var presenceText = $('#talkNowPresenceText');
	var presenceImage = $('#talkNowPresenceImage');
	
	// Create new dropdown list items
	if(null == availableUsers || availableUsers =='') {
		dropDownSpan.hide();
		presenceImage.attr( 'src', staticLocalTalkNowURL+'phone-notready.gif' );
		presenceText.html('We are currently busy.');
		initSearchBox();
	} else {
		dropDownSpan.show();
		presenceImage.attr( 'src', staticLocalTalkNowURL+'phone-ready.gif' );
		presenceText.html('We are ready to talk to you.'); 	
		
		// Get the previously selected item (as the lekane demo refreshes - its annoying if the selection is lost
		// when we rebuild the dropdown)
		var previouslySelected = $('#userDropDown:selected')[0];
		
		initSearchBox();
		var count = 1;

		for( var i=0; i < availableUsers.users.length; i++ ) {
			var text = availableUsers.users[i].name;
			var value = availableUsers.users[i].searchPattern;
			var option = new Option( text, value );
			
			if ( value == previouslySelected ) {
				option.selected = true;
			}
			dropDown.options[count] = option;
			count += 1;
		}
	}
};

function initSearchBox() {
	var dropDown = $('#userDropDown');
	// Clear the select box
	while (dropDown.firstChild){
		dropDown.removeChild(dropDown.firstChild);
	}	

	// Add the 'everyone' option mapped to LekaneWeb group
	var option = new Option( 'Everyone', searchPattern );
	option.selected = true;
	dropDown.append(option);
}


/**
 * This is where you define what you want to do when you receive a response to the callback request.
 * @param caller is who will be calling the web user back, or null if the request timed out.
 */
var myCallRequestCallback = function callRequestCallback( callerDetails ) {
	
	// We are using the prototype shorthand for getting the elements on the sample.html page.
	var presenceText = $('#talkNowPresenceText');
	var presenceImage = $('#talkNowPresenceImage');
	
	// If the caller hasn't been set it comethrough to javascript as null
	if( callerDetails && callerDetails.success ) {
	 	// Someone from the requested group has accepted the call request and will call the customer back
	 	presenceImage.attr( 'src', staticLocalTalkNowURL+'phone-ready.gif' );
		presenceText.html( callerDetails.details.name + ' will call you back'); 	
	} else {
	 	// No one has accepted the call request within the time period, so we display that someone will contact them
	 	presenceImage.attr( 'src', staticLocalTalkNowURL+'phone-notready.gif' );
		presenceText.html('We are currently busy, we will call you back soon');
	}
};


/**
 * An example function that shows the format of the data to send to TalkNow server.
 * This example takes some data that was generated on the server side of the form key and value's
 * stored in an Array. It adds in the phone number entered on the web page and uses the user Id from the
 * dropdown menu (real time updated with available users).
 */
function callRequestData() {
	// Further data can be passed to the TalkNow server in this way.
	data[0][0] = 'phoneNumber';
	data[1][0] = $('#phoneNumber').val();
	
	data[0][1] = 'notes';
	data[1][1] =  $('#notes').val();
	
	// This example changes the phone image to an animated icon to give the web user feedback that something is happening.
	var presenceImage = $('#talkNowPresenceImage');
	presenceImage.attr( 'src', staticLocalTalkNowURL + 'phone-in-progress.gif' );
	
	// Get the currently selected user from the dropdown
	var dropDown = $('#userDropDown')[0];
	var selectedOption = dropDown.options[dropDown.selectedIndex]
	var searchPattern = selectedOption.value;	

	sendCallRequest( searchPattern, data, metadata, myCallRequestCallback );
}

function setVisibility( objectToShow ) {
	// Get the id of the form
	var obj = $(objectToShow);
	if (obj.css('display')=="none") {
		obj.show();
	}
	else {
		obj.hide();
	}	
}

/**
 *  Retrieve TalkNow content and display on page
 */
function enableTalkNowIfServerIsActive() {
	$.post( proxyURL,
		{
			url: lekaneServerURL + '/lekane/TalkNowStatus2',
			cmd: 'details',
			deployment: talkNowDeploymentId,
			searchPattern: talkNowSearchPattern
		},
		function( responseDetails, textStatus, xmlHttpRequest ) {
			$('#talknow').load( staticLocalTalkNowURL+'/talknow.fragment',
				function( responseText, textStatus, xmlHttpRequest ) {
					myWhoIsAvailableCallback( jQuery.parseJSON( responseDetails ) );	
				}
			);
		}
	);
}

