	var urlBase = '/';

	var $false = function(){
		return false;
	}
	
	function strpos(haystack, needle, offset){
    // Finds position of first occurrence of a string within another  
    // 
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
	}
	
	var cVechtLust = new Class({
		
		sName:'cVechtLust'		
		, oCommunicator:null
		, oPopup:null
		, oScroller:null
		, w_aLinks:null
		, w_sDefaultTitle:'Vechtlust'
		, w_sMain:null
		, w_sSub:null
		, w_bSkipPopup:null
		, w_oCursor:null
		, w_oaUrl:{}
		
		, initialize:function(){
			
			// to prevent IE to bug on morph this has to be set once
			$$('nav')[0].style.backgroundPosition = '0px 0px';
			
			// object for ajax requests
			this.oCommunicator = new Request.JSON({
				'onRequest':  this.loading.start.bind(this)
				, 'onComplete': this.loading.stop.bind(this)
				, 'onSuccess':  this.fReceive.bind(this)
				, 'noCache':    true
			})
			
			// object for scrolling
			this.oScroller = new Fx.Scroll('h_scroller', {
					'link':       'cancel'
				, 'duration':   750
				, 'transition': Fx.Transitions.Circ.easeOut // http://demos111.mootools.net/Fx.Transitions
				, 'fps':        25
			});
			
			// object for dragging
			
			if (!Browser.Platform.ipod){
				this.oDragger = new Drag('h_scroller', {
							'style':      false
						, 'invert':     true
						, 'modifiers':  {x: 'scrollLeft', y: 'scrollTop'}
						, 'onStart':    this.fDragStart.bind(this)
						, 'onComplete': this.fDragComplete.bind(this)
				});
			}
			
			
			// get all page elements
			this.w_aPages = $$('#h_scroller .h_page');
			
			// get all navigation links
			this.w_aLinks = $$('nav a');
			
			// disable default link action and replace with AJAX request
			this.fReplaceMenuLinkAction();
			
			this.fReplacePageItemLinkAction();
			
			// set current location
			
			// go to current page
			t_sPageName = '';
			if(document.location.hash == ''){
				var t_oEl = $$('.current_page')[0];
				if($type(t_oEl) != false){
					t_sPageName = t_oEl.get('id').replace('page_', '');
				}
			}
			else{
				var t_sHash = document.location.hash.substr(1);
				this.w_sMain = t_sHash;
				
				if(t_sHash.indexOf('&sub=') !== -1){
					this.w_sMain = t_sHash.substr(0, t_sHash.indexOf('&sub='));
					this.w_sSub  = t_sHash.substr(t_sHash.indexOf('&sub=') + 5);
					this.fSend('?page='+this.w_sMain+'&sub='+this.w_sSub);
				}
				//this.oScroller.toElement($('page_'+this.w_sMain));
				t_sPageName = this.w_sMain;
			}
			if(t_sPageName != ''){
				this.fScroll(t_sPageName);
			}
			
			// remove class from all elements with class current_page
			$$('.current_page').each(
				function(item, x){
					item.removeClass('current_page');
				}
			);
			
			// remove all anchors
			$$('a.anchor').each(
				function(t_elAnchor){
					t_elAnchor.dispose();
				}
			);
		}
		
		, loading:{
				start:function(){
				$('h_content').adopt(this.loading.w_elLoading);
			}
			, stop:function(){
				this.w_elLoading = this.loading.w_elLoading.dispose();
			}
			, w_elLoading:Element('div', {'class': 'h_loading', 'html': ''}).adopt([
				  Element('div', {'class': 'background'})
				, Element('div', {'class': 'center'}).adopt(
					  Element('img', {'src': './_img/ajax-loader_sm.gif'})
				)
			])
		}
		
		, fScroll:function(p_sPageName){
			this.oScroller.toElement('page_'+p_sPageName);
			this.fSetActive(p_sPageName);
			this.fHidePopUp();
		}
		
		, fSend:function(p_sUrl, getProjectData){
			url = p_sUrl;
			if (url.match(/http/)) { // IE7
				url = url.replace(/http:\/\/[\w\.]*/, '');
				alert(url);
			}
			if (getProjectData && !url.match(/sub=contact/)) {
				splitHash = url.match(/(\w*)\/(\d*)\/[\w\W]*$/);
				newPage = (splitHash ? splitHash[1] : null);
				newId = (splitHash ? splitHash[2] : null);
				url = '?page=' + newPage + '&sub=' + newId;
			}
			var t_oaData = {
				'url':  urlBase + url
				, 'data': {
					  'async': true
				}
			}
			this.oCommunicator.send(t_oaData);
		}
		
		, fLoadContent:function(t_sPageName){
			// check, loaded; url exists
			if(($type($('page_'+t_sPageName).getElements('.loading img')[0]) == false) || ($type(this.w_oaUrl[t_sPageName]) == false) || ($type(this.w_oaUrl[t_sPageName]) == '')){
				return;
			}
			
			// get url
			var t_sUrl = this.w_oaUrl[t_sPageName];

			// load url
			this.fSend(t_sUrl);
			
			// remove url to prevent reload
			this.w_oaUrl[t_sPageName] = '';
		}
		
		, fReceive:function(p_aJSON){
			//alert(p_aJSON.content);
			//, fReceive:function(responseTree, responseElements, responseHTML, responseJavaScript){
			/*

				p_aJSON = {
						level : 'main'/'sub'
					, current_page:'pagename'
					, content:''
				}
			*/
			if(!p_aJSON){
				return;
			}
			
			if(p_aJSON.level == 'main'){
				//get element container
				var t_ePageItemContainer = $('page_'+p_aJSON.current_page).getElements('.h_items')[0];
				
				//hide container, place content and fade in
				t_ePageItemContainer.fade('hide');
				t_ePageItemContainer.empty().set('html', p_aJSON.content);
				t_ePageItemContainer.fade(1);
				
				this.fReplacePageItemLinkAction();
			}
			else{
				this.fShowPopUp(p_aJSON);
			}
		}
		
		, fReplaceMenuLinkAction:function(){
			// get all links in nav	
			this.w_aLinks.each(function(t_eLink, t_iKey){
				// for contact we make an exception
				if(t_eLink.get('id') == 'b_contact'){
					t_eLink.set('href', '?page=&sub=contact');
					this.fChangeLink(t_eLink, false);
					return;
				}
				
				// get href from link and split in "?" and "#" parts
				var t_sLink = t_eLink.get('href');
				var t_sHash = t_sLink.substr(t_sLink.indexOf('#'));
				t_sLink = t_sLink.substr(0, t_sLink.indexOf('#'));
				var t_sId = t_eLink.get('id').replace('b_', '');
				
				// store "?" part in array for later usage
				this.w_oaUrl[t_sId] = t_sLink;
				
				// set link to "#" part for bookmarking
				t_eLink.set('href', t_sHash);
				
				// set scrollto
				t_eLink.addEvent('click', function(){
				
					this.fScroll(t_sId);
				}.bind(this));
			}, this);
		}
		
		, fReplacePageItemLinkAction:function(){
			$$('a.page_item').each(function(t_elPageItemLink){
				//check if link has been changed
				if((t_elPageItemLink.get('target') == '') || ($type(t_elPageItemLink.get('target')) == false)){
					return;
				}
				this.fChangeLink(t_elPageItemLink, false);
			}, this);
			
			//blur a's on focus to remove dotted line in IE
			$$('a').addEvent(
				  'focus'
				, function(){
					this.blur();
				}
			);
		}
		
		, fChangeLink:function(p_oEl, p_bSub){
		
			//  clear target
			p_oEl.set('target', '');
			
			// set click to false
			p_oEl.addEvent('click', $false);
			
			// grab Href
			var t_sHref = p_oEl.get('href');
			//alert(t_sHref);
			
			// set Href
			//p_oEl.set('href', '#' + newPage + '&sub=' + newId);
			p_oEl.set('href', t_sHref);
			
			// add click event
			p_oEl.addEvent('click', function(e){
				// if scolling was going on, skip the popup
				if(this.w_bSkipPopup){
					this.fResetSkipPopup(this);
					return;
				}
				
				// cause the link is made inactive by 'click' 'false', set the correct location
				document.location.hash = p_oEl.get('href');

				// send request to collect page
				this.fSend(p_oEl.get('href'), true);
				
			}.bind(this));
		}
		
		, fSetSkipPopup:function(p_bValue){
			this.w_bSkipPopup = p_bValue;
		}
		
		, fResetSkipPopup:function(p_oThis){
			p_oThis.fSetSkipPopup(false);
		}
		
		, fDragStart:function(){
			$('h_scroller').setStyle('cursor', 'grabbing');
			$('h_scroller').setStyle('cursor', '-moz-grabbing');
			$('h_scroller').setStyle('cursor', '-webkit-grabbing');
		}
		
		, fDragComplete:function(){
			this.oScroller.toElement
			$('h_scroller').setStyle('cursor', 'grab');
			$('h_scroller').setStyle('cursor', '-moz-grab');
			$('h_scroller').setStyle('cursor', '-webkit-grab');
			
			t_elNearest = this.fGetNearestPageElement();
			this.fSetActive(t_elNearest.get('id').replace('page_', ''));
			this.fSetSkipPopup(true);
			
			var fResetSkipPopup = this.fResetSkipPopup.create({
		      arguments: this
		    , delay:     500
			});
			fResetSkipPopup();
		}
		
		, fGetNearestPageElement:function(){
			var t_oCurrentScroll = $('h_scroller').getScroll();
			var t_oCurrentSize = $('h_scroller').getSize();
			
			var t_oScrollerCenter = {
					x:t_oCurrentScroll.x + (t_oCurrentSize.x/2)
				, y:t_oCurrentScroll.y + (t_oCurrentSize.y/2)
			};
			
			var t_iLowestDistance = null;
			var r_elNearest = null;
			this.w_aPages.each(function(t_elPage){
				var t_oPagePos = t_elPage.getPosition($('h_scroller'));
				var t_oPageSize = t_elPage.getSize();
				
				var t_oPageCenter = {
						x:t_oPagePos.x + (t_oPageSize.x/2)
					, y:t_oPagePos.y + (t_oPageSize.y/2)
				};
				
				var t_oDiff = {
						x:t_oPageCenter.x - t_oScrollerCenter.x
					, y:t_oPageCenter.y - t_oScrollerCenter.y
				}
				
				var t_iDistance = Math.sqrt(Math.pow(t_oDiff.x,2) + Math.pow(t_oDiff.y,2));
				
				if(($type(t_iLowestDistance) == false) || (t_iLowestDistance > t_iDistance)){
					t_iLowestDistance = t_iDistance;
					r_elNearest = t_elPage;
				}
				
			}, this);
			
			return r_elNearest;
		}
		
		, fSetActiveOld:function(p_elPageActive){
			// get page name
			var t_sPageID = p_elPageActive.get('id').replace('page_', '');
			
			document.title = t_sPageID+' - '+this.w_sDefaultTitle;
			
			// set class for nav element (places tab)
			$$('nav')[0].set('class', t_sPageID);
			// remove 'current' class from all buttons
			this.w_aLinks.each(
				function(t_aLink){
					t_aLink.set('class', '');
				}
			);
			// set new 'current' class
			if($type($('b_'+t_sPageID)) !== false){
				$('b_'+t_sPageID).set('class', 'current');
			}
		}
		
		, fSetActive:function(p_sPageName){
			this.w_sCurrent_page = p_sPageName;
			this.fLoadContent(p_sPageName);
			document.title = p_sPageName+' - '+this.w_sDefaultTitle;
			document.location = '#'+p_sPageName;
			
			// move tabblad
			var t_oaPos = $('b_'+p_sPageName).getPosition( $$('nav')[0] );
			$$('nav')[0].morph({
				'background-position':t_oaPos.x+'px 0px'
			});
			
			// fade text
			this.w_aLinks.each(function(t_aLink){
				t_aLink.tween('color', ( t_aLink.get('id').replace('b_', '') == p_sPageName ? '#fff' : '#000' ));
			},this);
		}
		
		, fShowPopUp:function(p_oaData){
			
			var w_iWidth  = p_oaData.w;
			var w_iHeight = p_oaData.h;
			
//			this.w_oaPopup = {
//					'background':Element('div', {'id': 'popup_background'}).addEvent('click', this.fHidePopUp.bind(this))
//				, 'balloon':Element('div', {'id': 'popup_positioner'}).adopt(
//					Element('div', {'id': 'popup','style':'width:'+w_iWidth+'px;height:'+w_iHeight+'px;'}).adopt([
//							Element('div', {'class': 'balloon_top'})
//						, Element('div', {'class': 'balloon_mid'}).adopt([
//									Element('div', {'class': 'balloon_speak'})
//							, Element('div', {'class': 'content', 'html':p_oaData.content})
//						])
//						, Element('div', {'class': 'balloon_bottom'}).adopt(
//							Element('div', {'html': 'sluiten', 'class': 'b_sluiten'}).addEvent('click', this.fHidePopUp.bind(this))
//						)
//					])
//				)
//			};
		
			
			this.w_oaPopup = {
				  'background':Element('div', {'id': 'popup_background'}).addEvent('click', this.fHidePopUp.bind(this))
				, 'balloon':Element('div', {'id': 'popup_positioner'}).adopt(
					Element('div', {'id': 'popup','style':'width:'+w_iWidth+'px;height:'+w_iHeight+'px;'}).adopt([
						  Element('div',{'class':'dragger top-left'})
						, Element('div',{'class':'dragger top'})
						, Element('div',{'class':'dragger top-right'})
						, Element('div',{'class':'dragger right'})
						, Element('div',{'class':'dragger bottom-right'})
						, Element('div',{'class':'dragger bottom'})
						, Element('div',{'class':'dragger bottom-left'})
						, Element('div',{'class':'dragger left'})
						, Element('div',{'class':'dragger speak'})
						, Element('div',{'class':'h_content'}).adopt(
							  Element('div',{'class':'content', 'html':p_oaData.content})
						)
						, Element('div', {'html': 'sluiten', 'class': 'b_sluiten'}).addEvent('click', this.fHidePopUp.bind(this))
					])
				)
			};
			
			// change links
			// if($$('body').get('class') != 'sub'){
				var t_aLinks = this.w_oaPopup.balloon.getElements('.part_content').getElements('a')[0];
				if($type(t_aLinks) == 'array'){
					t_aLinks.each(function(t_elLink){
						if((strpos(t_elLink.get('href'), 'sub=') !== false)){
							this.fChangeLink(t_elLink, true);
						}
					}, this);
				}
			// }
			$('h_popup').fade('hide');
			$('h_popup').empty();
			$('h_popup').adopt(this.w_oaPopup.background);
			$('h_popup').adopt(this.w_oaPopup.balloon.fade('hide'));
			$('h_popup').fade('show');
			
			if($('popup').getElements('.content')[0]) {
				new Drag($('popup'),{
					  handle:$('popup').getElements('.dragger')
					, onDrag:function(p_elDrag){
						$$('.dragger').setStyle('cursor', 'grabbing');
						$$('.dragger').setStyle('cursor', '-moz-grabbing');
						$$('.dragger').setStyle('cursor', '-webkit-grabbing');
					}  
					, onComplete:function(p_elDrag){
						$$('.dragger').setStyle('cursor', 'grab');
						$$('.dragger').setStyle('cursor', '-moz-grab');
						$$('.dragger').setStyle('cursor', '-webkit-grab');
					}
				});
			}
			
			
			var t_aThumbButton = this.w_oaPopup.balloon.getElements('.part_thumb');
			if($type(t_aThumbButton) == 'array'){
				
				t_aThumbButton.each(function(t_elButton){
					
					t_elButton.addEvent('click', $false);
					t_elButton.addEvent('click', function(){
						t_aThumbButton.each(function(t_elBut){
							
							t_elBut.removeClass('active_button');
							
							if(t_elBut == this){
								t_elBut.addClass('active_button');
							}
							
						}, this);
						
						$$('.pageitem_part').each(function(t_elPageItemPart){
							t_elPageItemPart.setStyle('display', ((t_elPageItemPart.hasClass(t_elButton.get('href').replace('#', ''))) ? 'table' : 'none'));
						}, this);
					});
				}, this);
			}
			
			this.w_oaPopup.balloon.fade(1);
		}
		
		, fHidePopUp:function(){
			if($type(this.w_oaPopup) === false){
				return;
			}
			document.location = '#'+this.w_sCurrent_page;
			this.w_oaPopup.balloon.fade(0);
			this.fRemovePopup.delay(710);
		}
		
		, fRemovePopup:function(){
			$('h_popup').empty();
		}
		
	});

