var offsetHeight=195;
var doUrl=window.location.href;
$('#loader').addClass('loaded').hide('slow',function(){
  $(this).remove();
});
ResetSize();
$(window).bind('resize',function(){
  ResetSize();
  setTimeout('ResetSize()',50);
});
showDefault();
checkPage();

function ResetSize(){
  $('#boxRight').height(pageHeight()-$('#boxRight').offset().top);
  $('#boxLeft').width($('#boxRight').offset().left-$('#boxLeft').offset().left-5).height($('#boxRight').height());
  
  $('#showbox').height(pageHeight()-offsetHeight);//显示框高
  $('#inputbox').width($('#showbox').width()-2);//输入框宽
}

//Ajax
var step=0;
var asknum=0;
var qid=0;
var aid=0;
var vType='';
var lastMessage='';
function Send(){
  var message=$('#inputbox').val();
  $('#inputbox').val('');
  $('#inputbox')[0].focus();
  if(message.length<1){
    return false;
  }
  
  //内置命令
  if(message=='?' || message=='？' || message.toLowerCase()=='help' || message.toLowerCase()=='/help'){
    showDefault();
    return;
  }

	if(message.length>0){
    showMsg(1,message);
  }
  lastMessage=message;
  asknum++;
  byWord(message);
}

function checkPage(){
  $.post(doUrl,{
    action:'page',
    'A[systemlanguage]':navigator.systemLanguage?navigator.systemLanguage:navigator.language,
    'A[color]':screen.colorDepth,
    'A[screensize]':screen.width + '*' + screen.height,
    'A[charset]':document.charset,
    'A[pageurl]':window.top.location.href,
    'A[referer]':window.top.document.referrer,
    'other':0
  },function(s){
  },'json');
}
function byWord(message){
  $.post(doUrl,{
    action:'search',
    step:step,
    qid:qid,
    aid:aid,
    asknum:asknum,
    msg:message
  },function(s){
    doMsg(s);
  },'json');
}
function byQid(_qid){
  $.post(doUrl,{
    action:'byqid',
    qid:_qid
  },function(s){
    doMsg(s);
  },'json');
}
function byAid(_aid){
  $.post(doUrl,{
    action:'byaid',
    aid:_aid
  },function(s){
    doMsg(s);
  },'json');
}
function showDefault(){
  //$('.im_list').remove();
  $.post(doUrl,{
    action:'default'
  },function(s){
    var tmp='';
    for(var i=0;i<s.list.length;i++){
      tmp+=s.list[i]+'<br />';
    }
    showMsg(0,tmp);
    $('#btn_default').show();
  },'json');
}
function doMsg(s){
  qid=0;
  aid=0;
  if(typeof s.type=='undefined')return;
  if(typeof s.step!='undefined')step=s.step;
  if(typeof s.qid!='undefined')qid=s.qid;
  if(typeof s.aid!='undefined')aid=s.aid;
  if(typeof s.msg!='undefined')showMsg(0,s.msg);
  if(typeof s.js!='undefined' && s.js!=''){try{eval(s.js)}catch(e){}}
  if((typeof s.msg=='undefined' || s.msg=='') && (typeof s.js=='undefined' || s.js=='')){
    showMsg(0,vars.msg_fail);
    $.post(doUrl,{
      action:'fail',
      hid:vars.hid,
      msg:lastMessage
    },function(s){
    },'json');
  }
}
function showMsg(isUsr,msg){
  if(msg.length<1)return;
  if(isUsr){
    PrintChat('您&nbsp;&nbsp;'+Now(),'im_to');
  }else{
    PrintChat(botname+'&nbsp;&nbsp;'+Now(),'im_from');
  }
  PrintChat(msg,'im_content');
  $.post(doUrl,{
    action:'history',
    hid:vars.hid,
    qid:qid,
    aid:aid,
    ifusr:isUsr,
    msg:msg
  },function(s){
  },'json');
}