$(function () {
	$('#page_index h2 a img').each(function() {
		
		var src;
		src = $(this).attr('src');
		
		if(src) {
			src = src.replace(/.gif/, '1.gif');	
		}
		
		$(this).parent().parent()
		.css(
			{
				'background':'url(' + src + ')',
				'background-position':'left top',
				'background-repeat':'no-repeat'
			}
		);
			
	});
	$('#page_index h2 a img').mouseover(function() {
		
		$(this).css('opacity', 0);
			
	});
	$('#page_index h2 a img').mouseout(function() {
	
		$(this).css('opacity', 1);
		
	});
	
	function next_body_roll(ids, no) {
		//alert(ids + ' : ' + no);
		var no;
		var widh_next
		var ids;
		var size;
		
		size = $('#' + ids + ' .body_rolls div').size();
		
		$('#' + ids + ' .body_roll .body_rolls div').eq(size - 1).attr('id');
		
		get_id = $('#' + ids + ' .body_roller:eq(' + no + ')').attr('id');
		
		if(no > 0 && get_id.match(/0$/)) {
			
			//alert('test');
			$('#' + ids + ' .body_rolls').css({'left':'0'});
			$('#' + ids + ' .body_roller:last').remove();
			width = $('#' + ids + ' .body_roller:first').width();
			$('#' + ids + ' .body_rolls').width($('#' + ids + ' .body_rolls').width() - width);
			no = 0;
			
		} else if(get_id.search(size-1) > 0) {
			
			$('#' + ids + ' .body_roller:first').clone().appendTo('#' + ids + ' .body_rolls');
			
			
			width = $('#' + ids + ' .body_roller:first').width();
			//alert('ids: ' + ids + ' width: ' + width + ' body_rolls: ' + $('#' + ids + ' .body_rolls').width());
			$('#' + ids + ' .body_rolls').width($('#' + ids + ' .body_rolls').width() + width);

			
		}
						    		
		return no;
			
	}
	function get_index_id(Jthis) {
		var ids;
		Jthis.parents('div').each(function() {
			if($(this).attr('id')) {
				if($(this).attr('id').match(/index_/)) {
					//alert($(this).attr('id'));
					ids = $(this).attr('id');
				}
			}
			
		});
		return ids;
	}
	function move_square(ids, no) {
		var size;
		var no;
		var ids;
		
		size = $('#' + ids + ' .scroll_navi span img').size();
		
		if(no >= size) {
			no = 0;
		}
		$('#' + ids + ' .scroll_navi span img').each(function(index) {
			
			if(index == no) {
				
				$(this).css({'opacity':'1'});
				
			} else {
				
				$(this).css({'opacity':'0.5'});
				
			}
		
		});
		
	}
	function move_text(ids, no) {
		var ids;
		var no;
		var set_id;
		//alert('test');
		size = $('#' + ids + ' .scroll_navi span img').size();
		
		if(no >= size) {
			no = 0;
		}
		//alert('#' + ids + '_text' + no);
		set_id = ids + '_text';
		//alert(set_id);
		//$('#index_about_text4').hide();
		$('[id^=' + set_id + ']').each(function(index) {
			if(index == no) {
				//alert('test');
				$(this).show();
			} else {
				$(this).hide();	
			}
		});
		//$('#' + set_id).show();
			
	}
	function body_roll(ids, no) {
		var no;
		var ids;
		
		move_square(ids, no);
				
		$('#' + ids + ' .body_rolls')
		.animate(
			{
				'top':'0px'
			},
			20000,
			function() {
				
				
				no = next_body_roll(ids, no);
				
				body_rolls_left = -384 * (no + 1);
				no++;
				
				move_square(ids, no);						
				
				move_text(ids, no);		
				
				
				
				$('#' + ids + ' .body_rolls')
				.animate(
					{
						'left':body_rolls_left + 'px'
					},
					1000,
					function() {
						
						
						body_roll(ids, no);
						
					}
				);
			}
		);
	}
	function body_rolls() {
		var no;
		var type;
		
		$('.body_rolls').each(function() {
			ids = $(this).parent('div').parent('div').attr('id');
			
			body_roll(ids, 0);
			
			$('#' + ids + ' .scroll_navi img').mouseover(function() {
				
				$(this).css({'opacity':'0.9'});
				
				
			});
			$('#' + ids + ' .scroll_navi img').mouseout(function() {
				var square_nos;
				var ids;
				
				if($(this).parent().parent().get(0).tagName == 'EM') {
					$('.scroll_navi em img').css({'opacity':'0.5'});
					
				} 
				ids = get_index_id($(this));
				$('#' + ids + ' .scroll_navi span img').each(function(index) {
					if($(this).css('opacity') == 1) {
						square_nos = index;
					}
					
				});
					
				move_square(ids, square_nos);	
				
					
				
			});
			$('#' + ids + ' .scroll_navi img').click(function(e) {
				
				var no;
				var order;
				e.preventDefault();
				
								
				ids = get_index_id($(this));
				
				$('#' + ids + ' .body_rolls').stop();
				$('#' + ids + ' .body_rolls').stop();
				$('#' + ids + ' .body_rolls').stop();
				//alert(no + ' no');
				if($(this).parent().parent().get(0).tagName == 'EM') {
					
					order = -1 * Math.floor(($('#' + ids + ' .body_rolls').css('left').replace(/px/,'') - 0) / 384);
					
					//alert(order);
					
					prev_next = $('#' + ids + ' .scroll_navi em img').index($(this));
					//alert(prev_next);
					order = order - 0;
					if(prev_next == 0) {
						order--;
						
					} else {
						//alert('here');
						//alert(no + ' no' );
						size = $('#' + ids + ' .scroll_navi span img').size();
						//alert(order);
						order++;
						//alert(order + ' +');
						
					}
					//alert(size);
					//alert(order);
					//alert(no);
					if(order <= 0) {
						//alert(order);
						order = 0;
						//alert(order);
					} else if(order >= size) {
						
						order = size -1;
					}
					//alert(order);
					no = order;
				} else {
					
					no = $('#' + ids + ' .scroll_navi span img').index($(this));
					
				}
				//alert(order);
				//alert(no);
				//no = 0;
				
				
				
				
				
				body_rolls_left = -384 * no;
				
			
				no = next_body_roll(ids, no);
				
				square_no = no;
				move_square(ids, no);						
				move_text(ids, no);		
				
				$('#' + ids + ' .body_rolls')
				.animate(
					{
						'left':body_rolls_left + 'px'
					},
					1000,
					function() {
						
						square_no = no;
						move_square(ids, no);						
						$('#' + ids + ' .body_rolls')
						.animate(
							{
								'top':'0px'
							},
							20000,
							function() {
								
								body_roll(ids, no);
								
							}
						);
					}
				);
						
			});
		});
		
		
	}
	
	$('.scroll_navi img').css({'opacity':'0.5'});
	$('[id^=index_services_text]').each(function(index) {
		if(index != 0) {
			$(this).hide();
		}
	});
	$('[id^=index_about_text]').each(function(index) {
		if(index != 0) {
			$(this).hide();
		}
	});
	
	body_rolls();

	$('.scroll_navi a').click(function(e) {
		e.preventDefault();
	});
	
	$('.body_roller').show();
	
	
});
