	$(document).ready(function(){
		var galleryCount = -1;
		var curGallery = 0;
		var imageCount = -1;
		var curImage = 0;
		var duration = 1;
		var titleIndex = 0;
		var t;
		
		function initialize(){
			$('#rotation_images ul').each(function(){
				galleryCount++;
				imageCount = -1;
				$(this).find('li').each(function(){
					imageCount++;
				});
			});
			var galleryP = galleryCount+1;
			curGallery = Math.floor(Math.random() * galleryP);
			preload();
			t = window.setTimeout(play,8000);
		};
		function prevGallery(){
			window.clearTimeout(t);
			unloadGallery();
			if ( curGallery > 0 ) {
				curGallery = curGallery-1;
			} else {
				curGallery = galleryCount;
			}
			//loadGallery();
			return false;
		};
		function nextGallery(){
			window.clearTimeout(t);
			unloadGallery();
			if ( curGallery < galleryCount ) {
				curGallery++;
			} else {
				curGallery = 0;
			}
			//loadGallery();
			return false;
		};
		function prevImage(){
			window.clearTimeout(t);
			$('#rotation_images ul').eq(curGallery).find('li').eq(curImage).fadeOut('slow').removeClass('thumbOn');
			$('#rotation_thumbs li').eq(curImage).removeClass('thumbOn');
			if ( curImage > 0 ) {
				curImage = curImage-1
			} else {
				curImage = 4;
			}
			$('#rotation_images ul').eq(curGallery).find('li').eq(curImage).fadeIn('slow');
			$('#rotation_thumbs li').eq(curImage).addClass('thumbOn');
			return false;
		};
		function nextImage(){
			window.clearTimeout(t);
			$('#rotation_images ul').eq(curGallery).find('li').eq(curImage).fadeOut('slow').removeClass('thumbOn');
			$('#rotation_thumbs li').eq(curImage).removeClass('thumbOn');
			if ( curImage < 4 ) {
				curImage++;
			} else {
				curImage = 0;
			}
			$('#rotation_images ul').eq(curGallery).find('li').eq(curImage).fadeIn('slow');
			$('#rotation_thumbs li').eq(curImage).addClass('thumbOn');
			return false;
		};
		function thumbClick(){
			window.clearTimeout(t);
			$('#rotation_images ul').eq(curGallery).find('li').eq(curImage).fadeOut('slow').removeClass('thumbOn');
			$('#rotation_thumbs li').eq(curImage).removeClass('thumbOn');
			curImage = $('#rotation_thumbs li').index(this);
			$('#rotation_images ul').eq(curGallery).find('li').eq(curImage).fadeIn('slow');
			$('#rotation_thumbs li').eq(curImage).addClass('thumbOn');
		};
		function changeTitle(){
			$('#rotation_title').css('background','url('+$('#rotation_titleAndLink ul li').eq(curGallery).find('img').attr('src')+') no-repeat').fadeIn('slow',function(){
				$('#rotation_title_link').attr('href',$('#rotation_titleAndLink ul li').eq(curGallery).find('a').attr('href'));
			});
			
		};
		function changeBG(){
			$('#rotationWrap').css('background','url('+$('#rotation_projectBackgrounds ul li').eq(curGallery).find('img').attr('src')+') center top no-repeat').fadeIn('slow');
		};
		function unloadGallery(){
			$('#rotation_images ul').eq(curGallery).fadeOut('slow');
			$('#rotation_images ul').eq(curGallery).find('li').fadeOut('slow');
			$('#rotationWrap').fadeOut('slow');
			$('#rotation_thumbs li').eq(curImage).removeClass('thumbOn');
			$('#rotation_title').fadeOut('slow',function(){preload();});
		};
		function loadGallery(){
			$('#rotation_images ul').eq(curGallery).show();
			curImage = $('#rotation_images ul').eq(curGallery).find('li').index($('#rotation_images ul').eq(curGallery).find('.rotation_startPhoto').eq(0));
			$('#rotation_images ul').eq(curGallery).find('li').eq(curImage).fadeIn('slow',function(){
				
			});
			changeTitle();
			changeBG();
			$('#rotation_thumbs li').eq(curImage).addClass('thumbOn');
			$('#rotation_thumbs li').bind('click',thumbClick);
			$('#rotation_prevImage').bind('click',prevImage);
			$('#rotation_nextImage').bind('click',nextImage);
			$('#rotation_nextProject').bind('click',nextGallery);
			$('#rotation_prevProject').bind('click',prevGallery);	
		};
		function play(){
			nextGallery();
			t = window.setTimeout(play,7000);
		};
		function imagesLoaded(){ return true; };
		function preload(){
			for(var x=0;x<5;x++){
				var pic1 = new Image(); 
				pic1.onLoad = imagesLoaded();
				pic1.src = $('#rotation_images ul').eq(curGallery).find('li').eq(x).find('img').attr('src');
			};
			loadGallery();
		};
		initialize();
	});
