

$(function(){
	
	var writePromotion = function () {
	
		$imgPath = 'share/img/home/'
		
		jQuery.preloadImages = function(){
			for(var i = 1; i < 8; i++){
				$newImg = jQuery('<img id='+'"n'+i+'" class="photo new_img">').attr('src', $imgPath+'new_'+i+'.jpg').css('opacity', 0);
				$oldImg = jQuery('<img id='+'"o'+i+'" class="photo old_img">').attr('src', $imgPath+'old_'+i+'.jpg').css('opacity', 0);
				$('.wrapper').append($newImg);
				$('.wrapper').append($oldImg);
			}
			$('#promotion .wrapper').append('<img id="text1" class="text text1" src="'+$imgPath+'text1.gif" alt="Department of Neurosurgery" />',
											'<img id="text1_2" class="text text2" src="'+$imgPath+'text1_2.gif" alt="Department of Neurosurgery" />',
											'<img id="text2" class="text text1" src="'+$imgPath+'text2.gif" alt="Niigata University" />',
											'<img id="text2_2" class="text text2" src="'+$imgPath+'text2_2.gif" alt="Niigata University" />'
			);
			$('.text').css('opacity', 0);
		}
		
		$.preloadImages();
		
		
		$('#promotion .copy').fadeIn(3000, function(){
			$('#promotion .wrapper').delay(500).animate({ opacity: 0 },1500, function(){
				$('#promotion .copy').css({
					opacity : 0,
					width : '269px',
					height : '16px',
					background : 'url('+$imgPath+'copy_s.png)',
					left : '635px',
					top : '121px'
				});
				$('#promotion .wrapper').css({ backgroundColor : '#FFF', opacity: 1 });
				$('#o1').animate({ top: '6px', opacity : 1 }, 800);
				$('#o2').delay(100).animate({ left : 0, opacity : 1 }, 800);
				$('#o3').delay(200).animate({ left : '262px', opacity : 1 }, 800);
				$('#o4').delay(300).animate({ top : '172px', opacity : 1 }, 800);
				$('#o5').delay(400).animate({ left : '434px', opacity : 1 }, 800);
				$('#o6').delay(500).animate({ top : '6px', opacity : 1 }, 800);
				$('#o7').delay(600).animate({ top : '159px', opacity : 1 }, 800, function(){
				$('.new_img').css('opacity', 1);	
				$('#text1').animate({ opacity : 1 }, 1500);
					$('#text2').animate({ opacity : 1 }, 1500, function(){
						$('.text2').css('opacity', 1);
						$('#promotion .copy').animate({ opacity : 1 }, 1000, function(){
							for(var i = 1; i < 8; i++) {
								$('#o'+i).delay(i*100).animate({opacity : 0}, 1000);
							}
							$('#text1').delay(1800).animate({ opacity : 0 }, 500);
							$('#text2').delay(1900).animate({ opacity : 0 }, 500, function(){
								
								setTimeout(function(){ changeImg() }, 3000);
								
								function changeImg($prevNum) {
	
									$Num = Math.floor(Math.random() * 7) + 1
									if($prevNum == $Num) {
										changeImg($Num);
										return;
									} else {
										$('#o'+$Num).animate({ zIndex : 1, opacity : 1 }, 1500, function(){
											$('#n'+$Num).animate({ opacity : 0 , zIndex : 2 }, 100, function(){
												$('#n'+$Num).animate({ opacity : 1 }, 1500, function(){
													$('#o'+$Num).animate({ zIndex : 0, opacity : 0 }, 1500, function(){
														$('#n'+$Num).animate({ zIndex : 0}, 100, function(){
															changeImg($Num);
														});
													});
												});
											});
										});
									}
								}
							});
						});
					});
				});
			});
		});
	}
	
	function setupInformation() {
		var INFO = {};

		INFO.getLatestYear = function() {
			return parseInt($('#information ul:first a:first').text());
		};
		
		INFO.show = function(year) {
			var latestYear = INFO.getLatestYear();
			var tabs = $('#information ul:first a');
			var tabIndex = latestYear - year;
						
			for (var i = 0; i < tabs.length; i++) {
				var tab = tabs[i];
				if (i == tabIndex) {
					$(tab).addClass('selected');
				} else {
					$(tab).removeClass('selected');
				}
				$('#info' + (latestYear - i)).css('display', (i == tabIndex) ? 'block' : 'none');
			}
		};
		
		var tabs = $('#information ul:first a');
		
		$.each(tabs, function() {
			$(this).click(function(){
				INFO.show(parseInt($(this).text()));
				return false;
			});
		});
		
		
		INFO.show(INFO.getLatestYear());
	}

	
	writePromotion();
	setupInformation();
	
});

