// JavaScript Document

/*
 * plugin:jquery.easySlideGallery.js
 * version:1.0(2010.04.16)
 * requires:jQuery v1.3.2 later
 * author:Manabu Kushimoto(http://web-park.org)
 */

jQuery.noConflict();

window.onload = jQuery(function($){

	$.fn.easySlideGallery = function(w,h,t){
		
		//* id名 *//
		var gallery = {
			slideImg:"#slideImg",
			pagenation:"#pagenation",
			previous: "#previous",
			next:"#next"
		}
			
		//* スライド画像のclass名 *//
		var active = {
			on:"onActive",
			pre:"preActive",
			nex:"nexActive"
		}
		
		//* スライドする領域の幅・高さを指定 *//
		var galleryW = $(gallery.slideImg).width();
		var galleryH = $(gallery.slideImg).height();
		var viewSize = { w:w? w:galleryW, h:h? h:galleryH }

		$(gallery.slideImg).css({ width:viewSize.w + "px", height:viewSize.h + "px", overflow:"hidden" });		

		//* 各スライド画像の設定 *//
		$("> *" , gallery.slideImg).each(function(i){
			//* スライド画像にclassを割り当てる *//
			$("> *:first-child" , gallery.slideImg).addClass(active.on);
			$("> *" , gallery.slideImg).not("*:first-child" , gallery.slideImg).addClass(active.nex);
			
			//* 初期状態では「前へ」を非表示にする *//
			if($("> *:first-child" , gallery.slideImg).attr("class").match(active.on)){
				$(gallery.previous).css({ display: "none" });
			}
			
			//* スライド画像の配置用変数 *//
			var imgH = $("> *" , gallery.slideImg).eq(i).height();
			var topPosition = parseInt((viewSize.h/2 - imgH/2) + "px");
			var firstLeft = parseInt((viewSize.w/2 - $("> *:first-child" , gallery.slideImg).width()/2) + "px");
			var nexPosition = viewSize.w + "px";
			
			//* スライド画像の初期配置 *//
			$("."+active.nex).css({ left: nexPosition });
			$("> *" , gallery.slideImg).eq(i).css({ top: topPosition });
			$("> *:first-child" , gallery.slideImg).css({ left: firstLeft });			
	
			//* スライドの速さ *//
			var speed = { t:t? t:300 }
			
			//* クラス削除のオブジェクト *//
			var removeOn = function(){
					$(this).removeClass(active.nex+" "+active.pre)
					$(this).addClass(active.on)
				}
	
			//* 「次へ」ボタンが押されたとき *//
			$(gallery.next).click(function(){
				$(gallery.previous).css({ display: "block" });
				if($("> *:last-child" , gallery.slideImg).prev().attr("class").match(active.on)){
					$(gallery.next).css({ display: "none" });
				}
				var prePosition = $("." + active.on).width();
				$("." + active.on).stop().animate({ left: -prePosition },speed.t,
				function(){
					$(this).removeClass(active.on)
					$(this).addClass(active.pre)
				});
				var nextOn = parseInt((viewSize.w/2 - $("." + active.on).next().width()/2) + "px");
				$("." + active.on).next().stop().animate({ left: nextOn },speed.t,removeOn);
				return false;
			});
			//* 「前へ」ボタンが押されたとき *//
			$(gallery.previous).click(function(){
				$(gallery.next).css({ display: "block" });
				if($("> *:first-child" , gallery.slideImg).next().attr("class").match(active.on)){
					$(gallery.previous).css({ display: "none" });
				}
				$("." + active.on).stop().animate({ left: nexPosition },speed.t,
				function(){
					$(this).removeClass(active.on);
					$(this).addClass(active.nex);
				});
				var prevOn = parseInt((viewSize.w/2 - $("." + active.on).prev().width()/2) + "px");
				$("." + active.on).prev().stop().animate({ left: prevOn },speed.t,removeOn);
				return false;
			});
		});
		
	};
});
