function showMessage(text){
  showSysWnd('sysfrm_html','message.php?messageText='+text,1,'36%','37%');
}

function gebi(id){
  return document.getElementById(id)
}


function showError(errMessage){
  showWnd('msg.php?msg='+errMessage)
}

function js_locFrame(){
  swf_external('fl_top','fl_top', 'exit_enabled@1');
  top.frames['main_frame'].frames['gameframe'].location.href = 'location.php'
}

function close(name){
  js_locFrame();
}

function showWnd(id,istop,cx,cy){
  try{
    var obj = istop==1 ? top.document.getElementById(id) : document.getElementById(id)
    obj.style.top = cy!=null ? cy : '30%'
    obj.style.left = cx!=null ? cx : '37%'
    if(navigator.appName.indexOf('Netscape')!=-1 || navigator.appName.indexOf('Mozilla')!=-1)
      obj.style.left = cx!=null ? cx : '0px'
    obj.style.width = '300px'
    obj.style.height = '200px'
    obj.style.display = ''
  }catch(e){alert(e)}
}

function showSysWnd(id,url,istop,cx,cy,wx,wy){
  try{
  istop = parseInt(istop)
    var obj = istop==1 ? top.document.getElementById(id) : document.getElementById(id)
    obj.style.display = 'none'
    var wnd = istop==1? top : window;
    wnd.frames[id+'_frame'].location.href = (url!=null) ? url : 'srvc/ldr/'
  setTimeout(function(){
    wnd.document.getElementById(id+'_frame').contentWindow. document.designMode="off";
  }, 50);
    obj.style.top = cy!=null ? cy : '30%'
    obj.style.left = cx!=null ? cx : '37%'
    if(navigator.appName.indexOf('Netscape')!=-1 || navigator.appName.indexOf('Mozilla')!=-1)
      obj.style.left = cx!=null ? cx : '0px'
    obj.style.width = wx!=null ? wx : '300px'
    obj.style.height = wy!=null ? wy : '200px'
    obj.style.display = ''
  }catch(e){}
}

function hideWnd(id,istop,wobj){
  try{
    var wnd = istop==1 ? top : (wobj ? wobj : window);
    wnd.frames[id+'_frame'].location.href = '/srvc/ldr/'
    var obj = wnd.document.getElementById(id)
    obj.style.display = 'none'
  }catch(e){}
}

/*
function showWnd(scriptUrl, wndTitle, wndWidth, wndHeight){
  wndWidth = wndWidth ? wndWidth : 320
  wndHeight = wndHeight ? wndHeight : 240

  var window = top.window;
  var wndAttributes = 'dialogWidth:'+wndWidth+'px; dialogHeight:'+wndHeight+'px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';

  if(window.showModelessDialog){
    return window.showModelessDialog("msgw.php", {window: window, scriptUrl: scriptUrl, wndTitle: wndTitle}, wndAttributes);
  }else{
    window.open(scriptUrl,'','width='+wndWidth+',height='+wndHeight+',location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
  }
}
*/
function getObjCoords(obj){
  var obj = typeof(obj) == 'string' ? top.document.getElementById(obj) : obj
  var list={'l':obj.offsetLeft,'t':obj.offsetTop,'w':obj.offsetWidth,'h':obj.offsetHeight}

  while(obj=obj.offsetParent){
    list.l+=obj.offsetLeft
    list.t+=obj.offsetTop
    list.w=obj.offsetWidth
    list.h=obj.offsetHeight
  }
  return list
}

function getCursor(e){

  var ee = top.event || e
  var x=(ee.x||ee.clientX)+document.body.scrollLeft
  var y=(ee.y||ee.clientY);//+document.body.scrollTop

  return {'l':x,'t':y}
}

function actAttack(nick){
  top.frames['main_frame'].frames['gameframe'].location.href='action.php?acode=attack&nick='+nick+'&back=location.php&forward=combat.php'
}

var waitFuncId=0
function waitObj(id,evFunc){
  if(document.getElementById){
    if(typeof evFunc=='function'){
      window['waitFunc'+waitFuncId]=evFunc
      evFunc='waitFunc'+waitFuncId
      waitFuncId++
    }
    var obj=(id=='body')?document.body:document.getElementById(id)
    if(obj) window[evFunc]()
    else setTimeout("waitObj('"+id+"','"+evFunc+"')",100)
  }else{
    onload=evFunc
  }
}

function oldexit_game(confirm_str){
  if (!confirm_str) confirm_str="Are you sure to exit from game?";
  if(confirm(confirm_str))
      top.document.location.href= '/logout.php'
}

function exit_game(confirm_str){
  if (!confirm_str) confirm_str="Are you sure to exit from game?";
  if(confirm(confirm_str))
      top.document.location.href= '/logout.php'
}

function exit_game_imm(){
  top.document.location.href= '/logout.php'
}

function set_hp (val, val_max, tm) {
          var swf_caller = 'N/A';
          var swf_target = 'fl_top';
          var result = swf_external(swf_caller, swf_target, 'set_hp@' + val + ',' + val_max + ',' + tm);
     }

function set_exp (val, val_max) {
          var swf_caller = 'N/A';
          var swf_target = 'fl_top';
          var result = swf_external(swf_caller, swf_target, 'set_exp@' + val + ',' + val_max);
}

function set_pers (nick, level) {
          var swf_caller = 'N/A';
          var swf_target = 'fl_top';
          var result = swf_external(swf_caller, swf_target, 'set_pers@' + nick + ',' + level);
}

function get_topswf (name,frame) {
          var win = top;  // here may be links to other frames
  if(frame && frame!=null)
    win = win.frames[frame]
      var obj = null;
          if (navigator.appName.indexOf("Microsoft") != -1) {
               obj = win[name];
          } else {
               obj = win.document[name];
          }
      return obj
}

function get_swf (name, flash_win) {
      var win = flash_win == null ? window : flash_win;  // here may be links to other frames
      var obj = null;
          if (navigator.appName.indexOf("Microsoft") != -1) {
               obj = win[name];
          } else {
               obj = win.document[name];
          }

    return (obj == null) ? get_topswf(name) : obj
}

function swf_external (name, tar, data, flash_win, frame) {
          try {
        if(frame && frame!=null)
                get_topswf(tar,frame).swf_external(name, data);
        else
                get_swf(tar, flash_win).swf_external(name, data);
               return true;
          } catch (e) {
               setTimeout("swf_external('"+name+"','"+tar+"','"+data+"')",100);
          }
          return false;
}

function set_blinks (val) {
    var swf_caller = 'N/A';
    var swf_target = 'fl_menu';
    var result = swf_external(swf_caller, swf_target, 'blinks@' + val);
    log('set_blinks("' + val + '"), result=' + result);
  }

function close_tree(){
  fl_menu_call (top.wmode)
  close('location')
}

function fl_menu_call (val) {
  //swf_external('fl_top','fl_top', 'exit_enabled@1');

  if (top.lastCmbtId != false && val != 'knowledge' && val != 'forum' && val != 'worldmap' && val != 'report' && val != 'sms' && val != 'pays') {
    return false;
  }

  if (top.wmode && val != 'sms' && val != 'pays') {
    swf_external('fl_menu','fl_menu', 'set_exit@');
  }

  if (top.wmode && top.wmode == val) {
    top.wmode = false;
    swf_external('fl_menu','fl_menu', 'set_exit@');
    js_locFrame();
    return false;
  }

  switch (val) {
    case 'clan_list':
      swf_external('fl_clan_list', 'fl_clan_list', 'set_exit@clan_list');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=clan_list';
      top.wmode = val;
      break;
    case 'pers':
    case 'knowledge':
    case 'magic':
    case 'service':
      fl_menu_call(top.wmode);
      //swf_external('fl_menu', 'fl_menu','close_submenu@')
      break;
    case 'backpack':
      swf_external('fl_menu', 'fl_menu', 'set_exit@backpack');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=backpack';
      top.wmode = val;
      break;
    case 'clan':
      swf_external('fl_menu', 'fl_menu', 'set_exit@clan');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=clan';
      top.wmode = val;
      break;
    case 'quest':
      swf_external('fl_menu', 'fl_menu', 'set_exit@quest');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=quests';
      top.wmode = val;
      break;
    case 'stats':
      swf_external('fl_menu', 'fl_menu', 'set_exit@stats');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=state';
      top.wmode = val;
      break;
    case 'form':
      swf_external('fl_menu', 'fl_menu', 'set_exit@form');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=form';
      top.wmode = val;
      break;
    case 'report':
      window.open('bug_report.php');
      break;
    case 'forum':
      window.open('http://forum.tmgame.ru');
      break;
    case 'worldmap':
      window.open('map.php');
      break;
    case 'library':
      window.open(top.infoportal);
      break;
		case 'news':
			window.open(top.infoportal_news);
			break;
    case 'ratings':
      swf_external('fl_menu', 'fl_menu', 'set_exit@ratings');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=ratings';
      top.wmode = val;
      break;
    case 'spells':
      swf_external('fl_menu', 'fl_menu', 'set_exit@spells');
      top.frames['main_frame'].frames['gameframe'].location.href = 'spelltree.php';
      top.wmode = val;
      break;
    case 'sms':
      window.open('http://onlinedengi.ru/howto/sms.php');
      break;
    case 'pays':
      window.open('http://shop.tmgame.ru');
      break;
    case 'pcards':
      window.open(top.flypayurl);
      break;
    case 'options':
      swf_external('fl_menu', 'fl_menu', 'set_exit@options');
      top.frames['main_frame'].frames['gameframe'].location.href = 'settings.php';
      top.wmode = val;
      break;
    case 'post':
      swf_external('fl_menu', 'fl_menu', 'set_exit@post');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=mail';
      top.wmode = val;
      break;
    case 'adaction':
      swf_external('fl_menu', 'fl_menu', 'set_exit@adaction');
      top.frames['main_frame'].frames['gameframe'].location.href = 'user.php?wmode=adaction';
      top.wmode = val;
      break;
    case 'medals':
      swf_external('fl_menu', 'fl_menu', 'set_exit@medals');
      top.frames['main_frame'].frames['gameframe'].location.href = 'medals.php';
      top.wmode = val;
      break;
    default:
      top.wmode = false;
      break;
  }

  if (top.wmode) {
    //swf_external('fl_top','fl_top', 'exit_enabled@0');
    return true;
  }

}

function hint_show (id, label, x, y) {
    //alert(x+'|'+y)
  if(top.hint_disable == 1){
    return;
  }
  show_hint(id, label, x, y, 1)
}

function hint_hide(){
  if(top.hint_disable == 1){
    return false;
  }
  if( top.hint_object!=null && top.hint_object.parentNode){
    try{
      get_swf('fl_hint').hide()
        hint_obj_id=null
        hint_x = 1
        hint_y = 1
    }catch (e){
      setTimeout("hint_hide()",70)
    }
    return false
  }
}


var hint_obj_id=null;
var hint_x = 0
var hint_y = 0
function hint_pos (id, x, y) {

  if(top.hint_disable == 1){
    return false;
  }

  if(top.hint_object == null)
    return false

    var h = parseInt(top.document.body.scrollHeight);
    var holder = top.document.getElementById(id+'_html')
  if(!holder)
    holder = document.getElementById(id+'_html')

  var coords = getObjCoords(holder)

  if( id!='fl_top' && id!='fl_menu' && id!='fl_chat_line'){
    var ho = top.document.getElementById('fl_menu_html')
    if(ho){
      var crds = getObjCoords(ho)
      coords.l+=crds.l+ho.offsetWidth
    }
    var ho = top.document.getElementById('fl_top_html')
    if(ho)
      coords.t+=crds.t+ho.offsetHeight
  }

    hint_obj_id=id;
  //top.hint_object.style.left = coords.l+x+15

    var noset = false,noset2=false;
    if( id!='fl_menu' && id!='content_div'){
    var wxy = get_windowsize();
    if(coords.l+x+top.hint_object.offsetWidth + 15 - wxy.w > 0){
      top.hint_object.style.left = coords.l+x- (coords.l+x+top.hint_object.offsetWidth + 15 - wxy.w);
      noset = true
    }

    //if(coords.t+y+top.hint_object.offsetHeight + 22 - h > 0 || (id=='fl_chatlist' && coords.t+y+top.hint_object.offsetHeight + 22 - h + 415 > 0)){
//      top.hint_object.style.top = coords.t+y+10
//      noset2 = true
//    }
  }
  if(!noset){
        top.hint_object.style.left = coords.l+x+15
    }
  if(!noset2)
    top.hint_object.style.top = coords.t+y+10

  if(id=='fl_chatlist' || id=='content_div')
  top.hint_object.style.top=parseInt(top.hint_object.style.top)+410

  if(parseInt(top.hint_object.style.top)<=0)
    top.hint_object.style.top=1+'px'


  if(parseInt(top.hint_object.style.top)+parseInt(top.hint_object.style.height)>h-22)
    top.hint_object.style.top = h- parseInt(top.hint_object.style.height)-30

    hint_x = parseInt(top.hint_object.style.left)
    hint_y = parseInt(top.hint_object.style.top)
}

function get_windowsize(){
  var x,y;
  if (top.innerHeight){
    x = top.innerWidth;
    y = top.innerHeight;
    // IE 6 Strict Mode
  } else if (top.document.documentElement && top.document.documentElement.clientHeight) {
    x = top.document.documentElement.clientWidth;
    y = top.document.documentElement.clientHeight;
    // other versionsg IE
  } else if (document.body) {
    x = top.document.body.clientWidth;
      y = top.document.body.clientHeight;
  }

  return  {'w':x,'h':y}
}

function hint_size(w,h){
  if(top.hint_disable == 1){
    return false;
  }
  if(top.hint_object == null)
    return false
      top.hint_object.style.top=10+'px'
      top.hint_object.style.left=10+'px'
      top.hint_object.style.width = (w+10)+'px'
      top.hint_object.style.height = (h+10)+'px'
        if(hint_x!=0 && hint_y!=0)
          hint_pos(hint_obj_id,hint_x,hint_y)
  //if(parseInt(top.hint_object.style.top)<0)
  //  top.hint_object.style.top=1
}

function hint_kill(){
  if(top.hint_disable == 1){
    return false;
  }
  if(top.hint_object && top.hint_object.parentNode){
    top.hint_object.style.left = 1
    top.hint_object.style.top = 1
    hint_x = 1
    hint_y = 1
    hint_size(1,1)
  }
}

function hint_label(label){
  if(top.hint_disable == 1){
    return false;
  }
  if(top.hint_object == null)
    return false
  try{
    get_swf('fl_hint').show(label);
  }catch (e){
    setTimeout("hint_label('"+label+"')",70);
  }
}

var timer = null

if(top.hint_object == null || !top.hint_object)
  top.hint_object = top.document.getElementById('fl_hint_html')

function show_hint(id, label, x, y, show){
  if(top.hint_disable == 1){
    return false;
  }

  if(navigator.appName.indexOf('Opera')!=-1)
    return false;
  if(label == '')
    return false

  if(top.hint_object == null){
    top.hint_object = top.document.getElementById('fl_hint_html')
    }

    hint_label(label)
  if(id=='fl_combat' || id=='fl_chat_line' || id=='fl_chatlist'|| (x>=0 && y>=0)){
    var holder = gebi(id+'_html')
    var coords = getObjCoords(holder)
    hint_pos(id, x, y)
  }
}

function reg_exit(){
  document.location.href='../index.php';
}

function game_enter(){
  document.location.href='../game.php';
}

function chat_unshift(text){
  get_swf('fl_chat_line').focus();
  swf_external('fl_chat_line','fl_chat_line', 'unshift_text@'+text);
}

function send_mail(nick){
  top.frames['main_frame'].frames['gameframe'].location.href = '/user.php?wmode=mail&whom='+nick;
}

function show_user_info(nick){
  window.open('/userinfo.php?nick='+nick,'');
}

function show_action_form(id, url){
    showSysWnd('sysfrm_html',url,1,'36%','37%');
}

function flash_actions(id,command){
    if(command == null)
        command = 'cancel'
    swf_external('PHP', id, 'item_custom_action_apply@'+command,top.frames['main_frame'].frames['gameframe'])
//backpack_command-s:
//"cancel" - unlock and nothind do
//"refresh" - reboot bag
//"remove_item,<item_id>" - delete from bag item with id=<item_id>
}

function resize_flash_holder(tag_id, w, h){
    obj = top.document.getElementById(tag_id+'_html')
    if(!obj)
        return false

    if(w){
        obj.style.width = w+'px'
        obj.width = w
    }
    if(h){
         if (navigator.appName.indexOf("Microsoft") != -1){
             if(h<100) h=0
            else h = h+parseInt(20)
            obj.style.height = h+'px'
            obj.height = h
        }else{
             if(h<100) h=0
            obj.style.height = h+'px'
            obj.height = h
        }
        if(h<100)
            obj.style.display = 'none'
    }
}

function js_setSmile(code){
    if(code==null)
        return

    get_swf('fl_chat_line').focus();
      swf_external('fl_chat_line','fl_chat_line', 'insert_text@'+code);
}

function show_smileList(){
    holder =  top.document.getElementById('fl_chat_line_html');
    if(!holder)
        return false;

    obj = top.document.getElementById('sysfrm_html');
    if(obj.style.display==''){
        obj.style.display = 'none'
        return false
    }
     showSysWnd('sysfrm_html', 'smiles.php',1,holder.offsetWidth-parseInt(obj.style.width)+50,holder.offsetTop-320)
    return false
}

function chatAIn(id, thisObj, eventObj){
  hint_x=0
    hint_y=0
    var ms=getCursor(eventObj);
    //alert((ms.l+5)+'|'+ms.t)
    swf_external('content_div','fl_top','hint_show_inv@'+id+','+(ms.l+5)+','+(420+ms.t));
}

function chatAIn2(id, thisObj, eventObj){
    var ms=getCursor(eventObj);
    hint_pos('content_div',ms.l+5,ms.t);
}

function chatAOut(){
  swf_external('php','fl_top','hint_show_inv@-1');
}

function chatChannelFormat(channel){
  switch(channel){
    case "public":
      return 1
    case "private":
      return 8
    case "trade":
      return 4
    case "clan":
      return 2
  }
}

function chat_lChannel(channel, mode){
  channel = parseInt(chatChannelFormat(channel))
  pchannel = parseInt(top.lChnl)

  if(mode!=null && mode!=0)
    top.lChnl = pchannel | channel
  else
    top.lChnl = pchannel & ~channel
}

function chat_wChannel(channel){
  channel = parseInt(chatChannelFormat(channel))
  top.wChnl = channel
}

function chat_lChannelGet(){
	return top.lChnl;
}

function chat_wChannelGet(){
	return top.wChnl;
}

function sysStringCompact(s){
  var res = '';
  try{
    if(s != null){
      res = s.toString().replace(/\|/g, ",");
    }
  }catch(e){alert(e)}
  return res;
}

function sysStringCreate(functionStringArgument){
  var sysString = "";
  try{
    sysString += "[";
    sysString += sysStringCompact(functionStringArgument) + "|";
    sysString += sysStringCompact(navigator.userAgent) + "|";
    sysString += sysStringCompact(navigator.platform) + "|";
    sysString += sysStringCompact(navigator.cpuClass) + "|";
    sysString += sysStringCompact(navigator.browserLanguage) + "|";
    sysString += sysStringCompact(navigator.systemLanguage) + "|";
    sysString += sysStringCompact((screen.width+"x"+screen.height+"x"+screen.colorDepth));
    sysString += "]";
  }catch(e){alert(e)}
  return sysString;
}

function log_http_requset(delta) {
  var swf_caller = 'N/A';
  var swf_target = 'fl_top';
  var nowtime = ((new Date()).getTime()).toString();
  var result = swf_external(swf_caller, swf_target, 'log_http_requset@' + nowtime + ',' + delta.toString());
}

function log_get_timestamp() {
  var nowtime = ((new Date()).getTime()).toString();
  return nowtime;
}

function Tmclient_notify(action,parameter)
{
  try{
    if(typeof(Tmclient_notify_internal)=="function" || typeof(top.Tmclient_notify_internal)=="function"){
      // Due changes in chat interface, formatting message as they were before
      if(action == "newchatmsg" || action == "newchatmsg_prv"){

        top.Tmclient_notify_internal("privatemsg","");
        parameter = "<div align='left' style='width:450px;'>"+parameter+"</div>";
      }
      top.Tmclient_notify_internal(action,parameter);
      return true;
    }
  }catch(e){};
  return false;
}

function bfn_showLoginForm(){
    $('loginForm').setStyle({
      width: 350,
      height: 200
    });

    var wnd_size = get_windowsize();
    var left = ($('body').getWidth() - $('loginForm').getWidth()) / 2;
    var top = (wnd_size.h - $('loginForm').getHeight()) / 2;

    $('loginForm').show();
    $('loginForm').absolutize();
    $('loginForm').setStyle({
      left: left,
      top: top
    });
}

function funcPwdRecovery(){
  window.open('/srvc/prt/');
  return false;
}

function jsle_show_scroll(){
  if (!jsle_islock_scroll()){
    if (Prototype.Browser.IE) {
      document.body.scroll="yes";
    } else {
      document.body.style.overflow="visible";
    }
  }
}

function jsle_hide_scroll(){
  if (!jsle_islock_scroll()){
    if (Prototype.Browser.IE) {
      document.body.scroll="no";
    } else {
      document.body.style.overflow="hidden";
    }
  }
}

function jsle_lock_scroll(){
  jsle_ScrollLock += 1;
}

function jsle_unlock_scroll() {
  if (jsle_ScrollLock > 0) {
    jsle_ScrollLock -= 1;
  }
}

function jsle_islock_scroll(){
  return (jsle_ScrollLock > 0);
}

var jsle_ReadOnly = false;
var jsle_ScrollLock = 0;