/** JavaScript Functions for the management of a Link */
function linkAction(containerID, actionURL, onLoadingHTML) {
	//Default loading message
	if(typeof(onLoadingHTML) == 'undefined' 
		|| onLoadingHTML.length == 0
		|| onLoadingHTML == 'null'
	) {
		onLoadingHTML = '<p style="text-align:center"><img src="/tamtamy/img/wait.gif" /></p>';
	}
	$('#'+containerID).html(onLoadingHTML).load(encodeURI(actionURL));
	return false;
}

/** JavaScript Functions for the management of a Tabbed Panel */
function switchTongue(containerID, tongueID, normalTongueClass, selectedTongueClass, onLoadingHTML, bodyID, beforeJS, afterJS) {
	//After switching JavaScript to execute
	if(typeof(beforeJS) != 'undefined' && 
		 beforeJS != 'null' &&
		 beforeJS.length != 0) {
		eval(beforeJS);
	}

	//Default CSS classes
	if(typeof(normalTongueClass) == 'undefined' 
		|| normalTongueClass.length == 0
		|| normalTongueClass == 'null'
	) {
		normalTongueClass = '';
	}
	if(typeof(selectedTongueClass) == 'undefined' 
		|| selectedTongueClass.length == 0
		|| selectedTongueClass == 'null'
	) {
		selectedTongueClass = '';
	}
	
	//Default loading message
	if(typeof(onLoadingHTML) == 'undefined' 
		|| onLoadingHTML.length == 0
		|| onLoadingHTML == 'null'
	) {
		onLoadingHTML = '<p style="text-align:center"><img src="/tamtamy/img/wait.gif" /></p>';
	}
	
	//Default body ID
	if(typeof(bodyID) == 'undefined' 
		|| bodyID.length == 0
		|| bodyID == 'null'
	) {
		bodyID = '_body_' + containerID;
	}


	actionURLKey = containerID+tongueID;
	actionURL = tabbedActionURL[actionURLKey];
 	$('#'+bodyID)
 		.fadeOut()
 		.empty()
 		.html(onLoadingHTML)
 		.fadeIn("fast")
 		.load(
 			encodeURI(actionURL),
 			{},
 			function() {
 				//I need this way... with a direct selector sometimes it doesn't catch the selected tongue
 				if(selectedTongueClass != '' && normalTongueClass != '') {
 					$('li.'+selectedTongueClass, $('#'+containerID)).each(
 						function(i,el) {
 							$(this).removeClass(selectedTongueClass).addClass(normalTongueClass);
 							return;
	 					}
	 				);
 					$('#'+containerID + ' li').removeClass(selectedTongueClass);
 					if (normalTongueClass != '' && normalTongueClass != null){
 						$('#'+tongueID, $('#'+containerID)).removeClass(normalTongueClass);
 					}
 					$('#'+tongueID, $('#'+containerID)).addClass(selectedTongueClass);
	 				$('#'+containerID+' a').removeClass('on');
	 				$('#'+tongueID+' a').addClass('on');
	 			}
	 			
	 			//After switching JavaScript to execute
	 			if(typeof(afterJS) != 'undefined' 
					&& afterJS != 'null' 
					&& afterJS.length != 0) {
					eval(afterJS);
				}
 			}
 		);
 	
 	return false;
 }
 
 //If I'm showing the tongueID, refresh its contents 
 function autoRefreshTongue(containerID, tongueID, normalTongueClass, selectedTongueClass, onLoadingHTML, bodyID, beforeJS, afterJS) {
 	if($('#'+tongueID, $('#'+containerID)).attr('class') == selectedTongueClass) {
 		return switchTongue(containerID, tongueID, normalTongueClass, selectedTongueClass, onLoadingHTML, bodyID, beforeJS, afterJS);
 	}
 }
 
/** user presence */

	//Make draggable a content
	function makeDraggableJoinChat(objectID) {
		 $("#"+objectID).draggable(
			{
				ghosting:	true,
				opacity:	0.8,
				fx:			200,
				revert:		true,
				onStart: function() {
				makeDroppableJoinChat("#chatWindow");
				
					/*
					//alert('startDrag: doc='+$("body").css("z-index")+' win=' +$("#chatWindow").css("z-index")+ " this=" +$(this).css("z-index"));
					$(this).css("z-index", 910);					
					$("#chatWindow").css("z-index", 900);
					$("#chatWindow > *").each(
						function(i,elll) {
							$(this).css("z-index", 900);
						}
					);	
					*/					
					//$("body").css("z-index", "10");
					//alert('startDrag: win=' +$("#chatWindow").css("z-index")+ " this=" +$(this).css("z-index")+" this.class="+$(this).attr('class'));
				},
				onStop: function() {
					//$(this).css("z-index", "10");
					//$("#chatWindow").css("border","none").css("z-index", "90");
					//$("#chatWindow").css("border","medium #000000 dashed").css("z-index", "700");
					//$(this).css("z-index", "10");
					//$("body").css("z-index", "10");
					//alert('stopDrag: doc='+$("body").css("z-index")+' win=' +$("#chatWindow").css("z-index")+ " this=" +$(this).css("z-index"));
				} 
			}
		);
	}

	// Make droppable an area
	function makeDroppableJoinChat(objectID) {
		$("#"+objectID).droppable(
			{
				tolerance:		'intersect',
				accept :		'dndJoinChat',
				ondrop:	function(drag) 
				{
					//Get the userID
					dragID = drag.id;
					dragUserID = "" + $('#'+dragID + ' > span').text();
					
					//Build the service Message
					msg = "join(\""+dragUserID+"\");";

					//Get the Group
					destGID = $("#groupCID",this).val();
					group = chatGroupCont.getGroupFromClientID(destGID);
					//Send the message
					if(group != false) {
						sendMessage(group, msg);
					}
				}
			}
		);	
		
	}
	