/*--------------------------------
 *
 * top ロード
 *
--------------------------------*/

$(function(){
	var IMG_ID = 'rImage';
	var NAVI_ID = 'jcarousel-skin-tango';
	
	var count = 8;
	var currentImage = 1;
	var nextImage = 2;
	var timerId;
	var fspeeds = 300;
	var delay = 7500;
	var loopInterval = 2000;
	
	//navigation - set event
	var navi = $('#main_visual .jcarousel-skin-tango a').mouseover(onMouseover);

	//start animation
	timerId = setTimeout(loop, delay);
	
	//rotation image
	function loop(){
		//fadeout current image
		$('#'+IMG_ID+currentImage).fadeOut(fspeeds);
		
		//fadein next image
		$('#'+IMG_ID+nextImage).fadeIn(fspeeds);
		
		//focus gation
		var navi = $('#'+NAVI_ID+nextImage);
		if(navi.size() > 0) chagesNaviImage(navi.children('img:first'));
		else resetNaviImage();
		
		currentImage = nextImage;
		nextImage = ++nextImage>=count ? 1 : nextImage;
		
		timerId = setTimeout(loop, (currentImage==0 ? delay+loopInterval : delay));
	}
	
	//navigation mouseover
	function onMouseover(){
		var id = this.id;
		if(currentImage == id.replace(NAVI_ID, "")) return false;
		
		setTimeout(changeImage, 300);

		function changeImage(){
		
			clearTimeout(timerId);				
			nextImage = id.replace(NAVI_ID, "");
			loop();

			chagesNaviImage($('#'+id).children('img:first'));
		}
	}
	
	function chagesNaviImage(elem){
		resetNaviImage();
		elem.attr('src', elem.attr('src').replace(/\.(gif|jpg|png)/,".$1"));
	}
	
	function resetNaviImage(){
		$('#main_visual .jcarousel-skin-tango img').each(function(idx, obj){
			$(obj).attr('src', $(obj).attr('src').replace(/\\.(gif|jpg|png)/, '.$1'));
		});
	}
});

/*--------------------------------
 *
 * item全体のリンク
 *
--------------------------------*/
$(function(){
	$(".submenu .unit").each(function(idx, obj){
		var url = $(obj).children(".link").children("a").attr("href");
		$(obj).click(function(){location.href = url});
		$(obj).hover(
			function(){
				$(this).css("background-color", "#e0edf6");
				var img = $(this).children(".label").children("img");
				img.attr("src", img.attr("src").replace(".gif","_on.gif"));			
			},
			function(){
				$(this).css("background-color", "transparent");
				var img = $(this).children(".label").children("img");
				img.attr("src", img.attr("src").replace("_on.gif",".gif"));
			}
		);
	});
});

