function getElement (id)
{
  if (document.getElementById)
  	return document.getElementById (id);

  if (document.all)
	  return document.all[id];

  if (document.layers)
   	return document.layers[id];
}

function posLeft (obj)
{
  if (obj.offsetParent)
	{
    var curleft = 0;
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
    return curleft;
  }
  if (obj.x)
    return obj.x;
}

function posTop (obj)
{
  if (obj.offsetParent)
  {
    var curtop = 0;
    while (obj.offsetParent)
    {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
    return curtop;
  }
  if (obj.y)
    return obj.y;
}

setCookie = function (name, value, days)
{
  var expires;
  
  if (days)
  {
    var date = new Date ();
    
    date.setTime (date.getTime () + days * 86400000);
    expires = "; expires=" + date.toGMTString ();
  } else expires = "";
  
  document.cookie = name + "=" + value + expires + "; path=/";
}

getCookie = function (name)
{
  var re = new RegExp ("(\;|^)[^;]*(" + name + ")\=([^;]*)(;|$)");
  var res = re.exec (document.cookie);
  
  return (res != null ? res[3] : null);
}

// ------------------------------------------------------------------------------------------------
// window


lnk_new = function (url) { window.open (url, '_blank'); }

lnk_lnk = function (url) { window.open (url, '_self'); }

lnk_dlg = function (url)
{
  var dialog = window.open (url, "_blank", "left=300, top=200, width=400, height=400, menubar=no, status=no, location=no, resizable=no, toolbar=no");
  dialog.dialog = true;
}

lnk_upd = window.parent.lnk_upd;


window.init = function ()
{
  this.ctrls = Array ();
  this.showedModal = null;

  // IE55 has a serious DOM1 bug... Patch it!
  if (/msie 5\./i.test (navigator.userAgent))
  {
    document._getElementsByTagName = document.getElementsByTagName;
    document.getElementsByTagName = function ( sTagName )
    {
      if (sTagName == "*")
        return document.all; else return document._getElementsByTagName (sTagName);
    }
  }


  if (window.dialog != 'undefined' && typeof (window.opener) != 'undefined' && window.opener != null)
  {
    var wo = window.opener;

    wo.showedModal = window;

    window.lnk_upd = function (params)
    {
      if (typeof (window.opener) != 'undefined' && window.opener != null)
      {
        var wo = window.opener;
        var arr = params.split ('&');

        for (i = arr.length; --i >= 0; )
        {
          var parts = arr[i].split ('=');
          var el = wo.document.getElementsByName (parts[0])[0];
          
          if (el)
            el.value = parts[1];
        }

        window.close ();
      }
    }
  }
}

window.onload = function (e)
{
  var ds = document.getElementsByTagName ("DIV");
  var dl = ds.length;
  for (var d = dl - 1; d >= 0; d-- )
  {
    var dsl = ds[d];

    if (dsl.className == '')
      continue;
    if (dsl.className == 'ctrlDialog')
      { this.ctrls[this.ctrls.length] = new Form (dsl); continue; }
    if (dsl.className == 'ctrlTabPane')
      { this.ctrls[this.ctrls.length] = new TabPane (dsl); continue; }
    if (dsl.className == 'ctrlTable')
      { new Table (dsl); continue; }
    if (dsl.className == 'ctrlDatePicker')
      { new DatePicker (dsl); continue; }
    if (dsl.className == 'ctrlDateInterval')
      { new DateInterval (dsl); continue; }
  }
  
  var splash = getElement ('splash');
  if (splash)
    document.body.removeChild (splash);
}

window.onunload = function (e)
{
  for (var cl = this.ctrls.length; --cl >= 0; )
  {
    this.ctrls[cl].dispose ();
    this.ctrls[cl] = null;
  }
  this.ctrls = null;

  if (typeof (window.opener) != 'undefined')
  {
    var wo = window.opener;

    if (wo != null && typeof (wo.showedModal) != 'undefined')
      wo.showedModal = null;
  }
}

window.onfocus = function (e)
{
  if (this.showedModal != null)
    this.showedModal.focus ();
}

document.onclick = function (e)
{
  if (window.showedModal != null)
    window.event.returnValue = false;
}

// ------------------------------------------------------------------------------------------------
// funkce

onFrameLoad = function ()
{
  idi = getElement ('bodyContainer');
  ifr = getElement ('pluginFrame');

  plus_height = 0;
  ith = getElement ('townHeader');
  if (ith != null)
    plus_height = ith.offsetHeight;

  frs = frames['pluginFrame'].document.body.scrollHeight;
  ifr.style.height = frs;

  if (navigator.userAgent.indexOf ('Mozilla') != -1)
  {
    frs += plus_height;

    if (frs > idi.scrollHeight)
    {
      idi.style.height = frs + 2;
    } else
    {
      free = document.height - getElement ('topContainer').offsetHeight - getElement ('bottomContainer').offsetHeight + 2;

      if (idi.scrollHeight > free)
      {
        idi.style.height = frs > free ? frs : free;
      }
    }
  }
}

onQuickChange = function (el)
{
  window.location = el.value;
}

onBlockShow = function (block_id)
{
  btn_el = getElement ('block_btn_' + block_id);
  table_el = getElement ('block_' + block_id);

  if (table_el != null)
  {
    dis = table_el.style.display

    if (dis == 'none')
    {
      btn_el.src = 'images/btn_up.gif';
      table_el.style.display = 'block';
    } else 
    {
      btn_el.src = 'images/btn_down.gif';
      table_el.style.display = 'none';
    }
  }
}

var ajax_request;

loadAsyncData = function (url, onreadyproc)
{
  ajax_request = false;

  if (window.XMLHttpRequest)
  {
    // branch for native XMLHttpRequest object
    try {
      ajax_request = new XMLHttpRequest ();
    } catch (e) {
      ajax_request = false;
    }
  } else if (window.ActiveXObject)
  {
    // branch for IE/Windows ActiveX version
    try {
      ajax_request = new ActiveXObject ("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        ajax_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        ajax_request = false;
      }
    }
  }

  if (ajax_request)
  {
    ajax_request.onreadystatechange = onreadyproc;
    ajax_request.open ('GET', url, true);
    ajax_request.send ('');
  }
}

onMapInfo = function (url, in_out)
{
  info_el = getElement ('mapInfo');

  if (info_el != null)
  {
    if (in_out == 1)
    {
      // zjistime pozici bloku s mapou
      body_el = getElement ('bodyBody');
      if (body_el != null)
      {
        loadAsyncData (url, onMapInfoResponse);

        info_el.style.left = posLeft (body_el) + 510;
        info_el.style.top = posTop (body_el) + 20;

        info_el.style.display = 'block';
      }
    } else
    {
      info_el.style.display = 'none';
      info_el.innerHTML = '<div class="info_header">načítám..</div>';
    }
  }
}

onMapInfoResponse = function  ()
{
  // only if req shows "loaded"
  if (ajax_request.readyState == 4)
  {
    // only if "OK"
    if (ajax_request.status == 200)
    {
      info_el = getElement ('mapInfo');

      if (info_el != null)
      {
        info_el.innerHTML = ajax_request.responseText;
        ajax_request.abort ();
        ajax_request = false;
      }
    } else alert ("There was a problem retrieving the XML data:\n" + ajax_request.statusText);
  }
}

var result_url = '';

onResultInfo = function (e, url)
{
  if (result_url == url)
    return;

  result_url = url;

  info_el = getElement ('resultInfo');
  if (info_el != null)
  {
    info_el.innerHTML = '';

    if (url != false)
    {
      loadAsyncData (url, onResultInfoResponse);

      info_el.style.left = e.clientX + document.body.scrollLeft;
      info_el.style.top = e.clientY + document.body.scrollTop;

      info_el.style.display = 'block';
    } else info_el.style.display = 'none';
  }
}

onResultInfoResponse = function  ()
{
  if (ajax_request.readyState == 4)
  {
    if (ajax_request.status == 200)
    {
      info_el = getElement ('resultInfo');

      if (info_el != null)
        info_el.innerHTML = ajax_request.responseText;
    }

    ajax_request.abort ();
    ajax_request = false;
  }
}

onNewsInfo = function (e, url)
{
  if (result_url == url)
    return;

  result_url = url;

  info_el = getElement ('newsInfo');
  if (info_el != null)
  {
    info_el.innerHTML = '';

    if (url != false)
    {
      loadAsyncData (url, onNewsInfoResponse);

      info_el.style.left = e.clientX + document.body.scrollLeft;
      info_el.style.top = e.clientY + document.body.scrollTop;

      info_el.style.display = 'block';
    } else info_el.style.display = 'none';
  }
}

onNewsInfoResponse = function  ()
{
  if (ajax_request.readyState == 4)
  {
    if (ajax_request.status == 200)
    {
      info_el = getElement ('newsInfo');

      if (info_el != null)
        info_el.innerHTML = ajax_request.responseText;
    }

    ajax_request.abort ();
    ajax_request = false;
  }
}

onRankChange = function (season, town, rank, change)
{
  rank = rank + change;
  loadAsyncData ("/ajx_services.php?service=1&season_iid="+season+"&town_iid="+town+"&new_rank="+rank, onRankChangeResponse);
}

onRankChangeResponse = function  ()
{
  if (ajax_request.readyState == 4)
  {
    // only if "OK"
    if (ajax_request.status == 200)
    {
      // refreshujeme
      window.location.reload (false);
    } else alert ("There was a problem retrieving the XML data:\n" + ajax_request.statusText);
  }
}


window.init ();
