Array.prototype.shuffle = function() {

 	var len = this.length,
	i = len;

	while (i--) {

		var p = parseInt(Math.random() * len),
		t = this[i];

		this[i] = this[p];
		this[p] = t;

 	}

};

(function($) {
	
	var Content = {

		init: function() {
			
			var self = this;

			return self;

		},

		display: function(selector) {
			
			var self = this;
				
			if (!$(selector).hasClass('current')) {

				$('#content > .current').css('opacity', 1).removeClass('current').animate({
					opacity: 0
				}, 400);		

				var $current = $(selector);

				$current.css('opacity', 0).addClass('current').animate({
					opacity: 1
				}, 400);

				var $jscrollpane = $('.jscrollpane', $current);

				if ($jscrollpane.length != 0) {
				
					if (typeof $jscrollpane.data('jsp') == 'undefined') {

						$jscrollpane.jScrollPane();

					}
					
				}

			}

			return self;

		}

	};

	var Galleries = {
		
		$menu_galleries: null,
		$content_collection: null,
		$content_collection_slider: null,

		$nav_collection_slider: null,

		$xhr: null,

		Content: null,

		galleries: [],
		current_gallerie: null,
		current_uri_images: null,

		init: function(content) {
			
			var self = this;

			self.$menu_galleries = $('#menu-bottom .last ul');
			self.$content_collection = $('#galleries .col1');
			self.$content_collection_slider = $('#galleries .col2 figure');

			if (self.$content_collection.length == 0) return false;

			self.$nav_collection_slider = $('#galleries .col2 nav a');

			self.Content = content;

			self._getGalleries(function() {
				
				self._setGalleries();

			});

			return self;

		},

		_getGalleries: function(callback) {
			
			var self = this,
			callback = callback || $.noop;

			if (self.$xhr != null) self.$xhr.abort();

			self.$xhr = $.ajax({
				url: 'galleries.xml',
				dataType: 'xml',
				success: function(response) {

					$('gallerie', response).each(function(i) {

						var collections = [];

						$('collection', this).each(function(i) {

							var pieces = [];

							$('piece', this).each(function(i) {

								var images = [];

								$('image', this).each(function(i) {

									var src = [];

									$('src', this).each(function(i) {

										src[$(this).attr('size')] = $(this).text();

									});

									images.push({
										display: $(this).attr('display') || 'no',
										legend: $('> legend', this).text(),
										src: src
									});

								});

								pieces.push({
									name: $('> name', this).text(),
									images: images
								});

							});

							collections.push({
								name: $('> name', this).text(),
								urlkey: $('> urlkey', this).text(),
								pieces: pieces
							});

						});

						self.galleries.push({
							name: $('> name', this).text(),
							urlkey: $('> urlkey', this).text(),
							collections: collections
						});

					});

					self.$xhr = null;

					callback();

				}
			});

			return self;

		},

		_setGalleries: function() {
			
			var self = this;

			$(self.galleries).each(function() {
				
				var $a = $('<a />').attr('href', '#' + this.urlkey).text(this.name).data('gallerie', this),
				$li = $('<li />').append($a).insertBefore($('.last', self.$menu_galleries).prev());

				$a.on('click', function(e) {
					
					e.preventDefault();

					var $this = $(this),
					$parent = $this.parent();

					if ($parent.hasClass('current')) return false;

					self.$content_collection_slider.parent().hide();

					$('#menu-bottom li').removeClass('current');
					$parent.addClass('current');
					$('#menu-bottom li a').trigger('mouseleave');

					self.current_gallerie = $this.data('gallerie');
					self.current_uri_images = self.current_gallerie.urlkey + '/';

					self._setCollection();

					self.Content.display('#galleries');

				});

			});

			self.$nav_collection_slider.on('click', function(e) {
				
				e.preventDefault();

				var $this = $(this),
				$img_current = $('.current', self.$content_collection),
				$img_next = $img_current.next(),
				$img_prev = $img_current.prev(),
				$section = $img_current.parent().parent();

				if ($this.hasClass('next')) {
					
					if ($img_next.length != 0) {
						$('a', $img_next).trigger('click');
					} else {
						
						if ($section.next().length != 0) {
							$('a:first', $section.next()).trigger('click');
						} else {
							$('section:first a:first', self.$content_collection).trigger('click');
						}

					}

				}

				if ($this.hasClass('prev')) {
					
					if ($img_prev.length != 0) {
						$('a', $img_prev).trigger('click');
					} else {
						
						if ($section.prev().length != 0) {
							$('a:last', $section.prev()).trigger('click');
						} else {
							$('section:last a:last', self.$content_collection).trigger('click');
						}

					}

				}

			});

			return self;

		},

		_setCollection: function() {
			
			var self = this,
			items = [];

			self.$content_collection.empty();
			self.$content_collection_slider.empty();

			$(self.current_gallerie.collections).each(function() {

				var current_uri_images = self.current_uri_images + this.urlkey + '/';
				$h2 = $('<h2 />').text(this.name),
				$ul = $('<ul />'),
				$section = $('<section />').append($h2).append($ul).appendTo(self.$content_collection);

				$(this.pieces).each(function() { 
					
					var piece = this;

					$(piece.images).each(function() {

						if (this.display == 'yes') {

							var $img = $('<img />').attr('src', 'images/galleries/' + current_uri_images + '25x25/' + this.src['25x25']),
							$a = $('<a />').attr('href', '#1').append($img).data('image', this),
							$li = $('<li />').css('opacity', 0).append($a).appendTo($ul);

							if (typeof piece.open == 'undefined') {
								items.push($li);
							} else {
								$li.addClass('open').removeAttr('style');
							}

							$a.on('click', function(e) {

								e.preventDefault();

								if ($li.hasClass('current')) return false;

								if (self.$content_collection_slider.parent().is(':hidden')) {
									self.$content_collection_slider.empty().parent().show();
								}

								var $slider_img = $('img', self.$content_collection_slider),
								$slider_aside = $('aside', self.$content_collection_slider);

								if ($slider_img.length != 0) {

									$slider_img.stop().animate({
										opacity: 0
									}, 400, function() {
										
										$(this).remove();

									});

									$slider_aside.stop().animate({
										opacity: 0
									}, 400, function() {
										
										$(this).remove();

									});

								}

								var image = $(this).data('image'),
								$img = $('<img />').attr('src', 'images/galleries/' + current_uri_images + '510x380/' + image.src['510x380']).css('opacity', 0).appendTo(self.$content_collection_slider),
								$aside = $('<aside />').text(image.legend).css('opacity', 0).appendTo(self.$content_collection_slider);

								$img.stop().animate({
									opacity: 1
								}, 400);
								$aside.stop().animate({
									opacity: 1
								}, 400);

								$('.current', self.$content_collection).removeClass('current');

								if (!$li.hasClass('open')) {

									$li.addClass('open').find('img').css('opacity', 0).stop().animate({
										opacity: 1
									}, 400);

									piece.open = 'yes';

								}

								$li.addClass('current');

								$img.on('click', function() {
									
									var window_dimensions = {
										height: $(document).height(),
										width: $(document).width()
									},
									popin_position = {
										left: (window_dimensions.width / 2) - (930 / 2),
										top: (window_dimensions.height / 2) - (615 / 2)
									},
									$section = $('<section />'),
									$next = $('<a />').attr('href', '#1').addClass('next'),
									$prev = $('<a />').attr('href', '#1').addClass('prev'),
									$close = $('<a />').attr('href', '#1').addClass('close'),
									$span = $('<span />'),
									$nav = $('<nav />').append($next).append($prev).append($close).append($span),
									$lightbox = $('<div />').addClass('lightbox').append($nav).append($section).css({
										left: popin_position.left,
										position: 'fixed',
										top: popin_position.top
									}),
									$overlay = $('<div  />').addClass('overlay').css({
										height: window_dimensions.height,
										left: 0,
										opacity: 0,
										position: 'fixed',
										top: 0,
										width: window_dimensions.width
									}).append($lightbox).appendTo('body').stop().animate({
										opacity: 1
									}, 400);

									$(piece.images).each(function() {

										var $img = $('<img />').attr('src', 'images/galleries/' + current_uri_images + '825x615/' + this.src['825x615']).css({
											opacity: 0
										}).appendTo($section);

									});

									$('img:first', $section).css('opacity', 1).addClass('current');

									$span.text('1 / ' + piece.images.length);

									$prev.on('click', function(e) {
										
										e.preventDefault();

										var $current = $('.current', $section),
										$current_next = $current.next();

										if ($current_next.length != 0) {
											
											$current.stop().animate({
												opacity: 0
											}, 400).removeClass('current');

											$current_next.stop().animate({
												opacity: 1
											}, 400).addClass('current');

											$span.text(($current_next.index() + 1) + ' / ' + piece.images.length);

										} else {
											
											$current.stop().animate({
												opacity: 0
											}, 400).removeClass('current');

											$(':first', $section).stop().animate({
												opacity: 1
											}, 400).addClass('current');

											$span.text('1 / ' + piece.images.length);

										}

									});

									$next.on('click', function(e) {
										
										e.preventDefault();

										var $current = $('.current', $section),
										$current_prev = $current.prev();

										if ($current_prev.length != 0) {
											
											$current.stop().animate({
												opacity: 0
											}, 400).removeClass('current');

											$current_prev.stop().animate({
												opacity: 1
											}, 400).addClass('current');

											$span.text(($current_prev.index() + 1) + ' / ' + piece.images.length);

										} else {
											
											$current.stop().animate({
												opacity: 0
											}, 400).removeClass('current');

											$(':last', $section).stop().animate({
												opacity: 1
											}, 400).addClass('current');

											$span.text(piece.images.length + ' / ' + piece.images.length);

										}

									});

									$close.on('click', function(e) {
										
										e.preventDefault();

										$overlay.stop().animate({
											opacity: 0
										}, 400, function() {
											
											$(this).remove();

										})

									});

								});

							});

						}

					});

				});

			});

			items.shuffle();

			animate();

			function animate(i) {
				
				var i = i || 0;

				if (i >= items.length) return false;

				$(items[i]).stop().animate({
					opacity: 1
				}, 400);

				i++;

				setTimeout(function() {
					
					animate(i);

				}, 50);

			}

			return self;

		}

	};

	$(function() {

		$('#home h1, #home h2, #home a').css('opacity', 0);

		setTimeout(function() {

			$('#home h1').animate({
				opacity: 1
			}, 800, function() {
				
				$('#home h2').animate({
					opacity: 1
				}, 800, function() {
					
					$('#home a').on('click', function(e) {

						e.preventDefault();

					}).animate({
						opacity: 1
					}, 800, function() {

						$('#home').css('cursor', 'pointer').on('click', function() {
							
							$(this).stop().animate({
								opacity: 0
							}, 400, function() {

								$(this).remove();

							});

							var $menu_bottom_items = $('#menu-bottom li').css({
								opacity: 0,
								visibility: 'visible'
							});

							setTimeout(animate, 400);

							function animate(i) {
								
								var i = i || 0;

								if (i >= $menu_bottom_items.length) return false;

								$menu_bottom_items.eq(i).stop().animate({
									opacity: 1
								}, 400);

								i++;

								setTimeout(function() {
									
									animate(i);

								}, 400);

							}

						});

					});

					$('#drapes div.first').animate({
						marginLeft: -428
					}, 800, function() {
						
						$('#drapes').remove();

					});
					$('#drapes div.last').animate({
						marginLeft: 856
					}, 800);

				});

			});

		}, 500);

		var content = Content.init();

		Galleries.init(Content);

		$('a[rel="external"]').on('click', function() {
			
			$(this).attr('target', '_blank');

		});

		$('a[href="#about"], a[href="#press"], a[href="#contact"]').on('click', function(e) {
			
			e.preventDefault();

			var $this = $(this),
			$parent = $this.parent();

			if ($parent.hasClass('current')) return false;

			$('#menu-bottom li').removeClass('current');
			$parent.addClass('current');
			$('#menu-bottom li a').trigger('mouseleave');

			content.display($this.attr('href'));

		});

		$('#about aside a').on('click', function(e) {
			
			e.preventDefault();

			var $this = $(this),
			$parent = $this.parent().parent();
			$parent_parent = $parent.parent();

			$parent_parent.find('.current').css('opacity', 1).removeClass('current').animate({
				opacity: 0
			}, 400);

			if ($parent.hasClass('fr')) {

				$parent_parent.find('.en').css('opacity', 0).addClass('current').stop().animate({
					opacity: 1
				}, 400);

			} else {
				
				$parent_parent.find('.fr').css('opacity', 0).addClass('current').stop().animate({
					opacity: 1
				}, 400);

			}

			$('.jscrollpane', $parent.parent()).jScrollPane();

		});

		$('#menu-bottom').on('mouseenter', 'a', function() {
			
			$(this).stop().animate({
				color: '#fff'
			}, 'fast');

		}).on('mouseleave', 'a', function() {
			
			var $this = $(this),
			$parent = $this.parent();

			if (!$parent.hasClass('current')) {

				$(this).stop().animate({
					color: '#848484'
				}, 'fast');

			}

		});

	});

})(jQuery);
