(function($){
	$.fn.wpgallery = function() {
		var size = $('li', this).size()
		var position = 0;
		var $piclist = $('ul.piclist', this);
		var piclistWidth = $('li', this).size() * $('ul.piclist', this).width()
		var shift = $($piclist, this).width()
		var movingTime = 1000;
		var slideTime = 4000;
		
		$piclist.width(piclistWidth)
		
		var $next = $('.next', this)
		var $prev = $('.prev', this)
		var $play = $('.play', this)
		var $stop = $('.stop', this)
		
		
		function checkFirst(){
			if (position == 0){
				$prev.hide()
				return false
			} else {
				$prev.show()
				return true
			}
		}
		function checkLast(){
			if (position == -size+1){
				$next.hide()
				return false
			} else {
				$next.show()
				return true
			}
		}
		function checkBorders(){
			checkFirst()
			checkLast()
		}
		checkBorders()
		
		
		function forward(){
			$piclist.animate({
				left: (position-1)*shift + 'px'
			}, movingTime)
			position--;
			checkBorders()
		}
		function backward(){
			$piclist.animate({
				left: (position+1)*shift + 'px'
			}, movingTime)
			position++;
			checkBorders()
		}
		function rewind(){
			$piclist.animate({
				left: 0 + 'px'
			}, movingTime)
			position = 0;
			checkBorders()
		}
		
		
		$next.click(function(){
			forward()
		})
		
		$prev.click(function(){
			backward()
		})
		
		function autoSlide(){
			if (checkLast()) {
				forward()
			} else {
				rewind()
			}
		}
		
		$play.click(function(){
			autoSlide()
			interval = setInterval(autoSlide, slideTime)
			$play.hide()
			$stop.show()
		})
		$stop.click(function(){
			clearInterval(interval)
			$stop.hide()
			$play.show()
		})
		
	};
})(jQuery);

$(document).ready(function(){
	$('.gallery').wpgallery();

(function($){
	$collHeaders = $('.collapse h3');
	$('.collapse .table-type-1').wrap('<div class="coll-wrap"></div>')
	$collContents = $(".coll-wrap");
	$collContents.not(':first').hide(1000);


	$collHeaders.wrapInner('<span></span>')
	.children('span')
	.css({'borderBottom':'1px dashed #000','cursor':'pointer'})
	.click(function(){
		$(this).parent().next($collContents).slideToggle()
	});
	
})(jQuery);

})