/*
 * 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({ visibility: "visible",cursor: "default" })
				.html("<img src='../../images/bnr_box_up_off.png' alt='scroll_up_off' />");
			}
			//* ５件未満では「上へ」も非表示にする *//
			if($("> *:last-child" , scrollGallery.scrollImg).attr("class").match(active.on)){
				$(scrollGallery.up).css({ visibility: "visible", cursor: "default" })
				.html("<img src='../../images/bnr_box_down_off.png' alt='scroll_down_off' />");
				$("#banner .item").css({ background: "url('../images/banner_border.jpg') no-repeat center bottom" });
			}	
			
			//* スライド画像の配置用変数 *//
			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, visibility: "hidden" });
			$("> *" , scrollGallery.scrollImg).eq(i).css({ left: leftPosition });
			$("> *:first-child" , scrollGallery.scrollImg).css({ top: firstTop });
			
			
			height_ch();
			
			function height_ch() {
			
				$(".onActive").css({ top:"0", margin:"-1px 0 0 0" });
				
				var def_height = parseInt(240);
				var reheight = parseInt($("."+active.on).height());
				
				if (reheight > def_height) {
					$("#gallery").css({ height:reheight });//コンテンツの高さに合わせる。
					$("#scrollImg").css({ height:reheight });
				} else {
					$("#gallery").css({ height:def_height });//これはデフォルト。
					$("#scrollImg").css({ height:def_height });
				}
			
			
			}
			
			//* スライドの速さ *//
			var speed = { t:t? t:200 }
			
			//* クラス削除のオブジェクト *//
			var removeOn = function(){
				$(this).removeClass(active.nex+" "+active.pre);
				$(this).addClass(active.on);
				
				height_ch();
				$("."+active.pre).css({ visibility: "hidden" });
				$("."+active.on).css({ visibility: "visible" });
			}

			//* 「上へ」ボタンが押されたとき *//
			
			$(scrollGallery.up).click(function(){
											   
				//初期表示状態はクリックイベントを動かさない。
				if($("> *:last-child" , scrollGallery.scrollImg).attr("class").match(active.on)){
					$(scrollGallery.up).unbind('click');
				} else {
					scrollUp();
				}
											   
			});

			function scrollUp () {
				
				$(function() {
					$("."+active.on).next().css({ visibility: "visible" });
					$(scrollGallery.down).html("<img src='../../images/bnr_box_up.png' alt='scroll_up' />")
					$(scrollGallery.down).css({ visibility: "visible", cursor: "pointer" });
					$(scrollGallery.down).bind("click", function(){
						scrollDown();
					});
					if($("> *:last-child" , scrollGallery.scrollImg).prev().attr("class").match(active.on)){
						$(scrollGallery.up).css({ visibility: "visible", cursor: "default" }).unbind('click')
						.html("<img src='../../images/bnr_box_down_off.png' alt='scroll_down_off' />");
					}					
					
					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(){
				//初期表示状態はクリックイベントを動かさない。
				if($("> *:first-child" , scrollGallery.scrollImg).attr("class").match(active.on)){
					$(scrollGallery.down).unbind('click');
				} else {						 
					scrollDown();
				}
			});
			
			function scrollDown () {
			
				$(function() {
					$("."+active.on).prev().css({ visibility: "visible" });
					$(scrollGallery.up).html("<img src='../../images/bnr_box_down.png' alt='scroll_down' />")
					$(scrollGallery.up).css({ visibility: "visible", cursor: "pointer" });
					$(scrollGallery.up).bind("click", function(){
						scrollUp();
					});
					
					if($("> *:first-child" , scrollGallery.scrollImg).next().attr("class").match(active.on)){
						$(scrollGallery.down).css({ visibility: "visible", cursor: "default" }).unbind('click')
						.html("<img src='../../images/bnr_box_up_off.png' alt='scroll_up_off' />");
						
					}
					$("." + 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;
				});
			}
		});
	};
});

