function submit_form(id)
{
    document.getElementById(id).submit();
}

function hide(id)
{
    var obj = document.getElementById(id);
    obj.style.display = 'none';
}

function clearText(id)
{
    document.getElementById(id).innerHTML = '';
    document.getElementById(id).value = '';
}

function focuss(id)
{
    document.getElementById(id).focus();
}

function show(id)
{
    var obj = document.getElementById(id);
    obj.style.display = '';
}

function changeDisplay(name, obj2, text1, text2, focus)  //要改变显示隐藏的控件id， 要改变文本的控件对象， 切换的文本。
{
    var obj=document.getElementById(name);
    obj.style.display=(obj.style.display == 'none')?'':'none';
    obj2.innerHTML = (obj.style.display == 'none') ? text1 : text2;
    window.location.hash='hash';
    document.getElementById(focus).focus();
}

var replys = new Array();
var prebox = '';

function replyComment(user_id, username, divid, focus, box)
{
    if (prebox != '' && box != prebox)
    {
        replys.length = 0;
        hide(prebox);
    }
    prebox = box;
    if (user_id != '')
    {
        replys[user_id] = '<span class="reply-box">' + username + ' <a style="cursor:pointer" onclick="delReplyComment(\'' + user_id +'\', \'' + divid +'\', \'' + focus + '\')"><span class="error">X</span></a> </span>';
        replys[user_id] += '<input type="hidden" value="' + user_id + '" name="reply-ids[]"/>';
    }
    genReplyTitle(divid, focus, box);
}

function delReplyComment(user_id, divid, focus, box)
{
    delete replys[user_id];
    genReplyTitle(divid, focus, box);
}

function genReplyTitle(divid, focus, box)
{
    str = '回复 ';
    for (var i in replys)
        str += replys[i];
    if (str == '回复 ') str = '';
    document.getElementById(divid).innerHTML = str;
    window.location.hash = 'focus';
    show(box);
    document.getElementById(focus).focus();
	
}

var replyM = '';
function replyMessage(sendername, divid, focus, box )
{
    if (prebox != '' && box != prebox)
        hide(prebox);
    prebox = box;
    if (sendername != '')
        replyM= '<span class="reply-box">' + sendername + '</span>';
    genReplyMessageTitle(divid, focus, box);
}

function genReplyMessageTitle(divid, focus, box)
{
    str = '回复 ';
    str += replyM;
    document.getElementById(divid).innerHTML = str;
    window.location.hash = 'focus';
    show(box);
    document.getElementById(focus).focus();
}

//表格隔行显示
$(function(){
    $("#alternate tr:even").addClass("even");
})
//表格指向变色
$(function(){
    $("#alternate tr").mouseover(function(){
        $(this).addClass("hover");
    }).mouseout(function(){
        $(this).removeClass("hover");
    })
})
//编码框高度自适应
$(function(){
    $('textarea#source_code').focus(function(){
        $(this).height(550);
    })
})

$(function(){
    $('textarea#team_desc').autoResize({
        // On resize:
        onResize : function() {
            $(this).css({
                opacity:0.8
            });
        },
        // After resize:
        animateCallback : function() {
            $(this).css({
                opacity:1
            });
        },
        // Quite slow animation:
        animateDuration : 300,
        // More extra space:
        extraSpace : 80
    });
})

function realLen(str, len)
{
    var i,sum;
    sum=0;
    for(i=0;i<str.length;i++)
    {
        if (str.charCodeAt(i)>=0 && str.charCodeAt(i)<=255)
            sum=sum+1;
        else
            sum=sum+2;
        if (sum >= len) break;
    }
    return i;
}


function breakWord(intLen, name)
{
    var obj=document.getElementsByName(name);
    var lines=new Array();
    for (var i=0; i<obj.length; i++)
    {
        lines=obj[i].innerHTML.split('\n');
        obj[i].innerHTML = "";
        for (var j=0; j<lines.length; j++)
        {
            var line = lines[j];
            var strTemp = "";
            while(line.length>realLen(line, intLen))
            {
                strTemp += line.substr(0, realLen(line, intLen))+"\n";
                line = line.substr(realLen(line, intLen));
            }
            strTemp += line;
            obj[i].innerHTML += strTemp + "\n";
        }
    }
}

function key_submit(event, id)
{
    event = event ? event : (window.event ? window.event : null);
    if (event.ctrlKey && event.keyCode==13) document.getElementById(id).click();
}

function key_nav(event)
{
    event = event ? event : (window.event ? window.event : null);
    if (event.ctrlKey)
    {
        if (event.keyCode==37) window.location.href = document.getElementById('left').href;
        else if (event.keyCode==38) window.location.href = document.getElementById('up').href;
        else if (event.keyCode==39) window.location.href = document.getElementById('right').href;
    }
}

function page_extend(extend_id, url)
{
    $('#' + extend_id + ' img').show();
    $('#' + extend_id + ' a').hide();
    $.ajax({
        url: url,
        type: 'get',
        timeout: 10000,
        error: function(){
            $('#' + extend_id + ' a').show();
            $('#' + extend_id + ' img').hide();
            alert('error');
        },
        success: function(data) {
            $('#' + extend_id).replaceWith(data);
            $(function(){
                $("#alternate tr:odd").removeClass("even");
                $("#alternate tr:even").addClass("even");
            })
        }
    });
}
 
$(function(e){
    var b={
        1:"很水",
        2:"水",
        3:"普通",
        4:"较难",
        5:"变态"
    },
    g=$("#dialog_rate"),
    c=$("#dialog img"),
    d=function(f){
        var h=0||g.val();
        c.each(function(k){
            var i=this.src.replace(/\w*\.gif$/,((k<f)?"sth":((k<h)?"st":"nst"))+".gif");
            this.src=i;
        });
        if(f)
            $("#dialog_rateword").text(b[f]);
        else
            $("#dialog_rateword").text(h!=0?b[h]:"");
    };
    c.hover(function(){
        d(this.id.charAt(4))
    },function(){
        d()
    });
    if(g.attr("value")){
        c.click(function(){
            var f=this.id.charAt(4);
		var valuate_url=$("#form_valuate").attr("action");
            g.val(f);
		$.post(valuate_url,{dialog_rate:g.attr("value")},function(data){});	
            d(f);
        })
    }
    d();
});

$(function(){
	var collect_url=$("#form_collect").attr("action");
	var adminDelTag_url=$("#form_adminDelTag").attr("action");
  var userDelTag_url=$("#form_userDelTag").attr("action");


    	$("#dialog").dialog({
       	buttons:{
            		"保存":function(){
			$.post(collect_url,function(data){}); 
			$("#form_tag").submit();
			$("#dialog").dialog("close");
			}
            	},
       	width:400,
	       draggable:false,
       	resizable:false,
       	autoOpen:false,
       	bgiframe: true
    	});

    	$("#collect").click(function(){
       	$("#dialog").dialog("option","title","收藏本题");
        	$("#dialog").dialog("open");
    	});

	$(".adminDelTags").click(function(){
		$(this).hide();
		$.post(adminDelTag_url,
			{tag_id:$(this).attr("value")},
			function(data){
			});
  })

  $(".userDelTags").click(function(){
		$(this).hide();
		$.post(userDelTag_url,
			{tag_id:$(this).attr("value")},
			function(data){
			});
	});
}); 

$(function(){
$("#showtags").click(function(){
        if($("#advtags").is(":hidden")){
            $(this).html("缩起 ▲");
            $("#advtags").show();
        }
        else
        {
            $(this).html($(this).attr("rel"));
            $("#advtags").hide();
        }
    });
});

$(function(){
    var e={},c={};//e为选中的标签
    var b=function(h){
        if(e[h]){
            c[h]=true;
            $.each(e[h],function(k,j){
                $(j).removeClass("gract").addClass("rdact")
                })
        }
    };//打标签css
    var d=function(h){
        if(e[h]){
            delete c[h];
            $.each(e[h],function(k,j){
                $(j).removeClass("rdact").addClass("gract")
                })
        }
    };//取消打上的标签css

    var g=function(){
        var h=$.trim($("#tags_title").attr("value").toLowerCase()).split(" "),i={};
        $.each(h,function(k,j){
            if(j!=""){
                b(j);
                i[j]=true
            }
        });
        for(t in c){
            if(!i[t]){
                d(t)
            }
        }
    };

    $('.tagbtn').each(function(j){
        var h=$(this).text().toLowerCase();
        if(e[h]){
            e[h].push(this)
        }
        else{
            e[h]=[this]
        }
    }).click(function(){
        var h=$(this).text();
        var k=$.trim($("#tags_title").attr("value")).split(" "),o=false,j=h.toLowerCase(),l;
        k=$.grep(k,function(value){
            if(value.toLowerCase()==j){
                d(j);
                o=true;
                return false
            }
            else{
                return true
            }
        });//遍历k检查k中是否存在所点击的标签如果有则去除
        if(!o){
            k.push(h);
            b(j)
        }//如果不存在则添加到最后
        var n=k.join(" ");//用空格连接数组中的标签
        $("#tags_title").attr("value",(n.length>1)?$.trim(n)+" ":n);$("#tags_title").focus(); //如果数量多于1个标签
    });
    $("#tags_title").keyup(g);
});

$(function(){
  $('.pcomment-text').each(function(j){
    }).hover(function(){
      var mid = $(this).attr('mid')
      if(mid)
        submit_form('message_mark_'+mid)
  });
});
        /*var populate_tag_btns=function(g,h,b,e){
	if(b.length){
		var c=$("<dl><dt>"+g+"</dt></dl>"),f=$("<dd></dd>");
		$.each(b,function(k,d){var j=$('<div class="tagbtn"><span>').addClass(e[d.toLowerCase()]?"rdact":"gract").text(d);f.append(j).append(" &nbsp; ")});
		c.append(f);
		h.append(c)
	}
};*/



