$(document).ready(
	function()
	{
		doImages();
		doAgenda();
		
        $('.lightbox').lightBox(
        	{
			imageLoading:			'/jsf/images/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			imageBtnPrev:			'/jsf/images/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			imageBtnNext:			'/jsf/images/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			imageBtnClose:			'/jsf/images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			imageBlank:				'/jsf/images/lightbox-blank.gif'
        	
        	}
        );     

		if ($("body").hasClass("snow"))
		{        
        	$(document).snowfall({flakeCount: 500});
        }

	}
);

function doImages()
{
	$("img[align=left]").addClass('imgleft');
	$("img[align=right]").addClass('imgright');
}

function doAgenda()
{
	$(".datepickeragenda").datepicker({
		'beforeShowDay': datepickerAgenda,
		'onSelect': 
			function(selectedDate, inst) 
			{ 
				var instance = $(this).data("datepicker");
				var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);

				var iMaand = date.getMonth() < 9 ? "0"+(date.getMonth()+1) : (date.getMonth()+1);
				var iDag = date.getDate() < 10 ? "0"+date.getDate() : date.getDate();
				var sDatum = date.getFullYear()+''+iMaand+''+iDag;
				
				$.get("/agenda/?datum="+sDatum,
						function(data)
						{
							$("#content-main").replaceWith(data);		
						}
				);
			}
	});
}

function datepickerAgenda(date) 
{
	for (i = 0; i < bezet.length; i++) 
	{
		if (date.getMonth() == bezet[i][0]-1 && date.getDate() == bezet[i][1] && date.getFullYear() == bezet[i][2]) 
		{
			return [true, 'bezet'];
		}
	}

	return [false, ''];
}






$.fn.ccgallery = function(options)
{
	
    var defaults = {
						modalID:		'modal-wrap',
						modalWidth:		600,
						shadowColor:	"#000000",
						shadowOpacity:	0.6
						
	};
	
	var opts = $.extend(defaults, options);
    
	return this.each(
		function()
		{
			var $this = $(this);
            
			$.fn.ccgallery.bindModal(opts, $this);			
			
					
			$this.find("li a").click(
				function()
				{					
					$.fn.ccgallery.showShadow(opts);
					
					$(this).parent().parent().find("li.active").removeClass("active");
					$(this).parent().addClass("active");
					
					$("body div#"+opts.modalID+" img.large").attr("src", $(this).attr("href")).load(
							function()
							{
								$.fn.ccgallery.showModal(opts);
							}
					);
					
					return false;
				}
			);
		}
	);
}

$.fn.ccgallery.showShadow = function(opts)
{
	if ($("body div#ccSHADOW").length == 0)
	{
		var shadow = $("<div></div>").attr("id", "ccSHADOW")
										.css({
												background: opts.shadowColor,
												opacity: opts.shadowOpacity,
												position: "absolute",
												left: "0",
												top: "0",
												'z-index': 900
										});
		
		var iWidth = $("body").width();
		var iHeight = $(document).height();
		shadow.width(iWidth+"px");
		shadow.height(iHeight+"px");
		$("body").append(shadow);
	}
	else
	{
		var shadow = $("body div#ccSHADOW");
	}	
	
	shadow.show();
}

$.fn.ccgallery.showModal = function(opts)
{
	var modal = $("#"+opts.modalID);
	
	modal.css({
					"z-index":	1000,
					background:	"#FFFFFF"
//					position: "absolute"         WEGGEHAALD, MOEST VAN JURIEN.
			});
	
	$.fn.ccgallery.repositionModal(opts);
	modal.show();
}

$.fn.ccgallery.repositionModal = function(opts)
{
    var modal = $("#"+opts.modalID);
    var modalImgWidth = $("#"+opts.modalID + " img").attr('width');  
    	
	var left = (($("body").width()-opts.modalWidth)/2) + 100;
	var top = 150;//($(document).height()-modal.height())/2;
	
	top = $(window).scrollTop() + 150;

	
	modal.css({
				left: left+"px",
	//			top: top+"px",
                width: modalImgWidth +"px"
	});
    
    
}

$.fn.ccgallery.bindModal = function(opts, oBlock)
{
	$(window).scroll(
			function ()
			{
				$.fn.ccgallery.repositionModal(opts);
			}
	);
	
	var modal = $("#"+opts.modalID);
	
	modal.hide();
	
	modal.find(".next").click(
			function()
			{
				$.fn.ccgallery.doNext(opts, oBlock);
				return false;
			}
	);
	
	modal.find(".prev").click(
			function()
			{
				$.fn.ccgallery.doPrevious(opts, oBlock);
				return false;
			}
	);
	
	modal.find(".close").click(
			function()
			{
				$.fn.ccgallery.doClose(opts);
				return false;
			}
	);
	
	modal.hide(); 
}

$.fn.ccgallery.doClose = function(opts)
{
	$("body div#ccSHADOW").fadeOut();
	$("body div#"+opts.modalID).fadeOut();
}

$.fn.ccgallery.doNext = function(opts, oBlock)
{
	var next = oBlock.find("li.active + li");
	
	if (next.length == 0)
	{
		next = oBlock.find("li:first");
	}
	
	oBlock.find("li.active").removeClass("active");
	next.addClass("active");
	
	$("body div#"+opts.modalID+" img.large").attr("src", next.find("a").attr("href"));
	
	$.fn.ccgallery.repositionModal(opts);
}

$.fn.ccgallery.doPrevious = function(opts, oBlock)
{
	var next = oBlock.find("li.active").prev();
	
	if (next.length == 0)
	{
		next = oBlock.find("li:last");
	}
	
	oBlock.find("li.active").removeClass("active");
	next.addClass("active");
	
	$("body div#"+opts.modalID+" img.large").attr("src", next.find("a").attr("href"));
	
	$.fn.ccgallery.repositionModal(opts);
}
