Array.prototype.withEvery = function(what)
{
  for(var i = 0; i < this.length; i++) what(this[i])
};
Array.prototype.numSort = function()
{
  return this.sort(function(a, b) { return a - b });
};
var intro = !document.getElementById("text");

var ui = {
  "width": 736,
  "fontSize": 83,
  "font": "sans",
  "align": "justify"
};

    
var page = {
  "setWidth": function(n)
  {
    ui.width = [360, n, 960].numSort()[1];
    return n - ui.width;
  },
  "setFontSize": function(n)
  {
    ui.fontSize = [60, n, 160].numSort()[1];
    return n - ui.fontSize;
  },
  "setFont": function(n)
  {
    ui.font = n;
  },
  "setAlign": function(n)
  {
    ui.align = n;
  },
  "setState": function(s)
  {
    if(!intro)
    {
      document.getElementById("text").style.width = document.getElementsByTagName("dir")[0].style.width = ui.width + "px";
      document.documentElement.style.fontSize = ui.fontSize + "%";
    }
    document.documentElement.className = ui.font + " " + ui.align + "";
    if(this.tio)
    {
      clearTimeout(this.tio);
      this.tio = 0;
    }
    if(!s) this.to = setTimeout(function()
    {
      for(var p in ui) document.cookie = "ui." + p + "=" + ui[p] + "; expires=" + (new Date(2025, 12, 31)).toGMTString();
    }, 250);
  },
  "loadState": function()
  {
    document.cookie.split("; ").withEvery(function(n)
    {
      if(!n.match(/^ui/)) return false;
      ui[n.split("=")[0].substring(3)] = n.split("=")[1];
    });
    page.setWidth(1 * ui.width);
    page.setFontSize(1 * ui.fontSize);
    page.setFont(ui.font);
    page.setAlign(ui.align);
    page.setState(true);
  }
};


window.onload = function()
{
  if(!intro)
  {
  }
};
if(!intro)
{
  if(!document.write)
  {
    document.createElement = function(n)
    {
      return document.createElementNS(document.documentElement.getAttribute("xmlns:html"), "html:" + n)
    };
    document.body = document.documentElement;
    document.cookie = "K.32=j";
  }
}


page.loadState();

if(intro)
{
  (window.onresize = function()
  {
    var w = document.body.clientWidth;
    document.body.style.fontSize = (w == 480 ? 10 : Math.round(Math.min((w - 755) / 150, (w - 545) / 210)) * 6.25) + 75 + "%";
  })()
}
else
{
  var root = document.documentElement;
  var scrollbarWidth = (function()
  {
    var temp = document.createElement("div")
    temp.style.cssText = "position: absolute; top: 0; left: 0; width: 1px; height: 101%";
    var w = root.clientWidth;
    root.appendChild(temp);
    w -= root.clientWidth;
    root.removeChild(temp);
    return w;
  })();
  window.onload = function()
  {
    (window.onresize = function()
    {
      document.body.style.marginRight = ((window.innerHeight || root.clientHeight) < root.scrollHeight) ? "" : scrollbarWidth + "px";
    })();
  };

  document.body.onclick = document.body.onmouseup = function()
  {
    document.body.onmousemove = null;
  };
  
  var divName = document.getElementById("text").tagName;

  ["T", "C", "B", "R"].withEvery(function(r)
  {
    var re = document.createElement(divName);
    re.id = "resizer" + r;
    re.title = "Dvojklikem nastavíte výchozí " + (r == "R" ? "velikost písma" : "šířku");
    re.onmousedown = function()
    {
      if(arguments[0]) window.event = arguments[0];
      document.body.onmousemove = function()
      {
        if(arguments[0]) window.event = arguments[0];
        if(r == "R")
        {
          if(!page.setFontSize(ui.fontSize + (event.screenY - this.currentY) * 2 * (document.getElementsByTagName("dir")[0].offsetHeight - 10) / ui.fontSize)) this.currentY = event.screenY;
        }
        else
          if(!page.setWidth(ui.width + (event.screenX - this.currentX) * 2)) this.currentX = event.screenX;
        page.setState();
      };
      document.body.currentX = event.screenX;
      document.body.currentY = event.screenY;
    };
    re.ondblclick = function()
    {
      if(r == "R") page.setFontSize(83); else page.setWidth(736);
      page.setState();
    };
    (r == "R" ? document.getElementsByTagName("dir")[0] : document.getElementById("text")).appendChild(re);
  });


  var sw = document.createElement(divName);
  sw.id = "switch";
  sw.appendChild(document.createTextNode("T"));
  sw.onclick = function()
  {
    page.setFont({"sans": "serif", "serif": "gothic", "gothic": "vista", "vista": "sans"}[ui.font]);
    page.setState();
  };
  document.getElementById("text").appendChild(sw);
}

//  http://djpw.cz/154034
document.onkeydown = function(e)
{
	if((window.event || e).keyCode == 120)
    alert("Tady jsem neschoval nic tajného. Ani vtipného.");
};
