$(document).ready(function(){
	$('#contactList').live("click",function(){
		loadContact();
	});
	$('#contactDemand').live("click",function(){
		loadContact("demand");
	});
	$('#contactAdd').live("click",function(){
		loadContact("add");
	});

});


function loadContact(page){
	var arg="";
	if(page!= null){
		arg="toDisplay="+page;
	}
	loadContactBox(arg);
}

function FriendsList(pagination){
	var arg="pagin="+pagination;
	loadContactBox(arg);
}

function FriendsDemand(pagination){
	
	var arg="toDisplay=demand&pagin="+pagination;
	loadContactBox(arg);
}

function FriendsAdd(pagination){
	var arg="toDisplay=add&pagin="+pagination;
	loadContactBox(arg);
}

function AjoutDirect(name) {
	if(name != ""){
		//var serverUrl = "http://"+$("#server_name").html();
		$.ajax({
			type:"POST",
			url:"/game/contact.php",
			data:"action=addFriend&nameFriend="+name,
			success:function(msg1){
				pop("Demande d'amitié envoyée.");
			}
		});
	}
}

function loadContactBox(arg){
	openPopupPost("/game/contact.php", arg);
}

function acceptFriendDemand(valid,idFriend){
	//var serverUrl = "http://"+$("#server_name").html();
	$.ajax({
		type:"POST",
		url:"/game/contact.php",
		data:"action=acceptFriendDemand&valid="+valid+"&idFriend="+idFriend,
		success:function(msg){
			if (valid == true) {
				sendToActionScript(msg, "toutouStageJump", "acceptFriendDemand");
			}
			else{
				sendToActionScript('"null"', "toutouStageJump", "remFriend");
			}
		}
	});
	//appel au js qui va mettre ï¿½ jour le nombre d'XP directement dans flash
	loadContact("demand");
}

function delDemandFriend(idFriend){
	var serverUrl = "http://"+$("#server_name").html();
	$.ajax({
		type:"POST",
		url:"/game/contact.php",
		data:"action=delDemandFriend&idFriend="+idFriend,
		success:function(msg){
			loadContact("add");
		}
	});
}

function addFriend(){
	var name = $("#addFriendName").val();
	if (name != "") {
		var serverUrl = "http://" + $("#server_name").html();
		$.ajax({
			type: "POST",
			url: "/game/contact.php",
			data: "action=addFriend&nameFriend=" + name,
			success: function(msg1){
				pop(msg1);
				loadContact("add");
			}
		});
	}
}

function delFriend(idFriend){
	var serverUrl = "http://"+$("#server_name").html();
	$.ajax({
		type:"POST",
		url:"/game/contact.php",
		data:"action=delFriend&idFriend="+idFriend,
		success:function(msg){
		}
	});
	loadContact();
}

function lockFriend(idFriend){
	var serverUrl = "http://"+$("#server_name").html();
	$.ajax({
		type:"POST",
		url:"/game/contact.php",
		data:"action=lockFriend&idFriend="+idFriend,
		success:function(msg){
		}
	});
	loadContactBox();
}

function unlockFriend(idFriend, page){
	var serverUrl = "http://"+$("#server_name").html();
	$.ajax({
		type:"POST",
		url:"/game/contact.php",
		data:"action=unlockFriend&idFriend="+idFriend,
		success:function(msg){
		}
	});
	loadContactBox();
}

var containerHeight = 0;
var scrollCotent = '';
function getContainerHeight(o) {
   containerHeight = $(o).parent().height();
   containerHeight -= 20;
}
function getScrollContent(o) {
   sc = $(o).next();
   sch = sc.height();
}
$(document).ready( function() {

  $('.scroll-handle').Draggable(
	{
		zIndex: 	1000,
		containment:     'parent',
		axis:    'vertically',
		//opacity: 	0.7,
		onStart: function() { 
                    getContainerHeight( this );
                    getScrollContent( this )
                },
		onDrag : function(x,y)
		{
                      var move = sch - containerHeight;

                      if(y>0 && y<(containerHeight))
                         sc.top(- y/containerHeight*move + "px");

                      return {x: x,y: y}
		}
	}
 );
});
