function refresh_arrows(){
  if ($('in_scroll') && $('text_level') && ($('in_scroll').getHeight() < $('text_level').getHeight() - 20)){
    $('arrow_up').style.visibility = "visible";
    $('arrow_down').style.visibility = "visible";
  }
}

Event.observe(window, 'load', function(){
  refresh_arrows();
});

function start_scroll_up(){
  up = new PeriodicalExecuter(function(){
    if (parseInt($('text_level').getStyle('top')) < 0){
      $('text_level').style.top = parseInt($('text_level').getStyle('top')) + 8 + 'px';
    }
  }, 0.05);
}

function stop_scroll_up(){
  try {
    up.stop();
  } catch(e) {}
}

function start_scroll_down(){
  down = new PeriodicalExecuter(function(){
    if ((parseInt($('text_level').getStyle('top')) || 0) > ($('in_scroll').getHeight() - $('text_level').getHeight() + 16)){
      $('text_level').style.top = (parseInt($('text_level').getStyle('top')) || 0) - 8 + 'px';
    }
  }, 0.05);
}

function stop_scroll_down(){
  try {
    down.stop();
  } catch(e) {}
}

function ajax_link(event, str){
  Element.update('ind_text', str);
  $('indicator').style.left = Event.pointerX(event) -10 + 'px';
  $('indicator').style.top = Event.pointerY(event) - 10 + 'px';
  $('indicator').show();
}


function play(){
  var time_of_reloading = 4;
  $('image_play').replace('<div id="image_play"/>')
  $('image_stop').replace('<div id="image_stop" class="active" onclick="stop_play();"/>')
  $('image_play').removeClassName("active");
  $('image_stop').addClassName("active");
  pe = new PeriodicalExecuter(function(){
    $('short_descriptions_text').fade({duration: 1, afterFinish: function(){
      new Ajax.Request('/play', { asynchronous:true, evalScripts:true });
    }});
  }, time_of_reloading);
}

function image_next(){
  stop_play();
  $('short_descriptions_text').fade({duration: 1, afterFinish: function(){
    new Ajax.Request('/play?one=1', { asynchronous:true, evalScripts:true });
  }});
}

function image_prev(){
  stop_play();
  $('short_descriptions_text').fade({duration: 1, afterFinish: function(){
    new Ajax.Request('/play?prev=1&one=1', { asynchronous:true, evalScripts:true });
  }});
}

function stop_play(word, antiword){
  $('image_play').replace('<div id="image_play" class="active" onclick="play();"/>')
  $('image_stop').replace('<div id="image_stop"/>')
  try { pe.stop();} catch(e) {}
}

function swap_images(){
  var a = [
    new Effect.Fade('photo_level_0', {sync: true}),
    new Effect.Appear('photo_level_1', {sync: true}),
    new Effect.Appear('short_descriptions_text', { sync: true })
  ];
  new Effect.Parallel(a, { duration: 1.5});
}

function swap_text(thiss,text1,text2, left_right){
  var time = 0.4;
  thiss.style.backgroundPosition='top ' + left_right;
  $('short_descriptions_text').fade({ duration: time, afterFinish: function(){
    Element.update('short_descriptions_text','<h3>' + text1 + '</h3>' + text2);
    $('short_descriptions_text').appear({ duration: time});
  }});
}
