// --- Common ---

function $1(el) {
  return (typeof(el) == 'string' ? document.getElementById(el) : el);
}

function $1_(id, container, tag) {
  if (typeof(id) != 'string') return id;
  if (!container) return document.getElementById(id); 
  var list = container.getElementsByTagName(tag ? tag.toUpperCase() : '*');
  for (var i = 0, l = list.length; i < l; i++) {
    var el = list[i];
    if (el.id == id) return el;
  }
  return null;
}

function trim(s) { return s.replace(/^\s+/, '').replace(/\s+$/, '') }

function getAbsPos(el) {
  var pos = {left:0, top:0};
  while (el && el.tagName != 'BODY') {
    pos.left += el.offsetLeft;
    pos.top += el.offsetTop;
    el = el.offsetParent;
  }
  return pos;
}

function findSubChild(element, tag, className) {
  var list = element.getElementsByTagName(tag.toUpperCase());
  if (className == null) return (list.length > 0 ? list[0] : null);
  for (var i = 0, l = list.length; i < l; i++) {
    var el = list[i];
    if (el.className == className) return el;
  }
  return null;
}

function findParentNode(el, tag) {
  if (el == null) return null;
  tag = tag.toUpperCase();
  do {
    el = el.parentNode;
  } while (el != null && el.tagName != tag)
  return el;
}

function setupEvent(el, eventType, handler, capture) {
  if (el.attachEvent) el.attachEvent('on'+eventType, handler)
  else if (el.addEventListener) el.addEventListener(eventType, handler, capture);
}

function removeEvent(el, eventType, handler, capture) {
  if (el.detachEvent) el.detachEvent('on'+eventType, handler)
  else if (el.removeEventListener) el.removeEventListener(eventType, handler, capture);
}

function cancelEvent(event) {
  event.returnValue = false;
  if (event.preventDefault) event.preventDefault();
  event.cancelBubble = true;
  if (event.stopPropagation) event.stopPropagation();
}

function isLeftButtonEvent(event) {
  if (isIE) {
    if (event.button == 1) return true;
  } else 
    if (event.button == 0) return true;
  return false;
}


var
  mouseMoveListeners = [],
  mouseAbsPosX = 0,
  mouseAbsPosY = 0;

function addMouseMoveListener(handler, object) {
  var i, o;
  for (i = 0; i < mouseMoveListeners.length; i++) {
    o = mouseMoveListeners[i];
    if (o.method == handler && o.instance == object) return false;
  }
  if (mouseMoveListeners.length == 0)
    setupEvent(document, 'mousemove', mouseMoveEventHandler);
  mouseMoveListeners.push({method: handler, instance: object});
  return true;
}

function removeMouseMoveListener(handler, object) {
  var i, o;
  for (i = 0; i < mouseMoveListeners.length; i++) {
    o = mouseMoveListeners[i];
    if (o.method == handler && o.instance == object) {
      mouseMoveListeners.splice(i, 1);
      if (mouseMoveListeners.length == 0)
        removeEvent(document, 'mousemove', mouseMoveEventHandler);
      return;
    }
  }
}

function mouseMoveEventHandler(event) {
  if (document.body == null) return;
  mouseAbsPosX = event.clientX + document.body.scrollLeft;
  mouseAbsPosY = event.clientY + document.body.scrollTop;
  var i, o;
  for (i = 0; i < mouseMoveListeners.length; i++) {
    o = mouseMoveListeners[i];
    if (o.method) o.method.call(o.instance || window, event);
  }
}


function isNumber(prm) {
  return (typeof(prm.valueOf()) == 'number');
}

function isString(prm) {
  return (typeof(prm.valueOf()) == 'string');
}

function isFunction(prm) {
  return (typeof(prm.valueOf()) == 'function');
}

function isArray(prm) {
  return (typeof(prm.valueOf()) == 'array' || (typeof(prm) == 'object' && 'join' in prm));
}

var
  userAgent = navigator.userAgent,
  isKHTML = (userAgent.indexOf('KHTML') >= 0),
  isOpera = (userAgent.indexOf('Opera') >= 0),
  isIE = (!isOpera && userAgent.indexOf('MSIE') >= 0),
  isIE50 = (isIE && /MSIE 5\.0/.test(userAgent) && navigator.platform == 'Win32'),
  isMozilla = (!isKHTML && !isOpera &&  userAgent.indexOf('Gecko') >= 0);

function setAlpha(el, opacity) {
  if ('opacity' in el.style) el.style.opacity = opacity
  else if ('filter' in el.style) el.style.filter = 'alpha(opacity='+Math.round(opacity*100)+')';
}


if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(item, first) {
    for (var i = first || 0; i < this.length; i++) {
      if (this[i] == item) return i;
    }
    return -1;
  }
}

Array.prototype.addItem = function(item) {
  var i = this.indexOf(item);
  if (i < 0) {
    i = this.length;
    this.push(item);
  }
  return i;
}

Array.prototype.removeItem = function(item) {
  var i = this.indexOf(item);
  if (i >= 0) this.removeItemByIndex(i, true);
  return i;
}

Array.prototype.removeItemByIndex = function(i, shrink) {
  if (shrink) {
    if (this.splice) this.splice(i, 1)
    else {
      for (var j = i+1; j < this.length; j++) this[j-1] = this[j];
      this.length = this.length-1;
    }
  } else {
    if (i < this.length-1) this[i] = null
    else {
      do { this.length = (i--) } while (i >= 0 && this[i] == null);
    }
  }
  return true;
}

//-- Window ------------------------------------

var 
  WindowClassName       = 'window',
  WindowTitleClassName  = 'title',
  WindowBGClassName     = 'modal_window_background',
  WindowDefWidth        = 300,
  WindowDefHeight       = 200,
  WindowCloseBtnImg     = '/i/window/close.gif',
  WindowCloseTitle      = 'Close',
  WindowDefZIndex       = 10,
  WindowDefModalZIndex  = 1000,
  WindowDefModalBgColor = '#F5F5E2',
  WindowDefModalBgOpacity = 0.6,
  WindowDragOpacity     = 0.85,
  WindowContainerId     = 'abs_container';

function showWindow(id, title, content, w, h) {
  var wnd = document.getElementById(id);
  if (!wnd) wnd = newWindow(id, w, h);
  wnd.show(title, content);
  return wnd;
}

function showModalWindow(id, title, content, w, h) {
  var wnd = document.getElementById(id);
  if (!wnd) wnd = newWindow(id, w, h);
  wnd.showModal(title, content);
  return wnd;
}

function newWindow(id, w, h) {
  var wnd = document.createElement('DIV');
  wnd.id = id;
  wnd.className = WindowClassName;
  wnd.style.position = 'absolute';
  wnd.style.display = 'none';
  wnd.wndWidth = w || WindowDefWidth
  wnd.wndHeight = h || WindowDefHeight;
  wnd.wndPosX = 0;
  wnd.wndPosY = 0;
  wnd.innerHTML = Window_getInnerHtml();
  wnd.wndTitle = findSubChild(wnd, 'TD', 'title_content');
  wnd.wndContent = findSubChild(wnd, 'DIV', 'content');
  wnd.closeBtn = findSubChild(findSubChild(wnd, 'TD', 'close'), 'IMG');
  if (wnd.closeBtn) setupEvent(wnd.closeBtn, 'click', Window_closeBtnClick);
  wnd.visible = false;
  wnd.put = Window_put;
  wnd.show = Window_show;
  wnd.showModal = Window_showModal;
  wnd.afterShowHandler = Window_afterShowHandler;
  wnd.close = Window_close;
  wnd.hide = Window_close;
  wnd.bringToFront = Window_bringToFront;
  wnd.getDefaultPos = Window_getDefaultPos;
  wnd.moveTo = Window_moveTo;
  wnd.centerWnd = Window_centerWnd;
  return wnd;
}

function Window_getInnerHtml() {
  return '\
<table class="t" cellspacing="0"><tr>\n\
 <td class="l"></td>\n\
 <td class="m"><div class="t"><img src="/i/1px.gif" /></div><div class="tb"><img src="/i/1px.gif" /></div></td>\n\
 <td class="r"></td>\n\
</tr></table>\n\
<table class="m" cellspacing="0"><tr>\n\
  <td class="l"></td>\n\
  <td class="m">\n\
   <table class="'+WindowTitleClassName+'" cellspacing="0"><tr>\n\
    <td class="title_content"></td>\n\
    <td class="close"><img src="'+WindowCloseBtnImg+'" width="16" height="15" alt="'+WindowCloseTitle+'" title="'+WindowCloseTitle+'" /></td>\n\
   </tr></table>\n\
   <div class="content"></div>\n\
  </td>\n\
  <td class="r"></td>\n\
</tr></table>\n\
<table class="b" cellspacing="0"><tr>\n\
 <td class="l"></td>\n\
 <td class="m"><div class="bt"><img src="/i/1px.gif" /></div><div class="b"><img src="/i/1px.gif" /></div></td>\n\
 <td class="r"></td>\n\
</tr></table>\n\
'
}

function Window_closeBtnClick(event) {
  closeWindow(event.target || event.srcElement);
}

function Window_put(title, content) {
  if (title != null) this.wndTitle.innerHTML = title;
  if (content != null) this.wndContent.innerHTML = content;
}

var
  WindowSaveOnResize = null,
  WindowsZList = [],
  WindowsModalList = [],
  WindowsKeyDownListening;

function Window_show(title, content, isModal, x, y) {
  if (title || content) this.put(title, content);
  if (this.visible) {
    this.bringToFront();
    if (x != null || y != null) this.moveTo(x, y);
    return;
  }
  this.visible = true;
  this.isModal = isModal;
  var l = WindowsZList.length;
  this.zIndex = Math.max((l > 0 ? WindowsZList[l-1].zIndex+1 : 0),
                         (isModal ? WindowDefModalZIndex : WindowDefZIndex));
  WindowsZList.push(this);
  var containerBlock = (WindowContainerId ? $1(WindowContainerId) || document.body : document.body);
  if (isModal) {
    if (!this.bg) {
      this.bg = document.createElement('DIV');
      if (WindowBGClassName) this.bg.className = WindowBGClassName;
      this.bg.style.zIndex = this.zIndex;
      this.bg.style.position = 'absolute';
      this.bg.style.width = '100%';
      this.bg.style.height = Math.max(document.body.scrollHeight, document.body.clientHeight)+'px';
      this.bg.style.top = 0;
      this.bg.style.left = 0;
      var c = this.bgColor || WindowDefModalBgColor;
      var o = this.bgOpacity || WindowDefModalBgOpacity;
      if (o != null) {
        if (window.hasAlpha) {
          if (hasAlpha(this.bg)) {
            setAlpha(this.bg, o);
            if (c) this.bg.style.backgroundColor = c;
          }
        } else {
          if ('opacity' in this.style) {
            if (c) this.bg.style.backgroundColor = c;
            this.bg.style.opacity = o;
          } else if ('filter' in this.style) {
            if (c) this.bg.style.backgroundColor = c;
            this.bg.style.filter = 'alpha(opacity = '+Math.round(o*100)+')';
          }
        }
      }
    }
    containerBlock.appendChild(this.bg);
    if (WindowsModalList.length == 0) {
      WindowSaveOnResize = document.body.getAttribute('onresize');
      document.body.setAttribute('onresize',
        (typeof(WindowSaveOnResize) == 'string' || isMozilla ? 'Window_bgResize(event)' : Window_bgResize));
    }
    WindowsModalList.push(this);
  }
  if (!WindowsKeyDownListening) {
    setupEvent(document, 'keydown', Window_keyDownListener);
    WindowsKeyDownListening = true;
  }
  containerBlock.appendChild(this);
  if (!this.mouseListenerActive) {
    setupEvent(this, 'mousedown', Window_mouseDownListener);
    this.mouseListenerActive = true;
  }
  this.style.width = this.wndWidth+'px';
  this.style.zIndex = this.zIndex;
  this.style.visibility = 'hidden';
  this.style.display = 'block';
  if (x == null) x = this.getDefaultPos('x');
  if (y == null) y = this.getDefaultPos('y');
  this.moveTo(x, y);
  if (this.beforeShow) this.beforeShow();
  this.style.display = 'block';
  this.style.visibility = 'visible';
  this.afterShowHandler();
}

function Window_showModal(title, content, x, y) {
  this.show(title, content, true, x, y);
}

var
  WindowDefPosX = 0,
  WindowDefPosY = 0,
  WindowDefPosDelta = 30,
  WindowDefPosPadding = 100;

function Window_getDefaultPos(n) {
  if (typeof(n) == 'string') n = n.toUpperCase();
  if (n != 'X' && n != 'Y') return null;
  var body = document.body;
  var w = body[(n == 'X' ? 'clientWidth' : 'clientHeight')] - 2*WindowDefPosPadding;
  var p = window['WindowDefPos'+n] + WindowDefPosDelta;
  if (p + this[(n == 'X' ? 'wndWidth' : 'wndHeight')] > w) p = 0;
  window['WindowDefPos'+n] = p;
  p += WindowDefPosPadding + body[(n == 'X' ? 'scrollLeft' : 'scrollTop')];
  return p;
}

function focusElement(el, container) {
  el = $1_(el, container);
  if (el && el.focus) el.focus();
}

function Window_afterShowHandler(prm) {
  if (prm != 'fade') this.afterFade = null;
  if (this.afterShow) this.afterShow(prm);
  if (this.focusElementId) focusElement(this.focusElementId, this);
}

function closeTopWindow() {
  var i = WindowsZList.length;
  if (i > 0) WindowsZList[i-1].close();
}

function getWindowByElement(el) {
  while (el && el.tagName != 'BODY') {
    if (el.tagName == 'DIV' && el.className == WindowClassName) return el;
    el = el.parentNode;
  }
  return null;
}

function closeWindow(el) {
  var wnd = getWindowByElement(el);
  if (wnd) wnd.close();
}

function Window_close() {
  if (!this.visible) return;
  this.visible = false;
  if (this.dataRequestActive) {
    this.dataRequestActive = false;
    if (window.cancelDataRequest) cancelDataRequest(this.id);
  } else if (this.requestActive) {
    this.requestActive = false;
    requestFinished();
  }
  Window_removeMouseUpListener();
  this.style.display = 'none';
  this.style.visibility = 'hidden';
  if (this.parentNode) this.parentNode.removeChild(this);
  if (this.isModal) {
    this.bg.parentNode.removeChild(this.bg);
    WindowsModalList.pop();
    if (WindowsModalList.length == 0) {
      document.body.setAttribute('onresize', WindowSaveOnResize);
      if (WindowSaveOnResize == null)
        document.body.removeAttribute('onresize')
      else
        WindowSaveOnResize = null;
    }
  }
  WindowsZList.removeItem(this);
  if (this.onClose) this.onClose();
  this.mainForm = null;
  this.okBtn = null;
  if (this.reloadOnClose) location.reload(false);
}

function Window_afterFadeOut() {
  this.afterFade = null;
  if (this.parentNode) this.parentNode.removeChild(this);
}

function Window_bringToFront() {
  var k = WindowsZList.removeItem(this);
  if (k >= 0) {
    WindowsZList.push(this);
    var ind = (k > 0 ? WindowsZList[k-1].zIndex+1 : this.zIndex);
    for (var i = k, l = WindowsZList.length; i < l; i++) {
      with (WindowsZList[i]) {
        zIndex = ind;
        style.zIndex = ind;
      }
      ind++;
    }
  }
}

function Window_moveTo(x, y) {
  if (x != null) {
    this.wndPosX = x;
    this.style.left = x+'px';
  }
  if (y != null) {
    this.wndPosY = y;
    this.style.top = y+'px';
  }
}

function Window_centerWnd() {
  var x = Math.max(Math.round((document.body.clientWidth-this.offsetWidth)/2), 0) + document.body.scrollLeft;
  var y = Math.max(Math.round((document.body.clientHeight-this.offsetHeight)/2), 0) + document.body.scrollTop;
  this.moveTo(x, y);
}

function Window_bgResize(event) {
  for (var i = 0, l = WindowsModalList.length; i < l; i++) {
    var w = WindowsModalList[i];
    if (w.needCenter) w.moveTo(Math.round((document.body.offsetWidth-w.offsetWidth)/2), null);
    w.bg.style.height = Math.max(document.body.scrollHeight, document.body.clientHeight)+'px';
  }
  if (WindowSaveOnResize) {
    if (typeof(WindowSaveOnResize) == 'function') WindowSaveOnResize(event);
    else eval(WindowSaveOnResize);
  }
}

var
  WindowMouseUpListenerCount = 0;

function Window_addMouseUpListener() {
  WindowMouseUpListenerCount++;
  if (WindowMouseUpListenerCount == 1) {
    setupEvent(document.body, 'mouseup', Window_mouseUpListener);
  }
}

function Window_removeMouseUpListener() {
  if (WindowMouseUpListenerCount > 0) {
    WindowMouseUpListenerCount--;
    if (WindowMouseUpListenerCount == 0) {
      removeEvent(document.body, 'mouseup', Window_mouseUpListener);
    }
  }
}

function Window_keyDownListener(event) {
  if (event.keyCode == 27) closeTopWindow();
}

var
  curDragWindow = null;

function Window_mouseDownListener(event) {
  if (!isLeftButtonEvent(event) || curDragWindow != null) return;
  var src = event.target || event.srcElement;
  var wnd = src, inTitle = false;
  do {
    if (wnd.tagName == 'DIV' && wnd.className == WindowClassName) break;
    if (wnd.className == WindowTitleClassName) inTitle = true;
    wnd = wnd.parentNode;
    if (!wnd || wnd.tagName == 'BODY') return;
  } while (true);
  if (!wnd.isModal) wnd.bringToFront();
  if (!inTitle || src.tagName == 'IMG') return;  // window body or [X] button
  if (WindowDragOpacity < 1) setAlpha(wnd, WindowDragOpacity);
  wnd.draggingWindow = true;
  curDragWindow = wnd;
  var pos = getAbsPos(wnd);
  wnd.dragDeltaX = pos.left - event.clientX - document.body.scrollLeft;
  wnd.dragDeltaY = pos.top  - event.clientY - document.body.scrollTop;
  addMouseMoveListener(Window_mouseMoveListener, wnd);
  Window_addMouseUpListener();
  cancelEvent(event, true);
  return false;
}

function Window_mouseMoveListener(event) {
  var wnd = curDragWindow;
  wnd.moveTo(mouseAbsPosX + wnd.dragDeltaX, mouseAbsPosY + wnd.dragDeltaY);
}

function Window_mouseUpListener(event) {
  var wnd = curDragWindow;
  if (!isLeftButtonEvent(event) || wnd == null) return;
  removeMouseMoveListener(Window_mouseMoveListener, wnd);
  Window_removeMouseUpListener();
  if (WindowDragOpacity < 1) setAlpha(wnd, 1);
  wnd.draggingWindow = false;
  curDragWindow = null;
  if (wnd.afterDrag) wnd.afterDrag();
}

//-- Hint --

var
  HintContainerId = 'abs_container';

function Hint(type, defContent, w, h) {
  this.hintContainer = document.createElement('DIV');
  this.hintContainer.className = 'hint';
  this.type = type || 'text';
  this.defContent = defContent;
  this.width = w;
  this.height = h;
  this.zIndex = 2000;
  this.inContainer = false;
  this.visible = false;
  this.fastShow = true;
  this.show = Hint_show;
  this.hide = Hint_hide;
  this.updatePos = Hint_updatePos;
  if (mouseMoveListeners.length == 0) addMouseMoveListener(null);  // add null handler to know mouse pos
}

function Hint_show(content, w, h) {
  if (content == null) content = this.defContent;
  if (w == null) w = this.width;
  if (h == null) h = this.height;
  var hintCont = this.hintContainer;

  hintCont.innerHTML = 
   '<table border="0" cellspacing="0" cellpadding="0"><tr><td>'+
     (this.type == 'image' ?
       '<img src="'+content+'"'+(w ? ' width="'+w+'"' : '')+(h ? ' height="'+h+'"' : '')+'>' :
       '<b></b><div>'+content+'</div><b></b>') +
   '</td></tr></table>';
  if (w) hintCont.style.width = w;
  if (h) hintCint.style.height = h;

  if (this.visible) return;
  hintCont.hintObject = this;
  if (!this.mouseHandlerAdded) {
    addMouseMoveListener(Hint_updatePos, this);
    this.mouseHandlerAdded = true;
  }

  if (!this.inContainer) {
    (document.getElementById(HintContainerId) || document.body).appendChild(hintCont);
    this.inContainer = true;
  }
  if (this.zIndex) hintCont.style.zIndex = this.zIndex;
  if (this.width) hintCont.style.width = this.width;
  if (this.fastShow) 
    setVisible(hintCont, true)
  else
    fade(hintCont, 'in');
  this.visible = true;
  var t = hintCont.getElementsByTagName('TABLE')[0];
  this.hintWidth = t.offsetWidth;
  this.hintHeight = t.offsetHeight;
  this.updatePos();
}

function Hint_hide(how) {
  if (!this.visible) return;
  this.visible = false;
  var hintCont = this.hintContainer;
  if (how == 'fast' || this.fastShow) {
    setVisible(hintCont, false);
    if (this.mouseHandlerAdded) {
      removeMouseMoveListener(Hint_updatePos, this);
      this.mouseHandlerAdded = false;
    }
    hintCont.hintObject = null;
  } else {
    if (this.mouseHandlerAdded) hintCont.afterFade = Hint_afterFadeOut;
    fade(hintCont, 'out');
  }
}

function Hint_afterFadeOut() {
  this.afterFade = null;
  var obj = this.hintObject;
  this.hintObject = null;
  obj.mouseHandlerAdded = false;
  removeMouseMoveListener(Hint_updatePos, obj);
}

function Hint_updatePos() {
  var body = document.body;
  var d = (body.clientWidth + body.scrollLeft) - this.hintWidth;
  var x = Math.min(mouseAbsPosX, d);
  var y = mouseAbsPosY + 20;
  var h = this.hintHeight;
  var ch = body.clientHeight + body.scrollTop;
  if (y + h >= ch) y = Math.min(mouseAbsPosY, ch) - h;
  with (this.hintContainer.style) {
    left = x;
    top  = y;
  }
}

var
  hint = new Hint('text');

function hintHide() {
  hint.hide();
}

//-- Select emulation --

function initSelect(selId, selOptionsId, params) {
  var sel = $1(selId);
  var opt = $1(selOptionsId);
  sel.optionsElement = opt;
  opt.selectElement = sel;
  var selValue = sel.getAttribute('value');
  var divs = opt.getElementsByTagName('DIV');
  var opts = [];
  var selIdx = -1;
  for (var i = 0, l = divs.length; i < l; i++) {
    var div = divs[i];
    if (div.className == 'option') continue;
    div.optionIndex = opts.length;
    if (selValue && selIdx < 0 && div.getAttribute('value') == selValue) {
      div.className = 'selected';
      selIdx = div.optionIndex;
    }
    opts.push(div);
  }
  sel.options = opts;
  sel.selectedIndex = selIdx;
  sel.selectHighlited = false;
  sel.selectText = findSubChild(sel, 'SPAN');
  sel.selectLink = findSubChild(sel, 'A', 'select_link');
  sel.selectButton = findSubChild(sel, 'IMG', 'select_btn');
  setupEvent(sel, 'mouseover', selectMouseOver);
  setupEvent(sel, 'mouseout',  selectMouseOut);
  setupEvent(sel, 'mousedown', selectMouseDown);
  if (params) {
    for (var name in params) sel[name] = params[name];
  }
}

function getSelectByChild(el) {
  while (el) {
    if (el.tagName == 'DIV' && el.className == 'select') return el;
    el = el.parentNode;
  }
  return null;
}

var
  highlightedSelect;
  
function selectMouseOver(event) {
  var el = (event.target || event.toElement);
  var sel = getSelectByChild(el);
  if (sel && sel != highlightedSelect && (el == sel.selectText || (sel.selectLink && el == sel.selectLink) || el == sel.selectButton)) {
    highlightedSelect = sel;
    if (sel.activeImgName) {
      sel.passiveImgName = sel.selectButton.src;
      sel.selectButton.src = sel.activeImgName;
    } else {
      setAlpha(sel.selectText, 0.5);
//    if (sel.selectLink) setAlpha(sel.selectLink, 0.5);
      setAlpha(sel.selectButton, 0.5);
    }
  }
}

function selectMouseOut(event) {
  var el = (event.relatedTarget || event.toElement);
  var sel = getSelectByChild(el);
  if (highlightedSelect && sel != highlightedSelect) {
    if (highlightedSelect.passiveImgName) {
      highlightedSelect.selectButton.src = highlightedSelect.passiveImgName;
    } else {
      setAlpha(highlightedSelect.selectText, 1);
//    if (highlightedSelect.selectLink) setAlpha(highlightedSelect.selectLink, 1);
      setAlpha(highlightedSelect.selectButton, 1);
    }
    highlightedSelect = null;
  }
}

var
  openedSelectOptions;


//-- Preload images --

var
  preloadedImgs = [];

function preloadImg(src, path) {
  if (path == null) path = '';
  if (isArray(src)) {
    var res = [];
    for (var i = 0, l = src.length; i < l; i++) {
      var img = new Image();
      img.src = path + src[i];
      preloadedImgs.push(img);
      res.push(img.src);
    }
    return res;
  }
  var img = new Image();
  img.src = path + src;
  preloadedImgs.push(img);
  return img.src;
}

// Logo images
preloadImg(['arrowbig.gif', 'logo_back_tl.gif', 'log.gif', 'logo_back_tr.gif', 'logo_back_bl.gif', 'logo_back_rb.gif'], '/i/')

// Search button
preloadImg('/i/search_button_down.gif');


//-- Screen images --

function setScreenHandlers(id, num, text) {
  var el = $1(id);
  el.screenNum = num;
  el.screenText = text;
  setupEvent(el, 'mouseover', screenImgMouseOver);
  setupEvent(el, 'mouseout', screenImgMouseOut);
  setupEvent(el, 'click', screenImgClick);
}

var
  activeScreenBlock;

function getScreenBlock(el) {
  return (el ? (el.tagName == 'DIV' ? el : findParentNode(el, 'DIV')) : null);
}

function screenImgMouseOver(event) {
  var el = (event.target || event.toElement);
  var bl = getScreenBlock(el);
  if (bl && bl != activeScreenBlock) {
    activeScreenBlock = bl;
    showFullImg(bl);
    setScreen(bl.screenNum);
    if (bl.screenText) hint.show(bl.screenText, 170) 
  }
}

function screenImgMouseOut(event) {
  var el = (event.relatedTarget || event.toElement);
  if (el == fullImg) return;
  var bl = getScreenBlock(el);
  if (activeScreenBlock && bl != activeScreenBlock) {
    hideFullImg();
    hint.hide();
    activeScreenBlock = null;
  }
}

function screenImgClick(event) {
  openScreen(activeScreenBlock.screenNum);
}

var
  fullImg;

function showFullImg(el) {
  if (!fullImg) {
    fullImg = document.createElement('IMG');
    fullImg.id = 'full_img';
    fullImg.style.position = 'absolute';
    fullImg.style.border = 0;
    fullImg.style.zIndex = 10;
    fullImg.style.cursor = 'pointer';
    fullImg.src = fullImgName;
    setupEvent(fullImg, 'mouseout', screenImgMouseOut);
    setupEvent(fullImg, 'click', screenImgClick);
  }
  ($1('abs_container') || document.body).appendChild(fullImg);
  var pos = getAbsPos(el);
  fullImg.style.left = (pos.left+2)+'px';
  fullImg.style.top  = (pos.top+2)+'px';
  fullImg.style.display = 'block';
}

function hideFullImg() {
  if (fullImg) fullImg.style.display = 'none';
}

function setScreen(idx) {
  if (screenBox == null) screenBox = $1('screens').getElementsByTagName('DIV');
  if (screenBox[idx].className == 'current') return;    
  for (var i = 0, l = screenBox.length; i < l; i++) screenBox[i].className = '';
  screenBox[idx].className = 'current';
  bigSrc.src = screensSrc[idx];     
}

