﻿$(document).ready(function() {   
if ($('#commentform').length) {
    $('#commentform').submit(function(){    //ID为 commentform 的表单提交时发生的函数，也就是整个留言输入框 form 的ID。
        jQuery.ajax({
            url: 'http://www.rojoro.com/wp-content/themes/DarkLight_v103/comments-ajax.php',    //刚刚创建的 comments-ajax.php 文件的位置，建议用绝对路径。
            data: $('#commentform').serialize(),   
            type: 'POST',
            beforeSend: function() {
            $('#commenterror').hide();
                $('#commentload').slideDown();  // 让submit 向下滑出
            },
            error: function(request) {    //发生错误时
                $('#commentload').hide();    //隐藏  submit
                $('#commenterror').show("slow").html(request.responseText); //显示 error
            },
            success: function(data) {
                $('textarea').each(function(){
                    this.value='';
                });
                $('#commenterror').hide().html();
                if (!$('#commentlist').length) {
                $('#pinglist').before('<ol id="commentlist"></ol>');}
                $('#commentlist').append(data);    //向ID为 thecomments 的元素添加数据，也就是整个 ol 或 ul
                var new_comment = $('#commentlist li:last').hide();    //让最新添加的数据隐藏
                new_comment.slideDown(1000);    //再显示，这里是为了实现滑出的效果，不想要也可以直接显示
                $('#commentform:input').attr('disabled', true);
                $('#commentload').slideUp("slow");
        setTimeout(function() {
                $('#commentform:input').removeAttr('disabled');
                }, 10000);        //这里是设置10秒之后才可以再次留言，自行设置，单位毫秒。
            }
        });
       return false;
   } );
}})


/***********************************scroll*******************************************/
var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
    if (!destinationLink) destinationLink = document.getElementById(anchor);

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
ss.INTERVAL =setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 30;  //设置滑动时间
ss.addEvent(window,"load",ss.fixAllLinks);


/*************************************隐藏评论资料框*************************************/
   $(document).ready(function() {   //开始
   if($('input#author[value]').length>0){   //判断用户框是否有值
   $("#author_info").css('display','none');   //将id为author_info的对象的display属性设为none，即隐藏
   var change='<span  id="show_author_info" style="cursor: pointer;color: #BF514C;">Edit your profile &raquo;</span>';  //定义change，style是定义CSS样式，让他有超链接的效果，color要根据你自己的来改，当然你也可以在CSS中定义#show_author_info来实现，这样是为了不用再去修改style.css而已！
   var close='<span  id="hide_author_info" style="cursor: pointer; #color: #BF514C;">Close &raquo;</span>';   //定义close
   $('.form_row').append(change);   //在ID为welcome的对象里添加刚刚定义的change
   $('.form_row').append(close);    // 添加close
   $('#hide_author_info').css('display','none');   //隐藏close
   $('#show_author_info').click(function() {   //鼠标点击change时发生的事件
   $('#author_info').slideDown('slow')   //用户输入框向下滑出
   $('#show_author_info').css('display','none');   //隐藏change
   $('#hide_author_info').css('display','inline');  //显示close
   $('#hide_author_info').click(function() {  // 鼠标点击close时发生的事件
   $('#author_info').slideUp('slow')    //用户输入框向上滑
   $('#hide_author_info').css('display','none');  //隐藏close
   $('#show_author_info').css('display','inline'); })})}})  //显示change

/************** Reply ************************************/
function reply(authorId, commentId, commentBox) {
	var author = document.getElementById(authorId).innerHTML;
	var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n/g, "") + '</a>: ';
	appendReply(insertStr, commentBox);
}
function appendReply(insertStr, commentBox) {
	if(document.getElementById(commentBox) && document.getElementById(commentBox).type == 'textarea') {
		field = document.getElementById(commentBox);
	} else {
		alert("The comment box does not exist!");
		return false;
	}
	if (field.value.indexOf(insertStr) > -1) {
		alert("You've already appended this reply!");
		return false;
	}
	if (field.value.replace(/\s|\t|\n/g, "") == '') {
		field.value = insertStr;
	} else {
		field.value = field.value.replace(/[\n]*$/g, "") + '\n\n' + insertStr;
	}
	field.focus();
}

$(document).ready(function(){
$(".reply").hide();
$("#commentlist li").hover(function() {
$(this).find(".reply").show();
}, function() {
$(this).find(".reply").hide();
});
});