/*if (!window.console) var console = { log: function() { } };
*/
function fixateMenu(fixate) {
	//$('menu').removeClassName('fixate');
	//if (fixate) $('menu').addClassName("fixate");
	//return;
	
	var menuHeight = $('menu').getHeight();
	
	/*$("menuCloneDimensions").setStyle({
		height: menuHeight + 'px'
	});*/
	
	// als viewport kleiner is dan menuhoogte + 40 (margintop)
	// dan moet het menu niet fixed zijn
	var vp = document.viewport.getDimensions();
	if (vp.height < menuHeight + 40) {
		//$('menu').setStyle({ position: 'absolute' });
		$('menu').removeClassName("fixate");
	} else {
		$('menu').addClassName("fixate");
	}
}
Event.observe(window, 'load', fixateMenu);


document.observe("dom:loaded", function() {
	function jumpTo(element) {
		if (!element) return;
		var projectid = (/_([0-9]+)*$/.exec(element.id) || [null, null])[1],
			url = NSBase.options.webroot + 'project/' + projectid,
			categoryid = $('archiveMenu').down('.categories a.active').getAttribute('rel');
		
		if (categoryid && categoryid != 'all') {
			url += '/' + categoryid;			
		}
		window.location.href = url;
	}

	/*
	 * Previous | Next archief
	 */
	/*$$('.prevNextArchiveNavigation').invoke('observe', 'click', function(event) {
		var element = event.findElement('a');

		if (element) {
			event.stop();
			var active = $$('#archiveMenu .projects')[0].down('.active');
			var visible = $$('#archiveMenu .projects a').inject([], function(array, a) {
				if (a.visible())
					array.push(a);				
				return array;
			});
			
			// remove moreLink
			var moreLink = $('archiveMenu').down('.moreLink');
			if (visible && moreLink && visible.member(moreLink))
			  visible = visible.without(moreLink);
			
			if (!visible) return;
			
			var activeAmongVisible = visible.member(active);
			
			if (activeAmongVisible) {
				var index = visible.indexOf(active);
				if (element.hasClassName("next")) {
					if (index == visible.length-1)	jumpTo(visible[0]);
					else							jumpTo(visible[index+1]);
				}
				else {
					if (index == 0)	jumpTo(visible.last());
					else			jumpTo(visible[index-1]);
				}
			}
			else {
				if (element.hasClassName("next")) jumpTo(visible[0])
				else jumpTo(visible.last())
			}
		}
	});*/
	$$('.prevNextArchiveNavigation').invoke('observe', 'click', function(event) {
		var element = event.findElement('a');

		if (element) {
			event.stop();
			var active = $$('#archiveMenu .projects a.active')[0];
			var used = $$('#archiveMenu .projects a.used');
			
			var activeAmongUsed = used.member(active);
			
			if (activeAmongUsed) {
				var index = used.indexOf(active);
				if (element.hasClassName("next")) {
					if (index == used.length-1)	jumpTo(used[0]);
					else							jumpTo(used[index+1]);
				}
				else {
					if (index == 0)	jumpTo(used.last());
					else			jumpTo(used[index-1]);
				}
			}
			else {
				if (element.hasClassName("next")) jumpTo(used[0])
				else jumpTo(used.last())
			}
		}
	});
	
	$('toggleCategories').observe('click', function(event) {
		event.stop();
		var cats = $('archiveMenu').down('.categories');

		cats.toggle();
		if (cats.visible()) {
			this.addClassName('nomargin');
		}
		else { this.removeClassName("nomargin"); }
	});
	
	// observe
	var projects = $('archiveMenu').select('.projects a'),
		categories = $('archiveMenu').select('.categories a'),
		categoryProjects = [];
	
	categories.each(function(category) {
		var categoryid = category.getAttribute('rel');
		
		var elements = projects.inject([], function(array, project) {
			var rel = project.getAttribute('rel');
			
			if (rel && rel.split('.').member(categoryid)) {
				array.push(project);
			}
			return array;
		});
		
		categoryProjects.push({
			categoryid: categoryid,
			projects: elements
		});
	});
	
	$('archiveMenu').down('.categories').observe('click', function(event) {
		var element = event.findElement('a[rel]');
		
		if (element) {
			element.addClassName('active').siblings().invoke('removeClassName', 'active');
			
			var rel = element.getAttribute('rel');
			event.stop();
			
			setCategory(rel);
			
			if (rel == 'all') {
				$('archiveMenu').select('.projects a[rel]').invoke('show');
				$('toggleCategories').removeClassName('nomargin');
				$('archiveMenu').down('.categories').hide();
			}
			showActiveCategory();
		}
	});
	
	$('archiveMenu').down('.projects').observe('click', function(event) {
		var element = event.findElement('a');
		
		if (element) {
			event.stop();
			
			jumpTo(element);
		}
	});
	
	function removeMoreLink() {
		var moreWrapper = $('archiveMenu').down('.moreWrapper');
		if (moreWrapper) {
			//moreWrapper.down('.moreLink').remove();
			
			moreWrapper.select('a[rel]').each(function(element) {
				$('archiveMenu').down('.projects').insert(element);
			});
			moreWrapper.remove();
		}
	}
	
	function getProjectLinks() {
		return $$('#archiveMenu .projects a').inject([], function(array, a) {
			if (a.visible())
				array.push(a);				
			return array;
		});
	}
	
	// zorg voor een "More projects" link
	var maxProjectLinks = 18;
	function addMoreLink() {
		var visible = getVisibleLinks();
		var visibleElements = visible.splice(maxProjectLinks);
		
		// first restore
		//removeMoreLink();
		
		// als er een cookie geset is, nix doen
		// vind alle visible links
		
		/*var activeCategory = $('archiveMenu').down('.categories .active[rel]');
		if (activeCategory && Cookie.get('21bis_expandedMenu') == activeCategory.rel) {
		  // voeg alleen een less link toe als er meer
		  if (visible.length > maxLinks) { 
			addLessLink();
		  }
		  //return;
		}*/
		
		//if (!visible || visible.length <= maxLinks) return;
		
		
		//console.log(visibleElements);
		
		// als nu visible elements een element bevat met member active kappen we
		// vookom cookie gebruik op deze manier
		/*var containsActive = false;
		visibleElements.each(function(element) {
		  if (element.hasClassName('active')) containsActive = true;
		});
		if (containsActive) return; */
		
		
		// verberg alle visible items
		// alleen als de cookie niet geset is
		
		// voeg de wrapper toe
		var moreWrapper = new Element('div', { className: 'moreWrapper addMarginTop' });
		$('archiveMenu').down('.projects').insert(moreWrapper);
		
		// voeg more link toe
		var moreLink = new Element('a', { className: 'moreLink' }).update('More projects');
		moreLink.observe('click', showMoreLinks);
		moreWrapper.insert(moreLink);
		
		// voeg links aan de wrapper toe
		visibleElements.each(function(element) {
		  moreWrapper.insert(element);
		});
		
		
	}
	
	
	function addLessLink() {
	  if ($('archiveMenu').down('.lessLink')) return;
	  
	  // voeg less link toe
	  var lessWrapper = new Element('div', { className: 'lessWrapper addMarginTop' });
	  $('archiveMenu').down('.projects').insert(lessWrapper);
	
	  var lessLink = new Element('a', { className: 'lessLink' }).update('Less projects')
	  .observe('click', hideMoreLinks);
	  lessWrapper.insert(lessLink);
	}
	
	
	function setNavigation() {
		// als cookie geset is, expand
		if (Cookie.get('21bis_expandedMenu')) {
		  // alleen less link als er ...
		  $('archiveMenu').down('.lessWrapper').show();
		  //$('archiveMenu').down('.moreWrapper').removeClassName('addMarginTop').show();
		  $('archiveMenu').down('.moreWrapper').hide();
		}
		else {
		  $('archiveMenu').down('.moreWrapper').show();
		  $('archiveMenu').down('.lessWrapper').hide();
		}
		
		// als de actieve category in de morewrapper zit, show hem dan
		/*var activeCategory = $('archiveMenu').down('.moreWrapper .active[rel]');
		if (activeCategory) {
		  showMoreLinks();
		}*/
	}
	
	
	function getActiveCategory() {
	  return $('archiveMenu').down('.categories .active').getAttribute('rel');	
	}
	
	function setCategory(id) {
	  $('archiveMenu').select('.projects a').invoke('removeClassName', 'used');
	  if (id == 'all') {
		$('archiveMenu').select('.projects a').invoke('addClassName', 'used');
	  }
	  else {
		  var cat = categoryProjects.find(function(cp) {
				return cp.categoryid == id;
		  });
		var projects = cat.projects;
		
		if (cat.projects) {
		  cat.projects.invoke('addClassName', 'used');
		}
	  }
	}
	
	function showActiveCategory() {
	 //var id = getActiveCategory();

	  $('archiveMenu').select('.projects a').invoke('hide');
	  var used = $('archiveMenu').select('.projects .used');
	  used.invoke('hide');

	  var usedHidden = false;
	  if (!Cookie.get('21bis_expandedMenu')) {
		//hidden = used.splice(maxProjectLinks);
		used.each(function(element, i) {
		  //alert(typeof element);
		  if (i + 1 < maxProjectLinks) {
			element.show();
		  }
		});
	  } else {
		used.invoke('show');
	  }
	  
	  // more / less
	  // only has to be shown if there is a used hidden item
	  var used = $('archiveMenu').select('.used');
	  
	  $('archiveMenu').down('.moreless')[used.length > maxProjectLinks ? 'show' : 'hide']();		  
	}
	
	function showMoreLinks(event) {
	  if (event) event.stop();
	  
	  // show less link
	  $('archiveMenu').down('.moreWrapper').hide();
	  $('archiveMenu').down('.lessWrapper').show();
	  
	  // laat nu alles zijn uit de geselecteerde categorie
	  var activeCategory = getActiveCategory();
	  if (activeCategory) {
		Cookie.set('21bis_expandedMenu', activeCategory, { duration: 1 });
	  }
      showActiveCategory();
	  
	  //fixateMenu(false);
	  
	  return;
	}
	
	function hideMoreLinks(event) {
	  if (event) event.stop();
	  
	  $('archiveMenu').down('.moreWrapper').show();
	  $('archiveMenu').down('.lessWrapper').hide();
	  
	  Cookie.remove('21bis_expandedMenu');
	  showActiveCategory();
	  
	  //fixateMenu(true);
	}
	
	
	var IEVersion = (function(agent) {
		  var version = new RegExp('MSIE ([\\d.]+)').exec(agent);
		  return version ? parseFloat(version[1]) : -1;
	})(navigator.userAgent);
	
	var activeCategory = getActiveCategory();
	setCategory(activeCategory);
	
	if (!(Prototype.Browser.IE && IEVersion < 7))  {
		setNavigation();
		showActiveCategory();
		//fixateMenu(true);
		if (!$('archiveMenu').down('.projects .active').visible()) {
	      showMoreLinks();
		}
		$('archiveMenu').down('.moreless').setStyle('visibility:visible');	
	}
	
	var activeProject = (function() {
	  var element = $('archiveMenu').down('.projects .active');
	  return (/_([0-9]+)*$/.exec(element.id) || [null, null])[1];
	})();
	var used = $$('#archiveMenu .projects .used');
	var position = (function(u) {
	  var p = 0;
	  u.each(function(element, i) {
		 if (element.hasClassName("active")) p = i + 1;
	  });
	  return p;
	})(used);
	
	if (activeProject) {
		// set the position of this project in its category
		$$('.navigationArrows .position').each(function(element) {
		  var first = position < 10 ? '0' + position : position;
		  var last = used.length < 10 ? '0' + used.length : used.length;
			element.update(first + ' / ' + last);
		});
	}
	
	// Lightview
	if (!!window.Lightview) {
	  $(document.body).observe('click', function(event) {
		 var element = event.findElement('.projectItem .enlargeable, .projectItem .enlarge');
		 
		 if (element) {
		   event.stop();
		   Lightview.show(element.up('.projectItem').down('.enlarge .lightview'));
		 }
	  });
	
	}
	
	// observe
	$('archiveMenu').down('.moreLink').observe('click', showMoreLinks);
	$('archiveMenu').down('.lessLink').observe('click', hideMoreLinks);
});









