/*
 * plugin:jquery.easyScrollGallery.js
 * version:2.0(2010.04.26)
 * requires:$ v1.3.2 later
 * author:Manabu Kushimoto(http://web-park.org)
 */

jQuery.noConflict();

window.onload = jQuery(function($){

	$.fn.easyScrollGallery = function(w,h,t){
		
		//* id/class名 *//
		var scrollGallery = {
			scrollImg:"#scrollImg",
			pagenation:".pagenation",
			up: ".up",
			down:".down"
		}
			
		//* スクロール画像のclass名 *//
		var active = {
			on:"onActive",
			pre:"preActive",
			nex:"nexActive"
		}
		
		//* スライドする領域の幅・高さを指定 *//
		var scrollGalleryW = $(scrollGallery.scrollImg).width();
		var scrollGalleryH = $(scrollGallery.scrollImg).height();
		var viewSize = { w:w? w:scrollGalleryW, h:h? h:scrollGalleryH }

		$(scrollGallery.scrollImg).css({ width:viewSize.w + "px", height:viewSize.h + "px", overflow:"hidden" });		

		//* 各スライド画像の設定 *//
		$("> *" , scrollGallery.scrollImg).each(function(i){
			//* スライド画像にclassを割り当てる *//
			$("> *:first-child" , scrollGallery.scrollImg).addClass(active.on);
			$("> *" , scrollGallery.scrollImg).not("*:first-child" , scrollGallery.scrollImg).addClass(active.nex);
			
			//* 初期状態では「下へ」を非表示にする *//
			if($("> *:first-child" , scrollGallery.scrollImg).attr("class").match(active.on)){
				$(scrollGallery.down).css({ display: "none" });
			}
			
			//* スライド画像の配置用変数 *//
			var imgW = $("> *" , scrollGallery.scrollImg).eq(i).width();
			var leftPosition = parseInt((viewSize.w/2 - imgW/2) + "px");
			var firstLeft = parseInt((viewSize.w/2 - $("> *:first-child" , scrollGallery.scrollImg).width()/2) + "px");
			var firstTop = parseInt((viewSize.h/2 - $("> *:first-child" , scrollGallery.scrollImg).height()/2) + "px");
			var downPosition = viewSize.h + "px";
			
			
			//* スライド画像の初期配置 *//
			$("."+active.nex).css({ top: downPosition });
			$("> *" , scrollGallery.scrollImg).eq(i).css({ left: leftPosition });
			$("> *:first-child" , scrollGallery.scrollImg).css({ top: firstTop });			
	
			//* スライドの速さ *//
			var speed = { t:t? t:300 }
			
			//* クラス削除のオブジェクト *//
			var removeOn = function(){
					$(this).removeClass(active.nex+" "+active.pre);
					$(this).addClass(active.on);
				}
	
			//* 「上へ」ボタンが押されたとき *//
			$(scrollGallery.up).click(function(){
				$(scrollGallery.down).css({ display: "block" });
				if($("> *:last-child" , scrollGallery.scrollImg).prev().attr("class").match(active.on)){
					$(scrollGallery.up).css({ display: "none" });
				}
				var upPosition = $("." + active.on).height();
				$("." + active.on).stop().animate({ top: -upPosition },speed.t,
				function(){
					$(this).removeClass(active.on);
					$(this).addClass(active.nex);
				});
				var prevOn = parseInt((viewSize.h/2 - $("." + active.on).next().height()/2) + "px");
				$("." + active.on).next().stop().animate({ top: prevOn },speed.t,removeOn);
				return false;
			});
			//* 「下へ」ボタンが押されたとき *//
			$(scrollGallery.down).click(function(){
				$(scrollGallery.up).css({ display: "block" });
				if($("> *:first-child" , scrollGallery.scrollImg).next().attr("class").match(active.on)){
					$(scrollGallery.down).css({ display: "none" });
				}
				$("." + active.on).stop().animate({ top: downPosition },speed.t,
				function(){
					$(this).removeClass(active.on)
					$(this).addClass(active.pre)
				});
				var downOn = parseInt((viewSize.h/2 - $("." + active.on).prev().height()/2) + "px");
				$("." + active.on).prev().stop().animate({ top: downOn },speed.t,removeOn);
				return false;
			});

		});

	};
});

