/*** funkce pro odsraneni bilych znaku z koncu retezce ***/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

/*** test vyskytu hodnoty v poli ***/
Array.prototype.inArray = function(str) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == str) return true;
  }
	return false;
}

var browser  = {
  version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

/**
 * Vraci hodnotu vlastnosti stylu v pixelech (pripadne vraci "auto"). Pro IE.
 * Pouziti: IE_computedStyle.get(elementNode, CSS-Style);
 * Priklad: IE_computedStyle.get(el, "width"); IE_computedStyle.get(el, "margin-top");
 */
var IE_computedStyle = function () {
    var isIE = /*@cc_on!@*/0;
    var borderRegex = /thin|medium|thick/i; /* Regex for css border width keywords */
    var styleEl;
    
    /* GET POS */
    var getPos = function (which) {
        var curStyle = styleEl.currentStyle[which];
        return (/auto/i.test(curStyle)) ? "auto" : grabLength(curStyle) + "px";
    };
    
    /* GET MARGIN */
    var getMargin = function (which) {
        var curStyle = styleEl.currentStyle["margin" + which];
        return (/auto/i.test(curStyle)) ? "0px" : grabLength(curStyle) + "px";
    };
    
    /* GET BORDER WIDTH */
    var getBorderWidth = function (which) {
        var borderWidth = styleEl.currentStyle["border" + which + "Width"];
        if (styleEl.currentStyle["border" + which + "Style"] !== "none" && 
                ((/Top|Bottom/i.test(which) && styleEl.offsetHeight > styleEl.clientHeight) || 
                (/Right|Left/i.test(which) && styleEl.offsetWidth > styleEl.clientWidth))) {
            if (!borderRegex.test(borderWidth)) {
                return grabLength(borderWidth) + "px";
            } else if (borderRegex.test(borderWidth)) {
                var temp = document.createElement("DIV");
                temp.style.width = "10px";
                temp.style.border = borderWidth + " " + styleEl.currentStyle["border" + which + "Style"] + " #000000";
                styleEl.parentNode.appendChild(temp);
                borderWidth = Math.round((temp.offsetWidth-10)/2);
                styleEl.parentNode.removeChild(temp);
                return borderWidth + "px";
            }
        } else {
            return "0px";
        }
    };
    
    /* GET PADDING */
    var getPadding = function (which) {
        return grabLength(styleEl.currentStyle["padding" + which]) + "px";
    };
    
    /* GET WIDTH */
    var getWidth = function () {
        var width = styleEl.offsetWidth; /* Currently the width including padding + border */
        width -= parseInt(getPadding("Right"));
        width -= parseInt(getPadding("Left"));
        width -= parseInt(getBorderWidth("Right"));
        width -= parseInt(getBorderWidth("Left"));
        return width + "px";
    };
    
    /* GET HEIGHT */    
    var getHeight = function () {
        var height = styleEl.offsetHeight; /* Currently the height including padding + border */
        height -= parseInt(getPadding("Top"));
        height -= parseInt(getPadding("Bottom"));
        height -= parseInt(getBorderWidth("Top"));
        height -= parseInt(getBorderWidth("Bottom"));
        return height + "px";
    };
    
    /* GRAB LENGTH */
    var grabLength = function (length) {
        var temp = document.createElement("DIV");
        temp.style.width = length;
        styleEl.parentNode.appendChild(temp);
        length = Math.round(temp.offsetWidth);
        styleEl.parentNode.removeChild(temp);
        return length;
    };
    
    return {
        /* GET */
        get : function (el, styleProp) {   
            var rValue;
            styleEl = (typeof(el) === "string") ? document.getElementById(el) : el;	
            var styleProp = (typeof(styleProp) === "string") ? styleProp.toLowerCase() : "";
            styleProp = styleProp.replace(/\-/g, "");
            
            if (!isIE || !/block/i.test(styleEl.currentStyle["display"]))
                return;
            
            switch (styleProp) { /* Run through the valid css properties, return undefined if none match */
                case "top": rValue = getPos("top"); break;
                case "right": rValue = getPos("right"); break;
                case "bottom": rValue = getPos("bottom"); break;
                case "left": rValue = getPos("left"); break;
                case "margintop": rValue = getMargin("Top"); break;
                case "marginright": rValue = getMargin("Right"); break;
                case "marginbottom": rValue = getMargin("Bottom"); break;
                case "marginleft": rValue = getMargin("Left"); break;
                case "bordertopwidth": rValue = getBorderWidth("Top"); break;
                case "borderrightwidth": rValue = getBorderWidth("Right"); break;
                case "borderbottomwidth": rValue = getBorderWidth("Bottom"); break;
                case "borderleftwidth": rValue = getBorderWidth("Left"); break;
                case "paddingtop": rValue = getPadding("Top"); break;
                case "paddingright": rValue = getPadding("Right"); break;
                case "paddingbottom": rValue = getPadding("Bottom"); break;
                case "paddingleft": rValue = getPadding("Left"); break;
                case "width": rValue = getWidth(); break;
                case "height": rValue = getHeight(); break;
            }
            
            return rValue;
        }
    };
}();

/*** Vrati vypocitanou hodnotu vlastnosti elementu ***/
function ziskejVlastnost(element,vlastnost)
{
  var hodnota = 0;

	if (window.getComputedStyle) //firefox, opera...
    var hodnota = document.defaultView.getComputedStyle(element,null).getPropertyValue(vlastnost);
	else if (element.currentStyle) //ie
		//var hodnota = element.currentStyle[vlastnost];
		hodnota = IE_computedStyle.get(element,vlastnost);
	
  reg=new RegExp("((px)$)");
	if (hodnota && hodnota.search(reg)>0) return Math.round(parseFloat(hodnota));
	return false;
}


/*** odsraneni pevnych mezer v prazdnych divech ***/
function odstranitNbsp() {
  var divy=document.getElementsByTagName('div');
  for (var i=0; i<divy.length; i++) {
    if (divy[i].innerHTML.trim()=="&nbsp;") divy[i].innerHTML="";
  }
  var divy=document.getElementsByTagName('span');
  for (var i=0; i<divy.length; i++) {
    if (divy[i].innerHTML.trim()=="&nbsp;") divy[i].innerHTML="";
  }
  var odstavce=document.getElementsByTagName('p');
  for (var i=0; i<odstavce.length; i++) {
    if (odstavce[i].innerHTML.trim()=="") odstavce[i].style.display = "none"; 
  }
}

/*** obarvi bunky obecnych tabulek ***/
function obarvitTabulky() {
  var tabulky = document.getElementsByTagName("table"); //vsechny tabulky
  tabulka:
  for (var i = 0; i < tabulky.length; i++) {
    var tridy = tabulky[i].className.split(" "); //tridy v poli
    if (!tridy.inArray("obecna")) continue; //preskocit tabulky, pokud neni tridy obecna
    var bunky = tabulky[i].getElementsByTagName("td");
    //pruchod po radcih
    var radky = tabulky[i].getElementsByTagName("tr");
    var cisloRadku = 0; //pocitadlo radku, ve kterych nejsou bunky zahlavi
    var prvniNormRadek = 0;
    for (var j = 0; j < radky.length; j++) { //pruchod po radcich
      var zapati = false;
      var tridy = radky[j].className.split(" ");
      if (tridy.inArray("zapati")) zapati = true;
      var bunky = radky[j].getElementsByTagName("td"); //bunky v radku
      if (bunky.length == 0) { //zahlavi
        var bunky = radky[j].getElementsByTagName("th"); //nacteni bunek zahlavi
//        if (bunky.length < 2) continue; //zahlavi se nastavi pouze pokud je v tabulce dva a vice sloupcu
        for (var k = 0; k < bunky.length; k++) {
          var trida = "";
          if (cisloRadku == 0) {
            if (k == 0) { //prvni sloupec
              trida = "js_lh";
            } else if (k == bunky.length-1) { //posledni sloupec
              trida = "js_ph";
            } else {
              trida = "js_h";
            }
          } else {
             if (k == 0) { //prvni sloupec
              trida = "js_l";
            } else if (k == bunky.length-1) { //posledni sloupec
              trida = "js_p";
            } else {
              trida = "js_h2";
            }
          }
          bunky[k].className += (bunky[k].className.length > 0 ? " " : "") + trida; //uprava tridy stavajici bunky
        }
        prvniNormRadek = 1;
      } else if (zapati) {
        for (var k = 0; k < bunky.length; k++) {
          var trida = "js";
          if (k == 0) { //prvni sloupec
            trida = "js_l";
          } else if (k == bunky.length-1) { //posledni sloupec
            trida = "js_p";
          }
          bunky[k].className += (bunky[k].className.length > 0 ? " " : "") + trida; //uprava tridy stavajici bunky
        }
      } else { //bezne bunky
        if (prvniNormRadek == 1) {
          for (var k = 0; k < bunky.length; k++) {
            bunky[k].className += (bunky[k].className.length > 0 ? " " : "") + "js"; //uprava tridy stavajici bunky
          }
        }
        prvniNormRadek = 2;
      }
      cisloRadku ++;
      //if (cisloRadku % 2 == 0) radky[j].className += (radky[j].className.length > 0 ? " " : "") + "sudy";

      
    }
  }
}

pozadiCas=1.1;
function opravPozadi() {
  var kontejner = document.getElementById("kontejner_obal");
  var sirka = ziskejVlastnost(document.body,"width");
  if (!sirka || !kontejner) {
    pozadiCas=pozadiCas*2;
    setTimeout("opravPozadi()", pozadiCas);
    return;
  }
  
  var typ = false;
  
  if (navigator.appName == "Opera") typ = 3

  var pos1 = navigator.appVersion.indexOf('Safari');
  var pos2 = navigator.appVersion.indexOf('Version')
  if (pos1 > 0 && pos2 > 0) typ = 3
  
  var pos = navigator.appVersion.indexOf('Chrome');
  if (pos > 0) typ = 3
//alert(typ);

  //v komentari vzi posun pozadi vuci textu (pohledem uzivatele)
  kontejner = document.body; 
  var tridy = kontejner.className.split(" "); //tridy v poli  
  switch (typ) {
    case 1: //pozadi ujizdi doleva ()
      if (sirka > 1000 && sirka % 2 == 1) {
        if (tridy.inArray("poskok_levy")) break;
        kontejner.className += (kontejner.className.length > 0 ? " " : "") + "poskok_levy";
      } else {
        if (!tridy.inArray("poskok_levy")) break;
        var ntridy = "";
        for (var i = 0; i < tridy.length; i++) {
          if (tridy[i] != "poskok_levy") {
            if (ntridy.length > 0) ntridy += " ";
            ntridy += tridy[i];
          }
        }
        kontejner.className = ntridy;
      }
    break;
    case 3: //pozadi ujizdi doprava (opera, chrome, safari)
      //document.getElementById('cesta').innerHTML = sirka + "<br />" + kontejner.className + "<br />" + window.innerWidth;
      if (sirka > 1000 && sirka % 2 == 1) {
        if (tridy.inArray("poskok_pravy")) break;
        kontejner.className += (kontejner.className.length > 0 ? " " : "") + "poskok_pravy";
      } else {
        if (!tridy.inArray("poskok_pravy")) break;
        var ntridy = "";
        for (var i = 0; i < tridy.length; i++) {
          if (tridy[i] != "poskok_pravy") {
            if (ntridy.length > 0) ntridy += " ";
            ntridy += tridy[i];
          }
        }
        kontejner.className = ntridy;
      }
    break;
    case 4: //posun doprava (safari, nelze pomoci padding)
      kontejner = document.body;
      var tridy = kontejner.className.split(" "); //tridy v poli  
      if (sirka > 1000 && sirka % 2 == 1) {
        if (tridy.inArray("pozadi_vlevo")) break;
        kontejner.className += (kontejner.className.length > 0 ? " " : "") + "pozadi_vlevo";
      } else {
        if (!tridy.inArray("pozadi_vlevo")) break;
        var ntridy = "";
        for (var i = 0; i < tridy.length; i++) {
          if (tridy[i] != "pozadi_vlevo") {
            if (ntridy.length > 0) ntridy += " ";
            ntridy += tridy[i];
          }
        }
        kontejner.className = ntridy;
      }
    break;
    case 5: //pozadi ujizdi doprava o 2px a jen od urcite sirky okna (safari 3)

    break;
  }
  setTimeout("opravPozadi()", 500);
}


/*** cekani na nacteni html ***/
function nacteniHtml() {
  if (!document.getElementById("zapati")) {
    htmlCas=htmlCas*2;
    setTimeout("nacteniHtml()", htmlCas);
    return;
  }
  opravPozadi();
  odstranitNbsp();
  obarvitTabulky();
}
htmlCas=2;
nacteniHtml();

