var tags_dict=new Object();
var in_edit_status=false;
function moreinfo(){
	//in_surprise=false;	
		if (!in_surprise){
		$("#moreinfo").slideUp("");
		in_surprise=true;
		$("span.text1").text("显示详细信息").show();
		}
		else{
	    $("#moreinfo").slideDown("");
		in_surprise=false;
		$("span.text1").text("隐藏详细信息").show();
		}

	};
function addtofav(faction,ftopic,fchange){
$("#showstatus").html("<ul><li><IMG SRC='/images/ajax-loader.gif' align='absmiddle'>　正在操作，请稍后...</li></ul><br/>").show();
url="/doit.asp";
$.post(url, {
ac : "savetofav",
faction : faction,
pid : ftopic,
fchange : fchange},
function fshow(ret){
$("#topic_left").html(ret).slideDown("");
$("#showstatus").html("").hide("slow");
//$("#topic_left").hide();
$("#tag_edit_d").show();
fill_tag();
s_edit_tag();
}
);
}

function save_tm(){
    save_it(function(ret){
            in_edit_status=false;
            $("#topic_left").html(ret);
            $("#showstatus").html("保存完毕<br/>").hide("slow");
        }
    );
}
function save_it(cb){
    box = document.getElementById("privacy");
    var privacy = "";
    if (box && box.checked) {
        privacy = "private";
    }
    tags=document.tagform.mytag.value;
    url = "/doit.asp?pid="+ topic_id +"";
$("#showstatus").html("正在保存...<br/>").show();
    $.post(url, {
        mytag : tags,
        privacy : privacy,
        ac : "savetag"},
        cb);
}
function save_tags(){
    save_it(function(ret){
            in_edit_status=false;
            
           //$("#tag_edit_d").hide();

			$("#tag_edit_d").hide();
			$.post(url, {
ac : "showtag"},
function fashow(ret){
$("#topic_left").html(ret);
$("#showstatus").html("").hide("slow");
//$("#topic_left").hide();
$("#tag_show_d").show();
}
);



        }
    );
}
function save_comment(){
    save_it(function(ret){
            in_edit_status=false;
            $("#edit_link_d").show();
            $("#comment_edit_d").hide();
            show_text = comment == ""?"添加附注":comment;
            $("#comment_span").html(show_text);
            $("#comment_show_d").show();
            $("#ajax_save_bar").html("保存完毕<br/>").hide("slow");
        }
    );
}

function s_edit_tag(){
    if(!in_edit_status){
        $("#tag_show_d").hide();
        $("#tag_edit_d").show();
        $("#edit_link_d").hide();
        $("#tag_tip").show();
        mark_tags();
        in_edit_status=true;
        document.tagform.tags.focus();
    }
}
function s_edit_comment(){
    if(!in_edit_status){
        $("#comment_show_d").hide();
        $("#comment_edit_d").show();
        $("#edit_link_d").hide();
        in_edit_status=true;
        document.tagform.comment.focus();
    }
}
function edit_all(){
    if(!in_edit_status){
    mark_tags();
    $("#tag_show_d").hide();
    $("#tag_tip").show();
    $("#comment_show_d").hide();
    $("#tag_edit_d").show();
    $("#tag_save_bn").hide();
    $("#comment_save_bn").hide();
    $("#comment_edit_d").show();
    $("#edit_pane_d").show();
    $("#edit_link_d").hide();
    in_edit_status=false;
    }
}

function add_tags_dict(tag, id){
    if(!(tag in tags_dict)){
        tags_dict[tag] = id;
    }else{
        tags_dict[tag]=tags_dict[tag]+'|'+id;
    }
}
function arrLookup(arr, elem) {
    for (var i = 0; i < arr.length ; i++) {
        if (arr[i] == elem) {
            return i;
        }
    }
    return -1;
}
function fill_tag(){
    for(i=0;i<30;i++){
        add_tags_dict($("#t_"+i).html(), "t_"+i);
    }
}
function mark_tags(){
    intags = document.tagform.mytag.value;
    var tags_arr=intags.split(" ");
    for(t in tags_dict){
        ids = tags_dict[t].split('|');
        if(-1 != arrLookup(tags_arr, t)){
            for(id in ids){
                $("#"+ids[id]).removeClass("gract");
                $("#"+ids[id]).addClass("rdact");
            }
        }else{
            for(id in ids){
                $("#"+ids[id]).removeClass("rdact");
                $("#"+ids[id]).addClass("gract");
            }
        }
    }
}
function toggle_tag(obj){
    id = obj.id;
    tag=$("#"+id).html();
    orig_tags = document.tagform.mytag.value.replace(/(^\s*)|(\s*$)/g, "");
    ids = tags_dict[tag].split('|');
    var match = new RegExp('(?: |^)'+tag+'(?: |$)');
    if(match.test(orig_tags) == false){
        orig_tags+= " "+tag;
        for(id in ids)$("#"+ids[id]).removeClass("gract").addClass("rdact");
    }else{
        for(id in ids)$("#"+ids[id]).removeClass("rdact").addClass("gract");
        orig_tags = orig_tags.replace(match, ' ');
    }

    document.tagform.mytag.value = orig_tags;
}
