var $$ = $.fn;	/* $はJQueryオブジェクト */
$$.extend({		/* extend()はプロトタイプへ追加 */
	Slideshow : {
		Ready : function()
		{
			timeout = document.getElementById('tmpSlideContainer').getAttribute("speed");
			interval_id = 0;

			// hoverイベント パラメータは２つ。
			// 第一引数 - マウスオーバーで呼び出される関数
			// 第二引数 - マウスアウトで呼び出される関数
			$('div.tmpSlideshowControl').hover(
				function() {
					$(this).addClass('tmpSlideshowControlOn');
				},
				function() {
					$(this).removeClass('tmpSlideshowControlOn');
				}
			)
			// clickイベント
			$('div.tmpSlideshowControl').click( function()
			{
				$(this).Slideshow.Move($(this).attr('id').split('-').pop());
				clearInterval(interval_id);
			} );

			//最初は全部見えないようにしておく。
			$('div.tmpSlide').hide();
			$('div#tmpSlide-' + document.getElementById('tmpSlideContainer').getAttribute("currentpage") ).fadeIn(timeout);		//最初のページ表示
			$('div#tmpSlideshowControl-' + document.getElementById('tmpSlideContainer').getAttribute("currentpage") ).addClass('tmpSlideshowControlActive');	//最初のページのボタン

			// slideイベント
			interval = Number(document.getElementById('tmpSlideContainer').getAttribute("interval"));
			if( interval > 0 ){
		        interval_id = setInterval((function() {
					currentpage = Number(document.getElementById('tmpSlideContainer').getAttribute("currentpage"));
					nextpage = currentpage +1;
					if( document.getElementById("tmpSlide-" + nextpage) == null ){
						nextpage = 1;
					}
					$(this).Slideshow.Move(nextpage);
		        }), interval);
			}

		},

		Move : 	function(id) {
					timeout = document.getElementById('tmpSlideContainer').getAttribute("speed");

					//ボタンが押されても動かないようにする
		            document.getElementById('tmpSlideContainer').setAttribute("changingslide", Number(document.getElementById('tmpSlideContainer').getAttribute("changingslide"))+1);
					if( document.getElementById('tmpSlideContainer').getAttribute("changingslide") < 2 ){

						$('div.tmpSlide').stop();
						//$('div.tmpSlide').hide();
						
						// スライド操作
						var elem = $('div#tmpSlide-' + id );
						var elem_old = $('div#tmpSlide-' + document.getElementById('tmpSlideContainer').getAttribute("currentpage") );

						$('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

						//現在のページと同じページじゃないか確認
						if(id != document.getElementById('tmpSlideContainer').getAttribute("currentpage") ){

							// 通常表示
							//elem.show();
							
							// フェードイン1 初期不透明度0 -> 100% (例 1000ミリ秒で不透明度100%)
							elem_old.fadeOut(timeout);
							elem.fadeIn(timeout);
							
							// フェードイン2 初期不透明度 任意 -> 任意 (30%から1000ミリ秒で80%)
							/*elem.css({"opacity": "0.3"})
							elem.show();
							elem.fadeTo(1000,0.8);
							*/
							
							// アニメーション
							/*elem.css({"width": "20px", "opacity": "0.1"});
							elem.animate(
								{width: "100%", opacity: "1.0"},
								{duration: "slow"}
							);
							*/

							document.getElementById('tmpSlideContainer').setAttribute("currentpage", id);

							//シーン切り替えが終わったら操作OK
					        setTimeout((function() {
					            document.getElementById('tmpSlideContainer').setAttribute("changingslide", document.getElementById('tmpSlideContainer').getAttribute("changingslide") -1 );
					        }), timeout);

						}else{
							//切り替えOK
				            document.getElementById('tmpSlideContainer').setAttribute("changingslide", document.getElementById('tmpSlideContainer').getAttribute("changingslide") -1 );
						}
						$('div#tmpSlideshowControl-' + id ).addClass('tmpSlideshowControlActive');	//

					}else{
						//切り替えOK
			            document.getElementById('tmpSlideContainer').setAttribute("changingslide", document.getElementById('tmpSlideContainer').getAttribute("changingslide") -1 );
					}
				}


	}
});

$(function()
{
	$$.Slideshow.Ready();
});