//var cloud1Position = -250;
//var cloud2Position = -250;
//var cloud1Speed = Math.random()+.5;
//var cloud2Speed = Math.random()+.5;
//var maxWidth = $('body').width();

$('document').ready( function(){
	
	// Gallery Navigation
	var galleryBtns = $('span.itemBtn');
	
	var sections = $('article');
	var galleryNumber = 0;
	
	
	$(galleryBtns).click( function () {
		galleryNumber = $(galleryBtns).index(this);
		
		$(galleryBtns).removeClass('itemBtnSelected');
		$(this).addClass('itemBtnSelected');
		
		// hide and show the section you clicked on
		$(sections).hide();
		$(sections[galleryNumber]).show();
	});
	
	
	$(galleryBtns).hover( function () {
		$('span.title').html("Click to view: <strong>" + $(this).html() + "</strong>");
	}, function () {
		$('span.title').html("");
	});
	
	$('span.nextArrow').hover( function () {
		var allGalleries = $('span.itemBtn');
		
		var currentGallery = $(allGalleries).index($('span.itemBtnSelected'))+1;
		
		if(currentGallery < $(allGalleries).length){
			$('span.title').html("Click to view: <strong>" + $(allGalleries[currentGallery]).html() + "</strong>");
		} else {
			$('span.title').html("Click to view: <strong>" + $(allGalleries[0]).html() + "</strong>");
		}
	}, function () {
		$('span.title').html("");
	});
	
	$('span.nextArrow').click( function () {
		var galleryLength = $(galleryBtns).length - 1;
		
		if(galleryNumber < galleryLength){
			galleryNumber++;
		} else {
			galleryNumber = 0;
		}
		
		$(galleryBtns).removeClass('itemBtnSelected');
		$(galleryBtns[galleryNumber]).addClass('itemBtnSelected');
		
		// hide and show the section you clicked on
		$(sections).hide();
		$(sections[galleryNumber]).show();
		
		var allGalleries = $('span.itemBtn');
		
		var currentGallery = $(allGalleries).index($('span.itemBtnSelected'))+1;
		
		if(currentGallery < $(allGalleries).length){
			$('span.title').html("Click to view: <strong>" + $(allGalleries[currentGallery]).html() + "</strong>");
		} else {
			$('span.title').html("Click to view: <strong>" + $(allGalleries[0]).html() + "</strong>");
		}
	});
	
	$('span.prevArrow').hover( function () {
		var allGalleries = $('span.itemBtn');
		
		var currentGallery = $(allGalleries).index($('span.itemBtnSelected'))-1;

		if(currentGallery >= 0){
			$('span.title').html("Click to view: <strong>" + $(allGalleries[currentGallery]).html() + "</strong>");
		} else {
			$('span.title').html("Click to view: <strong>" + $(allGalleries[$(allGalleries).length-1]).html() + "</strong>");
		}
	}, function () {
		$('span.title').html("");
	});
	
	$('span.prevArrow').click( function () {
		var galleryLength = $(galleryBtns).length - 1;
		
		if(galleryNumber > 0){
			galleryNumber--;
		} else {
			galleryNumber = galleryLength;
		}
		
		$(galleryBtns).removeClass('itemBtnSelected');
		$(galleryBtns[galleryNumber]).addClass('itemBtnSelected');
		
		// hide and show the section you clicked on
		$(sections).hide();
		$(sections[galleryNumber]).show();
		
		
		var allGalleries = $('span.itemBtn');
		
		var currentGallery = $(allGalleries).index($('span.itemBtnSelected'))-1;
		
		if(currentGallery >= 0){
			$('span.title').html("Click to view: <strong>" + $(allGalleries[currentGallery]).html() + "</strong>");	
		} else {
			$('span.title').html("Click to view: <strong>" + $(allGalleries[$(allGalleries).length-1]).html() + "</strong>");
		}
	});
	
	$('body.webdesign div.thumbnails img').click( function() {
		var mainImages = $('img.mainImage');
		var thumbnailContainer = $('div.thumbnails');
		var sectionThumbnails = $(thumbnailContainer[galleryNumber]).children();
		
		$(sectionThumbnails).removeClass('selected');
		$(this).addClass('selected');
		
		//galleryNumber+1;
		var thumbPosition = $(sectionThumbnails).index(this);
		var customNumber = $(this).parent().parent().attr('imageNumber');
		
		$(mainImages[galleryNumber]).hide();
		$('div.loader').show();
		$(mainImages[galleryNumber]).load( function(){
			$('div.loader').hide();
			$(mainImages[galleryNumber]).show();											
		}).attr('src', 'images/websites/website'+(parseInt(customNumber))+'-Lg-'+(parseInt(thumbPosition)+1)+'.jpg');
		
		$(mainImages[galleryNumber]).attr('alt', $(this).attr('alt'))
		$(mainImages[galleryNumber]).attr('title', $(this).attr('title'));
	});
	
	$('body.printGallery div.thumbnails img').click( function() {
		var mainImages = $('img.mainImage');
		var thumbnailContainer = $('div.thumbnails');
		var sectionThumbnails = $(thumbnailContainer[galleryNumber]).children();
		
		$(sectionThumbnails).removeClass('selected');
		$(this).addClass('selected');
		
		//galleryNumber+1;
		var thumbPosition = $(sectionThumbnails).index(this);
		
		$(mainImages[galleryNumber]).hide();
		$('div.loader').show();
		$(mainImages[galleryNumber]).load( function(){
			$('div.loader').hide();
			$(mainImages[galleryNumber]).show();											
		}).attr('src', 'images/print/gallery'+(parseInt(galleryNumber)+1)+'-Lg-'+(parseInt(thumbPosition)+1)+'.jpg');
		
		$(mainImages[galleryNumber]).attr('alt', $(this).attr('alt'))
		$(mainImages[galleryNumber]).attr('title', $(this).attr('title'));
	});
	
	$('body.photoGallery div.thumbnails img').click( function() {
		var mainImages = $('img.mainImage');
		var thumbnailContainer = $('div.thumbnails');
		var sectionThumbnails = $(thumbnailContainer[galleryNumber]).children('span').children('img');
		
		$(sectionThumbnails).parent().removeClass('selected');
		$(this).parent().addClass('selected');
		
		//galleryNumber+1;
		var thumbPosition = $(sectionThumbnails).index(this);
		
		$(mainImages[galleryNumber]).hide();
		$('div.loader').show();
		$(mainImages[galleryNumber]).load( function(){
			$('div.loader').hide();
			$(mainImages[galleryNumber]).show();											
		}).attr('src', 'images/photography/gallery'+(parseInt(galleryNumber)+1)+'-Lg-'+(parseInt(thumbPosition)+1)+'.jpg');
		
		$(mainImages[galleryNumber]).attr('alt', $(this).attr('alt'))
		$(mainImages[galleryNumber]).attr('title', $(this).attr('title'));
	});
	

	// function to move clouds
	//function moveClouds(){
	//	if(cloud1Position <= -250){
	//		cloud1Speed = Math.random()+.5;
	//		cloud1Position = maxWidth;
	//	} else {
	//		cloud1Position -= cloud1Speed;
	//		$('div.test').css('left', Math.round(cloud1Position));
	//	}
	//	if(cloud2Position <= -250){
	//		cloud2Speed = Math.random()+.5;
	//		cloud2Position = maxWidth;
	//	} else {
	//		cloud2Position -= cloud2Speed;
	//		$('div.test2').css('left', Math.round(cloud2Position));
	//	}
	//}
	
	// create a loop to watch and move elements
	//window.setInterval(moveClouds, 30);
});