﻿jQuery.extend({
  taiyo : {
    timer: null,
	hover: false
  }
});

function timerStart() {
	clearTimeout($.taiyo.timer);
	$.taiyo.timer = setTimeout(function() {
		$('#carousel ul').routineSlide();
	}, 7000);
}

(function() {
	var curSlideNo = 0;
	var captionNo = 1;
	var curCaption = 0;
	var end = 0;
	
	
	
	$.fn.switchCaption = function(index) {
		var btn = index;
		var base = 0;
		var pos = 0;
		var cap = curCaption;
		
		
		if(cap > 3) {
			cap -= 4;
		}
		if(curCaption > 7) {
			curCaption -= 8;
			cap = curCaption;
		}
		if(cap != btn) {
			if(cap < btn) {
				base = btn - cap;
				pos = base * 17;
			} else {
				switch(cap) {
					case 1:
						base = 4 - 1;
						break;
						
					case 2:
						base = cap + btn;
						break;
						
					case 3:
						base = btn + 1;
						break;
					
					default:
						break;
				}
				
				pos = base * 17;
			}
			$('.caption ul').queue([]).animate({
				'top':'-='+pos+'px'
			}, 'slow', function() {
				curCaption += base;
				
				var head = $('.caption ul').position().top;
				if(head < end) {
					var number = Math.round(-head / 17);
					$('.caption ul').css('top', '0px');
					var caption = $('.caption ul li:lt('+number+')');
					var parent = $('.caption ul');
					$('.caption ul li:first-child').remove();
					parent.append(caption);
				}
				$('.caption ul li').removeClass('cur');
				$('.caption ul li:first-child').addClass('cur');

			});
		}
	}

	$.fn.changeCaption = function() {
		if(curCaption < 7) {
			curCaption++;
		} else {
			curCaption -= 7;
		}
		
		$('.caption ul').queue([]).animate({
			'top':'-=17px'
		}, 'slow', function() {
			var head = $('.caption ul').position().top;
			if(head < end) {
				var number = Math.round(-head / 17);
				$('.caption ul').css('top', '0px');
				var caption = $('.caption ul li:lt('+number+')');
				var parent = $('.caption ul');
				$('.caption ul li:first-child').remove();
				parent.append(caption);
			}
			$('.caption ul li').removeClass('cur');
			$('.caption ul li:first-child').addClass('cur');
		});
	}
	
	
	$.fn.routineSlide = function() {
		clearTimeout($.taiyo.timer);
		if(curSlideNo < 3){
			$(this).slideOut();
		} else {
			$(this).slideIn();
		}
	}
	$.fn.slideIn = function() {
		$('.slider li:first-child').fadeIn(1500, function() {
			$('.slider li:not(:first-child)').css('display', 'block');
			setTimeout(function() {
				$('.slider').slideOut();
			}, 7000);
		});
		curSlideNo = 0;
		$('#switch').setCurSwitch();
		$('.caption li').changeCaption(curSlideNo);
	}
	
	$.fn.slideOut = function() {
		clearTimeout($.taiyo.timer);
		$('li:eq('+curSlideNo+')', this).fadeOut(1200, function() {
			if(!$.taiyo.hover) {
				timerStart();
			}
		});
		curSlideNo++;
		$('#switch').setCurSwitch();
		$('.caption li').changeCaption(curSlideNo);
	}


	
	$.fn.switchSlide = function(index) {
		clearTimeout($.taiyo.timer);
		
		var slide = $('#carousel ul');
		
		curSlideNo = index;
		$('.slider li:lt('+curSlideNo+')').stop().fadeOut(1500);
		$('.slider li:eq('+curSlideNo+')').stop().fadeIn(1500, function() {
			$('.slider li:gt('+curSlideNo+')').css('display', 'list-item');
			timerStart();
		});
		$('#switch').setCurSwitch();
		$('.caption').switchCaption(curSlideNo);
	}
	
	$.fn.setCurSwitch = function() {
		$('li img', this).unbind('mouseenter').unbind('mouseleave');
		var curSrc = $('li#curSwitch img', this).attr('src');
		var src = $('li:eq('+curSlideNo+') img', this).attr('src');
		
		if(curSrc) {
			$('li#curSwitch img', this).attr('src', curSrc.replace(/_on\.gif/, ".gif"));
			$('li#curSwitch', this).removeAttr('id');
		}
		if(src) {
			$('li:eq('+curSlideNo+')', this).attr('id', 'curSwitch');
			var target = src.match(/(?:(?!_on).)*/);
			if(!target[0].match(/\.gif/)) {
				$('li#curSwitch img', this).attr('src', target[0]+"_on.gif");
			} else {
				$('li#curSwitch img', this).attr('src', src.replace(/\.gif$/, "_on.gif"));
			}
		}
		$('li img', this).rollover();
	}

})($);


$(function() {	
	//preload images
	function preload(imgs){
		for(var i = 0; i < imgs.length; i++){
			var imgObj = new Image();
			imgObj.src = imgs[i];
		}
	}
	preload([
		"/ut/common/images/top/img_slide_01.jpg",
		"/ut/common/images/top/img_slide_02.jpg",
		"/ut/common/images/top/img_slide_03.jpg",
		"/ut/common/images/top/img_slide_04.jpg",
		"/ut/common/images/top/btn_slide_01.gif",
		"/ut/common/images/top/btn_slide_02.gif",
		"/ut/common/images/top/btn_slide_03.gif",
		"/ut/common/images/top/btn_slide_04.gif",
		"/ut/common/images/top/btn_slide_01_on.gif",
		"/ut/common/images/top/btn_slide_02_on.gif",
		"/ut/common/images/top/btn_slide_03_on.gif",
		"/ut/common/images/top/btn_slide_04_on.gif",
	]);
	
	//set z-index
	var layerNo = 4;
	$('#carousel .slider li').each(function() {
		$(this).css('z-index', layerNo);
		layerNo--;
	});		   

	//rollover
	$('#switch li img').rollover();			

	//start slide show
	timerStart();
	
	//chenge news tab
	$('.tabs li').click(function() {
		$('#news .newsList').hide();
		$('#news .newsList#curNews').removeAttr('id');
		$('#news .newsList.'+$(this).attr('class')).attr('id', 'curNews');
		$('#news .newsList#curNews').show();
	});
				

	//stop slide show
	$('#carousel li, .caption li, #switch').hover(
		function() {
			$.taiyo.hover = true;
			clearTimeout($.taiyo.timer);
		},
		function() {
			$.taiyo.hover = false;
			var isMoving = false;
			isMoving = $('#carousel :animated');
			if(!isMoving.length) {
				timerStart();
			}
		}
	);
	
	//cursor pointer				   
	$('#switch li, .tabs li').hover(
		function() {
			$(this).css('cursor', 'pointer');
		},
		function() {
			$(this).css('cursor', 'default');
		}
	);
	
	//slide switch
	$('#switch li').click(function() {
		var isMoving = false;
		isMoving = $('#carousel :animated');
		if(!isMoving.length) {
			$(this).switchSlide($(this).index());
		}
	});
	
	
	$("#top #news .box .inner .topics dd:last").addClass("last");
	$("#top #news .box .inner .releases dd:last").addClass("last");
	$("#top #news .box .inner .products dd:last").addClass("last");
	$("#top #news .box .inner .financer dd:last").addClass("last");
	$("#top #news .box .inner .information dd:last").addClass("last");
	
});
