﻿/**
 * 会员展示页面js
 */
User.Show = Class.create();
User.Show.prototype = {
    initialize : function() {
	    this.NovaAJAX = {
	        validateMyShow : function(param, callback, id) { return nova_call('/QUser/~ajax/validateMyShow', param, callback, id); },
	        delateUser : function(param, callback, id) { return nova_call('/QUser/~ajax/delateUser', param, callback, id); },
	        createComment : function(param, callback, id) { return nova_call('http://www.youku.com/QMy/~ajax/createComment', param, callback, id, 1); },
	        isNeedVerify: function(param, callback, id) { return nova_call('http://www.youku.com/QUser/~ajax/isNeedVerify', param, callback, id, 1); },
			validateUser:function(param, callback, id) { return nova_call('http://v.youku.com/QVideo/~ajax/logon', param, callback, id,1); }
	    };
	    this.startAjax = false;
	    this.gbook = {'currOrder':0,'hasReply':false};
    },
    manageLink : function(userId){
        if(!islogin()) return;
        var __call = function(result){
            if(true == result) {
                var html = "<a class=\"fNav\" href=\"http://www.youku.com/my_set/type_show.html\" target=\"_blank\">设置及管理</a> | ";
                html += "<a class=\"fNav\" href=\"http://www.youku.com/my_set/type_frame.html\" target=\"_blank\">修改样式</a> | ";
                new Insertion.Before('report',html);
            }
        }
        this.NovaAJAX.validateMyShow({'userid':userId},__call);
    },
    modifyInfoLink : function(userId){
        if(!islogin() || $('modifyUserInfoLink')) return;
        var __call = function(result){
            if(true == result) {
                var html = "<a class=\"fBar\" href=\"http://www.youku.com/my_info/type_basic.html\" target=\"_blank\" id=\"modifyUserInfoLink\">修改</a>&nbsp;<span class=\"fBar\">|</span>&nbsp;";
                new Insertion.Before('hideUserInfo',html);
            }
        }
        this.NovaAJAX.validateMyShow({'userid':userId},__call);
    },
    addFavorite : function(target, url){
        if ( window.sidebar && "object" == typeof( window.sidebar ) && "function" == typeof( window.sidebar.addPanel ) ) {
            window.sidebar.addPanel( target, url, '' );
        } else if ( document.all && "object" == typeof( window.external ) ) {
            window.external.addFavorite( url, target );
        }
    },
    delateUser : function(userId){
        var __call = function(result){
            switch (result){
                case -1 : alert('参数校验失败，请及时与管理员取得联系！');break;
                case false : alert('举报失败，可能是数据库操作异常，请及时与管理员取得联系！');break;
                case true : alert('谢谢您的监督，网站内容部门会尽快处理您的举报！');break;
                default : alert('未知的返回值类型，请及时与管理员取得联系');
            }
        }
        this.NovaAJAX.delateUser({'userid':userId},__call);
    },
    gbookBtnAction : function(){
        var actZone = $("reply" + this.gbook.currOrder);if(empty(actZone)) return;
        if(actZone.visible()) actZone.style.display = "none";  
        else actZone.style.display = "block";
        
    },
    cancelReply : function(){
        var commentZone = $("comment" + this.gbook.currOrder);
        if(!empty(commentZone)){
            var replyBox = commentZone.lastChild;
            commentZone.removeChild(replyBox);
        }
        this.gbookBtnAction();
        this.gbook.currOrder = 0;
        this.gbook.hasReply = false;
        //$('gbook_hor').disabled = false;
    },
    checkCommentContent : function(content){
        var reg = /\[Yo1\]|\[Yo2\]|\[Yo3\]|\[Qoo4\]|\[Qoo5\]|\[Qoo6\]|\[Qoo7\]|\[Qoo8\]|\[Yo9\]|\[Qoo10\]|\[Qoo11\]|\[Qoo12\]|\[Qoo13\]|\[Qoo14\]|\[Qoo15\]|\[Yo16\]|\[Yo17\]|\[Qoo18\]/g;    // 创建正则表达式模式。
        var r = new Array();
        r = content.match(reg);   // 尝试匹配搜索字符串。
        if(!empty(r)){
            if(r.length>5){
                alert('表情个数不能多于5个！');
                if(!empty($('commentContent'+this.gbook.currOrder))){
                    $('commentContent'+this.gbook.currOrder).focus();
                }
				/*
                if(!empty($('commentBtn'+this.gbook.currOrder))){
                    $('commentBtn'+this.gbook.currOrder).disabled = false;
                }
				*/
                return false;
            }
        }
        if(trim(content).length == 0){
            alert('请填写留言内容！');
            if(!empty($('commentContent'+this.gbook.currOrder))){
                $('commentContent'+this.gbook.currOrder).focus();
            }
			/*
            if(!empty($('commentBtn'+this.gbook.currOrder))){
                $('commentBtn'+this.gbook.currOrder).disabled = false;
            }
			*/
            return false;
        }
        s = content.replace(reg,'');
        if(s.length>1000){
            alert('留言内容不能超过1000字！');
            if(!empty($('commentContent'+this.gbook.currOrder))){
                $('commentContent'+this.gbook.currOrder).focus();
            }
			/*
            if(!empty($('commentBtn'+this.gbook.currOrder))){
                $('commentBtn'+this.gbook.currOrder).disabled = false;
            }
			*/
            return false;
        }
        return true;
    },
    genReplyCommentContent : function(){
        var name = $('commentName'+this.gbook.currOrder).innerHTML;
        var content = $('content'+this.gbook.currOrder).innerHTML;
        content = content.replace(/<br>/gi,"");
        var newContent = content.split("\n");
        content = newContent.join("<newline>");
        content = content.replace(/<DIV.*\/DIV>/mgi,"");
        content = content.replace(/<newline>/gi,"\n");
        content = content.replace(/<IMG(\salt=(["\u4e00-\u9fa5]*))*\ssrc="([\:a-z0-9\.\/]*)smiley\/([YoQoo0-9]+)\.gif"(\salt=(["\u4e00-\u9fa5]*))*>/gi,"[$4]");
        return "[quote]"+name + "写道：\n"+content+"[/quote]";
    },
    showReplyBox : function(order){
        if(!islogin()){
            login(userShow.showReplyBox.bind(userShow, order));return;
        }
		update_code_img=true;
        if(empty(order)) return; if(this.gbook.hasReply) this.cancelReply();
        this.gbook.currOrder = order;
        
        replyNode = document.createElement("DIV");
        tmpHtml = '<form id="commentForm" name="commentForm"><div id="replyCopy" class="messageReply">'
        +'<textarea id="commentContent'+this.gbook.currOrder+'" class="border bgBlock" style="width:100%;height:50px;" name="commentContent'+this.gbook.currOrder+'" onFocus="verify_code(false,'+order+')"></textarea>'
		tmpHtml+='<div id="verify_code'+order+'" style="float:left;display:none" class="validate">'
		+'<input type="text" name="verify_code_value" id="verify_code_value'+order+'" value="输入右图验证码" />'
		+'<span><img class="key" width=66 height=22 align=absmiddle id="verify_code_img'+order+'"/></span> <a href="#" onclick="verify_code(true,'+order+');return false;">看不清？</a></div>'
		+'<div class="action" style="float:right">'
		+'<button id="commentBtn'+this.gbook.currOrder+'" name="commentBtn'+this.gbook.currOrder+'" onclick="javascript:userShow.commentReply();update_code_img=true" type="button">确定</button>'
	    +'<button onclick="userShow.cancelReply();" type="button">取消</button></div></div>'
		+'<div class="f_c"></div></form>';
		replyNode.innerHTML = tmpHtml;

	    var commentZone = $("comment" + this.gbook.currOrder);
        if(!empty(commentZone)) commentZone.appendChild(replyNode);
        $('commentContent'+this.gbook.currOrder).focus();
        
        this.gbookBtnAction();
        this.gbook.hasReply = true;
    },

    commentReply : function(){
        if(!islogin()){
            login(userShow.commentReply.bind(userShow));return;
        }
        var objectId = $('objectId').value;
        var content  = $('commentContent'+this.gbook.currOrder).value;
        
        if(!this.checkCommentContent(content)) {
            //$('gbook_hor').disabled = true;
            return;
        }

        //$('commentBtn'+this.gbook.currOrder).disabled = true;
        
	var verify_code="";
	if($('verify_code_value'+update_code_tmp) && $('verify_code_value'+update_code_tmp).value){
		verify_code= $('verify_code_value'+update_code_tmp).value;
	}

        content = content+"\n"+this.genReplyCommentContent();
        this.NovaAJAX.createComment({'objectid':objectId, 'content':content,'verify_code':verify_code},this.__comment_call__.bind(this,objectId));
    },
    commentReport : function(){
		
			var t_username = '';
			var t_password = '';
			if(!islogin()){
				if($('username').value==''){
					alert('请填Email或昵称.')
					$('username').focus();
					return false;
				}else{
					t_username = $('username').value;
				}
				if($('password').value==''){
					alert('请填写密码.')
					$('password').focus();
					return false;
				}else{
					t_password = $('password').value;
				}
				this.NovaAJAX.validateUser({username:t_username,password:t_password,forever:1},"validateUserCallBack");
				return;
			}
			var objectId = $('objectId').value;
			var t_content = $('gbooker').value;
			if(new User.Show().checkCommentContent(t_content) == false){
				return false;
			}
			validateUserCallBack(true);
			return;








/*
			Nova.QVideo.comment({videoId:t_videoId,content:t_content,username:t_username,password:t_password,pkId:t_pkid,verify_code:verify_code},'commentCallBack');
			return false;
			*/
        /*
		var objectId = $('objectId').value;
        var content  = $F('gbooker');
	var verify_code="";
	if($('verify_code_value'+update_code_tmp) && $('verify_code_value'+update_code_tmp).value){
		verify_code= $('verify_code_value'+update_code_tmp).value;
	}

        if(!this.checkCommentContent(content)) return;
        //$('gbook_hor').disabled = true;

        //this.NovaAJAX.createComment({'objectid':objectId, 'content':content,'verify_code':verify_code},this.__comment_call__.bind(this,objectId));
		this.NovaAJAX.createComment({'objectid':objectId, 'content':content,'verify_code':verify_code},'showtest');
		*/
    },
    flashOver: function() {
        var honerFlash = $('honerFlash');
        if (!honerFlash) return false;
        honerFlash.style.display = 'none';
    },
	checkCommentContent:function(content){
		var reg = /\[Yo1\]|\[Yo2\]|\[Yo3\]|\[Qoo4\]|\[Qoo5\]|\[Qoo6\]|\[Qoo7\]|\[Qoo8\]|\[Yo9\]|\[Qoo10\]|\[Qoo11\]|\[Qoo12\]|\[Qoo13\]|\[Qoo14\]|\[Qoo15\]|\[Yo16\]|\[Yo17\]|\[Qoo18\]/g;    // 创建正则表达式模式。
		var r=new Array();
		r = content.match(reg);   // 尝试匹配搜索字符串。
		if(r!=null){
			if(r.length>5){
				alert('表情个数不能多于5个');
				return false;
			}
		}
		s = content.replace(reg,'');
		if(s.length>1000){
			alert('评论内容不能超过1000字');
			return false;
		}
		return true;
	}
};
var userShow = new User.Show();

var update_code_img=true;
var update_code_tmp="";
function verify_code(renew,replyCommentOrder){
	if(renew!=undefined && renew){
		update_code_img=true;
	}
	if(replyCommentOrder==undefined){
		replyCommentOrder="";
	}
	update_code_tmp=replyCommentOrder;
	//if(update_code_img){
		userShow.NovaAJAX.isNeedVerify({}, 'verify_code_callback');
	//	update_code_img = false;
	//}
}
function verify_code_callback(result){
		if(result){
			var timer = new Date();
			var rand = timer.getTime()+"-"+Math.random();
			var src = 'http://v.youku.com/verify/'+"?"+rand;
			$('verify_code_img'+update_code_tmp).src=src;
			$('verify_code_value'+update_code_tmp).value='';
			Element.show('verify_code'+update_code_tmp);
		}
}
function showtest(result)
{
        switch(result){
            case -1 : alert('发表留言失败！');break;
            case -2 : alert('您已被管理员禁止发表留言！');break;
            case -3 : alert('内容过长，发表失败！');break;
            case -6 : alert('不能重复发表留言！');break;
            case -7 :
            case -8 : alert('您的留言中含有禁忌词！');break;
            case -9 : alert('请输入验证码');break;
            case -10 : alert('验证码输入错误，请重新输入');break;
            default : 
				try{
		Element.hide("verify_code"+update_code_tmp);
		$("verify_code_value"+update_code_tmp).value="";
				}catch(e){}
        		userShow.cancelReply(); 
				update_code_img=true;
				//nova_update('gbook','/api_ptuser/action_gbook_pid_XNjAw_id_'+objectId+'.html','','GET');
				var myDate = new Date();
				var templ = '<div class="message"><div class="userhead"><img title="'+decodeURI(Nova.Cookie.get('u'))+'" alt="'+decodeURI(Nova.Cookie.get('u'))+'" src="http://u.youku.com/user_icon/id_'+result+'" /></div><div class="content"><div class="messageInfo"><a href="http://u.youku.com/user_show/uid_'+decodeURI(Nova.Cookie.get('u'))+'" target="_blank">'+decodeURI(Nova.Cookie.get('u'))+'</a> '+myDate.getFullYear()+"-"+myDate.getMonth()+"-"+myDate.getDate()+" "+myDate.toLocaleTimeString()+'</div><div class="messageContent">'+Ubb.enCode($F('gbooker'))+'</div></div><div class="clear"></div></div>'
				Insertion.Top("messages",templ);
				$('gbooker').value='';
				if(islogin())Element.hide("login_div");
        }
}
function validateUserCallBack(obj){
	if(obj){
		var objectId = $('objectId').value;
		var content = $('gbooker').value;
		var verify_code=verify_code= $('verify_code_value').value;
		var userObj = new User.Show();
		userObj.NovaAJAX.createComment({'objectid':objectId,'content':content,'verify_code':verify_code},'showtest',objectId);
	}else{
		alert('用户名或密码错误，请重新输入！');
		$('username').focus();
	}
}

