var Interact = {
	friendIds : [],
	o: {
		pm: 	  'pm',
		updown:   'iUpdown',
		up:       'iUp',
		down:     'iDown',
		share:    'iShare',
		trans:    'iTrans',
		report:   'iReport',
		collect:  'iCollect',
		panel_share:   'panel_share',
		panel_trans:   'panel_trans',
		panel_report:  'panel_report',
		handle_share:  'handle_share',
		handle_trans:  'handle_trans',
		handle_report: 'handle_report',
		verify_code_commend: 'verify_code_commend',
		sendLinkButton: 'sendLinkButton',
		verify_code_commend_refresh: 'verify_code_commend_refresh',
		handle_checkfriends: 'handle_checkfriends',
		friendList: 'friendList',
		friends_isloaded: false
	},
	curPanel: null,
	curItem: null,
	updownType:"",
	
	faved:false,
	fav_process:false,
	
	init: function(){
		this.attach();
		this.initUpDowned();
		
	},
	
	attach: function(){
		var _this = this;
		
		var updown = $(this.o.updown);
		var up = $(this.o.up);
		var down = $(this.o.down);
		
		up.onclick = function(){
			Interact.updown("up");
		}
		down.onclick = function(){
			Interact.updown("down");
		}
		
		var collect = $(this.o.collect);
		collect.onclick = function(){
			if(Interact.faved || Interact.fav_process){
				return;
			}
			Nova.QVideo.addFav({videoId:videoId2}, Interact.addFavCallBack);
			Interact.fav_process = true;
		}
		
		
		var verify_code_commend = $(this.o.verify_code_commend);
		if(verify_code_commend){
			verify_code_commend.onfocus = function(){
				Interact.updateSendlinkVerifyCode();
			}
		}
		
		var verify_code_commend_refresh = $(this.o.verify_code_commend_refresh);
		if(verify_code_commend_refresh){
			verify_code_commend_refresh.onclick = function(){
				Interact.updateSendlinkVerifyCode(true);
			}
		}
		var sendLinkButton = $(this.o.sendLinkButton);
		if(sendLinkButton){
			sendLinkButton.onclick = function(){
				_this.sendlink();
			}
		}

		
		
		var share = $(this.o.share);
		var share_panel = $(this.o.panel_share);
		var share_handle = $(this.o.handle_share);
		
		if(share && share_panel && share_handle){
			share.onclick = function(){
				_this.showPanel(share_panel, share);
			};
			share_handle.onclick = function(){
				_this.hidePanel(share_panel, share);	
			};
		}
		
		var trans = $(this.o.trans);
		var trans_panel = $(this.o.panel_trans);
		var trans_handle = $(this.o.handle_trans);
		if(trans && trans_panel && trans_handle){
			trans.onclick = function(){
				_this.showPanel(trans_panel, trans);	
			};
			trans_handle.onclick = function(){
				_this.hidePanel(trans_panel, trans);	
			};
		}

		var report = $(this.o.report);
		var report_panel = $(this.o.panel_report);
		var report_handle = $(this.o.handle_report);
		if(report && report_panel && report_handle){
			report.onclick = function(){
				_this.showPanel(report_panel, report);	
			};
			report_handle.onclick = function(){
				_this.hidePanel(report_panel, report);	
			};
		}
		
		var friends_handle = $(this.o.handle_checkfriends);
		var friends_panel = $(this.o.friendList);
		friends_handle.onclick = function(){
			if(!islogin()){
				return openLoginDialog(videoLogin);
			}
			if(friends_panel.style.display != 'none'){
				friends_panel.style.display = 'none';
				this.innerHTML = '选择好友';
			}else if(friends_panel.style.display != 'block'){
				friends_panel.style.display = 'block';
				this.innerHTML = '收起列表';
				if(!Interact.friends_isloaded){
					 var __call__ = function(result){
						if(-1 == result) {
							return openLoginDialog(videoLogin);
						}else if (false == result){
							alert('数据库操作异常，请与管理员联系！');
						}else{
							Interact.friends_isloaded = true;
							friends_panel.innerHTML = result;
						}
					}
					nova_request(__call__, '/contact/friends/','','GET');
				}
			}
			
		}
	},
	updateSendlinkVerifyCode:function(renew){
		var img = $("verify_code_commend_img");
		if(img.src==undefined || img.src==""){
			renew = true;
		}
		if(renew){
			var timer = new Date();
			var rand = timer.getTime()+"-"+Math.random();
			var src = '/verify/'+"?"+rand;
			img.src=src;
			img.style.display="";
			$("verify_code_commend_refresh").style.display="";
		}
	},
	showPanel: function(panel, option){
		if(panel){
			if(this.curPanel != panel){
				if(this.curPanel){
					this.hidePanel(this.curPanel, this.curItem);					
				}
				panel.style.display = 'block';
				option.className = 'item current';
				this.curPanel = panel;
				this.curItem  = option;	
			}else{
				this.hidePanel(panel, option);
			}
			return true;
		}
		return false;
	},
	hidePanel: function(panel, option){
		if(panel){	
			panel.style.display = 'none';
			option.className = 'item';
			this.curPanel = null;
			this.curItem  = null;
			return true;
		}
		return false;
	},
	
	updown: function(type){
		if((act=Nova.Cookie.get("updown_"+videoId2))!==false &&act!=null){
			return Interact.showUpDowned(act);
		}
		Interact.updownType = type;
		Nova.QVideo.updown({videoId:videoId2,type:type}, this.updownCallback);
	},
	updownCallback : function(result, callid) {
		var typeString = "顶";
		if(Interact.updownType == "down"){
			typeString = "踩";
		}
		if(result==-1){
			alert('您已经顶/踩过');
			Interact.showUpDowned(0);
			return;
		}
	
		if(result==-3){
			alert(typeString+'操作失败。');
			return;
		}
	
		if(result==-4){
			alert('您不能'+typeString+'自己的视频。');
			Interact.showUpDowned(0);
			return;
		}
		if(2 == result){
		    RRFeedPublishTip(typeString);
		}

		if(Interact.updownType == "down"){
			Interact.showUpDowned(-1);
		}else{
			Interact.showUpDowned(1);
		}
		
	},
	
	showUpDowned : 	function (times){
		if(times == 0 || times == -1 || times == 1){
			var updown = $(this.o.updown);
			var upTimes = parseInt($('upVideoTimes').innerHTML);
			var downTimes = parseInt($('downVideoTimes').innerHTML);
			if(times == -1){
				if(Interact.updownType=="down")downTimes = downTimes + 1;
				updown.innerHTML = '您已踩过 ';
			}else if(times == 1){
				if(Interact.updownType=="up")upTimes = upTimes + 1;			
				updown.innerHTML = '您已顶过 ';
			}else{
				updown.innerHTML = '您已顶/踩过 ';
			}
			updown.innerHTML +=  '<span class="ico__up"></span><span class="num">'+upTimes+'</span>'
							 + '<span class="ico__down"></span><span class="num">'+downTimes+'</span>';
			updown.className = 'item disabled';
					
		

			Nova.Cookie.set("updown_"+videoId2,times,1);
		}
	},
	initUpDowned : function (){
	   if($('downVideoTimes2') && $('downVideoTimes2').innerHTML == "") $('downVideoTimes2').innerHTML = $('downVideoTimes').innerHTML;
	   if($('upVideoTimes2') && $('upVideoTimes2').innerHTML == "") $('upVideoTimes2').innerHTML = $('upVideoTimes').innerHTML;
		if((act=Nova.Cookie.get("updown_"+videoId2))!==false &&act!=null){
			return Interact.showUpDowned(act);
		}
	},
	addFavCallBack : function (result,callid){
		
		Interact.fav_process = false;
		var collect = $(Interact.o.collect);
		if(result==-1){
			openLoginDialog(videoLogin);
			return;
		}else if(result==-2){
			Interact.faved = true;
			collect.innerHTML = '已收藏。';
			collect.className = 'item disabled';
		}else if(result==-3){
			alert('收藏失败');
			return;
		}else if(result==-4){
			alert('您不能够收藏自己的视频');
			return;
		}
		
		if(2 == result){RRFeedPublishTip('收藏视频');}		
		
		Interact.faved = true;
		collect.innerHTML = '收藏成功';
		collect.className = 'item disabled';
	},
	sendlink:function(){
		_this = this;
	   var objType = $('objectType').value;
	   var objId = $('objectId').value;
	   if(empty(objType) || empty(objId)){
		   alert('参数错误！');
	   }
	   
	   var title = $('sendTitle').value;
	   var msg = $('sendMsg').value;
	   if(empty(title)){
		   alert('消息标题不能为空！');
		   $('sendTitle').focus();
		   return;
	   }
		  if (empty(msg) || msg.length < 8 || msg=='请填写推荐理由，至少八个字') {
		   alert('推荐理由不能为空，至少8个字!');
		   $('sendMsg').focus();
		   return;
	   }
	   
	   var userNames = $('userNames').value;
	   if(empty(userNames)){
		   alert('请选择优酷好友！');
		   return;
	   }
	   var userNameArray = userNames.replace('；',';').split(';');
	   
	   userNameArray = userNameArray.uniq().without('');
	   Interact.friendIds = Interact.friendIds.uniq();
	   if(userNameArray.length > 50 || Interact.friendIds.length > 50){
		   alert('选择的好友数量超过50人，超出系统限制！');
		   return;
	   }
	   var vc = $('verify_code_commend');
	   if(empty(vc.value) || empty(trim(vc.value)) || vc.value == '输入右图验证码') {
			alert('请输入验证码！');
			vc.focus();             
			return;                 
	   }                        
	   
	   var share = function(param, callback, id) { return nova_call('/QContact/~ajax/share', param, callback, id); };
		var __call__ = function(result){
			if(-1 == result){
				return openLoginDialog(videoLogin);
			}else if (-2 == result){
				alert('参数校验失败！');
			}else if (-3 == result){
				alert('发送好友数量超出系统限制！');
				return; 
			}else if (-4 == result){
				alert('验证码输入有误！');
				vc.focus(); vc.select();
				return;
			}else{
				alert('转发视频成功！');
				var trans = $(_this.o.trans);
				var trans_panel = $(_this.o.panel_trans);
				_this.hidePanel(trans_panel,trans);
			}
		}
	   
	   share({'objtype':objType,'objid':objId,'usernames':userNameArray.join(';'),'subject':title,'content':msg, 'verify_code':vc.value}
	, __call__);
	}


}
/*{{{*/

/**
 * 选中一个好友
 */
function selUser(chxObj){
    if(empty(chxObj)) return;
    else if (chxObj.checked) {
        pushUser(chxObj.title, chxObj.value);
    }
    else {
        withoutUser(chxObj.title, chxObj.value);
    }
}
/**
 * 文本框中增加一个好友
 */
function pushUser(userName,userId){
    userName = userName+';';
    var users = $('userNames').value;
    if(empty(users)){
        $('userNames').value = userName;
    }else{
        var lastChar = users.charAt(users.length-1);
        if(lastChar != ';' && lastChar != '；'){
            users = users + ';';
            $('userNames').value = users;
        }
        if(users.indexOf(userName) === -1) $('userNames').value = users + userName;
    }
	if(Interact.friendIds.indexOf(userId) === -1) Interact.friendIds.push(userId);
}
/**
 * 文本框中取消一个好友
 */
function withoutUser(userName,userId){
    var users = $('userNames').value;
    if(empty(users)){
        return;
    }else{
        
        if(users.indexOf(userName) !== -1) $('userNames').value = users.sub(userName + ';', '',10);
    }
	if(Interact.friendIds.indexOf(userId) !== -1) Interact.friendIds = Interact.friendIds.without(userId);
}
function selectCheckBox(){
    if(empty(Interact.friendIds)) return; 
    // 将容器中的id进行一次unique排重容错处理
    Interact.friendIds = Interact.friendIds.uniq();
    var checkBoxes = document.getElementsByName('chx');
    for(var i=0; i<checkBoxes.length; ++i){
        if(Interact.friendIds.indexOf(checkBoxes[i].value) !== -1){
            checkBoxes[i].checked = true;
        }
    }
}
/*}}}*/
window.nova_init_hook_js_interact = function(){Interact.init();}
