// JavaScript Document

$(function() {
    sponsorenSlide();
	gallery();
	
	galleryHeight();
	memberCalendarDetail();
});



function gallery() {
	$('ul.gallery').after('<div id="gallery-container"></div>');
	$('ul.gallery').galleria({
			history   : false, // deactivates the history object for bookmarking, back-button etc.
			insert    : '#gallery-container', // the containing selector for our main image. 
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } 
					// don't fade out if the parent is active
				)
			}


	});
}

function galleryHeight() {
	counter = 0;
	$('.gallery li').each(function(){
		counter++;	
	});
		
	$('.gallery').height(Math.ceil(counter/8)*80);
}

function sponsorenSlide() {
	$('div.pics').cycle({ 
		fx:	'fade',
		timeout: 5000
 	});
}

function footer(){  
    $("#footer").positionFooter(true);  
} 

function galleryNav() {
	buttons  = '<a href="#" id="last_btn">vorige</a>';
	buttons += '<a href="#" id="next_btn">volgende</a>';
	$('.galleria_container').after(buttons);
	
	$('#last_btn').click(function() {
								  alert('test');
    	$.gallery.prev();
        return false;
	});
	
	$('#next_btn').click(function() {
    	$.gallery.next();
        return false;
	});

}

function memberCalendarDetail() {
	getCalendarDisplay();
	$('#members-calendar .calendar-event .calendar-details').hide();
	$('#members-calendar .calendar-event').each(function() {
		html  = '<div class="display-text">bekijk details</div>';
		html += '<div class="hide-text">sluit details</div>';

		$(this).find('h4').before(html);
	});
	$('#members-calendar .display-text').show();
	$('#members-calendar .hide-text').hide();
        

	$('#members-calendar .display-text').click(function(){
		$(this).parent().find('.calendar-details').show('fast');
		$(this).hide();
		$(this).parent().find('.hide-text').show();
	});
	$('#members-calendar .hide-text').click(function(){
		$(this).parent().find('.calendar-details').hide('fast');	
		$(this).hide();
		$(this).parent().find('.display-text').show();
	});
	$('#calendarlist-legend tr').click(function(){
		findClass = $(this).find('.legendblock').attr('class');
		findClass = findClass.replace('legendblock ','');
		if (findClass != 'leden-algemeen') {
			if ($(this).find('img').attr('src') == 'images/site/eye-active.png') {
				$(this).find('img').attr('src', 'images/site/eye-inactive.png')
				$('#members-calendar .'+findClass).hide('fast');
			}
			else if ($(this).find('img').attr('src') == 'images/site/eye-inactive.png') {
				$(this).find('img').attr('src', 'images/site/eye-active.png')
				$('#members-calendar .'+findClass).show('fast');
			}
		}
		saveCalendarDisplay();
	});
}

function saveCalendarDisplay() {
	postParams = 'display-calendar=true';
	$('#calendarlist-legend tr').each(function(){
		findClass = $(this).find('.legendblock').attr('class');
		findClass = findClass.replace('legendblock ','');
		if (findClass != 'leden-algemeen') {
			if ($(this).find('img').attr('src') == 'images/site/eye-active.png') {
				value = 'active';
			}
			else if ($(this).find('img').attr('src') == 'images/site/eye-inactive.png') {
				value = 'inactive';
			}
			postParams += '&'+ findClass +'='+ value;
		}
	});
	//save settings to session
	 $.ajax({
	   type: "POST",
	   url: "display_calendar.php",
	   data: postParams,
	   async: false
	 });
}

function getCalendarDisplay() {
	postParams = 'get-display-calendar=true';

	 $.ajax({
		type: "POST",
		url: "display_calendar.php",
		data: postParams,
		async: false,
		success: function(data){
			//alert( "Data Received: " + data );
			display_array = data.split('&');
			
			for(i=0; i<display_array.length; i++) {
				var tempArray =  new Array();
				tempArray = display_array[i].split('=');
				classVar = tempArray[0];
				img = 'images/site/eye-'+ tempArray[1] +'.png';
				
				$('#calendarlist-legend td.'+ classVar).parent().find('img').attr('src', img);
				if (tempArray[1] == 'inactive') {
					$('#members-calendar .'+ classVar).hide();
				}
			}
		}
	});
}
