onez.wxpay=onez.wxpay||{};
(function(onez,wxpay){
  wxpay.option={};
  wxpay.init=function(option){
    wxpay.option=option;
  };
  wxpay.callback=function(option){
    wxpay.update(option.orderno);
  };
  wxpay.update=function(orderno){
    if($('#win-wxpay').length<1){
      return;
    }
    $.ajax({
      url: wxpay.option.updateurl,
      timeout : 60, 
      type:"POST",
      dataType : "json", 
      cache:false,
      data : {orderno:orderno}, 
      success:function(json){
        if(json.status=='ok'){
          if(typeof wxpay.payok=='function'){
            wxpay.payok(json);
          }else{
            location.reload();
          }
          /*
          if(typeof json.return_code=='string'){
            eval(json.return_code);
          }
          location.href=json.goto;
          */
          return;
        }
        setTimeout("onez.wxpay.update('"+orderno+"')",2000);
      },
      error: function(xhr,status,error){
        setTimeout("onez.wxpay.update('"+orderno+"')",2000);
      }
    });
  };
  wxpay.pay=function(url,postdata){
    if(typeof postdata=='string'){
      postdata=decodeURIComponent(postdata);
      postdata=JSON.parse(postdata);
    }
    if(typeof postdata!='object'){
      postdata={};
    }
    if(typeof postdata['goto']=='undefined'){
      postdata['goto']=window.location.href;
    }
    $.post(url,postdata,function(data){
      if(data.url){
        location.href=data.url;
        return;
      }
      $('#win-wxpay').remove();
      var win=$('<div id="win-wxpay" style="position: absolute;left: 0;top: 0;z-index:6200103;" />');
      var maskbg=$('<div class="win-wxpay-maskbg" style="position: absolute;left: 0;top: 0;background:#000;filter:alpha(opacity=50);-moz-opacity:0.5;-khtml-opacity: 0.5;opacity: 0.5;"></div>');
      var body=$('<dl style="position: absolute;background: #fff;padding: 20px;border-radius: 5px;box-shadow:0px 0px 8px #666;"><dt style="text-align:center;line-height:2;padding-bottom:10px">请使用微信扫描下列二维码</dt><dd><img src="'+data.qrcode+'" width="256" height="256" style="max-width:none;max-height:none" /></dd></dl>');
      maskbg.width($(window).width());
      maskbg.height($(window).height()+$(window).scrollTop());
      win.append(maskbg).append(body);
      win.click(function(){
        $('#win-wxpay').remove();
      }).appendTo($('body'));
      body.css({left:Math.floor(($(window).width()-body.width())/2)+'px'});
      
      var top=Math.floor(($(window).height()-body.height())/2);
      if(top<$(window).scrollTop()){
        top=$(window).scrollTop();
      }
      body.css({top:top+'px'});
      
      setTimeout("onez.wxpay.update('"+data.orderno+"')",2000);
    },'json');
  };
})(onez,onez.wxpay);