var g_current_user_nickname = "";
var g_current_user_avatar_url = "";

function Contact_Init(_sNickname, _sAvatarURL)
{
	var div = document.getElementById("formContactSearch");
	div.oldValue = div.innerHTML;
	//ImageLoader_Add
	var notifDiv = document.getElementById("notifMainDiv");
	var imgList = notifDiv.getElementsByTagName("IMG");
	for (i = 0; i < imgList.length; i++)
	{
		var src = imgList[i].src;
		imgList[i].id = "notifImg" + i;
		imgPreloader = ImageLoader_Add("notifImg" + i, imgList[i]);
		ImageLoader_Activate(imgPreloader, src);
	}
	/*imgList = document.getElementById("tabContactContainer").getElementsByTagName("IMG");
	for (i = 0; i < imgList.length; i++)
	{
		var src = imgList[i].src;
		imgList[i].id = "contact" + i;
		imgPreloader = ImageLoader_Add("contact" + i, imgList[i]);
		ImageLoader_Activate(imgPreloader, src);
	}*/
	g_current_user_nickname = _sNickname;
	g_current_user_avatar_url = _sAvatarURL;
}

function onContactOn(e)
{
	// On affiche le menu au bout d'un certain temps;
	//if (COMMON_GetClass(COMMON_GetTarget(e)) == "selected")
	{
		contactTarget = COMMON_GetTarget(e);
		Contact_ShowMenu();
	}
}

function Contact_ShowMenu()
{
	// Get the Table element
	document.getElementById("floatingContactMenu").style.display = "block";
	document.getElementById("floatingContactMenu").style.top = COMMON_GetTopPos(contactTarget);
	document.getElementById("floatingContactMenu").style.left = COMMON_GetLeftPos(contactTarget) + contactTarget.offsetWidth;
}

function Contact_HideMenu()
{
	document.getElementById("floatingContactMenu").style.display = "none";
	eContactOn = null;
	contactTarget = null;
}

function Contact_addFromMenu(e)
{
	if (contactTarget)
	{
		Contact_addFriend(contactTarget.iduser, 1);
	}
}

/***************** AJAX *************************************/
function Contact_Search()
{
	var sUser = document.getElementById('searchContactInput').value;
	if (sUser.length == 0)
		return;
	AJAX_SendRequest('./ajax/users.php?search&name=' + escape(sUser));
}

function Contact_follow(_iUserId)
{
	AJAX_SendRequest('./ajax/users.php?follow&id=' + escape(_iUserId));	
}

function Contact_unfollow(_iUserId)
{
	AJAX_SendRequest('./ajax/users.php?unfollow&id=' + escape(_iUserId));	
}

function Contact_addFriend(_iUserId, _bOnContactPage)
{
	AJAX_SendRequest('./ajax/users.php?addFriend&showContact='+_bOnContactPage+'&id=' + escape(_iUserId));
}

function Contact_responseAddFriend(_sResponse)
{
/*	if (_sResponse == '-2')
	{
		AW_Show(g_lang_contact_already_friends, true);
		return;
	}
	if (_sResponse == '-1')
	{
		AW_Show(g_lang_contact_unknown, true);
		return;
	}*/
	
	var td = document.getElementById('tabContactContainer');
	if (td)
		td.innerHTML = _sResponse;

	var imgAddContact = document.getElementById('contactAddPic');
	if (imgAddContact)
	{
		imgAddContact.onclick = null;
		imgAddContact.onclick = function(e) {Contact_removeFriend(_sResponse['id'], 0);};
		imgAddContact.src = "./img/contact_remove.png";
	}
}

function Contact_removeFriend(_iUserId, _bOnContactPage)
{
	AJAX_SendRequest('./ajax/users.php?removeFriend&showContact='+_bOnContactPage+'&id=' + escape(_iUserId));
}

function Contact_responseRemoveFriend(_sResponse)
{
	var imgAddContact = document.getElementById('contactAddPic');
	if (imgAddContact)
	{
		imgAddContact.onclick = null;
		imgAddContact.onclick = function(e) {Contact_addFriend(_sResponse['id'], 0);};
		imgAddContact.src = "./img/contact_add.png";
	}
}

function Contact_AnswerFriendRequest(iNotifId, iAnswer)
{
	AJAX_SendRequest('./ajax/users.php?answerAddFriend&id='+iNotifId+'&answer=' + escape(iAnswer));
}

function Contact_ResponseAnswerFriendRequest()
{
	
}

function Notification_ShowMenu(obj)
{
	/*child = obj.getElementsByTagName('DIV');
	
	for (var i = 0; i < child.length; i++)
	{
		if (COMMON_GetClass(child[i]) == "notifMenu")
			child[i].style.visibility = "visible";	
	}*/
}

function Notification_HideMenu(obj)
{
	/*child = obj.getElementsByTagName('DIV');
	
	for (var i = 0; i < child.length; i++)
	{
		if (COMMON_GetClass(child[i]) == "notifMenu")
			child[i].style.visibility = "hidden";	
	}*/
}

function Notification_ShowComment(obj, sendTxt)
{
	var divComment = document.getElementById("commentForm");
	if (divComment == null)
	{
		divComment = document.createElement('div');
		divComment.id = "commentForm";
		var input = document.createElement('TEXTAREA');
		input.cols = '50';
		input.rows = 3;
		input.id = 'commentFormInput';
		divComment.appendChild(input);
		var saut = document.createElement('BR');
		var a = document.createElement("A");
		a.href= "#";
		a.innerHTML = sendTxt;
		a.onclick = function ()
		{
			Notification_AddComment();
			return false;
		}
		divComment.appendChild(saut);
		divComment.appendChild(a);
	}
	else
	{
		divComment = divComment.parentNode.removeChild(divComment);
		var input = divComment.getElementsByTagName("TEXTAREA")[0];
		input.value = "";
	}
	obj.parentNode.parentNode.appendChild(divComment);
	input.focus();
	var divList = obj.parentNode.parentNode.getElementsByTagName("div");
	for (var i = 0; i < divList.length; i++)
	{
		if (COMMON_GetClass(divList[i]) == "notifDivCommentMain"
			|| COMMON_GetClass(divList[i]) == "notifDivCommentEnd")
		{
			divList[i].style.display = "block";
			break;
		}
	}
}

function Notification_AddComment()
{
	var input = document.getElementById('commentFormInput');
	var iNotifId = input.parentNode.parentNode.getAttribute('idnotif');
	var value = input.value;
	if (value.length == 0)
	{
		alert(g_lang_notif_comment_not_empty);
		return;
	}
	AJAX_SendRequest('./common/notifications.php?addcomment&value='+escape(value)+'&id=' + escape(iNotifId));
}

function Notification_ResponseAddComment(value)
{
	var divComment = document.getElementById("commentForm");
	var divList = divComment.parentNode.getElementsByTagName('div');
	var iDivCount = 1;
	for (var i = 0; i < divList.length; i++)
	{
		if (COMMON_GetClass(divList[i]) && (COMMON_GetClass(divList[i]) == "notifDivComment" || COMMON_GetClass(divList[i]) == "notifDivComment2"))
		{
			iDivCount++;
		}
	}
	var div = document.createElement('div');
	if (iDivCount%2)
		COMMON_SetClass(div, 'notifDivComment');
	else
		COMMON_SetClass(div, 'notifDivComment2');
	var sHTML = "<a href='../p/" + g_current_user_nickname + "' title='" + g_current_user_nickname + "'><img src='" + g_current_user_avatar_url + "' class='avatarNotifComment'><div class='notifCommentContent'>" + g_current_user_nickname + "</a><br/>" + value;
	sHTML = sHTML + "<div class='flowFoot'>";
	sHTML = sHTML + "<div class='dateSpanComment'>just posted</div>";
	sHTML = sHTML + "</div>";
	div.innerHTML = sHTML;
	
	var divList = divComment.parentNode.getElementsByTagName("div");
	for (var i = 0; i < divList.length; i++)
	{
		if (COMMON_GetClass(divList[i]) == "notifDivCommentMain")
		{
			divList[i].style.display = "block";
		}
		else if (COMMON_GetClass(divList[i]) == "notifDivCommentEnd")
		{
			divList[i].appendChild(div);
			divList[i].style.display = "block";
		}
		else if (COMMON_GetClass(divList[i]) == "notifDivCommentCount")
		{
			var spanCount = divList[i].getElementsByTagName('span')[0];
			spanCount.innerHTML = iDivCount;
			divList[i].style.display = "block";
		}
	}
	divComment = divComment.parentNode.removeChild(divComment);
}

function Notification_ShowHideComment(linkObj)
{
	var divList = linkObj.parentNode.parentNode.getElementsByTagName("div");
	var bShow = true;
	for (var i = 0; i < divList.length; i++)
	{
		if (COMMON_GetClass(divList[i]) == "notifDivCommentMain")
		{
			bShow = divList[i].style.display == "none"?true:false;
			divList[i].style.display = divList[i].style.display == "none"?"block":"none";
		}
		else if (COMMON_GetClass(divList[i]) == "notifDivCommentEnd")
		{
			divList[i].style.display = bShow?"block":"none";
			
		}
	}
}
function Notifications_OnChangeFilter(radioButton)
{
	sFilterGeneral = "";
	if (document.getElementById("filterAll").checked)
		sFilterGeneral = "1";
	else if (document.getElementById("filterVideos").checked)
		sFilterGeneral = "2";
	else if (document.getElementById("filterImages").checked)
		sFilterGeneral = "3";
	else if (document.getElementById("filterSites").checked)
		sFilterGeneral = "4";
	else if (document.getElementById("filterSounds").checked)
		sFilterGeneral = "5";
	else if (document.getElementById("filterDocs").checked)
		sFilterGeneral = "6";
	else if (document.getElementById("filterOthers").checked)
		sFilterGeneral = "7";
	
	sFilterPeople = "";
	if (document.getElementById("filterAllPeople").checked)
		sFilterPeople = "0";
	else if (document.getElementById("filterMe").checked)
		sFilterPeople = "1";
	else if (document.getElementById("filterName").checked)
	{
		list = document.getElementById('filterNameValueList');
		var optionList = list.getElementsByTagName("option");
		for (var i = 0; i < optionList.length; i++)
		{
			if (optionList[i].selected)
			{
				sFilterPeople = optionList[i].innerHTML;
				//sFilterPeople = document.getElementById('filterNameValue').value;
				break;
			}
		}

	}
	
	if (sFilterGeneral.length != 0)
	{
		if (sFilterPeople.length != 0)
			AJAX_SendRequest("./ajax/notifications.php?notif_general&filter=" + sFilterGeneral + "&filterPeople=" + sFilterPeople);		
		else
			AJAX_SendRequest("./ajax/notifications.php?notif_general&filter=" + sFilterGeneral);			
	}
	else
	{
		if (sFilterPeople.length != 0)
			AJAX_SendRequest("./ajax/notifications.php?notif_general&filterPeople=" + sFilterPeople);		
		else
			AJAX_SendRequest("./ajax/notifications.php?notif_general");
	}
}

function Contact_ShowPage(div, content)
{
	document.getElementById(div).innerHTML = content;
	registerCardCommunication();
}

function getNextPageFriend(iPage, nickname)
{
	AJAX_SendRequest("./ajax/users.php?getFriends&page=" + iPage + "&nickname=" + nickname);
}
function getNextPageFollowers(iPage, nickname)
{
	AJAX_SendRequest("./ajax/users.php?getFollowers&page=" + iPage + "&nickname=" + nickname);
}
function getNextPageFollowing(iPage, nickname)
{
	AJAX_SendRequest("./ajax/users.php?getFollowing&page=" + iPage + "&nickname=" + nickname);
}

function callbackUserPicture()
{
	var userPictureButton = document.getElementById("uploadUserPictureButton");
	userPictureButton.onchange = function (){
	}
	new AjaxUpload(userPictureButton,{
		//action: 'upload-test.php', // I disabled uploads in this example for security reasons
		action: './common/utilities/fileuploader.php?userpicture', 
		name: 'file',
		onSubmit : function(file, ext){
			// change button text, when user selects file			
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
		},
		onComplete: function(file, response){					
			// enable upload button
			this.enable();
			readJSonResponseAndExecute(response);
			COMMON_hideAddForm();			
		}
	});
}


function Contact_ChangeUserPicture(oElem)
{
	AJAX_SendRequest("./ajax/users.php?usePicture=" + oElem.value);
}

function openOrReduceNotifByDate()
{
	mainDiv = document.getElementById('notif_general');
	divList = document.getElementsByTagName('div');
	openNotifState = !openNotifState;
	for (i = 0; i < divList.length; i++)
	{
		if (COMMON_GetClass(divList[i]) == 'notifDayContent')
		{
			display = divList[i].style.display;
			divList[i].style.display = openNotifState ? "block" : "none"; 
			renameChildDate(divList[i].previousSibling, openNotifState, divList[i].childNodes.length);
		}
	}
}
var openNotifState =true; 
function openOrReduceNotifByDateByElement(obj)
{
	nextDiv = obj.nextSibling;
	display = nextDiv.style.display;
	bFadIn = false;
	if ($(nextDiv)) {
		if (display == "block" || display == "")
		{
			bFadIn = false;
			$(nextDiv).fadeOut();			
		}
		else
		{
			bFadIn = true;
			$(nextDiv).fadeIn();
		}
	}	

	//nextDiv.style.display = (display == "block" || display == "") ? "none" : "block"; 
	renameChildDate(obj, bFadIn, nextDiv.childNodes.length);
}
function renameChildDate(obj, openNotifState, count)
{
	value = obj.innerHTML;
	if (value.indexOf('(') != -1)
	{
		obj.innerHTML = value.substr(0, value.indexOf('('));
	}		
	if (!openNotifState)
	{
		obj.childNodes[0].innerHTML = obj.childNodes[0].innerHTML + " (" + count + ")";
	}
}



