/* Gallery.js - Home Design Photo/Facade gallery */

$(document).ready(function() {
	if ($('#gallery').length > 0) {
		$('#gallery #large-image .large').hide();
		$('#gallery a').click(function(event){ toggleCategory($(this.parentElement)); event.preventDefault(); });
		$('#gallery .thumbs img').click(function(event) { loadImage($(this)); event.preventDefault(); });
		
		toggleCategory($('#gallery li:first'));
	}
});

function toggleCategory(elem) {	
	if (elem.hasClass('selected') === false) {
		var oldCat = $('#gallery li.selected');
		oldCat.removeClass('selected');
		elem.addClass('selected');
		$('.thumbs', oldCat).slideUp(1000);
		$('.thumbs', elem).slideDown(1000);
		loadImage($('#gallery li.selected .thumbs img:first'));
	}
}

function loadImage(thumb) {
	if (thumb.hasClass('selected') === false) {
		$('#gallery #large-image .loader').fadeIn('fast');
		$('#gallery .thumbs img.selected').removeClass('selected');
		thumb.addClass('selected');
		var img = $(document.createElement('img'));
		
		img.bind('load', function() { setImage(this); });
		img.attr('src', thumb.attr('src').replace('-thumb.jpg', '.jpg'));
	}
}

function setImage(img) {
	$('#gallery #large-image .large').fadeOut('slow', 
		function() { $('#gallery #large-image .large').attr('src', img.src).fadeIn('slow',
			function() { $('#gallery #large-image .loader').fadeOut('fast'); }); });
}

/* Utility to shuffle an array */
Array.prototype.shuffle = function() {
	var s = [];
	while (this.length) s.push(this.splice(Math.random() * this.length, 1));
	while (s.length) this.push(s.pop());
	return this;
}

/* Feature - home page */

var ImagesToRotate;
var crntList = 1;
var imageLoading;
var startup = true;

$(document).ready(function() {
	if (window.GalleryImage && window.DisplayImage) {
		// save the first item so that it can always be first
		var firstDisplayImage = DisplayImage.shift();
		ImagesToRotate = [ { List: DisplayImage.shuffle(), Index: -1 }, { List: GalleryImage.shuffle(), Index: -1 } ];
		// now add it back
		ImagesToRotate[0].List.unshift(firstDisplayImage);
		
		window.setTimeout(changeImages, 0);
	}
});

function changeImages() {
	crntList = crntList == 0 ? 1 : 0;
	var index = ImagesToRotate[crntList].Index;
	index++;
	if (index >= ImagesToRotate[crntList].List.length) {
		index = 0;
	}
	ImagesToRotate[crntList].Index = index;
	
	imageLoading = document.createElement('img');
	imageLoading.onload = featureImageLoaded;
	imageLoading.src = ImagesToRotate[crntList].List[index];
}

function featureImageLoaded() {

	if (crntList == 0) {
		$('#feature .displayhome').animate({ top: '350px' }, 1000, 'swing', function() {
			$('#feature .displayhome img').attr('src', imageLoading.src);
			$('#feature .displayhome').css({ top: '125px', left: '-360px' }).animate({ left: '20px' }, 1000, 'swing');
		});
	} else {
		$('#feature .photogallery').animate({ right: '-490px' }, 1000, 'swing', function() {
			$('#feature .photogallery').css({ top: '-300px', right: '0px'}).attr('src', imageLoading.src).animate({ top: '0px' }, 1000, 'swing');
		});
	}

	if (startup == true) {
		window.setTimeout(changeImages, 1200);
		startup = false;
	} else {
		window.setTimeout(changeImages, 5000);
	}
}

