// JavaScript Document

$(function() {
	function size_check(no) {
		size = $('[id^=banner_text]').size();
		
		if(size <= no) {
			no = 0;	
		}
		
		return no;
	}
	function banner_text(no) {
		var no;
		var id;
		
		no = size_check(no);
		
		$('[id^=banner_text]').each(function () {
			id = $(this).attr('id').replace(/banner_text/, '');
			if(id == no) {
				$(this).show();	
			} else {
				$(this).hide();	
			}
		});
		
	}
	function banner_navi(no) {
		var no;
		var id;
		
		no = size_check(no);		
		$('#banner_navi span').each(function() {
			id = $('#banner_navi span').index($(this));	
			if(id == no) {
				$(this).children('a').children('img').css({'opacity':'1'});	
			} else {
				$(this).children('a').children('img').css({'opacity':'0.5'});	
			}
		});	
	} 
	function banner_img_reset(no) {
		var no;
		
		$('#banner_roll div').each(function() {
			$(this).css(
				{
					'left':'0',
					'top':'0',
					width:'1000px',
					height:'387px',
					opacity:'0'
				}
			);
		});
		$('#banner' + no).css({'opacity':'1'});	
	}
	function banner_roll(no) {
		var no;
		var id;
		var width;
		
		//alert($('#banner_roll div').size());
		if($('#banner_roll div').size() > 1) {
			if($('#banner_roll div').last().attr('id').replace(/banner/,'') == 0) {
				$('#banner_roll').css({left:'0'});
				$('#banner_roll div').last().remove();
				no = 0;
			}	
		}
		
		banner_no = no;
		
		banner_img_reset(no);
		
		width = $('#banner' + no).width();	
		
		width_next = $('#banner' + (no + 1)).width();
		
		if(width_next == null) {
			
			$('#banner0').clone().appendTo('#banner_roll');
			
			$('#banner_roll div').css({'opacity':'1'});
			width = $('#banner0').width();
			
			$('#banner_roll').width(width * (no + 2));
			
			left = -1 * ((no + 1) * width);							
		
		}
		
		banner_text(no);
		
		banner_navi(no);
		
		left = -1 * ((no + 1) * width);
		
		$('#banner_roll')
		.animate(
			{
				opacity:1
			}, 
			10000,
			function () {
				no++;
				$('#banner' + no).css({'opacity':'1'});
										
				banner_text(no);
		
				banner_navi(no);
		
				$('#banner_roll')		
				.animate(
					{
						'left':left + 'px'
					},
					1000,
					
					function() {
						
						banner_roll(no);
						
					}
				);	
			}
		)
	}
	
	$('.banner_text_bg').css({'opacity':'0.7'});
	$('#banner_navi img').css({'opacity':'0.5'});
	
	if($('#banner_roll div').size() > 1) {
		banner_roll(0);	
	}
	
	$('#banner_navi img').mouseover(function(e) {
		
		$(this).css({'opacity':'1'});
		
	});
	$('#banner_navi img').mouseout(function(e) {
		
		$(this).css({'opacity':'0.5'});	
		
		banner_navi(banner_no);
		
	});
	$('#banner_navi img').click(function(e) {
		e.preventDefault();
		var no;
		
		$('#banner_roll div').css({'opacity':'1'});
		no = $('#banner_navi img').index($(this));
		
		if($(this).parent('a').parent().get(0).tagName == 'EM') {
			
			order = -1 * Math.floor(($('#banner_roll').css('left').replace(/px/,'') - 0) * 0.001);
			
			size = $('#banner_navi span').size() - 1;
			
			if(no == 0) {
				no = order - 1;	
			} else {
				no = order + 1;	
			}
			if(no < 0) {
				no = 0;
			} else if(no > size) {
				no = size;
			}
		} else {
			no--;
		}
		banner_no = no;
		
		$('#banner_roll').stop();
		$('#banner_roll').stop();
		$('#banner_roll').stop();
		
		banner_text(no);
		
		banner_navi(no);
		
		width = $('#banner' + no).width();
		left = -1 * ((no) * width);
		
		$('#banner_roll')
		.animate(
			{
				'left':left + 'px'
			},
			1000,
			function() {
				
				banner_img_reset(no);
				banner_text(no);
		
				banner_navi(no);
		
				$('#banner_roll')
				.animate(
					{
						opacity:1
					},
					10000,
					function() {
				
						banner_roll(no);						
							
					}
				);	
			}
			
		)
		
	});
	$('#banner_navi a').click(function(e) {
		e.preventDefault();
	});
});

