			
$(function(){
	//初期設定
	$("ul#carouselInner").css("width",179*$("#carouselInner li").size()+"px");//179*数
	$("ul#carouselInner li:last").prependTo("ul#carouselInner");//最後のカラムを先頭へ移動
	$("ul#carouselInner").css("margin-left","-179px");//-179px移動
	//戻るボタン
	$("#carouselPrev").click(function(){
		$("#carouselPrev,#carouselNext").hide();
		$("#carouselInner").animate({
		marginLeft:parseInt($("ul#carouselInner").css("margin-left"))+179+"px"
		},"slow","swing",
		function(){
				$("ul#carouselInner").css("margin-left","-179px");
				$("ul#carouselInner li:last").prependTo("ul#carouselInner");
				$("#carouselPrev,#carouselNext").show();
		});
	});
	//進むボタン
	$("#carouselNext").click(function(){
		$("#carouselPrev,#carouselNext").hide();
		$("ul#carouselInner").animate({
		marginLeft:parseInt($("ul#carouselInner").css("margin-left"))-179+"px"
		},"slow","swing",
		function(){
				$("ul#carouselInner").css("margin-left","-179px");
				$("ul#carouselInner li:first").appendTo("ul#carouselInner");
				$("#carouselPrev,#carouselNext").show();
		});
	});
	//タイマー
	var timerID = setInterval(function(){
		$("#carouselNext").click();
	},4000);
	
	$("#carouselPrev img,#carouselNext img").click(function(){
		clearInterval(timerID);
	});
});


(function($){

	$.fn.slideshow = function(options){

		var options = $.extend({
			timer: 5000,
			speed: 1000,
			changeSpd: 1000,
			random: false,
			loop: false
		}, options);
		
		return this.each(function(){
		
			// prepare elements
			var $unit = $(this);
			var $slide = $('img', $unit);
			var timerID;
			var links = $('a', $unit).length;
			var clicked = false;
			var slides = $slide.length;
			var switches = 1;

			// init
			function init(){
				slideSet();
				timerID = setInterval(function(){
					slideSwitch(options.speed);
				}, options.timer);
				
				$slide.bind('click', slideClick);
			}

			// set first image
			function slideSet(){
				$slide.removeClass('active');
				// set first image in random order, if random is true
				var $start = (options.random) ? Math.floor(Math.random() * $slide.length) : 0;
				$($slide[$start]).addClass('active');
			}
			
			// slideSwitch
			function slideSwitch(sp){
				
				//prevent from continuous click
				$slide.unbind('click');
				
				var $active = $('img.active', $unit);
				
				//for loop
				if(options.loop || switches < slides || clicked) {
					if ($active.length == 0) $active = $('img:last-child', $unit);
					switches++;
					clicked = false;
				} else {
					clearInterval(timerID);
					$slide.bind('click', slideClick);
					return;
				}
				
				//pull the images in the order they appear in the markup
				//var $next = ($active.next().length) ? $active.next() : $('img:first-child', $unit);
				if(links){
					var $next = ($active.parent("a").next().length) ? $active.parent("a").next().find('img') : $('img:first', $unit);
				} else {
					var $next = ($active.next().length) ? $active.next() : $('img:first-child', $unit);
				}
				
				//set z-index lower than "class=active"
				$active.addClass('last-active');
			
				//transition
				$next
					.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, sp, function(){
						$active.removeClass('active last-active');
						$slide.bind('click', slideClick);
					});
			}
			
			// slideClick Action
			function slideClick(){
				clearInterval(timerID);
				clicked = true;
				slideSwitch(options.changeSpd);
				timerID = setInterval(function(){
					slideSwitch(options.speed);
				}, options.timer);
			}
			
			// execute
			init();

		});
	};
})(jQuery);


