/**
 * 邀请当前用户好友操作
 */
utils.sendInvite = {
		selectedFriends : [],
		
		/**
		 * 用于进行好友选择操作
		 * title : 提示信息
		 * explain : 导航说明
		 * explainValue : 导航说明信息
		 * describevt : 选择说明
		 * notice : 注意说明
		 * sureText : 确定按钮文字
		 * cancelText : 取消按钮文字
		 * sureCB : 确定按钮返回回调
		 * cancelCB : 取消按钮返回回调
		 */
		send : function(title , explain , explainValue , describevt , notice , sureText , cancelText ,  sureCB , cancelCB){
				utils.sendInvite.selectedFriends = [];
				var _html = '<div class="dee_forwardContWrap">'+
									'<div class="mb10"><span class="dee_winMsgEditTitle">'+explain+'：</span><div class="dee_winMsgEdit">'+explainValue+'</div></div>'+
									'<div>'+
									    '<span class="dee_winMsgEditTitle">'+(describevt)+'：</span>'+
									    '<div class="dee_winMsgEdit">' +
									       '<div class="receivers fl mr5" id="receivers_div"></div>' +
									       '<input type="button" id="selectName" value="选择好友" class="selectFriendBtn ml10" />' +
									       '<div id="errorMsg" style="display:none;margin: 30px 0 0 360px;color:red;">请选择车友！</div>' +
								        '</div>'+
									'</div>'+
									'<p class="redFont" style="clear:both;">'+(notice?notice:'注：发出邀请后，你选择的好友都会收到本邀请通知！')+'</p>'+
								
								
									'<div class="pop_addfriend_contactName" style="right:23px;top:96px;width:435px;">'+
											'<div class="wrap_friendGroup">'+
												'<div class="friendGroup verticalSelect">'+
													'<select id="sel_group"><option value="gps" selected>GPS车友</option><option value="fans">我的粉丝</option></select>'+
												'</div>'+
											'</div>'+
											'<ul id = "friendName" class="pop_addfriend_friendName verticalSelect" style="height:120px;">'+
												
											'</ul>'+
											'<div class="wrap_opt">'+
											   // '<a href="javascript:void(0);" id="selectOk" class="btn_auto" style="margin-right:190px;"><span>确定</span></a>'+
												'<a href="javascript:void(0);" id="selectAll" title="" style="_position:relative;_top:2px;">全选</a> | '+ 
												'<a href="javascript:void(0);" id="unSelect" title="" style="_position:relative;_top:2px;">不选</a> '+
                                                '<input type="button" value="确定" style="margin:0 5px 0 100px" class="btn40" id="selectOk">'+
											'</div>'+
									'</div>'+
							'</div>';
				$.msgBox.prompt( {
					title : title,
					html : _html,
					width:550,
					cls:"grayStyle",
					autoClose:false,
					textYes : sureText ? sureText : '发送',
					textNo : cancelText ? cancelText : '取消',
					beforClose : function(){
					},
					yesFun : function(){
						var _userIds = '';
						var addMembers = $(".btn_remove");
						for(var i=0;i<addMembers.length;i++){
							_userIds = _userIds + ","+addMembers.eq(i).attr("removeId");
						}
						
						if(!_userIds){
							$('#errorMsg').html("请选择车友！");
							$('#errorMsg').show();
							return false;
						}
						
						if(sureCB){
							$('#errorMsg').html("");
                            $('#errorMsg').hide();
							sureCB(_userIds?_userIds.substring(1):'');
							$.msgBox.close();
						}
					},
					noFun : function(){
						if(cancelCB){
							cancelCB();
						}
					}
			});
			
			$("#selectName").click(function(){
//				$.ajax( {
//					url : web4s.context_path
//					    + "/friend/carFriendGroup.action",
//					data : {},
//					type : "post",
//					dataType : "json",
//					success : function(data) {
//						if(data.result == 0 ){
//							var _data = [];
//							for(var key in data.friendGroup){
//								_data.push({id:key , name:data.friendGroup[key][0]});
//							}
//							utils.sendInvite.fillFriendTypes(_data);
//							$(".pop_addfriend_contactName").show("fast");		
//							$("#sel_group").sSelect();
//						}else{
//							$.msgBox.alert({
//								title : "警告",
//								msg :"查询好友类型列表失败！",
//								icon:"warning"
//							});
//						}
//					}
//				});
				$(".pop_addfriend_contactName").show("fast");
				$("#sel_group").find('option').removeAttr("selected");
				$("#sel_group").find('option[value="gps"]').attr("selected", "selected");
				$("#sel_group").sSelect();
				utils.sendInvite.fillFriends(0);
			});
			
			$("#submitBtn").click(function(){
			
			});
			
			$("#sel_group").change(function(){
				var type = $('#sel_group option:selected').val();
				if(type == "gps"){
				    utils.sendInvite.fillFriends(0);
				}else if(type == "fans"){
					utils.sendInvite.fillFans();
				}
			});
			
			//提交选中的联系人
			$("#selectOk").click(function(){
				var checkboxs = $("#friendName input:checked");
				for(var i=0;i<checkboxs.length;i++){
					if(!utils.sendInvite.selectedFriends[checkboxs.eq(i).val()]){
						utils.sendInvite.selectedFriends[checkboxs.eq(i).val()] = checkboxs.eq(i).val(); 
						var _tep = $('<a href="javascript:void(0);" title=""><span>'+checkboxs.eq(i).parent().find("font").text()+'</span><b class="btn_remove" removeId="'+checkboxs.eq(i).val()+'"></b></a>');
						_tep.appendTo($("#receivers_div"));
					}
				};
				$(".pop_addfriend_contactName").hide("fast");
			});
			
			//删除联系人
			$(".receivers").click(function(event){
				var _cell = $(event.target);
				if(_cell.attr('class') == 'btn_remove'){
					utils.sendInvite.selectedFriends[parseInt(_cell.attr('removeId'))] = null; 
					_cell.parent().remove();
				}
			});
			
			//全选
			$("#selectAll").click(function(){
				var checkboxs = $("#friendName :checkbox");
				for(var i=0;i<checkboxs.length;i++){checkboxs.eq(i).attr("checked","true");}
			});
			
			//全不选
			$("#unSelect").click(function(){
				var checkboxs = $("#friendName :checkbox");
				for(var i=0;i<checkboxs.length;i++){checkboxs.eq(i).attr("checked","");}
			});
			
			$('#gpsTitle').bind("focus",function(){
				$(this).css({"borderColor":"#D9D9D9","background":"#FFFBD5"});
				$('#donwGpsTips').html("&nbsp;");
			}).bind("blur",function(){
				$(this).css({"borderColor":"#D9D9D9","background":"#fff"});
			});
		},

		fillFriendTypes : function(types){
			var _tmp = null;
			$("#sel_group").html('');
			$('<option value="">全部好友</option>').appendTo($("#sel_group"));
			for(var key in types){
				_tmp = $('<option value="'+types[key].name+'">'+types[key].name+'</option>');
				_tmp.appendTo($("#sel_group"));
			}
			utils.sendInvite.fillFriends(0);
		},
		
		fillFriends : function(typeId){
			/**
			 * todo : 查询当前类别下的好友
			 */
			$.ajax( {
		        url : web4s.context_path
		                + "/friend/queryCarFriendList.action",
		        data : {groupNo : typeId},
		        type : "post",
		        dataType : "json",
		        success : function(data) {
		        	var friends = [];
		        	for(var key in data.carFriendList){
		        		friends.push({id:data.carFriendList[key].userId,name:data.carFriendList[key].nickName});
		        	}
		        	utils.sendInvite.fillFriendsCb(friends);
				}
	    	});
		},
		
		fillFans : function(){
			$.ajax( {
                url : web4s.context_path
                        + "/friend/queryFansList.action",
                data : {},
                type : "post",
                dataType : "json",
                success : function(data) {
                    var friends = [];
                    for(var key in data.fansModel){
                        friends.push({id:data.fansModel[key].userId,name:data.fansModel[key].nickName});
                    }
                    utils.sendInvite.fillFriendsCb(friends);
                }
            });
		},
		
		fillFriendsCb : function(friends){
			var _l = friends.length;
			var _li = null;
			var _span = null;
			$("#friendName").html('');
			for(var i = 0 ; i<_l ; i++){
				if( i%4 == 0){
					_li = $('<li></li>');
					_li.appendTo($("#friendName"));
				}
				_span = $('<span><input type="checkbox" name="cName" value="'+friends[i].id+'"/><font>'+toolUtil.interceptString(friends[i].name,5)+'</font></span>');
				_span.appendTo(_li);
			}
		}
};
