// jquery initialize


// delay plugin
jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};


$(function(){		   


	// lavalamp navig
	$(document).delay(1500, function() { // allow extra time to render css fontface
				
				
		// default lavalamp
		$('#wrapper ul.mainNav').lavaLamp({ 
			fx: "backout",
			speed: 700,
			click: function(event, menuItem){
            } 
		});
				
		$('#wrapper ul.mainNav .back').fadeIn('slow');


		// start page lavalamp
		$('#startWrapper ul.mainNav').lavaLampStart({ 
			fx: "backout",
			speed: 700,
			click: function(event, menuItem){
            } 
		});	
			
		$('#startWrapper .back').css('display', 'none');
				
		$('#startWrapper ul.mainNav').hover(
		function () {
			$('#startWrapper ul.mainNav .back').css('visibility', 'visible').fadeIn('fast');
		}, 
		function () {
			$('#startWrapper ul.mainNav .back').fadeOut('fast', 
				function() { $(this).css('visibility', 'hidden'); 
			});
		});
		
	});
	
	
	// flash audio
	$(document).delay(4000, function() {
		$('#audio').fadeIn(1000);
	});


    // jcarousel & galleria
    $('#galleria li').each(function(idx) {
        $(this).data('index', (++idx));
    });

    $('.jcarousel').jcarousel({
        scroll: 7,
        visible: 7,
        initCallback: initCallbackFunction
    })
	
    // hotkeys plugin: use arrows to control the gallery - left/right for both galleries
    $(document).bind('keydown', 'right', function (evt){ $.galleria.next(); });
    $(document).bind('keydown', 'left', function (evt){ $.galleria.prev(); });
    
    function initCallbackFunction(carousel) {
        $('#main').bind('image-loaded',function() {
            var idx =  $('.jcarousel li.active').data('index') - 2;
            
            carousel.scroll(idx);
            return false;
        });
        
        // hotkeys plugin: use arrows to control the gallery - up/down for jcarousel gallery
        $(document).bind('keydown', 'up', function (evt){ $('.jcarousel-next-horizontal').click(); return false; });
        $(document).bind('keydown', 'down', function (evt){ $('.jcarousel-prev-horizontal').click(); return false; });
    };

    // load and fade-in thumbnails
    $('.jcarousel li img').css('opacity', 0).each(function() {    
        if (this.complete || this.readyState == 'complete') { $(this).animate({'opacity': 1}, 300) } 
        else { $(this).load(function() { $(this).animate({'opacity': 1}, 300) }); }
    });


	// slideshow timer
	var slideshow = $("ul#slideShow");
	
	var active = false;
	
	function start_slideshow() {
		slideshow.everyTime('5s', 'slideshow', function() {
			$.galleria.next()
		})
	}
	
	function start_slideshow_lightbox() {
		$('#btnPrev, #btnNext, li.start').css('display', 'none');
		$('li.stop').css('display', 'list-item');
		slideshow.everyTime('5s', 'slideshow', function() {
			$.galleria.next()
		})
	}

	start_slideshow()
	

    $('#galleria').galleria({
							
        // #main is the empty div which holds the images
        insert: '#main',
        
        // disable history plugin
        history: false,
		
        // disable clickNext plugin
        clickNext : false, 
        
        // function fired when the image is displayed
        onImage: function(image, caption, thumb) { 
		 
            // allow time to fade old image out and fade new image in
            image.hide().animate({'opacity': 1}, 400).fadeIn(1000);
            
            // animate active thumbnail's opacity to 1, other list elements to 0.5
            thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.5)
			
            // add a title for the clickable image
            image.attr('title', 'Bitte klicken für grosse Ansicht');
			
            // check if thumbnail has a full size version of the pic, on click show it in a lightbox and stop slideshow
            var large = thumb.attr('longdesc');
						
            if(large) {
            image.wrap('<a href="' + large + '"></a>');
			
            // start slideshow when lightbox is closing
            image.parent('a').slimbox({ onstop: function () { start_slideshow_lightbox() } }); 
			image.parent('a').click(function () { slideshow.stopTime('slideshow'); });
			}
            
            // $('#img').data('currentIndex', $li.data('index')).trigger('image-loaded')
            $('#main').trigger('image-loaded')
        },
        
        // function similar to onImage, but fired when thumbnail is displayed
        onThumb: function(thumb) {
            var $li = thumb.parent(),
                opacity = $li.is('.active') ? 1 : 0.5;
            
            // hover effects for list elements
            $li.hover(
                function() { $li.fadeTo(200, 1); },
                function() { $li.not('.active').fadeTo(200, opacity); }
            )
        } 
		
    }).find('li:first').addClass('active') // display first image when Galleria is loaded
	
	// galleria buttons
	$(this).find('li.stop').click(function() {
		$('#btnPrev, #btnNext').fadeIn('fast');
		slideshow.stopTime('slideshow');
	});
	
	$(slideshow).find('li.start').click(function() {
		$('#btnPrev, #btnNext').fadeOut('fast');
		active = !active;
		$.galleria.next();
		start_slideshow()
	});
	
	
	// stop slideshow if large image view is active
	$(slideshow).find('img.replaced').click(function() {
		slideshow.stopTime('slideshow');
	});
		

	// slideshow buttons
	$('#slideShow li').click(function () {
		$('#slideShow li').toggle();
	});	

	
	// thumbs drawer / buttons
	$('.openDrawer').click(function(){
		$('img.thumb').css('height','85px');
		$('div#drawer').slideDown('fast');
	});
	
	$('.closeDrawer').click(function(){
		$('div#drawer').slideUp('fast');	
	});
	
	$('#thumbsDrawer li').click(function () {
		$('#thumbsDrawer li').toggle();
	});	
	
});