var serverId,serverName;
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}

$(function(){
  serverId = getQueryString('serverId');
  if(serverId == null) serverId = 1;
  
  switch(parseInt(serverId)){
      case 1: serverName = 'TT'; break;
      case 2: serverName = 'TD'; break;
      case 3: serverName = 'YG'; break;
      case 4: serverName = 'ZY'; break;
      case 5: serverName = 'XY'; break;
      case 6: serverName = 'QD'; break;
    }

})

function getViews(id) {
    var url = DOMAIN.cms + '/baijia/getViewsCount.do?callback=?';    
    var params = {
        "t": new Date().getTime(),
        "contentId":id
    };

    $.getJSON(url, params, function(data) {
        $('#views_'+id).html(toNum(data.datas.views));
    });
}

//显示已关注
function showYiGuanZhu(){
  $('.attention').hide();
  $('.attention-only').show();

  $('.guanzhu').hide();
  $('.quxiao').show();
  if(serverId == 1 || serverId == 3)
    $('.contantlaoshi').show();
}
//显示未关注
function showWeiGuanZhu(){
  $('.attention-only').hide();
  $('.attention').show();


  $('.guanzhu').show();
  $('.quxiao').hide();
  if(serverId == 1 || serverId == 3){   
    $('.jylianxibtn').show();
  }
}
//关注或取消关注
function guanzhu(id, isGuanZhu, isTip) {
  var url = DOMAIN.cms + '/baijia/guanzhu.do?callback=?';    
  var params = {
    "t": new Date().getTime(),
    "channelId":id,
    "isGuanZhu":isGuanZhu,
    "serverId":serverId
  };
  $.getJSON(url, params, function(data) {
    if(!data.datas.isLogin){
      location.href = 'gw://funccall?funcname=OnLoginServer;closeself=true';
    }else{        
      if(data.succes){
        if(isGuanZhu){
          showYiGuanZhu();
          if(isTip)            
            $('#att-succee').html('<p>恭喜您成功关注了'+ blogName +'</p>我们会为您及时推送他的相关文章 ').show(300).delay(3000).hide(300);
        }else{
          showWeiGuanZhu();
          if(isTip)
            $('#att-faile').html('<p>您已成功取消对'+ blogName +'的关注！</p>').show(300).delay(3000).hide(300);
        }
      }
    }
  });
}
//是否已关注
function isGuanzhu(channelId) {
  var url = DOMAIN.cms + '/baijia/isGuanzhu.do?callback=?';    
  var params = {
    "t": new Date().getTime(),
    "channelId":channelId
  };
  $.getJSON(url, params, function(data) {
    if(data.datas.isGuanZhu){
      showYiGuanZhu();
    }else{
      showWeiGuanZhu();
    }
  });
}

var adHtml;//广告
function addAD(type){
  var url = DOMAIN.sso+'/ajax/getLoginUser?callback=?';
  var params = null;
  $.getJSON(url, params, function(data){    
    var permission;
    if(data.code == 1){
      var userType = data.datas.u.userType;
      if(userType == 1)
        permission = 1;
      else if(userType >= 3)
        permission = 2;     
    }else{
      permission = 0;
    }

    url = DOMAIN.ydpApi + "/api/1/ad/zx/query/json?callback=?"
    params = {"server":serverName, "permission":permission};
    $.getJSON(url, params, function(data){    
      if(data.code == 1){
        var ad = data.detail;
        if(ad != null && typeof(ad) != "undefined"){
          ad.adLink = ad.adLink.replace(/=/g,"[equal]");
          if(ad.showWay == 0){
            ad.adLink = 'gw://webkittabdlg?mode=0;resize=1;url='+ ad.adLink;
          }else{
            ad.adLink = 'gw://loadview?view=CGWTabWebBrowser;kernel=WebBaseView;url='+ ad.adLink;
          }

          adHtml = '<a href="'+ ad.adLink +'"><img src="'+ ad.imgUrl +'" /></a>';        
          if(type == "content"){
              adContent();
          }else if(type == "list"){
              adList();
          }
        }
      }
    });
  });
}

function adContent(){
  $('.intro-listDiv').prepend('<div class="jy-bjlx-ad03">'+ adHtml +'</div>');
  $('.intro-listDiv').append('<div class="jy-bjlx-ad04">'+ adHtml +'</div>');
}

function adList(){
  $('.news-info:not(:has(.jy-bjlx-ad05))').append('<div class="jy-bjlx-ad05">'+ adHtml + '</div>');
}

function toNum(num){
  var out;
  if(num >= 10000){
    out = num/10000;
    out = out.toString();
    var point = out.indexOf(".");

    if(point != -1){
      if(out.substr(point+1, 1) == 0)
        out = out.substr(0, point);
      else
        out = out.substr(0, point+2);
    }
    out = out+"万";
  }else{
    out = num;
  }
  return out;
}