/**
 * @author yamamoto
 */


//現在位置
var num = 1;
//タグの数
var imgLength;
//画像配列
var basePath = "shared/topAnimation/images/";
var images = [ "i1.jpg" , "i2.jpg" , "i3.jpg" , "i4.jpg" , "i5.jpg" , "i6.jpg" , "i7.jpg" , "i8.jpg" , "i9.jpg" ];
var thumbs = [ "t1.jpg" , "t2.jpg" , "t3.jpg" , "t4.jpg" , "t5.jpg" , "t6.jpg" , "t7.jpg" , "t8.jpg" , "t9.jpg" ];
//URL配列
var uri = [ "#" , "exam/" , "exam/point/" , "shortmovie/utaou/" , "event/opencampus/report/suma.html" , "blog/portal/" , "cafe/journey/" , "cafe/nadeshiko/" , "cafe/recipe/" ];
//swf配列
var swfURL = [ "shared/topAnimation/swf/swf1.swf" , "shared/topAnimation/swf/swf2.swf" ];
//タイマー
var slideTimer;
//flash playerがインストールされているかどうか
var flashFlug;

//スライド時間
var dura = 800;
//タイマーの待ち時間
var delay = 5000;

$(document).ready(function(){
	nowIndex = "index";
	//flash判別
	var isFlashInstalled=function(){if(navigator.plugins["Shockwave Flash"]){return true;}try{new ActiveXObject("ShockwaveFlash.ShockwaveFlash");return true;}catch(a){return false;}}();
	if ( isFlashInstalled ) {flashFlug = true;} else {flashFlug = false;};
	
	imgLength = images.length;
	$("img#t1").corner();
	$("img#t2").corner();
	$("img#t3").corner();
	$("img#t4").corner();
	
});

//最初のアニメーション
function topAnimationStart() {
addSwf( swfURL[0] );
	//alert( "topAnimationStart" );
	$("p#big").animate( { opacity: 'show'}, { duration: 0, easing: 'swing' , complete:function(){
		
	}  } );
	$("#banner").delay(200).animate( { opacity: 'show'}, { duration: 800, easing: 'swing' } );
	$("p#small").delay(500).animate( { opacity: 'show'}, { duration: 800, easing: 'swing' } );

	$("img#prev").delay(800).animate( { opacity: 'show'}, { duration: 500, easing: 'swing' } );
	$("img#next").delay(800).animate( { opacity: 'show'}, { duration: 500, easing: 'swing' } );
	//
	//timerStart();
}

//動かす
function move(e) {
	if($("#flashcontent"))$("#flashcontent").css( "display" , "none" );
	
	mainLeft = "-1260";
	thumbLeft = "-503";
	if ( e.target.id == "t2" ) {
		num += 1;
	}
	if ( e.target.id == "next" ) {
		num += 1;
	} else if ( e.target.id == "prev" ) {
		num -= 1;
		mainLeft = "-30";
		thumbLeft = "-53";
	};
	if ( num == imgLength + 1 ) { num = 1 } else if ( num <= 0 ) { num = imgLength; };
	
	
	$("p#big").animate( { "marginLeft":mainLeft } , { duration:dura , easing:"easeOutQuart" , complete:function(){ init(); } } );
	$("p#small").animate( { "marginLeft":thumbLeft } , { duration:dura , easing:"easeOutQuart" , complete:function(){ smallInit(); } } );
	
	$("img#next").unbind( "click" , move );
	$("img#prev").unbind( "click" , move );
	$("img#t2").unbind( "click" , move );
	
	//リスナー再登録用タイマー
	var listenerTimer = setTimeout( addListener , dura );
	//タイマー
	timerInit();
	return false;
}
function trace(hoge){
	//$("#tracer").text(hoge);
}
//flashからのイベント
function getFlashFunc() {

	if(document.getElementById( "flashcontent" ))document.getElementById( "flashcontent" ).style.display = "none";
	//$("#flash").css("display", "none");
	mainLeft = "-1260";
	thumbLeft = "-503";

	num += 1;
	if (num == imgLength + 1) {
		num = 1
	};
	trace(num)
	$("p#big").animate({"marginLeft": mainLeft}, {duration: dura,easing: "easeOutQuart",complete: function(){init();}});
	$("p#small").animate({"marginLeft": thumbLeft}, {duration: dura,easing: "easeOutQuart",complete: function(){smallInit();}});
	//タイマー
	timerInit();
	return false;
}


//タイマーを初期化
function timerInit() {
	if ( flashFlug == false ) {
		//flashplayerがインストールされていなければタイマー初期化
		timerStop();
		timerStart();
	} else {
	
		if ( num == 1 ) {
			//flashplayerがインストールされていてswfのページならタイマー止めるだけ
			timerStop();
			//timerStart();	
		} else {
		$("#flashcontent").css( "display" , "none" );
			//flashplayerがインストールされていてswfのページじゃなければタイマー初期化
			timerStop();
			timerStart();			
		}
	}
	return false;
}

//メインイメージ初期化                      
function init() {
	//リンクの切り替え
	$( "a#linkBtn" ).attr( "href" , uri[num - 1] );
	
	//swf準備
	setUpSwf( num );
	$("p#big").css( "margin-left" , "-645px" );
	if ( num == 1 ) {
		mainAttr( images[imgLength - 1] , images[0] , images[1] );
		$("#flashcontent").css( "display" , "block" );
	} else if ( num == 2 ) {
		mainAttr( images[0] , images[1] , images[2] );
		
	} else if ( num == 3 ) {
		mainAttr( images[1] , images[2] , images[3] );
		//$("#flash").css( "display" , "block" );
	} else if ( num == 4 ) {
		mainAttr( images[2] , images[3] , images[4] );
	} else if ( num == 5 ) {
		mainAttr( images[3] , images[4] , images[5] );
	} else if ( num == 6 ) {
		mainAttr( images[4] , images[5] , images[6] );
	} else if ( num == 7 ) {
		mainAttr( images[5] , images[6] , images[7] );
	} else if ( num == 8 ) {
		mainAttr( images[imgLength - 3] , images[imgLength - 2] , images[imgLength - 1] );
	} else if ( num == 9 ) {
		mainAttr( images[imgLength - 2] , images[imgLength - 1] , images[0] );
	}
	return false;
}
//サムネイル初期化
function smallInit() {
	$("p#small").css( "margin-left" , "-278px" );
	if ( num == 1 ) {
		thumbAttr( thumbs[0] , thumbs[1] , thumbs[2] , thumbs[3] );
	} else if ( num == 2 ) {
		thumbAttr( thumbs[1] , thumbs[2] , thumbs[3] , thumbs[4] );
	} else if ( num == 3 ) {
		thumbAttr( thumbs[2] , thumbs[3] , thumbs[4] , thumbs[5] );
	} else if ( num == 4 ) {
		thumbAttr( thumbs[3] , thumbs[4] , thumbs[5] , thumbs[6] );
	} else if ( num == 5 ) {
		thumbAttr( thumbs[4] , thumbs[5] , thumbs[6] , thumbs[7] );		
	} else if ( num == 6 ) {
		thumbAttr( thumbs[5] , thumbs[6] , thumbs[7] , thumbs[8] );		
	} else if ( num == 7 ) {
		thumbAttr( thumbs[6] , thumbs[7] , thumbs[8] , thumbs[0] );		
	} else if ( num == 8 ) {
		thumbAttr( thumbs[7] , thumbs[8] , thumbs[0] , thumbs[1] );		
	} else if ( num == 9 ) {
		thumbAttr( thumbs[8] , thumbs[0] , thumbs[1] , thumbs[2] );		
	}
	return false;
}
//メインイメージを追加
function mainAttr( at1 , at2 , at3 ) {
	$("img#i1").attr( "src" , basePath + at1 );
	$("img#i2").attr( "src" , basePath + at2 );
	$("img#i3").attr( "src" , basePath + at3 );
	return false;
}
//サムネイルを追加
function thumbAttr( at1 , at2 , at3 , at4 ) {
	$("img#t1").attr( "src" , basePath + at1 );
	$("img#t2").attr( "src" , basePath + at2 );
	$("img#t3").attr( "src" , basePath + at3 );
	$("img#t4").attr( "src" , basePath + at4 );
	return false;
}
//イベント追加
function addListeners() {
	//リスナー
	$("img#prev").one( "click" , move ).mouseover( over ).mouseout( out );
	$("img#next").one( "click" , move ).mouseover( over ).mouseout( out );
	$("img#t2").one( "click" , move );
	$( "#big" ).hover( function(){$( this ).fadeTo( 100 , 0.7 );},function(){$( this ).fadeTo( 100 , 1.0 );} );
	$( "#banner" ).hover( function(){$( this ).fadeTo( 100 , 0.7 );},function(){$( this ).fadeTo( 100 , 1.0 );} );	
	$("div#mainImage").bind( "click" , navigatoToURL );
	
	//カーソル
	cursorPointer();
	return false;
}
function addListener() {
	$("img#prev").one( "click" , move );
	$("img#next").one( "click" , move );
	$("img#t2").one( "click" , move );
	return false;
}
//btnマウスオーバー
function over(e) {
	var num = "-=3px";
	if ( e.target.id == "next" ) { num = "+=3px"; } else { num = "-=3px"; };
	$(e.target).animate( { left:num } , 0 );
	return false;
}
//btnマウスアウト
function out(e) {
	var num = "-=3px";
	if ( e.target.id == "next" ) { num = "-=3px"; } else { num = "+=3px"; };
	$(e.target).animate( { left:num } , 0 );
	return false;
}
//リンク
function navigatoToURL() {
	if ( num == 4 ) {
		window.open(uri[num - 1],"_blank");
	} else if ( num == 3 ) {
		window.open(uri[num - 1],"_blank");
	} else if ( num == 6 ) {
		window.open(uri[num - 1],"_blank");
	} else {
		window.open(uri[num - 1],"_self");
	}
	return false;
}
//タイマースタート
function timerStart() {
	slideTimer = setInterval( getFlashFunc , delay );
	return false;
}
//タイマーストップ
function timerStop() {
	clearInterval(slideTimer);
	return false;
}
//カーソル
function cursorPointer() {
	$("div#mainImage").css( "cursor" , "pointer" );
	$("img#t2").css( "cursor" , "pointer" );
	$("div#banner").css( "cursor" , "pointer" );
	$("img#prev").css( "cursor" , "pointer" );
	$("img#next").css( "cursor" , "pointer" );
	return false;
}
//swfを追加する。
function setUpSwf( _num ) {
	if ( _num == 1 ) { addSwf( swfURL[0] ) };

}
//swfobject
function addSwf( url ) {
	var flashvars = {};
	var params = {
		wmode: "transparent"
	};
	var attributes = { id:"flashcontent" };
	swfobject.embedSWF( url , "flashcontent" , "610" , "390" , "9.0.45" , false , flashvars , params , attributes );
	$('#flashcontent').css( 'z-index' , '0' );
	$('img#prev').css( 'z-index' , '9999' );
}


