///django ajax post的时候需要这个方法
$('html').ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});

var mouseover_tid = [];
var mouseout_tid = [];
function nav_main()
{
    jQuery('#ul_nav > li').each(function(index){
        jQuery(this).hover( 
            // 鼠标进入                        
            function(){var _self = this;nav_slide(_self,index,"down");}, 
            // 鼠标离开
            function(){var _self = this;nav_slide(_self,index,"up");} 
        );                
    });
}

function nav_slide(obj,index,type)
{
    var _self = obj;    
    if(type=="down"){
        // 停止卷起事件
        clearTimeout(mouseout_tid[index]);                                
        // 当鼠标进入超过 0.2 秒, 展开菜单, 并记录到线程 ID 中
        mouseover_tid[index] = setTimeout(function() {
                jQuery(_self).find('ul:eq(0)').slideDown(100);
        }, 200);
    }
    else
    {
        // 停止展开事件
        clearTimeout(mouseover_tid[index]);
        // 当鼠标离开超过 0.2 秒, 卷起菜单, 并记录到线程 ID 中
        mouseout_tid[index] = setTimeout(function() {
                jQuery(_self).find('ul:eq(0)').slideUp(100);
        }, 200);
    }
}


function ajax_submit_commnet(news_id)
{
    $('#ajax_messages').html('您的内容正往服务器提交 请稍等...');
    name=$('#id_name').val();
    email=$('#id_email').val();
    weburl=$('#id_weburl').val();
    content=$('#id_content').val();
    //$('#id_submit').attr('disabled','disabled');
    if(name=='' ||email=='' ||content=='')
	    {
	         $('#ajax_messages').html('昵称,邮箱,内容不能为空.');
	         return;
	    }
    else{
        if(!isEmail(email)){$('#ajax_messages').html('邮箱格式不正确.');
        		}
        else if(weburl!=''&&!isURL(weburl)){
            $('#ajax_messages').html('网站地址格式不正确.');
            return;
        		}
        else{
            $.ajax({
                url: "/news/ajax-submit-commnet-"+news_id+"/",
                type: 'post',
                data:{"name":name,"email":email,"weburl":addhttp(weburl),"content":content,"news_id":news_id},
                dataType:"html",
                cache: false,
                error:function(){
                    $('#ajax_messages').html("程序内部出错，操作没有成功！")
                },
                ajaxError:function(){
                    $('#ajax_messages').html("程序内部出错，操作没有成功！")
                },
                success: function(ret){    
                    $('#ajax_messages').html('提交信息成功.');
                    if($('#no_comments')!=null){
                        $('#no_comments').css('display','none');
                    }
                    $('#comments').append(ret);
                    $('#id_content').val('')
                    }        
                });                
            }
    }
    
    //$('#id_submit').removeAttr('disabled');
}

function isEmail(strEmail) {
    if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function isURL(url) {
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)" 
  + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
        + "(([0-9]{1,3}.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
        + "|" // 允许IP和DOMAIN（域名）
        + "([0-9a-z_!~*'()-]+.)*" // 域名- www. 
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 二级域名 
        + "[a-z]{2,6})" // first level domain- .com or .museum 
        + "(:[0-9]{1,4})?" // 端口- :80 
        + "((/?)|" // a slash isn't required if there is no file name 
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
        var re=new RegExp(strRegex); 
    if (re.test(url)) 
        return true;
    else 
         return false;
}

function addhttp(str){
    if(str!=''&&str!=null){
    return 'http://' + str.replace(/^http:\/\//i, '');
    }
    else{return ''}
}

