 
/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);



$(document).ready(function(){
	
	function insertAgenda( ) {

		var numberOfMaxDisplayedItems = 4;
		var agendaItems = [
			// {date:"16 juli 2011", location:"Pleinfeest @ Caf&eacute; cafetaria Stynsgea in Augustinusga"},
			// {date:"23 juli 2011", location:"Caf&eacute; de Terp, Drachtstercompagnie"},
			// {date:"3 september 2011", location:"Caf&eacute; Scooters, Leeuwarden"},
			// {date:"10 september 2011", location:"Ureterp"},
			{date:"30 september 2011", location:"Caf&eacute; 't Kannet, Heerenveen", description: "Kroegentocht Heerenveen"},
			{date:"4 november 2011", location:"Caf&eacute; Scooters, Drachten"}
			//{date:"10 juni 2011", location:"Burum"}
			//{date:"15 april 2011", location:"Bakkeveen"},
			//{date:"12 maart 2011", location:"Scooters, Drachten"}
			//{date:"4 februari 2011", location:"Scooters, Drachten"}
			//{date:"30 februari 2011", location:"Timboektoe"},
			//{date:"1 april 2011", location:"Caf&eacute; Rottevalle in de Lits", description:"Entree is &euro; 500,-"}
		];

		var html = null;

		var length = Math.min(agendaItems.length, numberOfMaxDisplayedItems);
		if (length > 0) {

			// We hebben items in de agenda, gaan we weergeven.
			html = "<h1>Agenda</h1>";
			html += '<div id="agenda-rotator">';

			for (var i = 0; i < length; i++) {
				var agendaItem = agendaItems[i];
				var date = agendaItem.date;

				var location = agendaItem.location;

				html += '<div class="agenda-item">' + date + '<br /><span class="agenda-item-location">' + location + "</span>";

				var description = agendaItem.description;
				if (description != null && description != "") {
					html += '<br /><span class="agenda-item-description">' + description + "</span>";
				}
				html += "</div>";
			}
			
			html += '</div';
		}
		else {
			// Geen items, boeken?
			html = "<div class='agenda-item'>Boek via 06&nbsp;30&nbsp;375&nbsp;647 of <a href=\"mailto:info@maffick.nl\">info@maffick.nl</a></p>";
		}
		
		$("#agenda").html(html);
	}
	
$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();
		
		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;
		
		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];
		
		$("li.selected").removeClass("selected");
		// console.log($("#" + trgt));
		$("a[href='#" + trgt + "']").parent().addClass("selected");
		
		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		
		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});

	insertAgenda();
});

function insertFixedMenu() {
	insertMenuInternal("fixed_nav_wrapper", null);
}

function insertMenu(id) {
	insertMenuInternal("nav_wrapper", id);
}

function insertMenuInternal(className, id) {

	var navigationItems = [
		["Home", "top"],
		["Over Maffick", "over"],
		["Biografie", "bio"],
		["Foto's", "pictures"]
	];
	
	var html = "<div class=\"" + className + "\"";
	if (id != null) {
		html += " id=\"" + id + "\"";
	}
	html += ">";
	html += "<hr />";
	html += "<ul>";
	
	var length = navigationItems.length;
	for (var i = 0; i < length; i++) {
		var item = navigationItems[i];
		html += "<li>";
		html += "<a href=\"#" + item[1] + "\" class=\"scroll\">" + item[0] + "</a>";
		html += "</li>";
	}
	html += "</ul>";
	html += "<hr style=\"float:left;\"/>";
	html +="</div><br /><br />"
	document.write(html)
}

function insertGallery( ) {

	var mainDir = "images/gallery/";
	var images = [
		{src:"Jeffrey en Astrid.jpg"},
		{src:"Maffick.jpg"},
		{src:"32317352_full.jpg"},
		{src:"35760924_full.jpg"},
		{src:"51355444_full.jpg"},
		{src:"92773858_full.jpg"},
		{src:"2371139_full.jpg"},
		{src:"16626104_full.jpg"},
		{src:"35744524_full.jpg"},
		{src:"70075852_full.jpg"}
	];
	
	var html = null;
	
	var length = images.length;
	if (length > 0) {
		
		html = "<div id=\"galleria\">";
		
		for (var i = 0; i < length; i++) {
			var imageInfo = images[i];
			var path = mainDir + imageInfo.src;
			
			html += "<img src=\"" + path + "\" />";
		}
		
		html += "</div>";
		
		document.write(html);
		
		// Load the classic theme
		Galleria.loadTheme('js/galleria/themes/classic/galleria.classic.js');
		// Initialize Galleria
		$('#galleria').galleria();
	}
	else {
		html = "Onze foto's zijn nog even in de donkere kamer.";
		document.write(html);
	}
}




function updateMenus() {
	
	var scrollY = window.scrollY;
	
	var navWrappers = $('.nav_wrapper');
	
	var overlap = 10;
	
	//
	var above = null;
	var topmostY = 0;
	var next = null;
	var targetWrapper = null;
	for (var i = 0; i < navWrappers.length; i++) {
		var navWrapper = navWrappers[i];
		var wrapperY = navWrapper.offsetTop;
		navWrapper.style.visibility = "visible";
		if (scrollY > wrapperY) {
			above = navWrapper;
			break;
		}
		/*if (scrollY > wrapperY && wrapperY < scrollY + window.innerHeight - overlap) {
			above = navWrapper;
			topmostY = wrapperY;
			if (i < navWrappers.length - 1) {
				next = navWrappers[i + 1];
			}
		}*/
	}
	
	var fixedNav = $('.fixed_nav_wrapper')[0];
	if (above != null) {
		if (next != null && next.offsetTop < scrollY + window.innerHeight - overlap) {
			//fixedNav.style.top = scrollY - (scrollY + window.innerHeight - next.offsetTop - overlap);
			//console.log("next in view " + fixedNav.style.top);
		}
		else {
		//	fixedNav.style.top = scrollY;
			//console.log(fixedNav.style.top);
		}
		
		fixedNav.style.visibility = "visible";
		
		if (above.offsetTop + 40 < scrollY) {
			above.style.visibility = "hidden";
		}
	}
	else {
		fixedNav.style.visibility = "hidden";
		fixedNav.style.top = 0;
	}
}

