﻿/* COPYRIGHT JINDŘICH MIKA 2010 ALL RIGHTS RESERVED */
// horizontal version

// Obecné deklarace
var pages = 3;
var width = 850;
var sliderId = 'slider';
var descriptionId = 'text';
var timeouts = new Array();
var timeouts_loop = new Array();
var currentPage = 1;
var currentWidth = 0;
var loopSpeed = 8000; // miliseconds

// Vlastní funkce
function change(num,page) {
  element = document.getElementById(sliderId);
  newWidth = (page-1)*width;
  
  if(currentWidth == newWidth) {
    //alert('sameness accomplished by approximation, clearing...');
    clear();
    finalWidth == currentWidth;
  }
  else {
    finalWidth = Math.ceil(currentWidth+(((newWidth-currentWidth)/100)*num));
  }
  
  // bugfix, override
  if(num > 40) finalWidth = newWidth;

  element.style.backgroundPosition = '-'+finalWidth+'px 0px';
  currentWidth = finalWidth;
}

function clear() {
  for(i=1; i<=pages; i++) {
    element = document.getElementById(descriptionId+i);
    if(i != currentPage) {
      element.style.display = 'none';
    }
    else {
      element.style.display = 'block';
      setOpacity(descriptionId+i, 100);
    }
  }
  
  for(i=0; i<=timeouts.length; i++) {
    clearTimeout(timeouts[i]);
  }
}

function setOpacity(id, num) {
  element = document.getElementById(id);
  if(num > 0) {
    element.style.display = 'block';
    element.style.opacity = num/100;
    element.style.filter = 'alpha(opacity='+(num)+')';
  }
  else element.style.display = 'none';
}

function fadeEm(num) {
  for(i=0; i<=100; i+=10) {
    finalTime = i*2;
    timeouts[timeouts.length+1] = setTimeout('setOpacity(\''+descriptionId+currentPage+'\','+(100-i)+')', finalTime);
  }

  for(i=0; i<=100; i+=10) {
    timeouts[timeouts.length+1] = setTimeout('setOpacity(\''+descriptionId+num+'\','+i+')', (finalTime+i*2)-5);
  }
}

function changeTo(num) {  
  if(num != currentPage) {
    clear();
    fadeEm(num);
    document.getElementById('nav'+currentPage).className = 'off';
    document.getElementById('nav'+num).className = 'on';
    currentPage = num;
    
    for(i=0; i<=100; i++) {
      timeouts[timeouts.length+1] = setTimeout('change('+i+','+num+')', i*20);
    }
  }
}

function stopLoop() {
  for(i=0; i<=timeouts_loop.length; i++) {
    clearTimeout(timeouts_loop[i]);
  }
}

function loop() {
  time = 0;
  for(i=1; i<=pages; i++) {
    timeouts_loop[timeouts_loop.length+1] = setTimeout('changeTo('+i+')', time);
    time += loopSpeed;
    if(i==pages) timeouts_loop[timeouts_loop.length+1] = setTimeout('loop()', time);
  }
}

loop();
