(function(){
  // get guide list
  $.getJSON(
    '/guides/index.php?c=tip&a=getCommonGuides&orderby=favorer&n=5',
    function(ret) {
      if (!ret.status && ret.data) {
        var partial = '';
        var length = ret.data.guides.length;
        var guides = ret.data.guides;

        $.each(guides, function(i) {
          var e = this;
          partial +=
            '<li class="g-item">' +
              '<i class="g-num ' + (i < 2 ? 'r' + (i + 1) : '') + '">' +
                (i + 1) +
              '</i>' +
              '<h3 class="g-title">' +
                '<a class="block titletext J_Name"' +
                ' href="/guides/index.php?a=getGuideContent&id=' +
                e.id + '">' +
                  T(e.name) +
                '</a>' +
              '</h3>' +
              '<p class="g-author">' +
                '<a class="c-gray" href="/user/index.php?a=profile&uid=' +
                e.user_id +
                '">' + T(e.nick) + '</a>' +
              '</p>'
            '</li>';
        });


        if (partial) {
          var list = $(partial).appendTo($('.J_GuideList').empty());
          $k.ui.shorten($('.J_Name', list), 15);
        }
      }
    }
  );

  var WIHCount;
  var WIHs;
  var WIHRolling = true;
  function fill() {
    if (WIHRolling) {
      if (WIHCount==0){
        getWIH(false);
      }
      else {
        removeLast();
        prependToContent(WIHs[--WIHCount]);
      }
    }
    setTimeout(fill,2500);
  }

  function getWIH(init) {
    $.getJSON(
      "/guides/?c=tip&a=getCmts",
      function(ret){
        if (!ret.status) {
          WIHs = ret.data;
          WIHCount = WIHs.length;
          if (init) {
            var WIHIdx = WIHCount;
            while (WIHIdx>0){
              prependToContent(WIHs[--WIHIdx]);
            }
            fill();
          }
        }
      }
    );
  }
  function removeLast() {
    $(".J_WIHItem:last").remove();
  }
  function prependToContent(WIH) {
    if (WIH) {
      $(".J_WIHCtnr").prepend(assembleWIH(WIH));
      $(".J_WIHItem:first").show("slow");
    }
  }
  function assembleWIH(WIH) {
    var item = $('<div class="J_WIHItem" style="display:none;"></div>');
    var link = $(
      '<a href="/user/index.php?c=user&a=profile&uid=' +
      WIH.user_id +
      '"></a>'
    );
    var avatarDiv = $('<div class="J_WIHAvatar img-ctnr"></div>');
    var portrait = link.clone().append($('<img width="50" height="50" src="/resource/portrait/'+WIH.portrait+'"/>'));
    avatarDiv.append(portrait);
    var commentDiv = $('<div class="J_WIHComment"></div>');
    var textDiv = $('<div class="J_WIHText"></div>');
    textDiv.append(link.clone().append(WIH.nick));
    textDiv.append("："+WIH.cmt);
    var timeDiv = $('<div class="J_WIHTime">'+WIH.log_time+'</div>');
    commentDiv.append(textDiv);
    commentDiv.append(timeDiv);
    item.append(avatarDiv);
    item.append(commentDiv);
    item.append($('<div class="clear"></div>'));
    return item;
  }

  function sina_weibo_signin_ready(forwardurl){
    if (!$.trim(forwardurl)){
      forwardurl = '/guides/';
    }
    window.location = forwardurl;
  }
  $.easing.inQuint = function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t*t + b;
  }

  $(function(){
    // scrolling banner
    $(".J_Scroll").scrollable({
      speed: 300,
      vertical: true,
      keyboard: false,
      circular: true,
      easing: 'inQuint'
    }).autoscroll(4000);

    var scrollable = $(".J_Scroll").data("scrollable");

    $(".J_FuncIntro").mouseenter(function(){
      scrollable.stop();
      i = $(this).index('.J_FuncIntro');
      scrollable.seekTo(i);
    }).mouseleave(function(){
      scrollable.play();
    });

    // rolling comments
    $(".J_WIHCtnr").mouseover(function(){
      WIHRolling = false;
    }).mouseout(function(){
      WIHRolling = true;
    });

    getWIH(true);
  });
})();

function sinaWeiboCallback(url) {
  window.location = url;
}

