/*============================================================================ 
jquery.function.js
Copyright (C) C-brains Corporation All rights reserved.
============================================================================ */
//XMLのパス
var _xmlPath = "/swf/home/index.xml"
//画像のパス
var _imgpath = "/swf/home/"

//スライドの切り替わる時間
var slideInterval = 5000;

//画像配列
var imgAry = [];
//URL配列
var urlAry = [];

var _thumbFlag = true;
var current = 1;

//TimerId
var timerId;

//UI
var _thumbNailPanel;
var _thumbSpace;
var _closeBtn;

/**********************************************************
loadXML
--------
XMLファイルのロード
**********************************************************/
function loadXML(){
	$.ajax({
		type: 'GET',
		url: _xmlPath,
		dataType: "xml",
		success: function(xml) {
			parseXml(xml);
		}
	})
}

function parseXml(xml){
	$(xml).find("entry").each(function(){
		imgAry.push($(this).attr("fileName"));
		urlAry.push($(this).attr("lnkUrl"));
	});
	setThumb();
	loadImg(0)
}

/**********************************************************
setThumb
--------
サムネイルを配置
**********************************************************/
function setThumb(){
	if(imgAry.length==0 || urlAry.length==0) return;
	var _html = "<ul>";
	$(imgAry).each(function(){
		var thumb = this.split('_');
		thumb = thumb[0]+'_'+thumb[1]+'_thumb.jpg'
		_html+='<li><a href="#"><img src="'+_imgpath + thumb + '" alt=""></a></li>';
	});
	_html+="</ul>";
	_thumbSpace.html(_html);
	
	var li = _thumbSpace.find('ul>li');
	li.each(function(index){
		var _index = index;
		$(this).click(function(){
			loadImg(_index);
			return false;
		})
	})
	
}

/**********************************************************
サムネイルの移動
**********************************************************/
function moveThumb(direct){
	var ul = _thumbSpace.find('ul');
		if(direct=='up'){
		$('#thumbSpace').stop().animate({
			'top': '-144px'
		},function(){
			var _first = _thumbSpace.find('ul>li:first');
			_first.appendTo(_thumbSpace.find('ul'));
			$('#thumbSpace').css({'top':-72})
		});
	}else if(direct=='down'){
		$('#thumbSpace').stop().animate({
			'top': '0px'
		},function(){
			var _last = _thumbSpace.find('ul>li:last');
			_last.prependTo(_thumbSpace.find('ul'));
			$('#thumbSpace').css({'top':-72})
		});
	}
}


/**********************************************************
loadImg
--------
画像の読み込み
**********************************************************/
function loadImg(index){
	clearSlideTimer();
	var prev = $('#slideWrapper>.slide');
	prev.animate({
		opacity: 0
	},
	'1000',
	'easeOutCirc',
	function(){
		var _html = '<a href="'+ urlAry[index] + '"><img src="'+_imgpath + imgAry[index] +'" alt="" /></a>';
		//console.log("_html="+_html);
		$('#slideWrapper>.slide').html(_html);
		var newImg = $('#slideWrapper>.slide');
		newImg.animate({
			opacity: 1
		},
		'1000',
		'easeInCirc'
		)
	}
	);
	current = index+1;
	slideTimer();
}


/**********************************************************
toggleThumbs
--------
サムネイルパネルのトグル
**********************************************************/
function toggleThumbs(){
		if(_thumbFlag){
			$('#thumbnail').animate({
				left: '-136px'
			},
			'1000',
			'easeInOutCirc',
			function(){
				_thumbFlag = false;
				_closeBtn.attr({src:'/img/home/slide/btn_topics.png'});
			});
		}else{
			$('#thumbnail').animate({
				left: '0px'
			},
			'1000',
			'easeInOutCirc',
			function(){
				_thumbFlag = true;
				_closeBtn.attr({src:'/img/home/slide/btn_close.png'})
			});
		}
}

/**********************************************************
slideTimer
--------
タイマー処理
**********************************************************/
function slideTimer(){
	timerId = setInterval('timerHandler()',slideInterval);
}

function clearSlideTimer(){
	clearInterval(timerId);
}

function timerHandler(){
	//console.log("current="+current);
	loadImg(current);
	if(current>imgAry.length-1){
		current = 0;
	} 
	
}


/**********************************************************
init
--------
初期設定
**********************************************************/

function init(){
	_closeBtn = $('.closeButton img');
	_thumbNailPanel = $('#thumbNail');
	_thumbSpace = $('#thumbSpace');
	$('.closeButton>a').click(function(){
		toggleThumbs();
	});
	
	$('.upButton>a').click(function(){
		moveThumb('up');
	});
	
	$('.downButton>a').click(function(){
		moveThumb('down');
	});
	
	toggleThumbs();
	loadXML();
	slideTimer();
}

/**********************************************************
実行処理
**********************************************************/
$(document).ready(init);
