var sd = new Array;
var tc = 0;
var cw = 0;
var ch = 0;
var st = 0;
var sl = 0;
var cy = 0;
var cx = 0;
var sto = undefined;
var to = false;
var tt = undefined;
var se = new Array;
var tot = undefined;

function titov() {
  if ( this.onmouseoverold ) this.onmouseoverold();
  if ( !sd[ this.titid ] ) sd[ this.titid ] = titdiv( this );
  if ( sd[ this.titid ] ) {
    tt = sd[ this.titid ];
    sto = setTimeout( "to = true; titmv()", 1000 );
  }
}

function titou() {
  if ( this.onmouseoutold ) this.onmouseoutold();
  if ( sd[ this.titid ] ) sd[ this.titid ].style.top = -10000;
  if ( sto ) clearTimeout( sto );
  to = false;
  tt = undefined;
}

function titmv() {
  if ( this.onmousemoveold ) this.onmousemoveold();
  if ( to && tt ) {
    var t = cy + 16 + tt.offsetHeight < ch ? cy + st + 16 : ch + st - tt.offsetHeight;
    var l = cx + 16 > cw - tt.offsetWidth ? cw + sl - tt.offsetWidth : cx + sl + 16;
    if ( l < cx + 16 + sl && t < cy + 16 + st ) l = cx - tt.offsetWidth - 4 + sl;
    for ( var i in se ) {
      var po = se[i];
      if ( t < po.t + po.h && t + tt.offsetHeight > po.t ) {
        if ( l < cx && l < po.l + po.w ) l = po.l + po.w;
        if ( l < po.l + po.w && l + tt.offsetWidth > po.l ) t = po.t - tt.offsetHeight;
      }
    }
    tt.style.top = t;
    tt.style.left = l;
  }
}

function getPageOffset( el ) {
  if ( !el ) return false;
  var o = new Object;
  o.l = el.offsetLeft;
  o.t = el.offsetTop;
  o.w = el.offsetWidth;
  o.h = el.offsetHeight;
  while ( ( el = el.offsetParent ) != null ) {
    o.l += el.offsetLeft;
    o.t += el.offsetTop;
  }
  return o;
}

function titles() {
  if ( !document.body && document.readyState != "complete" ) return false;
  se = new Array;
  var sel = document.all.tags( 'select' );
  for ( var i = 0; i < sel.length; i ++ ) se[i] = getPageOffset( sel( i ) );
  cw = document.body.clientWidth;
  ch = document.body.clientHeight;
  st = document.body.scrollTop;
  sl = document.body.scrollLeft;
  document.body.onresize = function() {
    cw = document.body.clientWidth;
    ch = document.body.clientHeight;
    st = document.body.scrollTop;
    sl = document.body.scrollLeft;
  }
  document.body.onscroll = function() {
    st = document.body.scrollTop;
    sl = document.body.scrollLeft;
  }
  document.body.onmousemove = function() {
    cy = event.clientY;
    cx = event.clientX;
  }
  tc = document.all.length;
  for ( var i = 0; i < tc; i ++ ) {
    var t = document.all( i );
    if ( t.alt || t.title ) {
      if ( t.alt ) t.tit = t.alt;
      t.alt = '';
      if ( t.title ) t.tit = t.title;
      t.title = '';
      t.tit = t.tit.replace( /\r?\n/gi, '<br>' );
      t.tit = t.tit.replace( /\r/gi, '<br>' );
      t.tit = t.tit.replace( /(<br\s*\/?>\s*)+$/i, '' );
      t.titid = 'titdiv' + i;
      t.onmouseoverold = t.onmouseover;
      t.onmouseover = titov;
      t.onmouseoutold = t.onmouseout;
      t.onmouseout = titou;
      t.onmousemoveold = t.onmousemove;
      t.onmousemove = titmv;
    }
  }
}

function titdiv( t ) {
  if ( !document.body ) return false;
  var d = document.createElement( 'div' );
  d.id = t.titid;
  d.style.position = 'absolute';
  d.style.zIndex = 1000;
  d.style.border = 'thin ridge gold';
  d.style.filter = 'progid:DXImageTransform.Microsoft.Alpha( Opacity=90, Style=0 ) progid:DXImageTransform.Microsoft.Shadow(color=#000000,direction=135,strength=2)';
  d.innerHTML = '<div style="padding:3;background:url(/pictures/fon_std.gif)">' + t.tit + '</div>';
  d.style.top = -10000;
  d = document.body.insertBefore( d );
  d.style.width = d.clientWidth > 250 ? 254 : d.clientWidth + 4;
  tc = document.all.length;
  return d;
}

function check_titles() {
  if ( document.body && document.readyState == "complete" ) {
    if ( document.body.notit ) return false;
    if ( tc < document.all.length ) titles();
  }
  if ( tot ) clearTimeout( tot );
  tot = setTimeout( "check_titles()", 1500 );
}


function openw( url, name, width, height ) {
  
  if ( height < 100 ) height = 100; if ( width < 100 ) height = 100;
  if ( name == 'view' ) {
    width = 510;
    height = 640;
  }
  var top = screen.availHeight / 2 - height / 2;
  var left = screen.availWidth / 2 - width / 2;
  var w = window.open( url, name,
    'resizable=yes,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no' +
    ',top='+top+',left='+left+',height='+height+',width='+width );
  w.focus();
  return true;
}

function opensroll( url, name, width, height ) {
  
  if ( height < 100 ) height = 100; if ( width < 100 ) height = 100;
  if ( name == 'view' ) {
    width = 530;
    height = 640;
  }
  var top = screen.availHeight / 2 - height / 2;
  var left = screen.availWidth / 2 - width / 2;
  var w = window.open( url, name,
    'resizable=yes,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no' +
    ',top='+top+',left='+left+',height='+height+',width='+width );
  w.focus();
  return true;
}

function refreshframe( sid, dop ) {
        var tstr = parent.frames.mainwar.window.location.href;
        if (tstr.indexOf('?') > 1) tstr = tstr.substr(0,tstr.indexOf('?'));
        tstr = tstr + '?sid=' + sid;
        parent.frames.mainwar.window.location.href = tstr;
    if ( dop ) window.location.href = window.location.href + dop;
		else window.location.reload();
}


function decint( sid ) { 
	if (intr.innerText > 1 ) {intr.innerText--} 
	else {
	if (intr.innerText == 1 ) {
	var tstr = window.location.href;
        if (tstr.indexOf('?') > 1) tstr = tstr.substr(0,tstr.indexOf('?'));
        tstr = tstr + '?sid=' + sid;
        window.location.href = tstr;
		intr.innerText = 0;
		window.clearInterval(int1);
    }
    }
	
	}
	
var ZSHLD = "/img/zshld.gif";
var ZFGHT = "/img/zfght.gif";
var ZSHLDH = "/img/zshld_h.gif";
var ZFGHTH = "/img/zfght_h.gif";

function defender (var1,var2) {
	var DI = document.images;
	if (var1 == "head" || var2 == "head" ) DI.head.src = ZSHLD; else DI.head.src = ZSHLDH;
	if (var1 == "body" || var2 == "body" ) DI.body.src = ZSHLD; else DI.body.src = ZSHLDH;
	if (var1 == "belt" || var2 == "belt" ) DI.belt.src = ZSHLD; else DI.belt.src = ZSHLDH;
	if (var1 == "foot" || var2 == "foot" ) DI.foot.src = ZSHLD; else DI.foot.src = ZSHLDH;
}

function atacker (var1,var2) {
	var DI = document.images;
	if (var1 == "head") DI.ahead.src = ZFGHT; else DI.ahead.src = ZFGHTH;
	if (var1 == "body") DI.abody.src = ZFGHT; else DI.abody.src = ZFGHTH;
	if (var1 == "belt") DI.abelt.src = ZFGHT; else DI.abelt.src = ZFGHTH;
	if (var1 == "foot") DI.afoot.src = ZFGHT; else DI.afoot.src = ZFGHTH;
}

// Загрузка iframe (By Igor)

function loadnc( addr ) { 
  //var ncFrame = document.createElement('<IFRAME src="' + addr + '" style="display: none"></IFRAME>');
  var ncFrame = document.createElement('iframe');

    ncFrame.src = addr;
    ncFrame.style.display = 'none';
  document.body.appendChild(ncFrame);
}

// Чтобы генерация подсказок не тормозила отрисовку самой страницы
function generate_titles () {
  if (!tot && document.readyState == "complete") { // событие срабатывает 4 раза, нам нужен только 1 :)
    tot = setTimeout( "check_titles()", 2000 );
  }
}

//document.onreadystatechange = generate_titles;
if (document.all)
{
    document.onreadystatechange = generate_titles;
}
