$(document).ready(function() {		

	function openChatWindow(location){
		window.open(location,'','height=500,width=600,resizable=yes,toolbar=no,titlebar=no,menubar=no,statusbar=no,location=no' );
		return false;
	}


	// provide tooltips for 'greetingwall' links on consultants in 'whos online'
	$('.msgLink').tooltip({ showURL: false });
	// provide tooltips for 'chat' links on consultants in 'whos online'
	$('.chatLink').tooltip({ showURL: false });			
	
	$('.openChatWindow').click( function() {
		return openChatWindow($(this).attr('href'));
	});


	// ADD LINKS TO NON-CHATTERS TO ENABLE CHAT INVITE SENDING

	// define link to be inserted
	chatLink = '<span class="chatInvite" title="Invite this person to join you in the chatroom" href="/consultants/chatroom-invite.php"><img src="/consultants/chatroom/data/public/emoticon_happy_grey.png" width="16" height="16"></span>';

	// find all consultants who are not in the chatroom, append link
	var needLinks = $('.consultant_online').filter(function(index) {
		   return $(".chatLink", this).length == 0;
	 });
// removed temporarily
//	$('.msgLink', needLinks).after(chatLink);		
	
	$('.chatInvite').tooltip({ showURL: false });	// add tooltip for new links					
	
	// add ajax functionality to enable data to be sent and written to invite file
	$('.chatInvite').one("click", function(){	
		$(this).tooltip.block; // stop the tooltip			
		chatimage = $('img',this); // change the image to a clock to show we are waiting!
		$(chatimage).attr('src', '/images/icons/silk/clock.png');
		your_name = $(this).parent().find('h5').text(); // who are we inviting?

		// now, the ajax functionality
		$.post("/consultants/chatroom-invite.php", { invitee: your_name, inviter: my_name }, function(data){					
			alert(data); // let user know invite has been sent (or not)
			$(chatimage).attr('src', '/images/icons/silk/emoticon_happy.png'); // change clock to smiley
			openChatWindow('/consultants/chatroom.php'); // launch popup chatwindow
		  }
		 );			
		return false;
	});

	// SETUP AND LAUNCH FUNCTION TO CHECK INTERMITTENTLY FOR NEW INVITES
	var delay = 5000; // milliseconds between checks
	function checkChatroomInvites(){
		$.post("/consultants/chatroom-invite-check.php", { consultant_name : my_name }, function(names){
			if(names.length > 0){
				openChat = window.confirm('The following consultant(s) have invited you to join them in the TL chat lounge:\n\n'+names+"\n\nDo you wish to accept the invite?\n\nAccepting this invite will open the chatroom in a new window. (Please note: You must have pop-ups enabled for the chatroom to open automatically)");
				if (openChat){
					openChatWindow('/consultants/chatroom.php');
				}					
			} 
			// if there was no invite, or providing they didnt open the chat window, check again
			if(typeof(openChat)=="undefined" || !openChat){
				window.setTimeout(checkChatroomInvites, delay);													
			}
		})				
	}

	// begin recurring ajax function to check for invites
	window.setTimeout(checkChatroomInvites, delay);

	
 

});