String.prototype.getParameter = function(key) {
  var re = new RegExp(key + '=([^&]*)(?:&)?')
  return this.match(re) && this.match(re)[1]
}

var apiurl = "https://tkapi3.352722.com/lhc/lotteryHk"
var data   = {page:1,uid:'',token:'',fr:''}

var storage = window.localStorage
var pram = location.href
var loading = false
var gid = localStorage.getItem('gid');
if(!gid){
  var d = new Date()
  var gid = d.getTime()+Math.floor(Math.random()*(99999)+10000)
  localStorage.setItem('gid',gid)
}
data.appid = 102;
data.gid = gid
data.uid = storage.getItem("uid")
data.token = storage.getItem("token")

if(data.fr = pram.getParameter('fr')){
  storage.setItem('fr',data.fr)
}else{
  data.fr = storage.getItem('fr');
}

$('.content').css('margin-bottom','50px')

function checkCode(ret){
  if(ret.code==1003){
    $.alert(ret.msg,function() {
      storage.removeItem('uid')
      storage.removeItem('token')
      location.href = login_url()
    })
  }

  if(ret.code == 0){
    $.alert(ret.msg,function() {
    })
    return false
  }
}

function loadComments(itemid=0,page){
  data.itemid = itemid
  data.page = page;
  $.post(apiurl + '?a=comments',data,function(ret){
    if(ret.code = 1) $("#infoTpl").tmpl(ret).appendTo(".cgi-pl")
  })
}

function saveTopicReply() {
  data.content = $('#topicReplyContent').val()
  data.tid = $('#commentReplyCommentId').val()
  data.attr = uploadListed.join('|')
  if(!data.content) return $.alert('评论内容不能少于5个字')
  data.itemid = jy.id;
  $.post(apiurl + '?a=add_comment',data,function(ret){
    if(ret.code != 1) return checkCode(ret)
    $.alert(ret.msg,function() {
      window.location.reload(true)
    })
  })
}

function zan(type,typeid) {
  data.type = type
  data.typeid = typeid
  $.post(apiurl + '?a=zan',data,function(ret){
    if(ret.code != 1) return checkCode(ret)
    $('#zan'+typeid+' label').html($('#zan'+typeid).data('v')+ret.data)
    $('#zan'+typeid).addClass('hover')
    $.alert(ret.msg)
  })
}

function cai(type,typeid) {
  data.type = type
  data.typeid = typeid
  $.post(apiurl + '?a=cai',data,function(ret){
    if(ret.code != 1) return checkCode(ret)
      $('#cai'+typeid+' label').html($('#cai'+typeid).data('v')+ret.data)
      $('#cai'+typeid).addClass('hover')
      $.alert(ret.msg)
  })
}

function login_url() {
  var url = '/user/login.html'
  return url + "?callback=" +encodeURI(window.location.href) +"&fr=" + data.fr+"&gid="+data.gid
}

function share_url() {
  return encodeURI(window.location.href) +"?fr=" + data.fr
}

function logout() {
  $.post(apiurl + '?a=logout',data,function(ret){
    if(ret.code != 1) return checkCode(ret)
    $.alert(ret.msg,function() {
      storage.removeItem('uid')
      storage.removeItem('token')
      location.href = '/'
    })
  })
}

function checkin() {
  $.post(apiurl + '?a=checkin',data,function(ret){
    if(ret.code != 1) return checkCode(ret)
    $.alert(ret.msg)
  })
}

function commentReply(id, username) {
  $("#commentReplyCommentId").val(id);
  if (data.uid > 0) {
    $("#topicReplyContent").focus();
    if ($("#replyNameBox").length > 0) {
      $("#replyNameBox .reply-name span").html("@" + username);
      $("#replyNameBox").show();
      var w1 = $("#comment_add .quick-txt").width(), w2 = $(".reply-name").width()
      $("#topicReplyContent").width(w1 - w2 - 22)
    }
  } else {
    $.alert("请登录后操作",function() {
      location.href = login_url()
    })
  }
}

function commentReplyClear() {
  $("#commentReplyCommentId").val("0");
  $("#replyNameBox").hide();
  $("#topicReplyContent").width("100%")
}

function getDateTimeStamp(dateStr){
 return Date.parse(dateStr.replace(/-/gi,"/"));
}