$('document').ready(function(){
	
	// FX for #menu.
	$("#menu li").hover(
		function(){
			//$(this).parent().
			$(this).find("ul").fadeIn("fast");
			
			var imgsrc = $(this).find('a img').attr('src').replace('.png','_on.png');
			$(this).find('a img').attr('src', imgsrc);
		}, 
		function(){
			$(this).parent().find("ul").hide();
			var imgsrc = $(this).find('a img').attr('src').replace('_on.png','.png');
			$(this).find('a img').attr('src', imgsrc);
		}
	);
	$("#menu li").hoverClass("sfhover");
		
	// FX for .pagetop.
	if($('a.pagetop').length){
		$('a.pagetop').removeAttr('href');
		$('a.pagetop').click(function() { $.scrollTo('#header', 500); });
	}
	
	// FX for #topbnr.
	if($('#pr-wrapper').length){
		
		// Add banner images.
		$('#pr-images').append('<a id="pr-ph1" href="/courses/geneki/"><img src="/images/topbnr_ph_1.jpg" /></a>');
		$('#pr-images').append('<a id="pr-ph2" href="/courses/hse/"><img src="/images/topbnr_ph_2.jpg" /></a>');
		$('#pr-images').append('<a id="pr-ph3" href="/courses/dormitory/"><img src="/images/topbnr_ph_3.jpg" /></a>');

		var targetID = 2;
		
		// Switching images.
		var intervalID = setInterval(function(){
			if(targetID>3){targetID=1;}
			switchImage(targetID);
			targetID++;
		}, 5000);
		
		// Attach events to buttons.
		$('#pr-panel a').each(function(){
			$(this).css({ 'text-indent': '-9999px'});
			$(this).mouseenter(function(){
				targetID = $(this).parent().attr('id').replace('pr-btn','');
				switchImage(targetID);
				clearInterval(intervalID);
			});
			$(this).mouseleave(function(){ 
				targetID++;
				intervalID = setInterval(function(){
					if(targetID>3){targetID=1;}
					switchImage(targetID);
					targetID++
				}, 5000);
			});
		});
		
		function switchImage(i){
			$("#pr-images").clearQueue().stop();
			
			$("#pr-images").animate({
				'top': '-'+286*(i-1)+'px'
			}, 500);
			$('#pr-panel li').each(function(k,v){
				if($(this).attr('id')=='pr-btn'+i){
					if(i==1){ pos = '0'; }else{ pos=(i-1)*91; }
					$("#pr-btn"+i+' a').css({
						'backgroundPosition' : '-212px -'+pos+'px'
					});
					$(this).parent().animate({
						'backgroundPosition': '0 -'+(440-i*90)+'px'
					}, 0);
				}else{
					$("#pr-btn"+(k+1)+' a').css({
						'backgroundPosition' : '0 -'+(~~k)*91+'px'
					});
				}
			});
		}
		switchImage(1);
	}
			
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		//alert('');
		$(this).hover( 
			function() {
				$(this).addClass(c);
			},
			function() { $(this).removeClass(c); }
		);
	});
};	
