var firstload = false;
var mousex = 0;
var mousey = 0;
$(document).ready(function(){
	$.history.init(function(url) {
		if(url){
			pageload(url);	
		}else{
			if(firstload == true){
				pageload(homepage);
			}
			firstload = true;
		}
	});
	
	$("#menu #mainmenu a").click(function(){
		menuActivate($(this));
	});
	
	$(".submenu a").click(function(){
		submenuActivate($(this));
	});
	
	initAll();
		
	$('body').bind('mousemove',function(event){
		mousex = event.pageX;
		mousey = event.pageY;
	});
});

function menuActivate(el,skipsub){
	$("#mainmenu a.active").removeClass('active');
	if(!skipsub){
		$(".submenu a.active").removeClass('active');
	}
	el.addClass('active');
	$(".submenu.active").removeClass('active').hide('fast');
	$(".submenu[rel="+el.attr('href')+"]").addClass('active').show('fast');  
}

function submenuActivate(el){
	$(".submenu a.active").removeClass('active');
	el.addClass('active');
	$submenu = el.parents('.submenu');
	$parentButton = $("#mainmenu a[href="+$submenu.attr('rel')+"]");
	if(!$parentButton.hasClass('active')){
		menuActivate($parentButton);	
	}
}

function activateMedewerker(href){
	$(".medewerkersimages a.active").removeClass('active').find('img').stop().fadeTo(400,0.1)
	$(".medewerkersimages a[href="+href+"]").addClass('active').find('img').stop().fadeTo(400,1);
}

var publicatieActive = 0;

function checkArrowPublicatie(){
	if(publicatieActive == 0){
		$('.publicaties .navigatie a.top').fadeTo(100, 0.5)	
	}else{
		$('.publicaties .navigatie a.top').fadeTo(100, 1)	
	}
	if(publicatieActive >= $('.publicaties ul li').length-2){
		$('.publicaties .navigatie a.bottom').fadeTo(100, 0.5)	
	}else{
		$('.publicaties .navigatie a.bottom').fadeTo(100, 1)	
	}
}

function initAll(){
	
	$(".medewerkersimages a, #medewerkers a").hover(
		function(){
			$(".medewerkersimages a[href="+$(this).attr('href')+"] img").stop().fadeTo(400,1);
			$("#medewerkers a[href="+$(this).attr('href')+"]").addClass('hovered');
		},
		function(){
			if($(".medewerkersimages a[href="+$(this).attr('href')+"]").hasClass('active')){
				
			}else{
				$(".medewerkersimages a[href="+$(this).attr('href')+"] img").stop().fadeTo(400,0.1);	
				$("#medewerkers a[href="+$(this).attr('href')+"]").removeClass('hovered');			
			}
		}
	)
	
	$('.text .scroll').jScrollPane({scrollbarWidth:15, scrollbarMargin:10});
	
	$(document).vharesize({});
	
	initLinks();
	
	initFlags();
	
	if(currentscroller){
		$.fn.vhascroller.unloadplugin(currentscroller);
	}
	$(".flag").remove();
	
	if($("#scroller ul li").length > 0){
		setTimeout(function() { $("#scroller ul").vhascroller({});	$.fn.vhascroller.open(currentscroller); },40);
	}
	
	$('.publicaties .navigatie a.bottom').unbind('click').bind('click', function(e){
		e.preventDefault();
		publicatieActive += 1;
		if(publicatieActive >= $('.publicaties ul li').length-1){
			publicatieActive -= 1;	
		}
		$('.publicaties ul li').stop(true,true).hide('fast');
		$('.publicaties ul li:eq('+(publicatieActive)+')').stop().show('fast');
		$('.publicaties ul li:eq('+(publicatieActive+1)+')').stop().show('fast');
		checkArrowPublicatie();
	})
	
	$('.publicaties .navigatie a.top').unbind('click').bind('click', function(e){
		e.preventDefault();
		publicatieActive -= 1;
		if(publicatieActive < 0){
			publicatieActive += 1;	
		}
		$('.publicaties ul li').stop(true,true).hide('fast');
		$('.publicaties ul li:eq('+(publicatieActive)+')').stop().show('fast');
		$('.publicaties ul li:eq('+(publicatieActive+1)+')').stop().show('fast');
		checkArrowPublicatie();
	})
	publicatieActive = 0;
	checkArrowPublicatie();
	/*
	$('.btn_projectinfo').unbind('click').bind('click',function(e){
		if($('.projectinfo').hasClass('active')){
			$.fn.vhascroller.startautoslide(currentscroller);
			$('.projectinfo').stop().removeClass('active').animate({top:'400px'},500);															
		}else{
			$.fn.vhascroller.stopautoslide(currentscroller);
			$('.projectinfo').stop().addClass('active').animate({top:'0px'},500);															
		}
		e.preventDefault();
	});
	*/
	
	$('.btn_projectinfo').unbind('hover').hover(function(){
		$.fn.vhascroller.stopautoslide(currentscroller);
		if($.browser.msie){
			$('.projectinfo').stop().addClass('active').css({top:'0px'});
		}else{
			$('.projectinfo').stop().addClass('active').css({top:'0px', opacity: 0}).fadeTo(300, 1);
		}
	},function(){
		$.fn.vhascroller.startautoslide(currentscroller);
		if($.browser.msie){
			$('.projectinfo').stop().removeClass('active').css({top:'400px'});	
		}else{
			$('.projectinfo').stop().removeClass('active').fadeTo(300, 0, function(){ $(this).css({top:'400px'}) });	
		}
	});
		
	$("#ajaxformulier").unbind('submit').submit(function(){
		var form = $(this);
		var data = $(this).serialize();
		$(this).find('input, textarea, checkbox, radio, select').each(function(){
			if($(this).attr('title')){
				ar = $(this).attr('name').split('[]');
				naam = ar[0];
				data += '&veldnamen['+encodeURIComponent(naam)+']=' + encodeURIComponent($(this).attr('title'))
			}
		});
		$.ajax({
			type: 	$(this).attr('method'),
			url: 	$(this).attr('action'),
			data: 	data,
			success: function(msg){
				if(form.attr('target')){
					$("#"+form.attr('target')).html(msg);
				}
			},
			error: function(xhr, ajaxOptions, thrownError){
				alert(xhr.status);
				alert(thrownError);
			}
		});
		return false;
	});	
}


function initFlags(){
	$("[flag]:not([flaginit])").each(function(){
		$(this).attr('flaginit','1');
		$(this).vhaflag();
	});		
}

function initLinks(){
	$("a[href]:not([keyword]):not(.noajax):not([href^=mailto:]):not([target])").each(function(){
		var url = $(this).attr('href').replace($('base').attr('href'),'');
		$(this).attr('keyword',url);
		if($(this).attr('rel') == 'lightbox'){
			$(this).fancybox({
				'titlePosition'	: 'inside',
				'titleShow'		: true,
				'padding'		: 5,
				'onClosed'		: function() {
				}
			});
		}else{
			$(this).click(function(event){
				$.history.load($(this).attr('keyword'));
				event.preventDefault();
			});
		}
	});		
}

// HISTORY PLUGIN, INLADEN PAGINA
function pageload(hash) {
	if(hash != 'javascript:;' && hash != '#'){
		if(hash) {
			if($.browser.msie) {
				hash = encodeURIComponent(hash);
			}
			loadPage(hash);
		}
	}
}

// INLADEN PAGINA
function loadPage(href){
	if(href){
		if(currentscroller){
			$.fn.vhascroller.unloadplugin(currentscroller);
			setTimeout(function(){ loadAjaxPage(href); },400);
		}else{
			loadAjaxPage(href);
		}
	}
} 

function activateMenu(href){
	if($("#menu #mainmenu a[href="+href+"]").length > 0 && !$("#menu #mainmenu a[href="+href+"]").hasClass('active')){
		el = $("#menu #mainmenu a[href="+href+"]");
		$("#mainmenu a.active").removeClass('active');
		$(".submenu a.active").removeClass('active');
		el.addClass('active');
		$(".submenu.active").removeClass('active').hide('fast');
		$(".submenu[rel="+el.attr('href')+"]").addClass('active').show('fast');  
	}
	
	if($(".submenu a[href="+href+"]").length > 0 && !$(".submenu a[href="+href+"]").hasClass('active')){
		el2 = $(".submenu a[href="+href+"]");
		el = $("#menu #mainmenu a[href="+el2.parents('.submenu').attr('rel')+"]");
		if(!el.hasClass('active')){
			$("#mainmenu a.active").removeClass('active');
			$(".submenu a.active").removeClass('active');
			el.addClass('active');
			$(".submenu.active").removeClass('active').hide('fast');
			$(".submenu[rel="+el.attr('href')+"]").addClass('active').show('fast');
		}
		$(".submenu a.active").removeClass('active');
		el2.addClass('active');		
	}
}
function loadAjaxPage(href){
	$("#loader").show();
	typelink = false;
	if($("a[href="+href+"]").length > 0){
		if($("a[href="+href+"]").first().attr('rel') == 'medewerkerlink'){
			typelink = 'medewerker';
		}
	}
	
	for(var mc in FlagIntervals2){
		if(FlagIntervals2[mc]){
			clearTimeout(FlagIntervals2[mc]);	
			FlagIntervals2[mc] = null;
		}
	}
	for(var mc in FlagIntervals){
		if(FlagIntervals[mc]){
			clearInterval(FlagIntervals[mc]);	
			FlagIntervals[mc] = null;
		}
	}
	activateMenu(href);
	if(typelink == 'medewerker'){
		activateMedewerker(href);
		$('.default .text').fadeOut(200,function(){
			$.ajax({
				type: 		'GET',
				url: 		'loader.php',
				data: 		'p='+href,
				dataType:	'xml',
				cache: 		false,
				success: function(xml){
					_gaq.push(['_trackPageview', href]);
					$("#loader").hide();
					$(".default .text").html($(xml).find('html').text()).fadeIn(200);
				},
				error: function(xhr, ajaxOptions, thrownError){
					//alert(xhr.status);
					//alert(ajaxOptions);
				}
			});
		});
	}else{
		$('#contents').fadeOut(200,function(){
			$.ajax({
				type: 		'GET',
				url: 		'loader.php',
				data: 		'p='+href,
				dataType:	'xml',
				cache: 		false,
				success: function(xml){
					_gaq.push(['_trackPageview', href]);
					$("#loader").hide();
					if($(xml).find('activate').text()){
						activateMenu($(xml).find('activate').text());
					}
					if($(xml).find('closed').text() != '1'){
						$('#fullwidth').html($(xml).find('fullwidth').text()).fadeIn(200);
						$('#contents').html($(xml).find('html').text()).fadeIn(200);
						$('#scroller').html($(xml).find('projecten').text());
						$('#sidebarload').html($(xml).find('zijbalk').text()).fadeIn(200);
						$('#contentbottom').html($(xml).find('contentbottom').text()).fadeIn(200);
					}else{
						$('#sidebarload').html('').fadeIn(200);
						$('#fullwidth').html('').fadeIn(200);
						$('#scroller').html('<div id="scroller"><ul></ul></div>');
						$("#scroller ul").vhascroller({})	
						$('#contentbottom').html('').fadeIn(200);
					}
					$('#sidebar').animate({height:$(xml).find('sidebarHeight').text()+'px'},500);
					$('#fullwidth').animate({height:$(xml).find('fullwidthHeight').text()+'px'},500);
					initAll();
				},
				error: function(xhr, ajaxOptions, thrownError){
					//alert(xhr.status);
					//alert(ajaxOptions);
				}
			});
		});
		$('#sidebarload').fadeOut(200,function(){
		});
	}
	
}
