var useragent = navigator.userAgent;
var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;
var pos = useragent.indexOf('MSIE');

if (pos > -1) {
bVer = useragent.substring(pos + 5);
var pos = bVer.indexOf(';');
var bVer = bVer.substring(0,pos);
}
var pos = useragent.indexOf('Opera');
if (pos > -1)	{
bVer = useragent.substring(pos + 6);
var pos = bVer.indexOf(' ');
var bVer = bVer.substring(0, pos);
}
if (bName == "Netscape") {
var bVer = useragent.substring(8);
var pos = bVer.indexOf(' ');
var bVer = bVer.substring(0, pos);
}
if (bName == "Netscape" && parseInt(navigator.appVersion) >= 5) {
var pos = useragent.lastIndexOf('/');
var bVer = useragent.substring(pos + 1);
}

var isIE5plus = (parseFloat(bVer) >= 5.5 && bName == 'Microsoft Internet Explorer');

//Poblikavani uvodni stranky
var prvky = new Array();
prvky[0] = new Array(false, false, false);
prvky[1] = new Array(false, false, false);
prvky[2] = new Array(false, false, false);

function blinkFrontPage() {
  index1 = Math.floor(Math.random() * 3) + 1;
  index2 = Math.floor(Math.random() * 3) + 1;  

  if(prvky[index1 - 1][index2 - 1] == false) {  
    applyFilter(index1, index2);
  }
  else {
    disapplyFilter(index1, index2);
  }
  return setTimeout("blinkFrontPage();", (isIE5plus?1000:3000));
}

function applyFilter(idR, idC) {  
  currentElement = document.getElementById("grid" + idR + "" + idC + "_green");
  
  //Filtry funguji pouze v IE 5.5 a vyssi
  if(isIE5plus) {  
    currentElement.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=3, overlap=1.0)";                           
    if(currentElement.filters[0].status != 2) {
      currentElement.filters[0].Apply();
      currentElement.style.backgroundImage = "url('./bitmaps/grid" + idR + "" + idC + "_green.jpg')";
      currentElement.filters[0].Play();    
    }
  }
  else {
    currentElement.style.backgroundImage = "url('./bitmaps/grid" + idR + "" + idC + "_green.jpg')";
  }
  prvky[idR - 1][idC - 1] = true;
}

function disapplyFilter(idR, idC) {  
  currentElement = document.getElementById("grid" + idR + "" + idC + "_green");
  
  //Filtry funguji pouze v IE 5.5 a vyssi
  if(isIE5plus) {
    if(currentElement.filters[0].status != 2) {
      currentElement.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=3, overlap=0)";
      currentElement.filters[0].Apply();
      currentElement.style.backgroundImage = "url('./bitmaps/grid" + idR + "" + idC + ".jpg')";
      currentElement.filters[0].Play();
    }
  }
  else {
    currentElement.style.backgroundImage = "url('./bitmaps/grid" + idR + "" + idC + ".jpg')";
  }  
  prvky[idR - 1][idC - 1] = false;
}

// === Obarvovaci menu ===
var colorArray = new Array ('#ccc', '#D9D4B1', '#D2CB9B', '#E0D58A', '#fff', '#ccc', '#ddd', '#fff'); 
var colorTimer = "";
var uncolorTimer = "";
var uncolorTimerId = "";

var arrayUncolorTimerId = new Array();

function colorBG(objID) {
  if (colorTimer != "") clearTimeout(colorTimer);
  if (arrayUncolorTimerId[objID] != "") clearTimeout(arrayUncolorTimerId[objID]);
  cycledColorBG(objID, 0);
}

function cycledColorBG(objID, colorIndex) {
  document.getElementById(objID).style.backgroundColor = colorArray[++colorIndex];
  if (colorIndex < colorArray.length) {
    colorTimer =  setTimeout("cycledColorBG('" + objID + "', " + colorIndex + ")", 100);
  }
  else {

  }
}

function uncolorBG(objID) {
  if (colorTimer != "") clearTimeout(colorTimer);
  cycledUncolorBG(objID, colorArray.length);
}

function cycledUncolorBG(objID, colorIndex) {
  document.getElementById(objID).style.backgroundColor = colorArray[--colorIndex];
  if (colorIndex > 0) {
    arrayUncolorTimerId[objID] = setTimeout("cycledUncolorBG('" + objID + "', " + colorIndex + ")", 100);
  }
  else {
    document.getElementById(objID).style.backgroundColor = 'transparent';
    arrayUncolorTimerId[objID] = "";
  }
}

//Blikani loading
var blinkingColors = new Array("#999", "#aaa", "#bbb", "#ccc", "#ddd", "#eee", "#fff");
var blinkingElement = "";
var blinkingTimer = "";
var blinkIndex = 0;

function blinkOn(objID, iterate) {
  blinkingElement = objID;
  document.getElementById(blinkingElement).style.display = "block";  
  if(blinkIndex == 0) iterate=true;
  if(blinkIndex < blinkingColors.length && iterate==true) {
    document.getElementById(blinkingElement).style.color = blinkingColors[++blinkIndex];
    return blinkingTimer = setTimeout("blinkOn('" + objID + "', true)", 100);    
  }
  if(blinkIndex == blinkingColors.length) iterate=false;
  if(blinkIndex > 0 && iterate==false) {
    document.getElementById(blinkingElement).style.color = blinkingColors[--blinkIndex];
    return blinkingTimer = setTimeout("blinkOn('" + objID + "', false)", 100);
  }  
}

function blinkOff(objID) {
  if(blinkingTimer != "") {
    clearTimeout(blinkingTimer);
    document.getElementById(blinkingElement).style.display = "none";
  }  
}

function setMarginTop() {  
  if(document.getElementById('picture_area_img').height) {
    document.getElementById('picture_area_img').style.marginTop = Math.ceil((400 - parseInt(document.getElementById('picture_area_img').height)) / 2) + "px";
  }
}

//Filling picture area
function fillPictureArea(picture) {
  document.getElementById('picture_area_img').src = "./bitmaps/spacer.gif";
  document.getElementById('picture_area_img').src = picture;
  blinkOn('loading', true);
}

function showGrafikaDescription(elId) {
  elements = document.getElementById('text_area').getElementsByTagName('div');  
  for(i = 0; i < elements.length; i++){
    elements[i].style.display = "none";
  }
  
  elements = document.getElementById(elId).getElementsByTagName('div');  
  for(i = 0; i < elements.length; i++){
    elements[i].style.display = "block";
  }
  document.getElementById(elId).style.display = "block";  
}

var scrollTimer = "";
var imagesWidth = 640;
var imagesRowWidth = 0;
var margin_pictures = 10;
var IC = new Array();

function scrollImagesLeft() {
  element = document.getElementById('images_row');
  currentLeft = parseInt(element.style.left);
  
  if(currentLeft < 0) {
    element.style.left = (currentLeft + 10) + "px";    
  }
  scrollTimer = setTimeout("scrollImagesLeft()", 50);
}

function scrollImagesRight() {
  element = document.getElementById('images_row');
  currentLeft = parseInt(element.style.left);
  currentWidth = parseInt(element.style.width);
  
  if(currentLeft >= (-imagesRowWidth + imagesWidth)) {
  //if(currentLeft >= (-1200 + imagesWidth)) {
    element.style.left = (currentLeft - 10) + "px";    
  }
  scrollTimer = setTimeout("scrollImagesRight()", 50);
}

function stopScrolling() {
  clearTimeout(scrollTimer); 
}

function ICContains(value) {
  if(IC[value] == 1) return true;
    else return false;
}

function addImagesRowWidth(tid) {
  if(!ICContains(parseInt(tid.substr(8)))) {
    imagesRowWidth += document.getElementById(tid).width + margin_pictures;
  }
}

function setProperties() {
  if(element = document.getElementById('images_row')) {
    pictures = element.getElementsByTagName('img');
    for(i = 0; i < pictures.length; i++) {      
      if(pictures[i].width) {
        imagesRowWidth += pictures[i].width + margin_pictures;
        IC[parseInt(pictures[i].id.substr(8))] = 1;
      }
    }
    alert(imagesRowWidth);
  }
}
