jQuery(document).ready(function($) {
	
	//Algemeen
	function initPage() {
		pageID = "page-item-"+$("#page_id").text();
		parentID = "page-item-"+$("#parent_id").text();
		ID = 'ID_'+ $("#page_id").text();
		$("body").removeClass();
		$("body").addClass(ID);
		$(".nav li[class!="+pageID+"]").removeClass('current');
		$(".nav li."+pageID).addClass('current');
		if ($("#parent_id").text() !=0) {
			$("body").addClass("parentID_"+$("#parent_id").text());
			$(".nav li."+parentID).addClass('current');
		}
		$("#webbouwer").fadeIn('normal');
		$("ul li:first-child").addClass('first');
		$("ul li:last-child").addClass('last');
		
		$('.nav li').hover(function() {
			$(this).addClass('over');
			if (!$(this).hasClass('page-item-21'))
				$(this).find("ul").slideDown('medium');
			}, function() {
				$(this).find("ul").slideUp('medium');
			$(this).removeClass('over');
		});
		//Rollover
		$('.rollover').hover(function() {
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('title'));
			$(this).attr('title', currentImg);
			}, function() {
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('title'));
			$(this).attr('title', currentImg);
		});
		$("#close a").click(function() {
			$("#IE6_alert").css('display','none');
			return false;
		});
		//initMainPage();
		if ($('#portfolio')) {
			initPfolio();
		}
		//if ($('#sub')) {
		//	initSubpage();
		//}
	}

//Smooth Ajax Pageloading - Main
	
	function initMainPage() {
		var hash = window.location.hash.substr(1);
		var href = $('.ajax a').each(function(){
			var href = $(this).attr('href');
			if(hash==href.substr(0,href.length-5)){
				var toLoad = hash+'.html #core';
				$('#core_cont').load(toLoad, initPage);	
			}											
		});
	
		$('.ajax a[target!=_blank]').click(function(){				  
			var toLoad = $(this).attr('href')+' #core';
			$('#core').fadeOut('fast',loadContent);
			$("#webbouwer").fadeOut('fast');
			
			//window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
			window.location.hash = $(this).text();
			function loadContent() {
				$('#core').remove();
				$('#core_cont').load(toLoad,showNewContent);
			}
			function showNewContent() {
				$('#core_cont').fadeIn('fast', function() {
													initPage();
												});
			}
			return false;
		});
	}
	
	//Smooth Ajax Pageloading - Portfolio
	function initPfolio() {
		$("#slider").easySlider({ auto: false, numeric: true });
		
		//Scroller
		$('#thumbs_cont').mousemove(function(e) {
			var thumbMarg = 10;
			var contOfsetLeft = parseInt($('#thumbs_cont').offset().left);	
			var contWith = parseInt($('#thumbs_cont').width());
			var totWidth = parseInt(($('#thumbs li').width() + thumbMarg) * $('#thumbs li').length);
			var aniOfset = Math.round(((totWidth - contWith)/contWith) * (contOfsetLeft - e.pageX));
			$('#thumbs').animate({left: aniOfset}, { queue:false, duration:500});
			//$('#debug').html($('#thumbs li').width());
		});
	
		// initThumbs
		$('#thumbs li a').click(function(){
			//alert($(this).text());						  
			var toLoad = $(this).attr('href')+' #portfolio';
			$('#portfolio').fadeOut('fast',loadPortf);
			$('#load').remove();
			$('#wrapper').append('<span id="load">LOADING...</span>');
			$('#load').fadeIn('normal');
			function loadPortf() {
				$('#portfolio').remove();
				$('#pfolio_cont').load(toLoad,showPortf);
			}
			function showPortf() {
				$('#pfolio_cont').fadeIn('normal', function() {
													$("#slider").easySlider({ auto: false, numeric: true });
													$('#load').fadeOut('normal');
												});
			}
			return false;
		});
	}
	
	
	//In Smooth Pageload - Subpages
	function initSubpage() {
		$('#sub li a').click(function(){
			//alert($(this).text());						  
			var toLoad = $(this).attr('href')+' #left';
			$('#left').fadeOut('fast',loadSub);
			function loadSub() {
				$('#left').remove();
				$('#left_cont').load(toLoad,showSub);
			}
			function showSub() {
				$('#left_cont').fadeIn('fast');
			}
			return false;
		});
	}
	
	$.preloadCssImages();
	initPage();
});