/*
 * jQuery Slide Show
 * Copyright 2011 Yuya Amano
 */
(function($){
	$.fn.slideShow = function() {
		var _config = {
			slideBox : $('#topVisualSlide'),
			slideTargets : $('#topVisualSlide div.tvsBox:gt(0)'),
			slideDefault : $('#topVisualSlide div.tvsBox:eq(0)'),
			btnNext : $('#topVisualSlide-naviL a'),
			btnPrev : $('#topVisualSlide-naviR a'),
			winSize : 0,
			easing : 'easeOutQuad',
			speed : 1500,
			interval : 5000,
			firstInterval : 5000
		};
		_config.winSize = _config.slideBox.width();
		
		//set mouseover action for top
		var outControllerNK = $('#jsContentNK');
		var outControllerEK = $('#jsContentEK');
		var outControllerGK = $('#jsContentGK');
		outControllerNK.mouseover(function(){
			skipSlide('nk');
		});
		outControllerEK.mouseover(function(){
			skipSlide('ek');
		});
		outControllerGK.mouseover(function(){
			skipSlide('gk');
		});

		var _isAnimate = true;
		var _slideHasStarted = false;
		var _currentIndex = 0;
		var _nextIndex = 0;
		var _timeInterval;
		
		_config.btnNext.click(function(){
			if(_isAnimate){
				return;
			}
			stopAutoSlide();
			changeSlide('next');
		});
		_config.btnPrev.click(function(){
			if(_isAnimate){
				return;
			}
			stopAutoSlide();
			changeSlide('prev');
		});

		setTimeout(function(){
			_config.slideDefault.fadeIn(1000,null,function(){ _isAnimate = false; });
		},500);
		
		setAutoSlide();

		function skipSlide(idName)
		{
			if(!_slideHasStarted){
				return;
			}
			switch(idName){
				case 'nk'://0
					changeSlide('',0);
					break;
				case 'ek'://1
					changeSlide('',1);
					break;
				case 'gk'://2
					changeSlide('',2);
					break;
			}
		}
		
		function changeSlide(type, skipNumber)
		{
			
			//if the slide has not started yet
			if(!_slideHasStarted){
				startSlide(type);
				return;
			}
			
			var skipFlag = false;
			if(skipNumber > -1){
				skipFlag = true;
			}
			
			_isAnimate = true;

			stopAutoSlide();
			
			var positionLeft = 0;
			if(skipFlag){
				_nextIndex = skipNumber;
			}else{
				switch(type){
					case 'next':
						_nextIndex++;
						if(_nextIndex >= _config.slideTargets.length){
							_nextIndex = 0;
						}
						//positionLeft = _config.winSize;
						break;
					case 'prev':
						_nextIndex--;
						if(_nextIndex < 0){
							_nextIndex = _config.slideTargets.length - 1;
						}
						//positionLeft = _config.winSize * -1;
					break;
				}
			}
			if(_currentIndex > 0){
				_config.slideTargets.not(_currentIndex).css({'display':'none','opacity':0});
			}
			_config.slideTargets.eq(_currentIndex).css({
					left : 0,
					zIndex : 100
				});
			_config.slideTargets.eq(_nextIndex).css({
					left : 0,
					zIndex : 200,
					opacity : 0,
					display : 'block'
				}).animate({opacity:1},_config.speed,_config.easing,function(){
					_config.slideTargets.eq(_currentIndex).css('display','none');
					_currentIndex = _nextIndex;
					setAutoSlide();
				});
			/*
			_config.slideTargets.eq(_currentIndex).animate({
					left : positionLeft * -1
				},_config.speed,_config.easing);
			_config.slideTargets.eq(_nextIndex).css({
					left : positionLeft,
					display : 'block'
				}).animate({
					left : 0
				},_config.speed,_config.easing,function(){
					_config.slideTargets.eq(_currentIndex).css('display','none');
					_currentIndex = _nextIndex;
					setAutoSlide();
					_isAnimate = false;
				});
			*/
		}
		
		function startSlide(type)
		{
			_isAnimate = true;
			
			_config.slideDefault.fadeOut(2000);
			var nextIndex;
			var positionLeft = 0;
			switch(type){
				case 'next':
					nextIndex = 0;
					positionLeft = _config.winSize;
					break;
				case 'prev':
					nextIndex = _config.slideTargets.length - 1;
					positionLeft = _config.winSize * -1;
					break;
			}
			_config.slideTargets.eq(nextIndex).css(
				{
					display : 'none',
					left : 0
				}
			).fadeIn(_config.speed,_config.easing,function(){
					_slideHasStarted = true;
					_isAnimate = false;
					setAutoSlide();
				}
			);
			/*
			_config.slideTargets.eq(nextIndex).css(
				{
					display : 'block',
					left : positionLeft
				}
			).animate(
				{
					left : 0
				},_config.speed,_config.easing,function(){
					_slideHasStarted = true;
					_isAnimate = false;
					setAutoSlide();
				}
			);
			*/
		}

		function setAutoSlide()
		{
			if(_slideHasStarted){
				stopAutoSlide();
			}
			var int = 0;
			if(_config.firstInterval > 0){
				int = _config.firstInterval;
				_config.firstInterval = 0;
			}else{
				int = _config.interval;
			}
			_timeInterval = setInterval(function(){
				changeSlide('next');
			},_config.interval);
		}

		function stopAutoSlide()
		{
			_config.slideTargets.clearQueue().stop();
			_isAnimate = false;
			if(_timeInterval){
				clearInterval(_timeInterval);
			}
			_config.slideTargets.not(_currentIndex).css({'display':'none','opacity':0});
			_config.slideTargets.eq(_currentIndex).css({'display':'block','opacity':1});
		}
		
		$(window).resize(function(){
			_config.winSize = _config.slideBox.width();
		});
	
	}
})(jQuery);


jQuery(document).ready(function(){
	$('#topVisualSlide div.tvsBox').show().hide();
	jQuery.event.add(window, "load", function(){
		jQuery.fn.slideShow();
	});
});
