// data array of thumnails and full sized
var bgImages = [
	{ 
		'thumbnail': 'images/backgrounds/thumbnails/bg_thumb_wes5.jpg',
		'full': 'images/backgrounds/bg_full_wes5.jpg'	
	},
	{ 
		'thumbnail': 'images/backgrounds/thumbnails/bg_thumb_scoah_exterior.jpg',
		'full': 'images/backgrounds/bg_full_chicago.jpg'	
	},
	{ 
		'thumbnail': 'images/backgrounds/thumbnails/bg_thumb_mpls.jpg',
		'full': 'images/backgrounds/bg_full_mpls.jpg'	
	},
	{ 
		'thumbnail': 'images/backgrounds/thumbnails/bg_thumb_she.jpg',
		'full': 'images/backgrounds/bg_full_she.jpg'	
	},
	{ 
		'thumbnail': 'images/backgrounds/thumbnails/bg_thumb_lux.jpg',
		'full': 'images/backgrounds/bg_full_lux.jpg'	
	},
	{ 
		'thumbnail': 'images/backgrounds/thumbnails/bg_thumb_who.jpg',
		'full': 'images/backgrounds/bg_full_who.jpg'	
	}
];
var supersizedSlides = new Array();
var autoScrollerSpeed = 7000; // up from 4000
var loadedPhotos = 0;
var preloadTimer;


// add images to body as preloader
function addPreloaderPhotos(){
	//var imgHTML = '<div id="preloader" style="display: none;">';
	var imgHTML = '';
	for(var i=0;i<bgImages.length;i++){
		imgHTML += '<img src="'+bgImages[i].full+'" alt="preloadImage'+i+'" onload="photoLoaded('+i+');" />';
	}
	//imgHTML += '</div>';
	var newdiv = document.createElement('div');
	var divIdName = 'slideshowPreloader';
	newdiv.setAttribute('id',divIdName);
	newdiv.setAttribute('style','display: none;');
	newdiv.style.cssText = 'display: none;';
	newdiv.innerHTML = imgHTML;
	document.getElementById('container').appendChild(newdiv);
}

function photoLoaded(id){
	loadedPhotos++;
	//console.log("loaded "+loadedPhotos);
}


function nextSlide(){
	var $ = jQuery;
	if($){
		$.fn.nextslide();
		// selecte thumbnail
		var selected = $('#slideshowThumbs li.selected');
		selected.removeClass('selected');
		if($(selected).next().length > 0 ){
			$(selected).next().addClass('selected'); // select this thumbnail
		} else {
			$(selected).siblings('li:first').addClass('selected'); // select this thumbnail
		}
	}
}

function resetTimer(timer){
	clearInterval(timer);
	t = setInterval('nextSlide()',autoScrollerSpeed);
	return t;
}

function checkIfPreloaded(){
	//console.log('checking: '+loadedPhotos+' // '+bgImages.length);
	if(loadedPhotos == bgImages.length){
		// play slideshow
		initSlideshow();
		// remove timer
		clearInterval(preloadTimer);
	}
}


function initSlideshow(){
	// make navigation visible and highlight first thumbnail
	jQuery('#slideshowNav').show();
	jQuery('#slideshowThumbs li').first().addClass('selected');
	//automatically scroll slides
	var autoScroller = setInterval('nextSlide()',autoScrollerSpeed);
	// fade out text
	jQuery('#homepageHeadline').delay(5000).fadeOut(2600);
}



jQuery(document).ready(function($){
	//$.supersized({ slides	:  [ { image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.1/slides/quietchaos-kitty.jpg' } ]});
	
	// preload photos
	addPreloaderPhotos();
	
	// add preloader gif to background
	//$('#container').css('background','transparent url(images/preloader.gif) no-repeat 100px 200px');
	
	// loop through array and build thumbnails
	for(var i=0;i<bgImages.length;i++){
		var thumbHTML = '<li>';
		thumbHTML += '<a href="'+bgImages[i].full+'"><img src="'+bgImages[i].thumbnail+'" alt="'+bgImages[i].full+'" /></a>';
		thumbHTML += '</li>';
		$('#slideshowThumbs').append(thumbHTML);
		
		supersizedSlides.push( { 'image':bgImages[i].full } );
	}
	
	// set up supersized
	$.supersized({ 
			slides: supersizedSlides,
			//Functionality
			slideshow               :   0,		//Slideshow on/off
			autoplay				:	0,		//Slideshow starts playing automatically
			start_slide             :   1,		//Start slide (0 is random)
			random					: 	0,		//Randomize slide order (Ignores start slide)
			slide_interval          :   3000,	//Length between transitions
			transition              :   3, 		//0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
			transition_speed		:	1800,	//Speed of transition - up from 700
			new_window				:	1,		//Image links open in new window/tab
			pause_hover             :   0,		//Pause slideshow on hover
			keyboard_nav            :   0,		//Keyboard navigation on/off
			performance				:	0,		//0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
			image_protect			:	0,		//Disables image dragging and right click with Javascript
			image_path				:	'', //Default image path

			//Size & Position
			min_width		        :   0,		//Min width allowed (in pixels)
			min_height		        :   0,		//Min height allowed (in pixels)
			vertical_center         :   1,		//Vertically center background
			horizontal_center       :   1,		//Horizontally center background
			fit_portrait         	:   1,		//Portrait images will not exceed browser height
			fit_landscape			:   0,		//Landscape images will not exceed browser width
			
			//Components
			navigation              :   0,		//Slideshow controls on/off
			thumbnail_navigation    :   0,		//Thumbnail navigation
			slide_counter           :   0,		//Display slide numbers
			slide_captions          :   0		//Slide caption (Pull from "title" in slides array)
		});
	
	// set up thumbnails so click triggers supersized function
	$('#slideshowThumbs li a').click(function(){
		$.fn.gotoslide($(this).attr('href'));
		$.fn.resizenow(); // to fix broken sizing
		//$(this)..addClass('selected'); // deselect all thumbnails
		$(this).parent().addClass('selected').siblings().removeClass('selected'); // select this thumbnail
		
		// reset autoscroller
		if(typeof autoscroller !== "undefined"){
			autoScroller = resetTimer(autoScroller);
		}
		
		return false;
	});
	// set up arrows to go back and forth
	
	$('#slideshowNav a.nextButton').click(function(){
		$.fn.nextslide();
		//$.fn.resizenow(); // to fix broken sizing
		// selecte thumbnail
		var selected = $('#slideshowThumbs li.selected');
		selected.removeClass('selected');
		if($(selected).next().length > 0 ){
			$(selected).next().addClass('selected'); // select this thumbnail
		} else {
			$(selected).siblings('li:first').addClass('selected'); // select this thumbnail
		}
		
		// reset autoscroller
		if(typeof autoscroller !== "undefined"){
			autoScroller = resetTimer(autoScroller);
		}
		
		return false;
	});
	$('#slideshowNav a.previousButton').click(function(){
		$.fn.prevslide();
		//$.fn.resizenow(); // to fix broken sizing
		
		// select thumbnail
		var selected = $('#slideshowThumbs li.selected');
		selected.removeClass('selected');
		if($(selected).prev().length > 0 ){
			$(selected).prev().addClass('selected'); // select this thumbnail
		} else {	
			$(selected).siblings('li:last').addClass('selected'); // select this thumbnail
		}
		
		// reset autoscroller
		if(typeof autoscroller !== "undefined"){
			autoScroller = resetTimer(autoScroller);
		}
		
		return false;
	});

		
	
	// wait a while for images to load
	preloadTimer = setInterval('checkIfPreloaded()',1000); // every second
	//var slideShowDelayer = setTimeout('initSlideshow()',10000); // 10 seconds
	
	
});
