slideshowAnim1 = false;
slideshowAnim2 = false;

// init document

	$(document).ready(function() {
		
		// history plugin.
			initHistory();
			
		// image-carousel
			//if ($('#slideshow').length > 0) initSlideshow(2);
			
		$('#overlayBG').hide();
	});

// JS History Plugin

	function initHistory() {

		$.history.init(pageload);

		// set onlick event for buttons
		$("a[rel='history']").click(function(){
			// 
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			// moves to a new page. 
			// pageload is called at once. 
			$.history.load(hash);
			return false;
		});
	}

	function pageload(hash) {
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
			//$("#load").load(hash + ".html");
			$("#load").html(hash + ".html");
		} else {
			// start page
			$("#load").empty();
		}
	}

// Zoom Images

	function open_function () {
		return false;
	}
	
	// create custom animation algorithm for jQuery called "drop" 
	$.easing.drop = function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	};
		
	// loading animation
        //$.tools.overlay({ 
        //    onBeforeLoad:  function() {this.getOverlay().appendTo('body');}
        //});

	$.tools.overlay.addEffect("drop", function(css, done) { 
	   
	   // use Overlay API to gain access to crucial elements
	   var conf = this.getConf(),
		   overlay = this.getOverlay();           
	   
	   // determine initial position for the overlay
	   if (conf.fixed)  {
		  css.position = 'fixed';
	   } else {
		  css.top += $(window).scrollTop();
		  css.left += $(window).scrollLeft();
		  css.position = 'absolute';
	   } 
	   
		$('#overlayBG').css({width: $(document).width(), height: $(document).height(), opacity: '0.5', backgroundColor: '#000'});


	   
	   // position the overlay and show it
                //this.getOverlay().appendTo('body');

		$('#overlayBG').fadeIn('fast', function () {
			//overlay.css(css).show();
			overlay.css(css).fadeIn('fast');
		});

	   
	   
	   // begin animating with our custom easing
	   //overlay.animate({ /*top: '+=55',*/  opacity: 1}, 200, 'drop', done);
	   
	   /* closing animation */
	   }, function(done) {
		  /*this.getOverlay().animate({top:'-=55', opacity:0 }, 200, 'drop', function() {
			 $(this).hide();
			 done.call();      
		  });*/
		  
		  this.getOverlay().fadeOut('fast', function () {
			$('#overlayBG').fadeOut('fast');
			done.call();     
		  });
	   }
	);

// Image-Galery

	function initGalery () {	
	
		$("#image_galeryHolder img[rel]").overlay({
			effect: 'drop',
			mask: '#789'
		});
		
		$("#image_galeryNext").click(function() {
			
			$("#image_galeryHolder .image_paneAct").fadeOut('normal', function () {
				
				index = $(this).index()+1;

				if ( (index+1) > $("#image_galeryHolder .image_pane").size()) { 
					nextNr = 1;
				} else {
					nextNr = index+1;
				}
							
				$("#image_galeryHolder .image_paneAct").removeClass('image_paneAct');
				
				$("#image_galeryHolder .image_pane:nth-child("+nextNr+")").addClass('image_paneAct');
				
				$("#image_galeryHolder .image_paneAct").fadeIn('normal');
			});
			
		});		
		
		$("#image_galeryPrev").click(function() {
			
			$("#image_galeryHolder .image_paneAct").fadeOut('normal', function () {
				
				index = $(this).index()+1;
				
				if ( (index-1) < 1 ) { 
					nextNr = $("#image_galeryHolder .image_pane").size();
				} else {
					nextNr = index-1;
				}
				
				$("#image_galeryHolder .image_paneAct").removeClass('image_paneAct');
				
				$("#image_galeryHolder .image_pane:nth-child("+nextNr+")").addClass('image_paneAct');
				$("#image_galeryHolder .image_paneAct").fadeIn('normal');
			});
			
		});
	}
	
// Image-Carousel

	function initSlideshow (exclude_count) {
                
                //$('#iCarZoom .simple_overlay img').each( resizeImage($(this), 'fitToScreen', 0) );

		//$("#iCarZoom span[rel]").colorbox();
                $("a.zoomImage").colorbox({rel:'nofollow'}); 
			
		// create big images in DOM
			$('#slideshow ul li a').each(function(index) {

                               

				if (index > 0) {

				    //newImage = $(this).attr('href');
				    //$('#iCarZoom div:nth-child('+(index+1)+')').html( '<img src="'+newImage+'" alt="" />' );	
                                }

				$(this).bind('click', function(event) {  
					
					//$('#iCarZoom div.currZoom').addClass('loading');

					if (slideshowAnim1==false && slideshowAnim2==false) {
						slideshowAnim1==true;
						slideshowAnim2==true;
					
						event.preventDefault();
						targetImage = $(this).attr('href');
						targetContainer = $('img[src="' + targetImage + '"]').parent();

						$('#iCarZoom div.currZoom').fadeOut(2000, function () { 
							slideshowAnim1=false;	
						});				

						targetContainer.fadeIn(2000, function () {
							slideshowAnim2=false;							
						});
						
						$('#iCarZoom > div').removeClass('currZoom');
						targetContainer.addClass('currZoom');

						// set info message
						
							emptyInfo = $(this).next('.iCarMess').hasClass('iCarNoMess');
							info = $(this).next('.iCarMess').html(); 

							if ($('#iCarMess').css('display') == 'block') $('#iCarMess').fadeOut('normal');		
							
							if (!emptyInfo) {  
								$('#iCarMess').html(info);
								$('#iCarMess').fadeIn();
							} 	
					}
				});
			});
		
		$('#slideshow ul').css('paddingLeft','251px');
		
		$('#slideshow').serialScroll({
			items:'li',
			prev:'#iCarNav a.prev',
			next:'#iCarNav a.next',
			duration:1200,
            //offset:-251,
			lock:false,
			cycle:false,
			easing:'easeOutQuart',
            stop:true,
			//force:true,
			exclude:exclude_count,
			constant: false,
			jump: false,
			onAfter: function (targObj) { }
		});
		
		
		if($.browser.msie && $.browser.version.substring(0,navigator.userAgent.indexOf('.')) == '6'){
			  $('#slideshow ul').animate({
				paddingLeft: 0
			  }, 1000, function() {
				// Animation complete.
			  });
		} else {
			aktiv = window.setInterval("checkPageLoad()", 1000);
		}


			

	}
		var aktiv = 0;
		var intervalTimes = 0;
		
		function checkPageLoad () {

                  /* hängt sich sonst in der no_script-Page auf */
                  if ( ajaxComplete == null || ajaxComplete == 'undefined' ) {
                  
                      var ajaxComplete = 1;
                  }                  

		  intervalTimes++;
		  if (ajaxComplete == 1 || intervalTimes > 30)
			window.clearInterval(aktiv);
			//$('#slideshow').trigger( 'next' );
			  $('#slideshow ul').animate({
				paddingLeft: 0
			  }, 1000, function() {
				// Animation complete.
			  });

		}


/*
$(function() {
	var zIndexNumber = $('.section.selected').css('zIndex'); alert('selected index: ' + $('.section.selected').css('zIndex'));
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
});*/
