// If page is ready
$(document).ready(function(){	
	

	initBasicPage();
	initImageBar();
	
	$("#welkom").click(function(){			
		var url = $('#camping-julianahoeve-btn a').attr('href')+'/';
		
		$.getJSON(url, {ajax : true}, function(json){				
			$('#home-page').empty();
			$('#home-page').append(json.content);			
			$('#camping-part-content .inactive-default-page-canvas').stop().animate({'marginLeft' :640}, 500);		
			
			initBasicPage();
			
		});				
		return false;
	});

	$('#contact-route-btn').click(function() {								
		$('#main-menu ul li a').removeClass('active');	
		showCampingPartContent(this);		
		return false;
	});
		
	$('#main-menu ul li a.intern').click(function() {				
		$('#main-menu ul li a').removeClass('active');
		$(this).addClass('active');			
		showCampingPartContent(this);		
		return false;
	});
	
	/*$('#camping-de-zwinhoeve-btn a').click(function() {		
		$('#main-menu ul li a').removeClass('active');
		showCampingPartContent(this);		
		return false;		
	});		*/
	
	$('#language-chooser-btn').click(function(){
		
		if($('#language-chooser ul').hasClass('visible')) {
			$('#language-chooser ul').slideUp();	
			$('#language-chooser ul').removeClass('visible');
		} else {
			$('#language-chooser ul').slideDown();
			$('#language-chooser ul').addClass('visible');
		}		
		return false;
	});
});


function showCampingPartContent(target) {
	
	var url = $(target).attr('href')+'/';	
	
	$.getJSON(url, {ajax : true, level: 1}, function(json){	
				
		$('#camping-part-content .active-default-page-canvas').empty();
		$('#camping-part-content .active-default-page-canvas').append(json.content);
		initBasicPage();
		initImageBar();		
		
		$('#camping-part-content .active-default-page-canvas').stop().animate({'marginLeft' :0}, 500, function(){									
				
				$('#home-page').empty();
			
				$(this).removeClass('active-default-page-canvas');
				$('#camping-part-content .inactive-default-page-canvas').addClass('active-default-page-canvas');
				$('#camping-part-content .inactive-default-page-canvas').removeClass('inactive-default-page-canvas');
				$(this).addClass('inactive-default-page-canvas');			
				$('#camping-part-content .active-default-page-canvas').css('margin-left', 640);		
				$('#camping-part-content .active-default-page-canvas').empty();
				
			
				// IF YOU COME FROM MY JULIANAHOEVE PART
				if(getCleanValue($('#container-canvas').css('margin-left')) > -720) {
					
					$('#camping-julianahoeve-btn').hide();
					$('#mijn-julianahoeve-btn').show();
					
					$('#welkom-login').hide();
					$('#welkom').show();	
					
					$('#container-canvas').stop().animate({marginLeft : -720}, 500);
					$('#totem').stop().animate({'marginLeft' : 162}, 500);
					
				}
		});		
		
	});
	
}


function initBasicPage()
{
	cufonIt();
	initializeContactMap();
	
	Shadowbox.init({
		players:    ["iframe"], 
		skipSetup: true 
	});
	Shadowbox.setup(); 
	
	
	$('.printversion-btn').click(function() {		
		window.open(this.href);				
		return false;
	});
	
	if($('input.datepicker').length > 0) {
		$('input.datepicker').datepicker({
			dateFormat: 'dd-mm-yy',
			showAnim: 'blind',
			firstDay: 1,
			changeYear: true,
			yearRange: 'c-100:c+10'
		});
	}
	
	$('#home-blocks .block').click(function() {
		var url = $(this).find('.invisible-link').attr('href');
		window.location = url;
		return false;
	});
	
	$('#news-holder a').click(function() {		
		showCampingPartContent(this);		
		return false;		
	});	
	
	
	if($('.addthis_toolbox').length > 0) {
		addthis.toolbox('.addthis_toolbox');
	}
}


function initImageBar() {
	
	$('.image-bar .thumbs .open-image-bar').click(function() {
		
		$(this).parent().parent().stop().animate({'marginLeft' : -125});
		$('.close-image-bar').show();
		
		var url = $(this).attr('href')+'/';
		var target = $(this);
		
		$.getJSON(url, {ajax : true, functionName: 'getImageDetails'}, function(json){
			
			target.parent().parent().find('.image-detail').empty();
			target.parent().parent().find('.image-detail').append(json.content);
			
		});	
		
		return false;
	});
	
	$('.close-image-bar').click(function() {
		$('.close-image-bar').hide();
		$('#camping-part-content .inactive-default-page-canvas .inactive-sub-page-canvas .image-bar').animate({'marginLeft' : 352});
		return false;
	}); 

}



function handleSubMenuClick(target, showSub) {
	
	if(showSub) {
		$('.sub-menu a').removeClass('active');
		$(target).addClass('active');
		
		$('.sub-sub').hide();
		$(showSub).show();
	}

	var url = $(target).attr('href')+'/';
	
	$.getJSON(url, {ajax : true, level: 2}, function(json){	
		
		
		$('#camping-part-content .inactive-default-page-canvas .active-sub-page-canvas ').empty();
		$('#camping-part-content .inactive-default-page-canvas .active-sub-page-canvas ').append(json.content);
		
		initBasicPage();
		initImageBar();
		
		$('#camping-part-content .inactive-default-page-canvas .active-sub-page-canvas ').stop().animate({'marginLeft' :0}, 500, function(){	
						
			$(this).removeClass('active-sub-page-canvas');
			$('#camping-part-content .inactive-default-page-canvas .inactive-sub-page-canvas').addClass('active-sub-page-canvas');
			$('#camping-part-content .inactive-default-page-canvas .inactive-sub-page-canvas').removeClass('inactive-sub-page-canvas');
			$(this).addClass('inactive-sub-page-canvas');			
			$('#camping-part-content .inactive-default-page-canvas .active-sub-page-canvas').css('margin-left', 480);
			
	
		});			
	});	
	
	return false;
}

function nextOrPreviousObjects(url) {
	
	$.getJSON(url, {ajax : true, functionName: 'getObjectsResultOnly'}, function(json) {	
		$('#object-result-holder').empty();
		$('#object-result-holder').append(json.content);
		initBasicPage();
	});
		
}

function switchSitePart(part) {
		
	switch(part) {
		case 0:
			
			$('#main-menu ul li a').removeClass('active');
			
			$('#camping-julianahoeve-btn').show();
			$('#mijn-julianahoeve-btn').hide();		
			
			$('#welkom-login').show();
			$('#welkom').hide();			
			
			var url = $('#mijn-julianahoeve-btn a').attr('href')+'/';
			
			$.getJSON(url, {ajax : true}, function(json){	
				$('#mijn-julianahoeve-part').empty();
				$('#mijn-julianahoeve-part').append(json.content);
				$('#container-canvas').stop().animate({'marginLeft' :((part * 720) * -1)}, 1000);
				$('#totem').stop().animate({'marginLeft' : 414}, 1000);
				initBasicPage();
				cufonIt();
			});

			break;
		case 1:
			
			$('#camping-julianahoeve-btn').hide();
			$('#mijn-julianahoeve-btn').show();
			
			$('#welkom-login').hide();
			$('#welkom').show();				
			
			$('#camping-part-content .inactive-default-page-canvas').css('margin-left', 640);			
								
			
			var url = $('#camping-julianahoeve-btn a').attr('href')+'/';
			
			$.getJSON(url, {ajax : true}, function(json){				
				$('#home-page').empty();
				$('#home-page').append(json.content);				
				$('#container-canvas').stop().animate({'marginLeft' :((part * 720) * -1)}, 1000, function(){
					$('#mijn-julianahoeve-part').empty();
				});
				$('#totem').stop().animate({'marginLeft' : 162}, 1000);
				cufonIt();
			});			
			break;
	}	
	
	return false;
}

function addGuest(url)
{
	
	$('#add-guest-error').hide();
	
	var firstName 	= $('#first_name').val();
	var lastName	= $('#last_name').val();
	var birthDay	= $('#birthday_guest').val();
	var city		= $('#city_guest').val();
	
	if(firstName.length < 1 || lastName.length < 2 || city.length < 2) {
		$('#add-guest-error').slideDown();
	} else {
		
	
		$.getJSON(url, 
				{
				ajax : true, functionname : 'addGuest', 
				first_name 	: firstName,
				last_name 	: lastName,
				birthday 	: birthDay,
				city 		: city
				}, function(json){
			$('#guests-overview-holder').empty();
			$('#guests-overview-holder').append(json.content);
			
			$('#first_name').val('');
			$('#last_name').val('');
			$('#birthday_guest').val('');
			$('#city_guest').val('');
			
		});
	
	}
}

function deleteGuest(url, key) {
	
	$.getJSON(url, 
			{
				ajax : true, functionname : 'deleteGuest', 
				key  : key
			}, function(json){
		$('#guests-overview-holder').empty();
		$('#guests-overview-holder').append(json.content);
	});
	
}

function showIncorrectForm() {
	
	$('#incorrect-form-holder').slideDown(500);
	$('#sigup-status-detail').slideUp(500);
	
}

function cufonIt() 
{
	Cufon.replace('.news-gothic a',{fontFamily : 'news-gothic' , hover: true});
	Cufon.replace('a.news-gothic',{fontFamily : 'news-gothic' , hover: true});
	Cufon.replace('.news-gothic',{fontFamily : 'news-gothic'});
	
	
	Cufon.replace('.dakota a',{fontFamily : 'dakota' , hover: true});
	Cufon.replace('a.dakota',{fontFamily : 'dakota' , hover: true});
	Cufon.replace('.dakota',{fontFamily : 'dakota'});
}

function cleanUrl(url) {
	return url.replace(baseUrl,'/');
}

function getCleanValue(value)
{
	if(value != undefined) {	
		return value.replace(/([\d.]+)(px|pt|em|%)/,'$1');
	} else {
		return 0;	
	}
}

function initializeContactMap() {   
	if($('#contact-map').length > 0) {
		var latlng = new google.maps.LatLng(51.7303268, 3.7527219);
	    var myOptions = {
	      zoom: 10,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    var map = new google.maps.Map(document.getElementById("contact-map"), myOptions);
	
	    var image = '/img/google-maps-icon.png';
	    var myLatLng = new google.maps.LatLng(51.7303268, 3.7527219);
	    var beachMarker = new google.maps.Marker({
	        position: myLatLng,
	        map: map,
	        icon: image
	    });
	}
}

function confirmLeave(text) {
	if (confirm(text)) {
		return true;
	} else {
		return false;
	}
} 




