Number.prototype.mod = function(n) { // fix javascript modulo operator
  return ((this % n) + n) % n;
}

$(function() {  
  
  var time = 750
  var hidden_pos = 410
  
  var move_scene = function(to, callback) {
    var with_rand = true    
    
    if (to == 'down') { to = header_height; with_rand = false }
    if (to == 'up') { to = hidden_pos }    
  
    to = to || hidden_pos    
        
    var seed = 30
    var rand = function(min, max ) {
      if (with_rand = false)  { return 0 }  
      return min + parseInt(Math.random() * (max - min + 1))
    }
              
    $('#horizont_2').delay(rand(0,200)).animate({top:to}, time / 2 )
    $('#horizont_3').delay(rand(0,200)).animate({top:to+ 57}, time / 2)  
    $('#horizont_4').delay(rand(0,200)).animate({top:to+ 107}, time / 2, callback)
  }    


  var show_page = function() {
    $("#interface").fadeIn(time)   
    $('#container').animate({top:header_height - 460 })       
    $("#subnavigation")
      .css({ 'top':(hidden_pos + 50)+'px'}) 
      .show()      
      .animate({top:(header_height-30)+'px'})       
    $('#imprint').hide().fadeIn(time)             
    $('#parts').fadeIn(time)       

    $('#steps').fadeIn(time)
    // $('#footer').fadeIn(time)
    $('.step').hide().fadeIn(time)   
    
    move_scene('down')     
  }
  
  var hide_page = function(link) { 
    $("#interface").fadeOut(time / 3)    
    $('#container').animate({top:0})
    $('.step').fadeOut(time / 3)       
    $('.part').fadeOut(time / 3)     
    $('#imprint').fadeOut(time / 3)          
    $("#subnavigation").animate({top:(hidden_pos+ 40)+'px'}, time)         
    move_scene('up', function() {
      window.location = link.find('a').attr('href')
    });       
  }
  
  show_page()
  
  $('#logo').click(function(e) {
    e.preventDefault();          
    hide_page($(this))        
  })
  
  $('#main li').click(function(e) {
    e.preventDefault();          
    hide_page($(this))        
  })
  
  $('.feature.front').click(function(e) {
    e.preventDefault();          
    hide_page($(this))        
  })
  
  $('#business li').click(function(e) {
    e.preventDefault();          
    hide_page($(this))        
  })  
  
  $('#subnavigation li').click(function(e) {
    e.preventDefault();          
    hide_page($(this))    
  })

  $("#next").click(function(e) {     
    e.preventDefault();           
    hide_page($(this))    
  })   
  
  
  $("#prev").click(function(e) { 
    e.preventDefault();           
    hide_page($(this))    
  })
  


})

