/*
	新闻频道
 */

$(document).ready(function(){
						   
    var url = window.location.href;
	
    var url_array = url.split( "/" );
	
    var type = parseInt(url_array[url_array.length - 2 ]);
    if( type > 0 ){
        $("#news_index").removeClass("on");
		
        //当前的导航栏class设置为on
        $("#news_"+type).addClass("on");
    }
       
});

function form_button() {
    var form = $("#commentary_form");
    var remark=form.find("input[name='remark']");
    var star=0;
    var number =0;
    var num = 0;
    var acticle_id=form.find("input[name='acticle_id']").val();
    if(remark.val()==null)
    {
        star=0;
    }else{
      
        for(var i = 0;i<document.getElementsByName('remark').length;i++){
            num=document.getElementsByName('remark')[0].value;
            number=document.getElementsByName('remark')[i].value;
        } 
        star= num-number+1;

    }

    $.ajax({
        cache: true,
        type: "POST",
        url:base+"news/get_by_commentary/"+acticle_id+"/"+star,
        data:$('#commentary_form').serialize(),
      
        async: false,
        error: function(request) {
        
            alert("error");
        },
        success: function(data) {
       
            var ar= $.parseJSON(data);
     
            for(var i = 0; i<ar.length;i++){
                var str="  <li class='comment depth_1'>"+
                " <input type=‘text' style='visibility:hidden'  value ='"+ar[i].remark+"' id='remark' name='remark'>"+
                "  <div class='c_floor'>"+ar[i].remark+"#"+
                "</div>"+
                " <div class='c_avatar'><img class='avatar' src='images/img/thumb.jpg' alt='' /></div>"+
                "   <div class='c_main'>"+
                "   <p>"+ar[i].content+"</p>"+
                "   <div class='c_meta'>"+
                "    <span class='c_author'>用户名：<a href=''>"+ar[i].username+"</a></span>"+ar[i].publish_time+             
                "   </div>"+
                " </div>"+
                " </li>";
                
                  
                var tmp = document.createElement('depth_1');
                tmp.innerHTML =str;//补全你的内容
                document.getElementById('depth_1').appendChild(tmp);
           
              
            }         
        } 
    });
    
}
/**
 * 发布评论
 */
function form_submit() {
    var form = $("#commentary_action");
    var acticle_id=form.find("input[name='acticle_id']").val();
    if( form.find("content").val() == ""){
        alert("内容 不能为空");
        return;
    };
 
    $.ajax({
        cache: true,
        type: "POST",
        url:base+"news/add_commentary",
        data:$('#commentary_action').serialize(),
        async: false,
        error: function(request) {
            alert("error");
        },
        success: function(data) {
            if(data == "ok"){
                alert("发布成功!");
                document.getElementById("commentary_action").reset();
                document.URL= location.reload();
            }else{
                alert("请先登录，再发送消息!");
            }
        }
    });
}


function form_refresh() {
    var form = $("#commentary_form");
    var acticle_id=form.find("input[name='acticle_id']").val();


    $.ajax({
        cache: true,
        type: "POST",
        url:base+"news/refresh/"+acticle_id,
        data:$('#commentary_form').serialize(),
      
        async: false,
        error: function(request) {
        
            alert("error");
        },
        success: function(data) {
            alert(data);
            var ar= $.parseJSON(data);
            alert(ar);
            for(var i = 0; i<ar.length;i++){        
                var str="  <li class='comment depth_1'>"+
                " <input type=‘text' style='visibility:hidden'  value ='"+ar[i].remark+"' id='remark' name='remark'>"+
                "  <div class='c_floor'>"+ar[i].remark+"#"+
                "</div>"+
                " <div class='c_avatar'><img class='avatar' src='images/img/thumb.jpg' alt='' /></div>"+
                "   <div class='c_main'>"+
                "   <p>"+ar[i].content+"</p>"+
                "   <div class='c_meta'>"+
                "    <span class='c_author'>用户名：<a href=''>"+ar[i].username+"</a></span>"+ar[i].publish_time+             
                "   </div>"+
                " </div>"+
                " </li>";
                
                //               document.getElementById('commentlist').innerHTML(tmp);
                //                var tmp = document.createElement('commentlist');
                //         tmp.innerHTML =str;
                document.getElementById('commentlist').innerHTML(tmp);
                tmp.innerHTML =str;//补全你的内容
                document.getElementById('commentlist').appendChild(tmp);
              
            }         
        } 
    });
    
}
