Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

Array.prototype.removeKey = function(key) {
    for (var i=0; i<this.length; i++) {
        if (key == this[i]) this.remove(i);
    }
};

function FbFriendPicker() {
    this.onSubmit = null;   //callback function
    this.allFriends = new Array(); //所有的朋友
    this.selectedFriends = new Array();   //選取的朋友
    this.onPickCbFn = null; //選取時呼叫的callback function
}

FbFriendPicker.prototype.init = function(){
    this.allFriends = new Array(); //所有的朋友
    this.selectedFriends = new Array();   //選取的朋友
}

FbFriendPicker.prototype.getPickedFriends = function(){
    return this.selectedFriends;
}

FbFriendPicker.prototype.loadFriends = function(uid, containerId, conditions){
    var fbPicker = this;
    fbPicker.init();
    
    var cbFn = function(){
        //onPick callback
        if (fbPicker.onPickCbFn) {
            fbPicker.onPickCbFn($(this).attr('uid'));
        }
        
        if ('friend_not_picked' == $(this).attr('class')) {
            $(this).attr('class', 'friend_picked');
            fbPicker.selectedFriends.removeKey($(this).attr('uid')); //確保安全，以免重複
            fbPicker.selectedFriends[fbPicker.selectedFriends.length] = $(this).attr('uid');
        } else {
            $(this).attr('class', 'friend_not_picked');
            fbPicker.selectedFriends.removeKey($(this).attr('uid'));
        }
        $('#bb_fri_picker_id').html(fbPicker.selectedFriends.length);
    }
    
    var pickAll = function() {
        $('.friend_not_picked').attr('class', 'friend_picked');
        //fbPicker.selectedFriends = fbPicker.allFriends.clone();
        
        fbPicker.selectedFriends = new Array();
        for (var i=0; i<fbPicker.allFriends.length; i++) {
            fbPicker.selectedFriends[i] = fbPicker.allFriends[i].uid;
        }
        
        $('#bb_fri_picker_all').html(lang.CLEAR);
        $('#bb_fri_picker_all').attr('state', 'empty');
        $('#bb_fri_picker_id').html(fbPicker.selectedFriends.length);
    }
    
    var clearAll = function() {
        $('.friend_picked').attr('class', 'friend_not_picked');
        fbPicker.selectedFriends = new Array();
        $('#bb_fri_picker_all').html(lang.SEL_ALL);
        $('#bb_fri_picker_all').attr('state', 'fill');
        $('#bb_fri_picker_id').html(fbPicker.selectedFriends.length);
    }
    
    var doAll = function() {
        return ('fill' == $(this).attr('state')) ? pickAll() : clearAll();
    }
    
    var send_pick = function() {
        if (fbPicker.onSubmit) {
            fbPicker.onSubmit(fbPicker.selectedFriends);
        }
    }

    var api = FB.Facebook.apiClient;
    var fql = 'select uid, name, pic_square, profile_url from user where uid in (select uid2 from friend where uid1=' + uid + ') '+conditions;
    FB_RequireFeatures(["Connect"], function(){
    api.fql_query(fql, function(friends) {
        fbPicker.allFriends = friends;
        data = '<div><span id="bb_fri_picker_all" state="fill">'+lang.SEL_ALL+'</span>　'+lang.SEL_NUM+'(<span id="bb_fri_picker_id">0</span>)</div><div id="bb_fri_picker_innerDiv">';
        for (i=0;i<friends.length;i++) {
            data += '<div class="friend_not_picked" uid="'+friends[i].uid+'"><div class="f1Div"><img style="width:50;height:50px" src="'+friends[i].pic_square+'" /></div><div class="f2Div">'+friends[i].name+'</div></div>';
        }  
        data +='</div>';
        $('#'+containerId).html(data);
        $('#bb_fri_picker_innerDiv').css({"overflow-y":"scroll","height":"200px","width":"500px","border":"1px solid #ccc","color":"#555","background":"#fff"});  
        $('#bb_fri_picker_all').click(doAll);
        $('#bb_fri_picker_all').css({"cursor":"pointer"});  
        $('.friend_not_picked').click(cbFn);
    });
    })
}

FbFriendPicker.prototype.loadNonAppFriends = function(uid, containerId){
    return this.loadFriends(uid, containerId, ' AND is_app_user=0');
}

FbFriendPicker.prototype.loadAppFriends = function(uid, containerId){
    return this.loadFriends(uid, containerId, ' AND is_app_user=1');
}

FbFriendPicker.prototype.loadAllFriends = function(uid, containerId){
    return this.loadFriends(uid, containerId, '');
}