// JavaScript Document
$(document).ready(function(){
	var opacity = 1.0;
	/*window.setInterval(function(){$('#output').html("Counter:" + counter + " \n <br /> #bgA Classes: " + $('#bgA').attr('class') + "  \n <br/> #bgB Classes: " + $('#bgB').attr('class') + " \n <br/> bgA opacity: " + $('#bgA').css('opacity') + " \n <br/> bgB opacity: " + $('#bgB').css('opacity'));}, 100);
	*/
	var rand = Math.floor(Math.random()* 6) + 1;
	$('#container').before('<img src="img/bg/bg00' + rand  + '.jpg" alt="" style="opacity: 0.0;" id="bgA" class="activeBg" />\n <img src="img/bg/bg00' + 3  + '.jpg" alt="" style="opacity: 0.0;" id="bgB" class="nActiveBg" />');
	if($('#container').hasClass('noSlideshow')){
		opacity= 0.2;
	}

	$('.activeBg').animate({opacity: opacity}, 10000, bgSlideshow);
	
	
	
	// SLIDESHOW
	
	var numSlides = images.length;
	var counter = 0;	
	window.setInterval(count, 7000);
	var slide = false;
	var idle=true;
	var currentSlide = 0;
	var manual = false;
	
	
	$('#slideB').css({opacity: 0});
	
	function count(){
		if((idle && slide)){
			counter++;
			if(counter > numSlides -1){
				counter = 0;
			}
			slideshow(counter);
		}else if(manual){
			manual = false;
			counter++;
			if(counter > numSlides -1){
				counter = 0;
			}
			slideshowFast(counter);
		}
		
	}
	function slideshow(c){
		if($('#slideA').hasClass('active')){
			$('#loader').addClass('loading');
			var img = new Image();
			$(img).load(function () {
				$('#loader').removeClass('loading');
				$('#slideB').html(this);
				}).error(function () {
			}).attr('src', images[c]);
			
			$('#slideB').addClass('active');
			$('#slideA').removeClass('active');
			 
			$('#slideB').css({opacity: 0.0}); 
			$('#slideB').animate({opacity: opacity}, 1000);
			$('#slideA').animate({opacity: 0}, 1000);
		}else{
			var img = new Image();
			$(img).load(function () {
				$('#loader').removeClass('loading');
				$('#slideA').html(this);
				}).error(function () {
			}).attr('src',  images[c]);
			$('#slideA').addClass('active');
			$('#slideB').removeClass('active'); 
			$('#slideA').css({opacity: 0.0});
			$('#slideA').animate({opacity: opacity}, 1000);
			$('#slideB').animate({opacity: 0}, 1000);
		}
		currentSlide = c;
		
	}
	
	function slideshowFast(c){
		if($('#slideA').hasClass('active')){
			$('#slideB').empty();
			$('#loader').addClass('loading');
			var img = new Image();
			$(img).load(function () {
				$('#loader').removeClass('loading');
				$('#slideB').html(this);
				}).error(function () {
			}).attr('src', images[c]);
			
			$('#slideB').addClass('active');
			$('#slideA').removeClass('active');
			
			$('#slideB').animate({opacity: opacity}, 200);
			$('#slideA').animate({opacity: 0.0}, 300);
			
			
		}else{
			$('#slideA').empty();
			$('#loader').addClass('loading');
			var img = new Image();
			$(img).load(function () {
				$('#loader').removeClass('loading');
				$('#slideA').html(this);
				}).error(function () {
			}).attr('src', images[c]);
			$('#slideA').addClass('active');
			$('#slideB').removeClass('active'); 
			$('#slideA').animate({opacity: opacity}, 200);
			$('#slideB').animate({opacity: 0.0}, 300);
			
		}
		currentSlide = c;
		
	}

	///////////////////////////////////////////////////////
	
	function bgSlideshow(){
		
		var rand = Math.floor(Math.random()* 6) + 1;
		
		
		var a = $(".activeBg");
		var b = $(".nActiveBg");
		
		a.removeClass('activeBg');
		a.addClass('nActiveBg');
		b.removeClass('nActiveBg');
		b.addClass('activeBg');
		
		b.attr("src", "img/bg/bg00" + rand  + ".jpg");
		b.load(function(){
			var theWindow = $(window);
			var	$bgB = $("#bgB");
			var	$bgA = $("#bgA");
			var aspectRatioB = $bgB.width() / $bgB.height();
			var aspectRatioA = $bgA.width() / $bgA.height();
			if ( (theWindow.width() / theWindow.height()) < aspectRatioB ) {
						$bgB
							.removeClass('bgwidth')
							.removeClass('bgheight')
							.addClass('bgheight');
					} else {
						$bgB
							.removeClass('bgheight')
							.removeClass('bgwidth')
							.addClass('bgwidth');
			}
			if ( (theWindow.width() / theWindow.height()) < aspectRatioA ) {
						$bgA
							.removeClass('bgwidth')
							.removeClass('bgheight')
							.addClass('bgheight');
					} else {
						$bgA
							.removeClass('bgheight')
							.removeClass('bgwidth')
							.addClass('bgwidth');
			}
			
			$('.nActiveBg').animate({opacity: 0.0}, 10000);
			$('.activeBg').animate({opacity: opacity}, 10000, bgSlideshow);
		});
	}
	
	
	// BEHAVIOUR OF THE SITE, NAVIGATION ETC.
	
	$('.lineup_list').click(function() {
		$(this).next().fadeToggle("fast");
		return false;
	}).next().hide();
	
	$('#bilder').click(function() {
		if(slide){
			slide = false;
		}else{
			slide = true;
			slideshow(counter);
		}
	});

	$('#slideA').resize(function() {
  var child_height = $(this).css('height');
  $('#slideshow').css('height', child_height);
});

	$('#slideshow').hover(function() {
		idle=false;
		}, function(){
		idle= true;
	});
	
	
	$('#slideshow').click(function() {
		manual = true;
		count();
	});
	
	///////////////////////////////////////////////////////
});



// CHANGE BACKGROUND-IMAGE
	$(window).load(function(){
		
		// ARRAY STORING THE CACHED IMAGES 
	
	
	
	

	
	
   // PRELOAD GALLERY IMAGES AND BACKGROUND-IMAGES
$.preLoadImages(images);
$.preLoadImages(bgImages);

	
	
		
		
		
		
	
	 var theWindow        = $(window),
            $bgA             = $("#bgA"),
			$bgB			 = $("#bgB"),
            aspectRatioA      = $bgA.width() / $bgA.height();
			aspectRatioB      = $bgB.width() / $bgB.height();
			
	function setAspectRatios(){
       		aspectRatioA      = $bgA.width() / $bgA.height();
			aspectRatioB      = $bgB.width() / $bgB.height();
	}
	
        function resizeBg() {
	
                if ( (theWindow.width() / theWindow.height()) < aspectRatioA ) {
                    $bgA
                        .removeClass('bgwidth')
						.removeClass('bgheight')
                        .addClass('bgheight');
                } else {
                    $bgA
                        .removeClass('bgheight')
						.removeClass('bgwidth')
                        .addClass('bgwidth');
                }
				
				if ( (theWindow.width() / theWindow.height()) < aspectRatioB ) {
                    $bgB
                        .removeClass('bgwidth')
						.removeClass('bgheight')
                        .addClass('bgheight');
                } else {
                    $bgB
                        .removeClass('bgheight')
						.removeClass('bgwidth')
                        .addClass('bgwidth');
                }
        }

        theWindow.resize(function() {
				setAspectRatios();
                resizeBg();
        }).trigger("resize");
		
		
		
	
	
		
		
}); // $(window).load //

var images = ["bilder_hoehe_600/IMG_8592.jpg","bilder_hoehe_600/IMG_8595.jpg","bilder_hoehe_600/IMG_8596.jpg","bilder_hoehe_600/IMG_8601.jpg","bilder_hoehe_600/IMG_8602.jpg","bilder_hoehe_600/IMG_8603.jpg","bilder_hoehe_600/IMG_8621.jpg","bilder_hoehe_600/IMG_8622.jpg","bilder_hoehe_600/IMG_8625.jpg","bilder_hoehe_600/IMG_8628.jpg","bilder_hoehe_600/IMG_8629.jpg","bilder_hoehe_600/IMG_8630.jpg","bilder_hoehe_600/IMG_8631.jpg","bilder_hoehe_600/IMG_8633.jpg","bilder_hoehe_600/IMG_8634.jpg","bilder_hoehe_600/IMG_8635.jpg","bilder_hoehe_600/IMG_8636.jpg","bilder_hoehe_600/IMG_8638.jpg","bilder_hoehe_600/IMG_8639.jpg","bilder_hoehe_600/IMG_8641.jpg","bilder_hoehe_600/IMG_8644.jpg","bilder_hoehe_600/IMG_8645.jpg","bilder_hoehe_600/IMG_8648.jpg","bilder_hoehe_600/IMG_8649.jpg","bilder_hoehe_600/IMG_8650.jpg","bilder_hoehe_600/IMG_8651.jpg","bilder_hoehe_600/IMG_8652.jpg","bilder_hoehe_600/IMG_8653.jpg","bilder_hoehe_600/IMG_8654.jpg","bilder_hoehe_600/IMG_8655.jpg","bilder_hoehe_600/IMG_8656.jpg","bilder_hoehe_600/IMG_8657.jpg","bilder_hoehe_600/IMG_8659.jpg","bilder_hoehe_600/IMG_8660.jpg","bilder_hoehe_600/IMG_8662.jpg","bilder_hoehe_600/IMG_8663.jpg","bilder_hoehe_600/IMG_8665.jpg","bilder_hoehe_600/IMG_8667.jpg","bilder_hoehe_600/IMG_8668.jpg","bilder_hoehe_600/IMG_8669.jpg","bilder_hoehe_600/IMG_8670.jpg","bilder_hoehe_600/IMG_8671.jpg","bilder_hoehe_600/IMG_8673.jpg","bilder_hoehe_600/IMG_8674.jpg","bilder_hoehe_600/IMG_8676.jpg","bilder_hoehe_600/IMG_8677.jpg","bilder_hoehe_600/IMG_8679.jpg","bilder_hoehe_600/IMG_8680.jpg","bilder_hoehe_600/IMG_8681.jpg","bilder_hoehe_600/KATZ-2010+100.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-05.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-06.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-07.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-08.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-09.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-10.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-11.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-12.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-17.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-19.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-21.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-22.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-23.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-24.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-25.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-26.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-27.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-28.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-29.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-30.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-31.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-32.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-42.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-43.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-44.jpg","bilder_hoehe_600/KATZ-Party-2010-by-Alex-Linz-47.jpg"];

	// BACKGROUND-IMAGES
	
	var bgImages = ["bg001.png", "bg002.png", "bg003.png", "bg004.png", "bg005.png", "bg006.png"];


var cache = [];

// FUNCTION FOR PRELOADING IMAGES
$.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }

