function isEven(value) {
	if (value % 2 === 0) {
		return 0;
	} else {
		return 1;
	}
}

$(document).ready(function () {
	/* Grid show/hide
	-------------------------------------------------*/
	$('a#show-grid').toggle(function () {
		$('#grid').show();
		return false;
	}, function () {
		$('#grid').fadeOut(250);
		return false;
	});
	/* Intro Page
	------------------------------------------------- */
	var introImages	= $('#intro-images'),
		startingDelay = 1000,
		slideInitialDelay = 400,
		slideFadeSpeed = 1500,
		slideOffset = 2000,
		slideDurationShown = 2500,
		no_images = introImages.children().size(),
		x_spacing = 350,
		x_spread = 400,
		y_spread = 300,
		y_offset = 200,
		x_offset = 50,
		x_random = Math.random() * x_spread - (x_spread / 2),
		y_random = Math.random() * y_spread;
	function setIntoImages() {
		
		
		var count = 0,
			size = introImages.children().size();
		introImages.children().each(
			function () {
				// give this image a id marker
				$(this).attr('marker', count + 1);
				// create x and y
				var x = x_random + x_offset,
					y = 0;
				if (isEven(count) == 1) {
					y = (y_random / 3) + y_offset;
				} else {
					y = y_random + y_offset;
				}
				if ($(this).css('opacity') == 0) {
					$(this).css({'top' : y, 'left' : x});
				}
				// reset randomness
				x_random = Math.random() * x_spread;
				y_random = Math.random() * y_spread;
				// creat new count for grouping 2 images
				var newCount = ((count - isEven(count)) / 2),
				// is this the second slide of 2?
					offset = 0;
				if (isEven(count) == 1) {
					offset = slideOffset;
				}
				if (newCount !== 0) {
					startingDelay = 0;
				}
				// fade the image in
				$(this).delay(offset +  startingDelay + ((count - isEven(count)) / 2) * (slideInitialDelay + (slideFadeSpeed * 2) + slideDurationShown)).animate({
				    opacity: 1
				}, slideFadeSpeed, function () {
				    // Animation complete.					
					// fade slide out
					$(this).delay(slideDurationShown).animate({
						opacity: 0
						}, slideFadeSpeed , function () {
						// repeat function if on final slide
						if ($(this).attr('marker') == size) {
							setIntoImages();
						}
					});
				  }
				);
				
				count = count + 1;
			}
		);
	}
	
	introImages.children().click(function () {
		window.location.replace("/showcase");
	});
	

	setIntoImages();

	/* Project slideshow
	 ------------------------------------------------- */
	var slideshow	= $('.slideshow');
	
	$('.slideshow').cycle({		
		fx: 'scrollHorz',
		easing: 'easeInOutCubic',
		next: '#next',
		prev: '#previous',
		speed: 1750,
		before: function(currSlideElement, nextSlideElement) {
			// set number links
			var slideIndex = $(nextSlideElement).index() + 1;
			setNumberLinks(slideIndex);
		}
	});

	//slideshow.cycle('pause');

	function setNumberLinks(id) {
		var count = 1
		$('#numbers').children().each(
		    function(){
				$(this).removeClass("selected");
				var numId = $(this).attr("id");				
				if (numId == id) {
					$(this).addClass("selected");
				}
				count = count + 1;
		    }
		);	
	}

	$('.nums').click(function () {
		var index = parseFloat($(this).attr("id")) - 1;
		slideshow.cycle(index);
	});
	
	$('#map-link').click(function () {
		window.location.replace("http://maps.google.co.uk/maps?q=hamngatan+12+vaxholm&um=1&ie=UTF-8&hq=&hnear=0x465f86be692c4039:0xfee37aabee8069f0,Hamngatan+12,+185+32+Vaxholm,+Sweden&gl=uk&ei=8jWcTvz3JcOp8APr4pjfBQ&sa=X&oi=geocode_result&ct=title&resnum=1&ved=0CBwQ8gEwAA");
	});


});








































