//=============================== photogallery
var galleryObj = {
	 options:{
		 element : '',
		 autoplay: false,
		 autoplayTimeout : 0,
		 autoplayDelay : 5000,
		 animationTime : 2000
	},

	init: function(element, autoplay) {
		galleryObj.options.element = element;
		galleryObj.options.autoplay = autoplay;
		
		if(galleryObj.options.autoplayTimeout) clearTimeout(galleryObj.options.autoplayTimeout);
		
		gallery = $(galleryObj.options.element);
		
		if (gallery.children().length > 1) {
			
			gallery.children().not(':last').css('opacity', 0);
			
			if (galleryObj.options.autoplay) {
				galleryObj.options.autoplayTimeout = setTimeout('galleryObj.next(true)', galleryObj.options.autoplayDelay);

				$(galleryObj.options.element).hover(
					function(){
						clearTimeout(galleryObj.options.autoplayTimeout);
						galleryObj.options.autoplayTimeout = 0;
					},
					function(){
						clearTimeout(galleryObj.options.autoplayTimeout);
						galleryObj.options.autoplayTimeout = 0;
						galleryObj.options.autoplayTimeout = setTimeout('galleryObj.next(true)', galleryObj.options.autoplayDelay);
					}
				);
			}
		}
		
		$('#left.arrow', galleryObj.options.element).click(function() { galleryObj.prev() });
		$('#right.arrow', galleryObj.options.element).click(function() { galleryObj.next() });
	},
	
	
	prev: function() {
		if (!gallery.children().is(':animated')) {
			
			active = gallery.children(':first');
	
			active
				.remove()
				.css('opacity', 0)
				.insertAfter(gallery.children(':last'))
				.animate({opacity: 1}, galleryObj.options.animationTime, function() {
					$(this).siblings().css('opacity', 0);
				});
			
		}
	},
	
	
	next: function(startAutoplay) {
		if (!gallery.children().is(':animated')) {
				
			active = gallery.children(':last');
			
			active.prev().css('opacity', 1)
			
			active
				.animate({opacity: 0}, galleryObj.options.animationTime, function() {
					$(this).remove()
					$(this).insertBefore(gallery.children(':first'))
				});
		}
		if (galleryObj.options.autoplay && (startAutoplay==1)) {
			clearTimeout(galleryObj.options.autoplayTimeout);
			galleryObj.options.autoplayTimeout = 0;
			galleryObj.options.autoplayTimeout = setTimeout('galleryObj.next(true)', galleryObj.options.autoplayDelay);
		}
	}

}

