$(document).ready(function(){
	MAS.init();
	/*	   
	$(document).ready(function(){
		$('ul#navigation li').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); 
		});
	});*/
});

MAS = {
  init: function(){
		MAS.Navigation.init();
		MAS.Gallery.init();
		MAS.Stream.init();
		MAS.Carousel.init();
	}
};

MAS.Stream = {
	init: function(){
		$('#sidebar ul.stream_element').innerfade({ timeout:6000, speed:1000 } ); // see http://medienfreunde.com/lab/innerfade/
	}
};


MAS.Navigation = {
	init: function(){
		this.bindEvents();
	},
	
	bindEvents: function(){

		$('.navigation_element').mouseover(function(){			
			$(this).parent().find('.subnavigation').hide();
			$(this).find('.subnavigation').show();
			$(this).find('.subnavigation_children').show();
		});
		
		var hiConfig = {    
		     sensitivity: 7,
		     interval: 100,
		     over: function(){
								 $(this).show();
							 },
		     timeout: 700,
		     out: function(){
							  $(this).hide();											
				 			}
		};
		$('.subnavigation').hoverIntent(hiConfig);
	}
	
};

MAS.Carousel = {
	init: function(){
		$('ul.carousel').jcarousel({
			scroll: 1,
			auto: 5,
			wrap: 'both',
			initCallback: this.buttons    
		});	
	},
	
	buttons: function(carousel) {
		jQuery('.jcarousel-control li').bind('click', function() {
      carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
      return false;
    });
	}
	
	
};

MAS.Gallery = {
	
	galleryImages: null,
  currentImageIndex: null,
	
	init: function(){
		galleryImages = [];
		currentImageIndex = 0;
		$('#thumbnails li a').each(function(){
			galleryImages.push($(this).attr('href'));
		});

		this.bindEvents();
		this.setBackgroundCursor();	
		this.preloadImages();
	},
	
	bindEvents: function(){
		var down = 20;
		var hiConfig = {    
		     sensitivity: 7,
		     interval: 100,
		     over: function(){  		
  							  var container = $(this)
									container.children().show();					 
									if(container.height() == 20){ // skip if it's already up
										var up = container.css({ height:'auto' }).height();									
									  container.css({ height:down+'px' });
									  container.animate({height:up}, { queue:false });	
									}								
							  },
		     timeout: 300,
		     out: function(){
									var container = $(this);
								  container.animate({ height:down+"px" }, { queue:false, duration:500 });
								  setTimeout("$(this).children().hide()", 500)
							 }
		};		
		$('#thumbnails_wrapper').hoverIntent(hiConfig);

		$('#thumbnails li a').click(function(){
			var url = $(this).attr('href');
			MAS.Gallery.setBackground(url);
			MAS.Gallery.setCurrentImageIndexByUrl(url);
			return false;
		});
		
		$('#background_left').click(function(){
			if(MAS.Gallery.checkPreviousImageAvailable()){
				currentImageIndex--;
				MAS.Gallery.setBackground(galleryImages[currentImageIndex]);
				MAS.Gallery.setBackgroundCursor();
			}
		});
		$('#background_right').click(function(){
			if(MAS.Gallery.checkNextImageAvailable()){
				currentImageIndex++;
				MAS.Gallery.setBackground(galleryImages[currentImageIndex]);
				MAS.Gallery.setBackgroundCursor();
		  }
		});
	},
	
	/* helper */
	setBackgroundCursor: function(){
		if(MAS.Gallery.checkNextImageAvailable()){
			$('#background_right').addClass('available');
		}else{
			$('#background_right').removeClass('available');
		}

		if(MAS.Gallery.checkPreviousImageAvailable()){
			$('#background_left').addClass('available');
		}	else{
			$('#background_left').removeClass('available');
		}
	},
	checkNextImageAvailable: function(){
		if(currentImageIndex < galleryImages.length-1){
			return true;
		} else {
			return false;
		}
	},
	checkPreviousImageAvailable: function(){
		if(currentImageIndex > 0){ 
			return true;
		} else {
			return false;
		}
	},
	setCurrentImageIndexByUrl: function(url){
		for(i=0; i < galleryImages.length; i++){
			if(galleryImages[i] == url){
				currentImageIndex = i;
				MAS.Gallery.setBackgroundCursor();
			}
		}
	},
	
	preloadImages: function(){
		var images = [];
		$('#thumbnails li a').each(function(){
			images.push($(this).attr('href'));
		});
		$.preloadImages(images);
	},
	
	setBackground: function(file){		
		$('#background img').attr('src', file);
	}

	
};

jQuery.preloadImages = function(images){
  for(var i = 0; i < images.length; i++){
    jQuery("<img>").attr("src", images[i]);
  }
}