// ============================================================
// データ読込み
// 
// %update / 2011.10.04
// 
// %use / indispensableFunc.js
// %use / jquery
// %use / rotVis.js
// ============================================================

// jQueryのショートカットを用意
if(! $jQ) { var $jQ = jQuery; }
//if(! $jQ) { var $jQ = jQuery.noConflict(); }  // 『noConflict()』はjQueryのショートカット（『$』など）を無効にするので注意！

// 名前の衝突をふせぐために名前空間を用意する
var InsData;
if( ( InsData && ( (typeof InsData !== 'object') || !('isNamespace' in InsData) ) ) || (InsData === false) || (InsData === 0) ) {
	throw new Error('"InsData" already exists! You cannot define "InsData".');
} else if( InsData && ('isNamespace' in InsData) ) {
	throw new Error('Namespace "InsData" already exists!');
}
InsData = {};  // 名前空間用のオブジェ生成
InsData.toString = function() { return '[object InsData(is Namespace)]'; };
InsData.isNamespace = true;


// ------------------
// 【定数・変数】
// ------------------
InsData.filename = '/data/rotvis.json';  // データファイル
InsData.LIMIT_TIMEOUT = 30;  // タイムアウトの時間（sec）

InsData.idArea = 'RotVisArea';  // エリアid
InsData.idMainArea = 'RotVisMainArea';  // メイン画像エリアid
InsData.idCtrlArea = 'RotVisCtrlArea';  // コントローラーエリアid


// ------------------
// 【関数】
// ------------------
// なし


// ------------------------------------------------------------
// ▼処理
// ------------------------------------------------------------
$jQ(document).ready( function() {

	// ==============================
	// ■ファイルリストデータの読込み
	// %param / なし
	// ------
	// %return / なし
	( function() {

		var area_jq = $jQ('#' + InsData.idArea);  // エリア
		var mainArea_jq = $jQ('#' + InsData.idMainArea);  // メイン画像エリア
		var ctrlArea_jq = $jQ('#' + InsData.idCtrlArea);  // コントローラーエリア

		// ローディング
		var elemLoading_jq = $jQ('<p id="InsData_loading" class="txtSub01">データを読込んでいます...</p>');
		area_jq.append(elemLoading_jq);

		//alert('test');  // DBUG
		
		// 読込み
		$jQ.ajax( {

		type : 'GET',
		async : true,
		url : InsData.filename,
		dataType : 'json',
		timeout : InsData.LIMIT_TIMEOUT *1000,
		success : function(data, status) {
			//alert(status);  // DBUG
			
			var i = 0;
			var elem_jq = null;
			var itemTotal = 0;  // アイテムの総数

			if(data.itemArray.length > data.setting.itemMax) {  // アイテムの総数が最大値を超えているとき
				itemTotal = data.setting.itemMax;
			} else {
				itemTotal = data.itemArray.length;
			}

			// データの挿入
			/* HTML ex：
======
<!-- RotVisArea ====== -->
<div id="RotVisArea">
<div id="RotVisMainArea"><!-- メイン画像エリア（JSで挿入） -->
<!-- <ul>
<li><div><a href="_URL_"><img class="over" src="_SRC_" alt="_ALT_" width="910" height="270" /></a></div></li>
</ul>-->
</div><!-- /RotVisMainArea -->
<div id="RotVisCtrlArea"><!-- コントローラーエリア（JSで挿入） -->
<!-- <ul>
<li><div class="itemInnr01"><a href="_URL_"><img src="_SRC_" alt="_ALT_" width="284" height="45" /></a></div></li>
</ul>-->
</div><!-- /RotVisCtrlArea -->
</div>
<!-- /RotVisArea ====== -->
======
			*/
			
			// メイン画像エリア
			mainArea_jq.html('');  // クリア
			mainArea_jq.append('<ul></ul>');

			// コントローラーエリア
			ctrlArea_jq.html('');  // クリア
			ctrlArea_jq.append('<ul></ul>');

			for( i=0; i < itemTotal; i++) {

				// メイン画像エリア
				elem_jq = $jQ('<li><div><a href="_URL_"><img src="_SRC_" alt="_ALT_" /></a></div></li>');
				$jQ('ul', mainArea_jq).append(elem_jq);
				$jQ('a', elem_jq).attr( { href : data.itemArray[i].url } );  // URL
				$jQ('a', elem_jq).attr( { target : data.itemArray[i].target } );  // target
				$jQ('img', elem_jq).attr( { src : data.itemArray[i].mainImg } );  // メイン画像パス
				$jQ('img', elem_jq).attr( { alt : data.itemArray[i].alt } );  // ALT

				( function() {  // ロールオーバー
					var imgPre_ar = [];
					$jQ('img', elem_jq).each( function() {
						var imgSrc = $jQ(this).attr('src');
						imgPre_ar.push( new Image() );
						imgPre_ar[imgPre_ar.length-1].src = imgSrc;
					} );  // each()

					$jQ('img', elem_jq).mouseover( function() {
						var imgSrc = $jQ(this).attr('src');
						var matchPos = -1;

						if( imgSrc.indexOf('_on') === -1 ) {  // 『_on』が含まれていないとき
							imgSrc = imgSrc.replace(/\.(jpg|gif|png)$/, '_on.$1');
							$jQ(this).attr('src', imgSrc);
						}
					} );  //mouseover()

					$jQ('img', elem_jq).mouseout( function() {
						var imgSrc = $jQ(this).attr('src');
						var matchPos = -1;

						if( imgSrc.indexOf('_on') !== -1 ) {  // 『_on』が含まれているとき
							imgSrc = imgSrc.replace(/_on\.(jpg|gif|png)$/, '.$1');
							$jQ(this).attr('src', imgSrc);
						}
					} );  //mouseout()
				} )();

				// コントローラーエリア
				elem_jq = $jQ('<li><div class="itemInnr01"><a href="_URL_"><img src="_SRC_" alt="_ALT_" /></a></div></li>');
				$jQ('ul', ctrlArea_jq).append(elem_jq);
				$jQ('a', elem_jq).attr( { href : data.itemArray[i].url } );  // URL
				$jQ('a', elem_jq).attr( { target : data.itemArray[i].target } );  // target
				$jQ('img', elem_jq).attr( { src : data.itemArray[i].controlBtnImg } );  // コントローラー画像パス
				$jQ('img', elem_jq).attr( { alt : data.itemArray[i].alt } );  // ALT

			}
			// ---

			// ローディング削除
			elemLoading_jq.remove();

			// ローテーションビジュアル実行
			( function() {
				var timerRotVisExec = null;
				_rotVisExec();
				function _rotVisExec() {
					// タイマー停止
					if(timerRotVisExec !== null) {
						clearTimeout(timerRotVisExec);
					}

					if(RotVis.exec) {
						// アイテムの総数の設定
						RotVis.setItemTotal(itemTotal);
						
						// 1枚の静止時間（sec）の設定
						RotVis.setDurationSecFreeze(data.setting.durationSecFreeze);

						// 実行
						RotVis.exec();
					} else {
						// タイマー開始
						timerRotVisExec = setTimeout( '_rotVisExec()', 1000 );
					}
				}
				
				// ■window　イベント
				$jQ(window).unload( function() {
					// タイマー停止
					clearTimeout(timerRotVisExec);
				} );  // unload()
			} )();

		},
		error : function() {
			//alert( '<p>『' + InsData.filename + '』を読込めませんでした。</p>' );
			info_o.elemAreaJq_ar[0].prepend('<p class="txtAccent01">データ：『' + InsData.filename + '』を読込めませんでした。</p>');
		}//,

		} );  // ajax()
	} )();
	// /==============================

} );  // ready()

