/**
 * Menu
 */
var menu = function(){

	var menu_tag = '#menu';

	return {
		init: function() {

			/**
			 * Menu
			 */
			$('li', $(menu_tag)).hover(function(){
				$(this).find('ul').show();
			}, function(){
				$(this).find('ul').hide();
			});
		}
	}
}();

/**
* Top gallerij
*/

var headerGallery = function(){

	var images			= new Array();
	var current   = {
		index	: 0,
		id		: null
	};

	var imageId = '#GalleryImage';

	var self			= null;
	var obj				= null;
	var speed			= 4000;
	var cycleSpeed		= 5000;

	var	interval		= null;

	return {
		// Init
		init: function(){
			self = this;
			obj	= $('#header-gallery');

			// Voeg huidig plaatje toe
			current.id = '#' + $('#header-gallery img').eq(0).attr('id');
		},
		first: function(){
			$(current.id).attr('src', images[0]);
		},

		/**
		 * Zet het plaatjen naar de volgende
		 */
		nextImage: function(){
			self.setImage(self.nextIndex());
		},

		/**
		 * Zet een nieuw plaatje met een nieuwe animatie
		 */
		setImage: function(img) {
			obj.append('<img id="' + img.id.replace('#','') + '">');
			$(img.id).css({
				opacity: 0
			})
			.attr('src', images[img.index])
			.animate({
				opacity: 1
			}, speed, function(){
				$(current.id).remove();
				current = img;
			});
			
		},

		/**
		 * Roteer
		 */
		cycle: function(){
			self.nextImage();
		},

		/**
		 * Verkrijg de lengte van de gallerij
		 */
		galleryLength: function() {
			return images.length - 1;
		},

		/**
		 * Verkrijg de nieuwe index
		 */
		nextIndex: function() {
			var nextIndex = current.index >= images.length-1 ? 0 : current.index + 1
			return {
				index	: nextIndex,
				id		: imageId + nextIndex
			};
		},

		/**
		 * Voeg een plaatje toe aan de gallerij
		 */
		addImage: function(url){
			images.push(url);
		},

		/**
		 * Voeg een array toe aan de gallerij
		 */
		addImageArray: function(array){
			for(var i in array) {
				self.addImage(array[i]);
			}
		},

		/**
		 * Start het roteren van de gallerij
		 */
		startCycle: function() {
			interval = setInterval(function(){
				self.cycle();
			}, cycleSpeed);
		}
	}
}();

$(function(){
	menu.init();
	headerGallery.init();
	
});

