jQuery(document).ready(function() {
	load_friend_list();
});

var load_friend_list = function(offset) {
	//jQuery('#myFriendList').addClass('loading');
	jQuery.post('/modules/e-phonebook/ws/ws-friend_list.php', { offset : offset }, function(data) {
		//jQuery('#myFriendList').removeClass('loading');
		jQuery('#myFriendList').html(data);

		if (jQuery(document).tooltip) {
			//alert(jQuery('.comite a').size());
			jQuery('#myFriendList .friends a').tooltip({
				showURL: false
			});
		}
		//
		var str = new String(document.location);
		if (!str.match('note_index\.php') && !str.match('home\.php') && !str.match('user_form\.php')) {
			var _height = document.getElementById('subcontainer').clientHeight;
			jQuery('#content').css('height', _height+'px');
		}
		if (str.match('note_form\.php')) {
			jQuery('#content').css('height', 'auto');
		}
	
	});
	
}

var loadContactParticipation = function(participationTarget,offset,user,specialite,profession,lieu_travail) {
	jQuery('#jPop').addClass('loading');
	jQuery.post('/modules/e-phonebook/ws/ws-read_contact_participation.php', { participationTarget : participationTarget, offset : offset, user : user, specialite : specialite, profession : profession, lieu_travail : lieu_travail }, function(data) {
		jQuery('#jPop').removeClass('loading');
		jQuery('#jPop').html(data);
	});
	
}

var inviteFriend = function() {
	var buttons = {
		'Inviter': function() {
			jQuery('#jPop').addClass('loading');
			if (document.getElementById('emailFriend')) {
				emailFriend = document.getElementById('emailFriend').value;
			} else {
				emailFriend = '';
			}
			if (document.getElementById('friendName')) {
				friendName = document.getElementById('friendName').value;
			} else {
				friendName = '';
			}
			if (document.getElementById('joinMessage')) {
				joinMessage = document.getElementById('joinMessage').value;
			} else {
				joinMessage = '';
			}
			
			jQuery.post('/modules/invite/ws/ws-invite_friend.php', { emailFriend: emailFriend, friendName: friendName, joinMessage: joinMessage } , function(data) {
				jQuery('#jPop').removeClass('loading');
				jQuery('#jPop').html(data);
			});
		}
	}

	jPop.pop('jPop', {title: 'Inviter un confrère', width: 800, height: 500, buttons: buttons }, function() {
		jQuery('#jPop').parent().parent().children('.ui-dialog-buttonpane').children('button').addClass('button');
		jQuery.post('/modules/invite/ws/ws-invite_friend.php', { } , function(data) {
			jQuery('#jPop').html(data);
		});
	});
	
	return false;
}

var load_contact_detail = function(offset,user,specialite,profession,lieu_travail) {
	jQuery('#wsCurrentContact').addClass('loading');
	jQuery.post('/modules/e-phonebook/ws/ws-read_contact.php', { offset : offset, user : user, specialite : specialite, profession : profession, lieu_travail : lieu_travail } , function(data) {
		jQuery('#wsCurrentContact').removeClass('loading');
		jQuery('#wsCurrentContact').html(data);
		//load_his_friend_list(0,user);
	});
	
}

var add_friend = function(friendId) {
	jQuery.post('/modules/e-phonebook/ws/ws-addremove_friend.php', { friendId : friendId }, function(data) {
		jQuery('#addToFriendLink').removeClass('showContactOpt');
		jQuery('#addToFriendLink').addClass('hideContactOpt');
		jQuery('#removeFriendLink').removeClass('hideContactOpt');
		jQuery('#removeFriendLink').addClass('showContactOpt');
		load_friend_list(0);
	});
}

var remove_friend = function(friendId) {
	jQuery.post('/modules/e-phonebook/ws/ws-addremove_friend.php', { friendId : friendId }, function(data) {
		jQuery('#addToFriendLink').removeClass('hideContactOpt');
		jQuery('#addToFriendLink').addClass('showContactOpt');
		jQuery('#removeFriendLink').removeClass('showContactOpt');
		jQuery('#removeFriendLink').addClass('hideContactOpt');
		load_friend_list(0);
	});
}



var load_his_friend_list = function(offset,user) {
	jQuery('#hisFriendList').addClass('loading');
	jQuery.post('/modules/e-phonebook/ws/ws-friend_list.php', { offset : offset, user : user }, function(data) {
		jQuery('#hisFriendList').removeClass('loading');
		jQuery('#hisFriendList').html(data);
	});
	
}

var open_contact_details = function(offset,user,specialite,profession,lieu_travail) {
	var buttons ;

	jPop.pop('jPop', {title: 'Détail du contact', width: 480, height: 448, buttons: buttons }, function() {
		jQuery('#jPop').parent().parent().children('.ui-dialog-buttonpane').children('button').addClass('button');
		jQuery.post('/modules/e-phonebook/ws/ws-read_contact.php', { offset : offset, user : user, specialite : specialite, profession : profession, lieu_travail : lieu_travail } , function(data) {
			jQuery('#jPop').html(data);
			
			//load_his_friend_list(0,user);
			
			//
			jQuery('#subcontainerContact .user_status').tooltip({
				showURL: false
			});
		});
	});
	
	return false;
}