(function($){
	
	$(function(){
				// optional images array, if empty will use just the elements already in slideshow div.
		var delay = 4000; /* Delay between animations */
		var fade = 1800; /* Duration of fade in animation */
		var curLayer = 0; /* start from layer number */
		var zIndex = 1; /* z-index value. layers will use this value +/- 1 either side */
		var shownum = false; /* Show navigable image layer icons or not */
		// private variables do not change
		var timer = null;
		var activeIndex = 0;
		var icons = [];
		/**
		 * Get childNode elements to be cycled though. Initialize with lowest zindex and 0 opacity.
		 */
		var layers = $('li.widget-gallery div.gallery').children('dl').each(function(){
			$(this).css({zIndex: zIndex-1, opacity:0});
			$('a', this).click(function(){return false;});
		});
	
		/**
		 * If curlayer is rand
		 */
		activeIndex = curLayer = curLayer == 'rand'? Math.floor(Math.random()*layers.length) : 0;
		/**
		 * Show numbers
		 */
		if(shownum && layers.length>1){
			nums = $('<div class="slideshownumbers"/>').appendTo($('div.gallery'));
			nums.css({zIndex: layers.length+zIndex});
			layers.each(function(i,item){
				var num = $('<span/>').appendTo(nums).html(i+1);
				num.bind('click',function(){
					if(activeIndex == i) return;
					clearTimeout(timer);
					layers.css({'opacity':0,zIndex:zIndex-1});
					curLayer = activeIndex = i;
					fadeInLayer(i, 900);
				});
			});
			icons = $('span', nums);
			$(icons[activeIndex]).addClass('active');
		}
		/**
		 * Bring Layer to front and Fade in Animation with callback to cycleLayers.
		 */
		var fadeInLayer = function(i, speed){
			var speed = speed || fade;
			clearTimeout(timer);
			activeIndex = i;
			$(layers[i]).css({zIndex:zIndex+1}).animate({'opacity':1}, speed, cycleLayers);
			if(shownum && layers.length>1){
				$(icons).removeClass('active');
				$(icons[activeIndex]).addClass('active');
			}
		}
		/**
		 * Calculate next image, current image and previous image and layer zindex and opacity accordingly
		 */
		var cycleLayers = function(){
			var l = layers.length, c = curLayer;
			$(layers[c<=0?l-1:c-1]).css({'opacity':0, zIndex:zIndex-1});
			$(layers[c=curLayer=c>=l?0:c]).css({'opacity':1,zIndex:zIndex});
			timer = setTimeout(function(){ fadeInLayer(c+1==l?0:c+1); ++curLayer;}, delay);
		}
		/**
		 * Start the cycle layer process.
		 */
		if(layers.length > 1) fadeInLayer(curLayer,100); else layers.css('opacity',1);
		

		$('div.gallery dd').each(function(i,item){
			if($(item).text() != ''){
				$(item).parent('dl').find('a').attr('title', $(item).text() );
			}
		});
		
		
		/**
		 * Lightbox Images
		 */
		var pth = '/wp-content/themes/cladding-partners/images/';
		var lbopts = {
			fixedNavigation:true,
			imageLoading: pth+'lightbox-ico-loading.gif',
			imageBtnClose: pth+'lightbox-btn-close.gif',
			imageBtnPrev: pth+'lightbox-btn-prev.gif',
			imageBtnNext: pth+'lightbox-btn-next.gif',
			imageBlank: pth+'lightbox-blank.gif',
			txtImage: 'Afbeelding',
			txtOf: 'van'
		}
		
		if($('body.home').get(0)){
			$('#slideshowopener div.gallery a').lightBox(lbopts);
			$('#gallery-1 a').attr('href', $('#refereniteshomelink a').attr('href') ).click(function(){
				window.location.href = $(this).attr('href');
				return true;
			});
		}else{
			$('div.gallery a').lightBox(lbopts);
			$('a.lightbox').lightBox(lbopts);
		}		
		
		$('#openpostslideshow a, #slideshowopener a').click(function(){
			var link = $('div.gallery a', $(this).parent()).get(0);
			$(link).trigger('click');
		});
		$('#referentiepost div.post-gallery img').click(function(){
			var link = $('div.gallery a', $(this).parent().parent()).get(0);
			$(link).trigger('click');
		});
		
		/**
		 * Projects Slider
		 */
		var totalLists = 0;
		var curIndex = 0;
		var ulWidth = 324;
		$('#slideprojects ul').each(function(i, item){
			totalLists++;
			if($('li.current-menu-item', item).get(0)){
				slideProjects(i);
				curIndex = i;
			}
		});
		
		$('#moreprojects').click(function(){
			curIndex = (curIndex+1 == totalLists)? 0 : curIndex+1;
			slideProjects(curIndex);
			return false;
		});
		
		function slideProjects(i){
			$('#slideprojects').animate({'scrollLeft': (ulWidth*i)});
		}
		
		// set maxhieghts
		if( $('#pagemain').height() > 1500) $('body').css({height:'auto'});
	
	});

})(jQuery);
