window.pos_scrolledText = 0;
window.pos_scrollBar_base = 20; // poloha uplne nahore
window.pos_scrollBar = 0;
window.pos_scrollBar_tmp = 0;
window.drag_start_y = null;
window.elem_scrollBar = null;

// from http://adomas.org/javascript-mouse-wheel/
function handleTextScroll(event){
  var delta = 0;
  if (!event) /* For IE. */
    event = window.event;
  if (event.wheelDelta) { /* IE/Opera. */
    delta = event.wheelDelta/120;
    /** In Opera 9, delta differs in sign as compared to IE.
    */
    if (window.opera)
      delta = -delta;
  } else if (event.detail) { /** Mozilla case. */
    /** In Mozilla, sign of delta is different than in IE.
     * Also, delta is multiple of 3.
     */
    delta = -event.detail/3;
  }
  /** Prevent default actions caused by mouse wheel.
   * That might be ugly, but we handle scrolls somehow
   * anyway, so don't bother here..
   */
  if (event.preventDefault)
    event.preventDefault();
  event.returnValue = false;

  /** If delta is nonzero, handle it.
   * Basically, delta is now positive if wheel was scrolled up,
   * and negative, if wheel was scrolled down.
   */
  if (delta) {
    initScrollBar();
    if ( visibleHeight < totalHeight ) {
      moveScrollbarByPixels(delta*-20);
    }
  }
}

function handleScrollbarMove(e){
  var e = e || window.event;
  delta = e.clientY - window.drag_start_y;
  moveScrollbarToPixels(delta,false);
}

function moveScrollbarByPixels(delta){
  if ( visibleHeight < totalHeight ) {
    moveScrollbarToPixels(delta, true);
  }

}

function moveScrollbarToPixels(delta, saveState){
  pos_scrollBar_tmp = pos_scrollBar_base + pos_scrollBar + delta;
  if ( pos_scrollBar_tmp < drag_min_pos) {
    pos_scrollBar_tmp = drag_min_pos;
  } else if (pos_scrollBar_tmp > drag_max_pos) {
    pos_scrollBar_tmp = drag_max_pos;
  }

  window.elem_scrollBar.style.top = pos_scrollBar_tmp;
  //            window.status = "Scrolled to : " + Math.round((pos_scrollBar_tmp-pos_scrollBar_base)/(drag_max_pos - drag_min_pos)*100);

  if( saveState) window.pos_scrollBar = pos_scrollBar_tmp - pos_scrollBar_base;
  scrollTextToPercent((pos_scrollBar_tmp-pos_scrollBar_base)/(drag_max_pos - drag_min_pos));
}

function setup(){   // definuje se v jednotlivych strankach 
  // initScrollBar();
  // initNews();
}

function initNews(){
  window.news_state = 100;      // 0.100%
  window.news_actual_item = 0;
  window.news_count = 5;        // pocet novinek

  window.news_state_values_opacity = Array();
  window.news_state_values_opacity[0] = "0.0";
  window.news_state_values_opacity[10] = "0.1";
  window.news_state_values_opacity[20] = "0.2";
  window.news_state_values_opacity[30] = "0.3";
  window.news_state_values_opacity[40] = "0.4";
  window.news_state_values_opacity[50] = "0.5";
  window.news_state_values_opacity[60] = "0.6";
  window.news_state_values_opacity[70] = "0.7";
  window.news_state_values_opacity[80] = "0.8";
  window.news_state_values_opacity[90] = "0.9";
  window.news_state_values_opacity[100] = "1.0";

  window.news_state_values_color = Array();
  window.news_state_values_color[0]   = "#000";
  window.news_state_values_color[10]  = "#222";
  window.news_state_values_color[20]  = "#333";
  window.news_state_values_color[30]  = "#444";
  window.news_state_values_color[40]  = "#555";
  window.news_state_values_color[50]  = "#666";
  window.news_state_values_color[60]  = "#777";
  window.news_state_values_color[70]  = "#999";
  window.news_state_values_color[80]  = "#bbb";
  window.news_state_values_color[90]  = "#ddd";
  window.news_state_values_color[100] = "#fff";

  document.getElementById('news_text_'+parseInt(window.news_actual_item)).style.visibility = "visible";
  showNewsItem();
}

function displayNewsItem(){
  document.getElementById('news_box').style.opacity = window.news_state_values_opacity[window.news_state];
}

function hideNewsItem(){
  if ( news_state > 0 ){
    window.news_state -= 10;
    window.displayNewsItem();
    window.setTimeout('hideNewsItem()',50);
  } else {
    window.news_state = 0;
    changeNewsItem();
  }
}

function showNewsItem(){
  if ( news_state < 100 ){
    window.news_state += 10;
    window.displayNewsItem(window.news_state);
    window.setTimeout('showNewsItem()',50);
  } else {
    window.news_state = 100;
    window.setTimeout('hideNewsItem()',7000);
  }
}

function changeNewsItem(){
  document.getElementById('news_text_'+parseInt(window.news_actual_item)).style.visibility = "hidden";

  window.news_actual_item += 1;
  if ( ! document.getElementById('news_text_'+parseInt(window.news_actual_item)) ){
    window.news_actual_item = 0;
  }
  document.getElementById('news_text_'+parseInt(window.news_actual_item)).style.visibility = "visible";
  window.showNewsItem(0);
}

function updateElementSizes(){
  document.getElementById('content_body').style.height = document.body.clientHeight - 280 - 80;
  document.getElementById('scroll_progress_bar').style.height = document.getElementById('scroller_container').offsetHeight  - 4*document.getElementById('scroll_up').offsetHeight;

  window.visibleHeight = document.getElementById('content_body').offsetHeight;
  //          window.status=visibleHeight;
  window.totalHeight = document.getElementById('text_container').offsetHeight;

  update_scroll_visibility();
}

function update_scroll_visibility(){
  if ( visibleHeight >= totalHeight ) {
    document.getElementById("scroller_container").style.visibility="hidden";
  } else {
    document.getElementById("scroller_container").style.visibility="visible";
  }
}

function initScrollBar(){
  document.ondragstart = function () { return false; };  /* cancel IE drag */

  // nastavi velikosti, pripadne schova posuvnik, kdyz nebude potreba
  window.onresize = updateElementSizes;  /* CSS height update for IE */
  updateElementSizes();       /* onLoad height update for IE */

  window.elem_scrollBar = document.getElementById('scroll_bar');
  window.pos_scroll_min = window.elem_scrollBar.style.top;

  if (window.addEventListener)
    /** DOMMouseScroll is for mozilla. */
    document.getElementById('content_body').addEventListener('DOMMouseScroll', handleTextScroll, false);
  /** IE/Opera. */
  window.onmousewheel = document.onmousewheel = handleTextScroll;

  window.drag_min_pos = pos_scrollBar_base;
  window.drag_max_pos = document.getElementById('scroller_container').offsetHeight - document.getElementById('scroll_up').offsetHeight - document.getElementById('scroll_bar').offsetHeight;
}

function startDrag(e){
  var e = e || window.event;
  if ( visibleHeight < totalHeight ) {
    window.document.onmousemove = handleScrollbarMove;
    window.document.onmouseup = stopDrag;

    window.drag_start_y = e.clientY;
  }

  if(e.preventDefault){
    e.preventDefault();
  }
}

function stopDrag(){
  window.document.onmousemove = null;
  window.document.onmouseup = null;
  window.pos_scrollBar = pos_scrollBar_tmp - pos_scrollBar_base;
  //window.status = "Stopped at : " + Math.round(pos_scrollBar/(drag_max_pos - drag_min_pos)*100);
}

function scrollTextToPercent(p){
  if ( visibleHeight < totalHeight ) {
    document.getElementById('text_container').style.top = -1 * (totalHeight - visibleHeight) * p;
  }
}

function A(obj){
  msg = "";
  for (prop in obj){
    msg += prop + ":" + obj[prop] + "\n";
  }
  alert(msg);
}



