$(document).ready(function() {
	var slideMinitime = 800;	
	
	function resetWebElements(){
		if(reset){
		
			if(screenWidth < 1367){
				$('#sign').toggle(1000);
	    		$('#footer').toggle(1000);
	    		$('.special').toggle(1000);
	    		$('#sidepanel').toggle('fast');
	   			$('.infowindow').css('display','none');
			
			}
			else{
		
				$('#header').toggle('fast');
				$('#footer').toggle('fast');
				$('#sidepanel').toggle('fast');
				$('.special').toggle(1000);
				$('.infowindow').css('display','none');
		
				if(photoMod){
					$('#photoMod').toggle('fast');	
				}
			}
			reset = false;
		}
	
		else{
	
				if(screenWidth < 1367) {
					$('#sign').toggle(1000);
	    			$('#footer').toggle(1000);
	    			$('.special').toggle(1000);
	    			$('#sidepanel').toggle('fast');
	   				$('.infowindow').fadeIn(2000, function(){
						setTimeout(function(){ $(".infowindow").fadeOut() }, 6000)
					});
			
				}
			
				else{
				//alert('Let op! Alle elementen op deze website worden nu gesloten. Wanneer u nogmaals op het icoon klikt maakt u dit ongedaan.');
					$('#header').toggle('fast');
					$('#footer').toggle('fast');
					$('.special').toggle(1000);
		
					if(photoMod){
					$('#photoMod').toggle('fast');
					}	
		
					$('#sidepanel').toggle('fast');
					$('.infowindow').fadeIn(2000, function(){
						setTimeout(function(){ $(".infowindow").fadeOut() }, 6000)
					});
				}
		
					reset = true;
		}
	}
	
	// Check if header and footer is loaded on first entering
	enterWebsite();

	
	// Hover effect on Globing
	$("img.a").hover(
		function() {
			$(this).stop().animate({"opacity": "0"}, "fast");
		},
		function() {
			$(this).stop().animate({"opacity": "1"}, "fast");
	});
	
	//show map on clicks
	$("a.viewMap").fancybox({
		'width'				: '75%',
		'height'			: '75%',
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'onStart'			: resetWebElements,
		'onClosed'			: resetWebElements,
		'transitionOut'		: 'none',
		'type'				: 'iframe'	
	});
	
	$('.resetWeb').click(function() {
		resetWebElements();
	});
	
	function closeImpress(){
		$('#sidepanel').animate({marginLeft: '-400px'}, slideMinitime, function() {
		    
		});
	}
	
	function openPanel() {
		$('#sidepanel').animate({marginLeft: '0px'}, slideMinitime, function() {
			$('#sidepanel').attr('class','open');
		});
	}
	function closePanel() {
		$('#sidepanel').animate({marginLeft: '-400px'}, slideMinitime, function() {
		    $('#sidepanel').attr('class','closed');
		});
	}
		
	$('a.xtrig').click(function() {
		var s_href = $(this).attr('href');
		var i_id = s_href.substr(1,(s_href.length - 1));
		content_id = i_id;
		
		//Display sidebar 
	 	if($('#sidepanel').attr('class') == 'closed') {
	 		setTimeout ("setContent()", 0);
			openPanel(content_id);
			
			setMenu();
			setPhotoMod();
			photoMeta();
			
		} else {
			closePanel();
			setTimeout ("setContent()", slideMinitime);
			setBackground(i_id);
			openPanel(content_id);
			setMenu();
			
			setPhotoMod();
			photoMeta();
		}
	});
	
});


// Basic function outside the docready function

var content_id = 0;
var firstVisit = true;
var reset = false;
var photoMod = false;
var bgLoading;
var defaultBgMap;
var bgMap;
var smallScreen;
var screenWidth	 = $(window).width();

function setContent() {
	//Define id's
	//Display content in sidebar based on i_id
	$('.sidecontent').css('display','none');
	$('div[id="content_'+content_id+'"]').css('display','block');	
} 

function setBackground(i_id) {
	//Change background based on i_id
	checkScreenRes();
	if(!bgLoading){
		bgLoading = true;
		bgMap = 'main_bg/';
		$('#bgLoading').fadeIn('fast');
		$('#bg_bottom').css('background-image', $('#bg_top').css('background-image'));
 		$('#bg_top').animate({opacity: 0}, 'fast', function() { $(this).css({'background-image': 'url('+PHOTOALBUM+bgMap+screenMapMain+'bg_'+scrn+i_id+'.jpg)'}).animate({opacity: 1});});
 	
 		 bgLoading = false;
		$('#bgLoading').fadeOut('fast');	
 	}
 	else{
		return false;
 	}
 
 }
 
function getPhotosAjax(){
	$('#photoModthumbs').html('');

	$.ajax({
		type		: 'GET',
		cache		: false,
		url			: URL+'/pages/getPhotos.ajax.php?id='+content_id+'',
		success: function(data) {
			$('#photoModthumbs').html(data);
			
			$("#photoModthumbs").live('mouseenter',function() {
				connectFancybox();
			});
			
			/*$('a.ptrig').live('click', function() {
			
			var s_href = $(this).attr('href');
			//Define map id this is the project folder in slimcms for uploading the images
			var map = $(this).attr('id');
			
			var p_id = s_href.substr(1,(s_href.length - 1));
			var photo_id = p_id;
	
	 		 changePhoto(photo_id,map); 
			});*/
		}
	})
}

function setPhotoMod(){
	if(content_id > 15){
		if(!photoMod){
			//$('#photoMod').toggle(1000);
			$('#photoMod').css('display','block');
			photoMod = false;
			getPhotosAjax();
		}
		else{
			photoMod = false;
			$('#photoMod').css('display','none');
		}
	}
	else{
		$('#photoMod').css('display','none');
		photoMod = false;
	}
}



function setMenu(){
	//Default set menu text-decoration: none;
	
	$('li').find('a').css('text-decoration', 'none');
	
	switch(content_id){
		
		case '1':
			$('#one').css('text-decoration' , 'underline');

		break;
		
		case '2': case '7': case '8': case '9': case '10': case '11': case '12 ': case '13': case '14': case '36': case '37':
			$('#four').css('text-decoration','underline');
		break;
		
		case '16': case '17': case '18': case '21': case '22': case '23': case '24': case '30': case '31':
			$('#three').css('text-decoration','underline');
		break;
		
		case '20':
			$('#two').css('text-decoration','underline');
		break;
		
		case '4':
			$('#three').css('text-decoration','underline');
		break;
		
		case '5':
			$('#five').css('text-decoration','underline');

		break;
		
		case '41':
			$('#six').css('text-decoration','underline');

		break;
		case '47':
			$('#seven').css('text-decoration','underline');

		break;
		
		default:
			$('#three').css('text-decoration','none');
		break;
	}
		        
}
function changePhoto(photo_id,map) {
	//Photoalbum module
	//alert(photo_id+'-'+map);
	
	checkScreenRes();		
	if(!bgLoading){
		bgLoading = true;
		$('#bgLoading').fadeIn('fast'); 
		$('#bg_bottom').css('background-image', $('#bg_top').css('background-image')); 
		$('#bg_top').animate({opacity: 0}, 'fast', function() { $(this).css({'background-image': 'url('+PHOTOALBUM+map+screenMap+'/bg_'+scrn+photo_id+'.jpg)'}).animate({opacity: 1});
	
			bgLoading = false;
			$('#bgLoading').fadeOut('fast');
		});
	}else{
		//alert('Nog ff wachten, nog ff wachten pizza');
		//$('#bgLoading').fadeIn('fast');
		return false;
	}
}

function changePhotoBg(){
	
	$('a.ptrig').click(function() {
		var s_href = $(this).attr('href');
		//Define map id this is the project folder in slimcms for uploading the images
		var map = $(this).attr('id');
		
		var p_id = s_href.substr(1,(s_href.length - 1));
		var photo_id = p_id;

		changePhoto(photo_id,map);
		
	});	
}



function enterWebsite(){

	if(!firstVisit){
		
		return false;
	
	}
	
	if(screenWidth < 1367){
		$('#sign').fadeIn(1000);
	    $('#footer').fadeIn(1000);
	   	$('#bg_top').fadeIn(1000);
	   	$('.special').fadeIn(1000);
		firstVisit = false;
	}
	
	else{
	  
	  $('#header').animate({
	    top: '+=50',
	    height: 'toggle'
	  }, 1000, function() {
	    $('#footer').fadeIn(1000);
	     $('#bg_top').fadeIn(1000);
	     $('.special').toggle(1000);
	     //$('#bg_bottom').fadeIn(1000);
	    firstVisit = false;
	  });
	}
}

function photoMeta(){
	
	$('#photometa').html('');

	$.ajax({
		type		: 'GET',
		cache		: false,
		url			: URL+'/pages/getMetaphoto.php?id='+content_id+'',
		success: function(data) {
			$('#photometa').html(data);
		}
	})
	
}

function checkScreenRes(){
	var screenHeight = $(window).height();
	var screenWidth	 = $(window).width();
	
	if(screenWidth < 1280){
		//fill screen variabel with prefix
		 screenMap = '/sm';
		 screenMapMain = 'sm/';
		 scrn = 'sm';
		 smallModus = true;
	}
	else{
		//fill screen variabel with nothing for bigpicture
		 screenMap = '';
		 screenMapMain = '';
		 scrn = '';
	}
}

function connectFancybox() {
	//Fancybox
	var slideshow_rel;	
	$("a[rel=photoalbum]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) { 
			if(!rotatingInterval) { slideshow_rel = 'paused';} else { slideshow_rel = 'playing';}
			return '<span id="fancybox-title-over">Afbeelding ' + (currentIndex + 1) + ' / ' + currentArray.length + '<a class="slideshow" rel="'+slideshow_rel+'">&nbsp;</a></span>';
		},
		'onComplete'		: function() {  $("a.slideshow").click(function() { 
			if($(this).attr('rel') == 'paused') {
				$(this).css('background-image','url('+URL+'/includes/systems/fancybox/fancybox_slide_pause.png)');
				$(this).attr('rel','playing');
				toggleRotating(false);
			} else {
				$(this).css('background-image','url('+URL+'/includes/systems/fancybox/fancybox_slide_play.png)');
				$(this).attr('rel','paused');
				toggleRotating(true);
			}
		}); }
	});
}
