
/**
 * Object that stores and helps to create an enhanced version of the to contact list
 * @namespace Object that stores and helps to create an enhanced version of the to contact list
 * @member BTT
 */
BTT.Contacts = {
		
		rootDir : '',
		displayContactDetails: '',
		
		
		showContactDetails : function() {
						
			// Make AJAX request to get the contact details
			var userDetailsUrl = (BTT.ctx ? BTT.ctx : '/ts') + '/utilities/ajax/getContactDetails';
			
			// Check if only 1 item exist in the selected list & display contacts is enabled
			// if true, then make an AJAX call to display the details for that contact
			if ($('.listSelection li').length === 1 && BTT.Contacts.displayContactDetails){
				
				var selectedContact = $('.listSelection p')[0].id.split('remove')[1];
				
				
				$.ajax({
					url: userDetailsUrl,
					cache: false,
					data: {
						contactID: selectedContact
					},
					success: function(contactDetails){
						
						// Check again if only on contact is selected before displaying the details
						if ($('.listSelection li').length === 1){
							
							$('.listSelection li:first')
							.append(contactDetails);
							
						}
						
					}

					
				});
				

			}
			else{
				$('ul.listSelection .contactDetails').remove();
			}

		},
		
		addContact : function(elem, selectAll){
			
			$("#deleteContacts").removeAttr("disabled");
			$("#deleteContacts").parents("div:eq(0)").removeClass("buttonDisabled");
			
			var contactId = elem.attr("id");
			var contactName = elem.parents('li').find("label").html();
			
			if(elem.is(':checked')){
				
				//alert(BTT.Contacts.addContact.contactName);
				
				var newItem = $('<li/>');
				var newContact = $('<p class="textLarge textBold" id="remove' +contactId+ '" />');
				var editContactID = contactId.split('selectedContactIDs')[1];
				
				newContact.append(contactName);
			
				// add the selected class to contact
				elem.parents('li').addClass('listRowSelected');
				
				// see if the selected contact is a Tradespace member
				if(elem.parents('li').find("p.contactsTradespace").length > 0){
					elem.parents('li:first').find("p.contactsTradespace").clone().appendTo(newContact);
				}

				newItem.append(newContact);

				$('.listSelection').append(newItem);

			}
			// Contact unchecked so remove from the selected list
			else {
				$('#' + contactId ).parents('li').removeClass("listRowSelected");
				$('#remove' + contactId ).parents('li').remove();
				
				if($(".listSelection li").length < 1){
					$("#deleteContacts").attr("disabled", "disabled");
					$("#deleteContacts").parents("div:eq(0)").addClass("buttonDisabled");
				}

			}

			//make a AJAX call to display contact details
			if(!selectAll){				
				BTT.Contacts.showContactDetails();
			}
		},
		
		
		// Removes all contacts from the invite list
		removeAll : function(){
			
			// un-check all the contacts from the contacts list
			$('.contactsList li').each(function(){
				$(this).find('input').attr('checked', false);
				$(this).removeClass('listRowSelected');
			});
			
			// remove all the contacts from the selected list
			$('.listSelection li').each(function(){
				$(this).remove();
			});
			
			// Disable/enable the 'Delete contacts' button
			if($(".listSelection li").length < 1){
				$("#deleteContacts").attr("disabled", "disabled");
				$("#deleteContacts").parents("div:eq(0)").addClass("buttonDisabled");
			}

	
		},
		
		
		
		// Functions to run to initialise the contacts list
		init: function () {
			
			// remove the "Add, to invite list" button (fallback)
			$('.contactsAddPanel div').remove();
			
			// get the root directory details from a hidden variable on  the page
			if ($('#JS_globalRootDir').length !== 0){
				BTT.Contacts.rootDir = $('#JS_globalRootDir')[0].value;
			}			
			
			// Check if the Contact details need to be displayed 
			if($('.JS_displayContactDetails').length >= 1){
				BTT.Contacts.displayContactDetails = true;
			}
			
			// Pre-populate the selected contacts
			$('.contactsList li input[type=checkbox]').each(function(){
				
				if($(this).attr('checked') === true){
					
					//check if the contact is not populated by the backend on the invite list 			
					if ($('#remove' + $(this).attr('id')).length < 1){
						BTT.Contacts.addContact($(this), false);
					}
				}
			});
			
			// enable/disable 'Delete contacts' button 
			if($(".listSelection li").length < 1){
				$("#deleteContacts").attr("disabled", "disabled");
				$("#deleteContacts").parents("div:eq(0)").addClass("buttonDisabled");
			}
			
			// Populate the selected(invite) list
			$('.contactsList input').click(function(){
				BTT.Contacts.addContact($(this), false);
			});
			
			// Select all contacts
			$('.selectAllContacts').click(function(){
				
				// remove the contact details from the selected list
				$('ul.listSelection .contactDetails').remove();
				
				$('.contactsList li input[type=checkbox]').each(function(){
					if($(this).attr('checked') === false){
						$(this).attr("checked", "checked");
						BTT.Contacts.addContact($(this), true);
					}
				});
				
				return false;
				
			});
			
			
			/* - REMOVING
			 -------------------------------- */
			
			//Remove from the selected(invite) list
			$('.listSelection input').live('click', function(event){
				
				alert("remove");
				
				var removeContact = this.id;
				
				// un-check the contact from 'My Contacts' list
				$('#' + removeContact.split("remove")[1]).parents('li').removeClass('listRowSelected');
				$('#' + removeContact.split("remove")[1]).attr('checked', false);
				
				
				// remove the contact from the invite list
				$(this).parents('li').remove();

				// Make a call to show details, details will be shown only if 1 contact is selected
				BTT.Contacts.showContactDetails();
				return false;

			});
			
			// Remove all selected contacts
			$('.removeAll').click(function(){
				BTT.Contacts.removeAll();
				return false;
			});
			
			$('.deselectAllContacts').click(function(){
				BTT.Contacts.removeAll();
				return false;
			});
			
		}
};




/* Initialise page when the DOM is ready
-------------------------------- */
$(document).ready(function () {
	
	BTT.Contacts.init();
	
});

