
	$(document).ready(function() {
		photoGallery.totalTimeDelay = 4000;
		photoGallery.autoPlay = false;
		photoGallery.playerid = '#photo-player';
		photoGallery.startGallery();

    	$('#slideshowslider').serialScroll({
    		items:'a.gotopicbtn.m0',
    		prev:'#slideshowslider-outer a.prevpics',
    		next:'#slideshowslider-outer a.nextpics',
    		offset:-2, //when scrolling to photo, stop __ before reaching it (from the left)
    		start:0, //as we are centering it, start at the 2nd
    		duration:400,
    		force:true,
    		stop:true,
    		lock:false,
    		cycle:false, //don't pull back once you reach the end
    		easing:'linear', //use this easing equation for a funny effect
    		jump: true //click on the images to scroll to them
    	});		
	});

	var photoGallery = {};

	photoGallery.startGallery = function() {
		photoGallery.timerDelay = "";
		photoGallery.paused = false;
		photoGallery.totalPhotos = 0;
		photoGallery.currentPhoto = 0;
		photoGallery.jumpToPhoto = -1;		
		photoGallery.player = $(photoGallery.playerid);
		photoGallery.images = $(photoGallery.playerid + ' div.photo-outer');
		photoGallery.autoPlay = (photoGallery.player.attr('title') == 'autoplay') ? true : false;
		photoGallery.totalPhotos = photoGallery.images.length;
				
		if (photoGallery.totalPhotos > 1)
		{
			photoGallery.currentPhoto = photoGallery.totalPhotos - 1;
		
			if (photoGallery.autoPlay)
				photoGallery.rotatePics();
			else
			{
				photoGallery.currentPhoto = 0;
				photoGallery.paused = true;
			}
			
			photoGallery.loadControls();
			photoGallery.loadGoToBtns();
			
			if (!photoGallery.autoPlay)
				photoGallery.highlightGoToBtn();
		}
	}

	photoGallery.reorderPhotos = function() {
		photoGallery.images.each(function (i) {
			$(this).css('zIndex',((photoGallery.totalPhotos - i) + photoGallery.currentPhoto) % photoGallery.totalPhotos);
		});
	}                     
	
	photoGallery.reclassifyPhotos = function() {
		var photocurr = photoGallery.currentPhoto;
		var photonext = (photocurr + 1) % photoGallery.totalPhotos;
		var photoprep = (photonext + 1) % photoGallery.totalPhotos;
		
		photoGallery.images.eq(photocurr).removeClass('showme').removeClass('highest');
		photoGallery.images.eq(photonext).addClass('highest');
		photoGallery.images.eq(photoprep).addClass('showme');
	}

	photoGallery.reclassifyPhotosReverse = function() {
		var photoprev = (photoGallery.currentPhoto + 1) % photoGallery.totalPhotos;
		var photocurr = (photoprev + 1) % photoGallery.totalPhotos;
		var photoprep = (photocurr + 1) % photoGallery.totalPhotos;

		photoGallery.images.eq(photoprep).removeClass('showme');
		photoGallery.images.eq(photoprev).addClass('showme');
		photoGallery.images.eq(photocurr).removeClass('highest');
	}
	
	photoGallery.rotatePics = function() {
		photoGallery.currentPhoto = photoGallery.currentPhoto % photoGallery.totalPhotos;
	
		photoGallery.images.eq(photoGallery.currentPhoto).stop().fadeOut( function() {
			photoGallery.reclassifyPhotos();														   

			$(this).show();
			photoGallery.currentPhoto++;
			
			if (photoGallery.paused != true)
				photoGallery.timerDelay = setTimeout(function() { photoGallery.rotatePics(); }, photoGallery.totalTimeDelay);	

			photoGallery.highlightGoToBtn();


			if ($(photoGallery.playerid + ' div.minipics_scroller a.current').attr('class') == 'gotopicbtn m0 current')		
			{
				if ((photoGallery.currentPhoto % photoGallery.totalPhotos) !== 0)
					$('#slideshowslider').trigger('next');
				else
					$('#slideshowslider').trigger('goto', [0]);
			}

		});		
	}

	photoGallery.rotatePicsReverse = function() {
		photoGallery.currentPhoto = photoGallery.currentPhoto % photoGallery.totalPhotos;

		var photoprev = (photoGallery.currentPhoto + 1) % photoGallery.totalPhotos;
		var photocurr = (photoprev + 1) % photoGallery.totalPhotos;

		photoGallery.reclassifyPhotosReverse();		


		photoGallery.images.eq(photocurr).stop().fadeOut( function() {

			photoGallery.images.eq(photoprev).addClass('highest');
			$(this).show();
			photoGallery.currentPhoto++;
			
			if (photoGallery.paused != true)
				photoGallery.timerDelay = setTimeout(function() { photoGallery.rotatePics(); }, photoGallery.totalTimeDelay);	

			if ($(photoGallery.playerid + ' div.minipics_scroller a.current').attr('class') == 'gotopicbtn m0 current')
			{
				if (photocurr !== 0)
					$('#slideshowslider').trigger('prev');
				else
					$('#slideshowslider').trigger('goto', [$(photoGallery.playerid + ' div.minipics_scroller a.gotopicbtn.m0').length]);
			}

			photoGallery.highlightGoToBtn(); 

		});		
	}

	photoGallery.rotatePicsRandom = function() {
		photoGallery.currentPhoto = photoGallery.currentPhoto % photoGallery.totalPhotos;
		
		var photocurr = photoGallery.currentPhoto;
		var photonext = (photocurr + 1) % photoGallery.totalPhotos;
		var photoprep = (photonext + 1) % photoGallery.totalPhotos;
		
		photoGallery.images.eq(photonext).removeClass('showme');
		photoGallery.images.eq(photoGallery.jumpToPhoto).addClass('showme');

		photoGallery.images.eq(photocurr).stop().fadeOut( function() {

			$(this).removeClass('highest').removeClass('showme');
			photoGallery.images.eq(photoGallery.jumpToPhoto).addClass('highest').addClass('showme');
			photoGallery.images.eq((photoGallery.jumpToPhoto + 1) % photoGallery.totalPhotos).addClass('showme');
			
			photoGallery.currentPhoto = photoGallery.jumpToPhoto;
			photoGallery.jumpToPhoto = -1;

			$(this).show();
			
			if (photoGallery.paused != true)
				photoGallery.timerDelay = setTimeout(function() { photoGallery.rotatePics(); }, photoGallery.totalTimeDelay);	

			photoGallery.highlightGoToBtn(); 
		});		
	}
	
	photoGallery.loadControls = function() {
		$('<div class="controls"><a class="nextbtn" title="Next" href="#"></a><a class="playstopbtn" title="Play/Pause" href="#"></a><a class="prevbtn" title="Previous" href="#"></a></div>').appendTo(photoGallery.playerid);
		photoGallery.setPlayStopBtn();
		photoGallery.setPrevBtn();
		photoGallery.setNextBtn();
		
		if (!photoGallery.autoPlay)
			$(photoGallery.playerid + '  a.playstopbtn').addClass('paused');
	}

	photoGallery.loadGoToBtns = function() {
        var picWidth = 18;	        
        var screenWidth = 116;
        var totalPics = photoGallery.images.length;
		var scrollerWidth = totalPics * picWidth;
		var rExpClass = /full/g;

		if (photoGallery.player.attr('class').search(rExpClass) > 0)
			screenWidth = 300;

		var picsPerScroll = Math.floor(screenWidth / picWidth);
        var totalScreens = Math.ceil(totalPics / picsPerScroll);
        var hideScrollerTxt = "";

        hideScrollerTxt = (totalScreens == 1) ?  " hidescroller" : "";
        //alert('picwidth: ' + picWidth + '\n' + 'screenWidth: ' + screenWidth + '\n' + 'totalPics: ' + totalPics + '\n' + 'scrollerWidth: ' + scrollerWidth + '\n' + 'picsPerScroll: ' + picsPerScroll + '\n' + 'totalScreens: ' + totalScreens + '\n'); 
        
		$('<div id="slideshowslider-outer" class="minipics' + hideScrollerTxt + '"><a href="#" title="&#171; move slider back" class="prevpics">&#171;</a><div id="slideshowslider" class="minipics-inner"><div class="minipics_scroller" style="width:' + scrollerWidth + 'px"></div></div><a href="#" title="move slider forward &#187;" class="nextpics">&#187;</a></div>').appendTo(photoGallery.playerid);

		photoGallery.images.each(function (i) {
			$('<a class="gotopicbtn m' + (i%picsPerScroll) + '" href="javascript:{}" name="' + i + '" title="Go to Pic #' + (i+1) + '"></a>').appendTo(photoGallery.playerid + ' .minipics_scroller');
		});			

		photoGallery.setGoToBtns();					
	}

	photoGallery.setPlayStopBtn = function() {
		$(photoGallery.playerid + '  a.playstopbtn').click( function () {
			if (photoGallery.timerDelay)
			{			
				clearTimeout( photoGallery.timerDelay );
				photoGallery.timerDelay = 0;	
				photoGallery.paused = true;				
				$(this).addClass('paused');				
			}
			else	
			{
				photoGallery.paused = false;
				photoGallery.timerDelay = setTimeout(function() { photoGallery.rotatePics(); }, photoGallery.totalTimeDelay);			
				$(this).removeClass('paused');				
			}

			return false;
		}); 	
	}

	photoGallery.setPrevBtn = function() {
		$(photoGallery.playerid + ' a.prevbtn').click( function () {
			clearTimeout( photoGallery.timerDelay );
			
			if (photoGallery.currentPhoto > 1)
				photoGallery.currentPhoto = (photoGallery.currentPhoto - 2) % photoGallery.totalPhotos;		
			else
				photoGallery.currentPhoto = (photoGallery.totalPhotos - 1) % photoGallery.totalPhotos;		

			photoGallery.rotatePicsReverse();
			
			return false;
		});	
	}
	
	photoGallery.setNextBtn = function() {
		$(photoGallery.playerid + ' a.nextbtn').click( function () {
			clearTimeout( photoGallery.timerDelay );	
			photoGallery.rotatePics();

			return false;
		}); 	
	}
	
	photoGallery.setGoToBtns = function() {
		$(photoGallery.playerid + ' a.gotopicbtn').click( function () {
			var goToPicNum = $(this).attr('name');
			clearTimeout( photoGallery.timerDelay );						
				
			if (goToPicNum >= 0)
				photoGallery.jumpToPhoto = (goToPicNum) % photoGallery.totalPhotos;		
			else
				photoGallery.jumpToPhoto = (photoGallery.totalPhotos) % photoGallery.totalPhotos;				

			photoGallery.rotatePicsRandom();
			
			return false;
		}); 	
	}
	
	photoGallery.highlightGoToBtn = function() {
		var currPic = photoGallery.currentPhoto % photoGallery.totalPhotos;
		$(photoGallery.playerid + ' .minipics_scroller a.current').removeClass('current');
		$(photoGallery.playerid + ' .minipics_scroller a').eq(currPic).addClass('current');
	}
	
	photoGallery.checkPlayBtn = function() {
		$(photoGallery.playerid + ' a.playstopbtn').addClass('paused');				
	}
