var fullopacity = 0;
var bypassed = false;
var fullimage = new Image();

window.onload = function() {

  // sirka galerie
  default_acc = -0.1; 
  default_slow = 0.9;
  gallery_width = 850; 
  min_space = 30;
  top_space = 20;
  spd = 0;
  spd_min = -2;
  slow = 1;
  acc = default_acc;
  document.getElementById('gallery').style.width = gallery_width + 'px';
  document.getElementById('gallery').style.display = 'block';
  document.getElementById('gallery_img_bg').style.height = thumbnail_height + 'px';
  
  left_space = (gallery_width / (photo_count - 1)) / 2 - (thumbnail_width / 2);
  photo_space = (gallery_width / (photo_count - 1)) - thumbnail_width;
  
  if (left_space < min_space) { left_space = min_space; }
  if (photo_space < min_space) { photo_space = min_space; }

  for (i = 0; i < photo_count; i++) {
    images[i]['x'] = left_space + i * (photo_space + thumbnail_width);
    images[i]['y'] = top_space; 
    images[i]['w'] = thumbnail_width;
    images[i]['h'] = thumbnail_height;
    images[i]['v'] = true;
    element = document.getElementById('image_' + i);
    // zastavenie
    element.onmouseover = function() {
      this.className = 'gallery_image_hover';
      slow = default_slow;
      acc = 0;
    }
    // znova spustenie
    element.onmouseout = function() {
      if (!bypassed) {
        this.className = 'gallery_image';
        acc = default_acc;
        slow = 1;
      }
    }
  }
  // priradenie udalosti k pasu medzi fotkami
  element = document.getElementById('gallery_img_bg');
  // zastavenie
  element.onmouseover = function() {
    slow = default_slow;
    acc = 0;
  }
  // znova spustenie
  element.onmouseout = function() {
    acc = default_acc;
    slow = 1;
  }
  // pozicia novej fotky
  new_pos = left_space + photo_count * (photo_space + thumbnail_width);
   
  updateImages(); 
  moveImages(); 
}

function updateImages() {
  for (i = 0; i < photo_count; i++) {
    element = document.getElementById('image_' + i);
    element.style.display = ((images[i]['v']) ? 'block' : 'none');
    if (images[i]['v']) {     
      element.style.left = images[i]['x'] + 'px';
      element.style.top = images[i]['y'] + 'px';
      element.style.width = images[i]['w'] + 'px';
      element.style.height = images[i]['h'] + 'px';
    }
  }
}

function moveImages() {
  // posuvam fotky a novu poziciu
  if (!bypassed) {
  new_pos += spd;  
  for (i = 0; i < photo_count; i++) {
    images[i]['x'] += spd;
    // ak fotka presla za hranicu, nahradim novu poziciu
    if (images[i]['x'] < -thumbnail_width) {
      images[i]['x'] = new_pos;
      new_pos += photo_space + thumbnail_width;
    }
    images[i]['v'] = (images[i]['x'] <= gallery_width);
  }
  if (spd < spd_min) {
    spd = spd_min;
  }
  spd += acc;
  spd *= slow;
  
  // updatenem
  updateImages();  
  }
  timer = setTimeout('moveImages()', 20);
}

function openLarge(filename) {
  bypassed = true;
  // prekryjem obraz
  fullopacity = 0;
  full = document.getElementById('fullscreen');
  full.style.opacity = fullopacity;
  full.style.filter = 'alpha(opacity=' + fullopacity*100 + ')';
  full.style.display = 'block';
  // nacitam obrazok
  fullimage = new Image();
  fullimage.src = filename;
  fadeInFull();  
  waitLoading();  
}

function getWinWidth() {
  if (typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    return window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    return document.body.clientWidth;
  }
}

function getWinHeight() {
  if (typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    return window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    return document.body.clientHeight;
  }
}

function continueOpening() {
  photo = document.getElementById('large_photo');
  
  leftPX = getWinWidth() / 2 - fullimage.width / 2;
  topPX = getWinHeight() / 2 - fullimage.height / 2;
  photo.style.left = leftPX + 'px';
  photo.style.top = topPX + 'px';
  
  photo.style.display = 'block';
  
  photo.innerHTML = '<a href="javascript:closeImage()"><img src="' + fullimage.src + '" title="Zavrieť" alt="Foto" /></a>';  
}

function waitLoading() {
  if (!fullimage.complete) {
    wait = setTimeout(waitLoading, 100);
  } else {
    continueOpening();
  }
}

function closeImage() {
  photo = document.getElementById('large_photo');
  photo.style.display = 'none';
  document.getElementById('fullscreen').style.display = 'none';
  bypassed = false;
  for (i = 0; i < photo_count; i++) {
    document.getElementById('image_' + i).className = 'gallery_image';
  }
}

function fadeInFull() {
  full = document.getElementById('fullscreen');
  fullopacity += 0.1;  
  full.style.opacity = fullopacity;
  full.style.filter = 'alpha(opacity=' + fullopacity*100 + ')';
  if (fullopacity < 0.5) {
    timer = setTimeout(fadeInFull, 20);
  } 
}
