$(document).ready(function(){

	//javascript on?
	document.getElementsByTagName('html')[0].className = 'hasJS';


	//window resize => resize backgrounds
	size_backgrounds();
	$(window).bind('resize', function() {
		size_backgrounds();
	});


	//'title' update, voorbereiding... array opzetten
	title_pos  = [];
	$('.virtual_page').each(function(index) {
		var dist_in_div= $(this).find(".spacer_top").height() + $(this).find(".title").height();
		var dist_top = ($(window).height()-300) / 2;
		var item_pos = $(this).position();
		var item_pos_top = item_pos.top;
		var change_at_distance = (item_pos_top+dist_in_div)-dist_top-200;
		var item_title = $(this).attr('name');
		var item_slug = $(this).attr('slug');
		//alert(change_at_distance);
		title_pos[index] = [];
		title_pos[index]['pos'] = [change_at_distance];
		title_pos[index]['title'] = [item_title];
		title_pos[index]['slug'] = [item_slug];
	});	
	

	//sroll
	var pane = $('#wrapper');
	pane.jScrollPane({ //scrollpane api
		autoReinitialise: true,
		animateScroll: true,
		showArrows: true,
		verticalGutter: -16
	}).bind( 'jsp-scroll-y', function(){
		$('img.lazy').asynchImageLoader({
			timeout: 0,
			offset: 100
		});
		check_title(); //run 'title' update function
		
		if(api.getContentPositionY() > 300){
			if($('.overlay_menu').length == 0){
				$('.menu').css({'position':'fixed','top':'0px','z-index':'99','width':'100%','height':'50px'}).addClass("overlay_menu").prev().css('padding-bottom', '50px');
			}		
		}else{
			if($('.overlay_menu').length != 0){
				$('.menu').css({'position':'relative'}).removeClass("overlay_menu").prev().css('padding-bottom', '0px');
			}
		}
		
	});
	api = pane.data('jsp');
	$('body').css({overflow: 'hidden'});


	//menu
	$('.nav').bind('click',function(){
		window.location = '#'+$(this).attr('vpage_id');
		
		 //backup, if hash controll fails when # already given
		var dist_prev = $("#"+$(this).attr('vpage_id')+ "_vpage .spacer_top").height() + $("#"+$(this).attr('vpage_id')+ "_vpage .title").height();
		var dist_top = ($(window).height()-300) / 2;
		var item_pos = $("#"+$(this).attr('vpage_id')+"_vpage").position();
		var item_pos_top = item_pos.top;
		api.scrollTo(0,(item_pos_top+dist_prev)-dist_top);
		
		return false;
	}).first().hide();


	//go back up
	$('.backtotop').bind('click',function(){
			window.location = '#Saskia_Zuiderduin';
			api.scrollTo(0,0);
			return false;
		}
	);


	//hash
	bind_hash_change();
	$(window).trigger( 'hashchange' );


	//google map
	setup_map();


	//image loader
	$('img.lazy').asynchImageLoader({
		event: "load",
		timeout: 0,
		offset: 0
	});


	//auto portfolio scroll
	$(".portfolio").mouseover(function() {
		$(this).mousemove(function(e){
			var lastLi = $(this).find('li:last-child');
			var divWidth =  $(window).width();
			var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth();	
			var mousePos = e.pageX;
			var left = (e.pageX - $(this).offset().left) * (ulWidth-divWidth) / divWidth;
			$(this).scrollLeft(left);
		});	
	}).css({overflow: 'hidden', 'cursor': 'ew-resize'});

	
});




//=== FUNCTIONS ===//

function bind_hash_change(){
	$(window).bind('hashchange', function(e) {
		if(location.hash!==''){
			if ($(location.hash+"_vpage").length) {
				var dist_prev = $(location.hash+"_vpage .spacer_top").height() + $(location.hash+"_vpage .title").height();
				var dist_top = ($(window).height()-300) / 2;
				var item_pos = $(location.hash+"_vpage").position();
				var item_pos_top = item_pos.top;
				api.scrollTo(0,(item_pos_top+dist_prev)-dist_top);
			}
		}
	});
}


function update_title() {

	//alert(title_pos[title_id]['title']+' | Saskia Zuiderduin Edelsmid');
	document.title = title_pos[title_id]['title']+' | Saskia Zuiderduin Edelsmid'; //set title

 	$(window).unbind('hashchange');
	setTimeout(function() {
	        window.location.hash = title_pos[title_id]['slug'];
	}, 100);
	setTimeout(function() {
	        bind_hash_change();
	}, 200);
	
};


var scrollTimer = null;
function check_title(){ //'title' update function, use array
	$.each(title_pos, function(i) {
		if(i > 0){
			if(api.getContentPositionY() > title_pos[i]['pos']){
				if(document.title!==title_pos[i]['title']+' | Saskia Zuiderduin Edelsmid'){ //alleen als hij nog niet ingesteld is
					//en alleen wanneer men al 1 sec stil staat.
					if (scrollTimer) clearTimeout(scrollTimer);
					scrollTimer = setTimeout(update_title, 1000);
					title_id = i;					
				}
			}else if(api.getContentPositionY() < title_pos[1]['pos']){
				clearTimeout(scrollTimer);
				document.title = 'Saskia Zuiderduin Edelsmid';
				if(window.location.hash!==''){
					window.location.hash = '#Saskia_Zuiderduin';
				}
			}
		}
	});
}

function size_backgrounds() {
	if($(window).width() < 1500){
		$('.background_image').css({'width':'1500px'});
	}else{
		$('.background_image').css({'width':'100%'});
	}
/*	
	if(typeof api != 'undefined'){
		api.reinitialise();
	}
*/
	
}


function setup_map() {
	var saskia_zuiderduin = new google.maps.LatLng(52.882099875569885, 5.56856095790863);
	var MapOptions = {
		scrollwheel: false,
		zoom: 11,
		center: saskia_zuiderduin,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), MapOptions);
	var marker = new google.maps.Marker({
		position: saskia_zuiderduin, 
		map: map,
		title:"Saskia Zuiderduin"
	});   
}
