jQuery(document).ready(function($) {

//$(function() {

	// reveal the qualmark logo


	// define the slides
	var arr_slides = [
		"/wp-content/themes/raftabout/images/slides/slide-1.jpg", 
		"/wp-content/themes/raftabout/images/slides/slide-2.jpg", 
		"/wp-content/themes/raftabout/images/slides/slide-3.jpg",
		"/wp-content/themes/raftabout/images/slides/slide-4.jpg", 
		"/wp-content/themes/raftabout/images/slides/slide-5.jpg", 
		"/wp-content/themes/raftabout/images/slides/slide-6.jpg",
		"/wp-content/themes/raftabout/images/slides/slide-7.jpg", 
		"/wp-content/themes/raftabout/images/slides/slide-8.jpg", 
		"/wp-content/themes/raftabout/images/slides/slide-9.jpg"
	];

	var num_loaded = 0;
	var obj_slides = {};

	// activate the slideshow loading box
	$("#slideshow-container").append('<div id="slideshow-loading">Loading slideshow: 0/'+arr_slides.length+'</div>');

	// preload each slide
	$.each(arr_slides, function(key, value){

		// initialise obj_slides so images are stored in order, despite how they load
		obj_slides[value] = false;

		var img = new Image();

		$(img)
		.load(function () {
			obj_slides[value] = this;
			num_loaded++;

			// update progress
			$("#slideshow-loading").text("Loading slideshow: "+num_loaded+'/'+arr_slides.length);

			// if complete then insert slideshow
			if (num_loaded==arr_slides.length) insert_slideshow();
		})
		.attr('src', value);

	});

	function insert_slideshow() {

		//console.log('Insert Slideshow');

		$("#slideshow-container").append('<div id="slideshow"></div>');

		// append each of our slide images to slideshow
		$.each(obj_slides, function(key, img){	
			$("#slideshow").append(img);
		});

		$("#slideshow-loading").remove();

		$("#slideshow-placeholder").fadeOut(500, function() {

			// enable slideshow and nav
			$('#slideshow') 
			.before('<div id="slideshow-nav">') 
			.cycle({ 
				pause: true,
				speed:  1300, 
				timeout: 5000,
				pager:  '#slideshow-nav' 
			});

			$('#slideshow-nav a').each(function(){
				$(this).addClass("slide"+$(this).text());
			});

			$("#slideshow").fadeIn(1000);

		});

	}

});	