$('.imgbox').hide();

$(document).ready(function() {
  
 /*$('html, body').css('overflow', 'hidden');
  $('#contents').css({
    overflow: 'hidden',
    position: 'fixed',
    height: '100%'
  });*/

  

  $('.imgbox').hide();
  $('.imgbox:empty').remove(); // get rid of empty content
  
  $('#logobutton').toggle(function() {
  	$('html').addClass('activebg');
  }, function() {
	$('html').removeClass('activebg');
  });

  $('.thumbdiv').find('img').removeAttr('title');
  
  $('#menu li a:contains("Impressum")').parent().remove();

  
  var imgboxcount = $('.imgbox img').length; // how many images are there?
  var isloaded = $('.illu[complete=complete]').length; // How many are already cached?
  var loadcount = isloaded; // set loadcount to number of already loaded img
  
  // append scroll buttons and hide them
  $("<div class='buttonsfloat'><div id='area1' class='area'><div class='ui-slider-handle'></div></div><p class='buttons'><span class='prev'></span><span class='next'></span></p></div>").hide().prependTo('#content');

  $('.area').height(355);
  // show menu2
  $('.menu2').show();
  
  // insert contact info
  $('#accordion ul:last a:last').parent().remove();
  $('#contactdrop').appendTo('#accordion ul:last').show();
  
  $('.imgbox p img').parent().css('margin-top', '0');
  
  // Attach impressum to main div
  $('#impressumouter').prependTo('.main');
  
  // Attach white overlay to be used with impressum
  $('<div id="whitebox"></div>').appendTo('body');
  
  // Update active thumb parent div
  function thumbparent() {
  
    if ($('.thumbdiv .active').parent().hasClass('active')) {
      return false;
    } else {
      $('.thumbdiv.active').removeClass('active').hide();
        $('.thumbdiv .active').parent().addClass('active').show();
      }
    }
  
  // Function to update thumb when scrolling
  function activethumb(value) {
    $('.thumbdiv .active').removeClass('active');
    $('.thumbdiv div:eq('+value+')').addClass('active');
    thumbparent();
  }
  
  // function to handle slider knob
   function slide(containheight, imgboxheight){
     $(function() {
       $('.area').slider({
         orientation: 'vertical',
         min: (containheight * -1 + 550),
         animate: true,
         max: 0,
         range: 'max',
         slide: function(event, ui) {
           $('#content .container').css('top',(ui.value));
           var currthumb = Math.round(ui.value / -imgboxheight);
           activethumb(currthumb);
         }
       });
       
       if (imgboxcount > 1) {
       $('.ui-slider-handle').hide().delay(500).fadeIn('slow');
       
       
       $('.buttonsfloat').delay(500).fadeIn('slow');
      
 
      }
     });
     
     
   }
  
  
   function thumbnavigation(containheight, imgboxheight) {
   $('.thumbdiv div').click(function() {
     $('.thumbdiv div.active').removeClass('active');
     $(this).addClass('active');
     var additionalth = $(this).parent().prevAll('.thumbdiv').length;
     var clickedindex = $(this).prevAll().length;
     containtop = (additionalth*18+clickedindex) * imgboxheight;
     knob(-containtop);
     $('#content .container').animate({top: -containtop}, 350);
   });

   }
        
  
  // function to enable navigation
  function enablenav(){
  var containheight = $('#content .container').outerHeight();
  var imgboxheight = $('.imgbox').outerHeight() + parseFloat($('.imgbox:eq(0)').css('margin-bottom'));
  var containtop = parseFloat($('#content .container').css('top'));
  
  thumbnavigation(containheight, imgboxheight)

  slide(containheight, imgboxheight);
  
  // get current scroll position
  $('.buttons span, .ui-slider-handle').mouseover(function() { 
    containtop = (Math.round((parseFloat($('#content .container').css('top')))/imgboxheight))*imgboxheight;
    });
    
    $('.ui-slider-handle').mouseup(function() { 
      containtop = (Math.round((parseFloat($('#content .container').css('top')))/imgboxheight))*imgboxheight;
      });
      
      
  
  // Define next button
  if (containtop <= 0) {
    $('.next').click(function() {
     containtop -= imgboxheight;
     if (containtop < -containheight) {
       containtop = -containheight+550;
     } 
     if (containtop >= (-containheight)){
       $('#content .container').stop();
       $('#content .container').animate({top: containtop}, 350);
       knob(containtop);
       activethumb(Math.round(-containtop / imgboxheight));
       thumbparent();
      }
      
    });
  }
  
  // Define Prev button
    $('.prev').click(function() {
      if (containtop <= 0){
       
        containtop += imgboxheight;
       
         if (containtop > 0) {

           containtop = 0;
           
         }
        if (containtop > (-containheight) || containtop == 0){
          $('#content .container').stop();
          $('#content .container').animate({top: containtop}, 350);
          knob(containtop);
          activethumb(Math.round(-containtop / imgboxheight));
          thumbparent();
        }
      } else {
       
      }
    });
    }
  
  // define function to adjust value of handle
  function knob(knobtop){
   $(".area").slider("option", "value", knobtop);
    }
  
  $('.thumbdiv:empty').remove();
  $('.thumbdiv').wrapAll('<div id="thumbouter"><div id="thumbinner"><div id="thumbcontainer">');
  $('#thumbouter').prependTo('.main');
  $('#thumbouter .thumbdiv:first').show().addClass('active');
  $('#thumbouter .thumbdiv:first div:first').addClass('active');
  
  // define function to run when all img are loaded
  function showall() {
    $('#imgcounter').delay(500).fadeOut('fast', function() {
      $(this).remove();
      $('.imgbox').show();
      enablenav();
      
	$('.descbottom').each(function(index) {
		var pheight = parseFloat($(this).outerHeight());

		var pmargin = 490 - pheight;
		$(this).css('margin-top', pmargin);
	});

	});
  }  
  
  // iterate all images, load them and adjust loadcount accordingly   TODO: MAKE SAME FOR THUMBS
  if ($('.imgbox img').length > 0){
      $('#content .container').append('<span id="imgcounter">loading <span>0</span> of '+imgboxcount+' images</span>'); // append loadcounter
      
      $('.imgbox img').each(function(index) {
		$(this).removeAttr('title');
        var loadstate = $(this).attr('complete');
        if (loadstate === true) {
          loadcount += 1;
          $('#imgcounter span').html(loadcount);
           if (loadcount == imgboxcount) {
              showall();
            }
        } else {
          $(this).load(function() {
            loadcount += 1;
            $('#imgcounter span').html(loadcount);
            
             if (loadcount == imgboxcount) {
                showall();
              }
            });
            }
          
         
        });    
      } else {
        $('.imgbox').show();
      }

  
  // Impressum stuff
  $('#impon').click(function() {
    $('#whitebox').show();
    $('#impressuminner').show('slide', { 
  	            direction: 'left'
  	            }, 400, function () {
                        $('#impressum h3.ttr.fixed').show(); 
                      });
  			});
  			
  $('#impressum h3.ttr.fixed').click(function() {
    $(this).hide();
      $('#impressuminner').hide('slide', { 
   	            direction: 'left'
   	            }, 400, function () {
                        $('#whitebox').hide(); 
                      });
   	                });
   	                
// thumb stuff
var thumbpagecount = $('.thumbdiv div').length;

if (thumbpagecount > 18) {
  $('<p class="divider th">&nbsp;|&nbsp;</p>').hide().appendTo('.menu2');
  $('.thumbdiv').each(function(index) {
    $('<p class="thumbbutton">'+(index+1)+'</p>').hide().appendTo('.menu2');
  });
} else if (thumbpagecount <= 1) {
  $('#thumbon, .divider').remove();
}

$('#thumbon').toggle(function() {
  $(this).text('close');
  $('#thumbinner').show('slide', { 
	            direction: 'left'
	            }, 400);
	$('.divider.th, .thumbbutton').css('display', 'inline');            
}, function() {
  $(this).text('thumbs');
  $('#thumbinner').hide('slide', { 
	            direction: 'left'
	            }, 400);
              $('.divider.th, .thumbbutton').hide();
});

$('.thumbbutton:first').addClass('active');


$('.thumbbutton').click(function() {
  $('.thumbbutton.active').toggleClass('active');
  $(this).addClass('active');
  var thindex = $(this).prevAll('.thumbbutton').length;
  $('.thumbdiv.active').removeClass('active').fadeOut('fast', function() {
    $('.thumbdiv:eq('+thindex+')').fadeIn('fast').addClass('active');
  });
  
  
});



  $('html').mouseover(function() {
    if ($(window).height() < 550 && parseFloat($('#content').css('margin-top')) > 0) {
    $('#content').animate({'margin-top': 0}, 200)  

    }});




 

// vertical adjust function 
function adjustmain(){
  var adjust = ($(window).height()-550) / 2;
  if (adjust > 0){
  $('.main, #content').css('margin-top', adjust);
//$('html').css('overflow', 'hidden');
  } else {
    //$('html').css('overflow', 'visible');
  //  $('<div id='solidbg'>').appendTo('body');
  }
} 	

adjustmain();

$(window).resize(function() {
  adjustmain(); 
});

 $('.imgbox:last').css('margin-bottom', '0px');
  
// Button fade effects  
  $('.prev, .next').mouseover(function() {
    $(this).fadeTo('fast', 1);
  });

  $('.prev, .next').mouseout(function() {
    $(this).fadeTo('fast', 0.7);
  });
  
// ...enable navigation via arrow keys. 39 = rightArr, 37 = leftArr, 38 = upArr, 40 = downArr, 27 = esc  	
  	$(document).bind('keydown',function(e){ 
      if (e.which == 40) {
          $('.next').trigger('click');
				} else if (e.which == 38) {
          $('.prev').trigger('click'); 
          }
			  });  
			  
			  
			  $(function() {
				  
			var actpoint = $('#menu li.active span').length;
			var lad = $('#menu ul:not(:has("span")) ul li.active').parent().parent().parent().prevAll('ul:not(:has("span"))').length;
			
            if (actpoint === 0){

              	$('#accordion').accordion({
    							header: '.section-title',
    							autoHeight: false,
    							collapsible: true,
								active: lad
    							
                }); 
                } else {
              	$('#accordion').accordion({
    							header: '.section-title',
    							autoHeight: false,
    							collapsible: true,
    							active: false
                });
              } 
            });
	

});

