/**
 * 会员展示页面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('/QMy/~ajax/createComment', param, callback, id); },
	        isNeedVerify: function(param, callback, id) { return nova_call('/QUser/~ajax/isNeedVerify', param, callback, id); }
	    };
	    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://u.youku.com/my_set/type_show.html\" target=\"_blank\">设置及管理</a> | ";
                html += "<a class=\"fNav\" href=\"http://u.youku.com/my_set/type_frame.html\" target=\"_blank\">修改样式</a>";
                new Insertion.After('report',html);
				$('report').parentNode.removeChild($('report'));
            }
        }
        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://u.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" 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();" 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;
    },
    __comment_call__ : function(objectId,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){}
				verify_code_img = "";
        		userShow.cancelReply(); 
				//update_code_img=true;
				nova_update('gbook','/user_gbook/id_'+objectId+'.html','','GET');
        }
    },
    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(){
        if(!islogin()){
            login(userShow.commentReport.bind(userShow));return;
        }
        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));
    },
    flashOver: function() {
        var honerFlash = $('honerFlash');
        if (!honerFlash) return false;
        honerFlash.style.display = 'none';
    },
	gbookWhisper: function() {
		if(!islogin()) {
			login(this.gbookWhisper);return;
		}
		var username = $('hostUserName').value;
		MM_openBrWindow('/my_mail/type_mini_receive_'+encodeURIComponent(username)+'_action_whisper','','location=no,scrollbars=no,width=410,height=320');
	}
};
var userShow = new User.Show();

//var update_code_img=true;
var update_code_tmp="";
var verify_code_img = "";
function verify_code(renew,replyCommentOrder){
	//if(renew!=undefined && renew){
	//	update_code_img=true;
	//}
	if(renew){
			verify_code_img = "";
	}
	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,callid){
		if(result){
				if(verify_code_img == ""){
					var timer = new Date();
					var rand = timer.getTime()+"-"+Math.random();
					verify_code_img = '/verify/'+"?"+rand;
					$('verify_code_value'+update_code_tmp).value='';
				}
			   if($('verify_code_img'+update_code_tmp).src.indexOf(verify_code_img)<0 && verify_code_img!=""){
				$('verify_code_img'+update_code_tmp).src=verify_code_img;
			   }
			Element.show('verify_code'+update_code_tmp);
		}else{
			Element.hide('verify_code'+update_code_tmp);
		}
}

