// PRARCH

$(document).ready(function() {
	PrarchCoarse.init();
	PrarchLecturer.init();
});

var ceilTo15 = function(n){
	return Math.ceil((n)/15)*15;
}

var PrarchCoarse = {

	classItem: 'prarch-item-coarse',
	classItemOpened: 'prarch-item-coarse-on',
	closedHeight: 30,

	init: function(el){
		$('.'+this.classItem).each(function(){
			var myClosedHeight = ceilTo15($(this).children('.prarch-box-link').children('.prarch-coarse-dozlinks').height());
			if(!myClosedHeight) myClosedHeight = PrarchCoarse.closedHeight;
			$(this).height(myClosedHeight);
			$(this).click(function(){ PrarchCoarse.open(this); });
			$(this).hover(function(){
				$(this).addClass('prarch-item-hover');
			}, function(){
				$(this).removeClass('prarch-item-hover');
			})
		});
	},

	open: function(el){
		if($(el).hasClass(this.classItemOpened)) return false; 
		this.closeAll();
		$(el).addClass(this.classItemOpened);
		var maxHeight = 0;
		$(el).children('.prarch-box').each(function(){
			var boxHeight = $(this).height();
			if(boxHeight > maxHeight) maxHeight = boxHeight;
		});
		//openHeight = ceilTo15(maxHeight+10);
		openHeight = maxHeight+10;
   		$(el).animate({height: openHeight+'px'}, 300);

		setTimeout(function() {
			var targetOffset = $(el).offset().top - 30;
			var currentScrollTop = $('html').scrollTop();
			if(!currentScrollTop) currentScrollTop = $('body').scrollTop();
			if(targetOffset < currentScrollTop){
				$('html,body').animate({scrollTop: targetOffset}, 500);
			}
		}, 300);
	},

	closeAll: function(){
		$('.'+this.classItemOpened).each(function(){
			PrarchCoarse.close(this);
		});
	},

	close: function(el){
		var myClosedHeight = ceilTo15($(el).children('.prarch-box-link').children('.prarch-coarse-dozlinks').height());
		if(!myClosedHeight) myClosedHeight = PrarchCoarse.closedHeight;
   		$(el).animate({height: myClosedHeight+'px'}, 300).removeClass(this.classItemOpened);
	}

}


var PrarchLecturer = {

	classItem: 'prarch-item-lecturer',
	classItemOpened: 'prarch-item-lecturer-on',
	closedHeight: 240,

	init: function(el){
		$('.'+this.classItem).each(function(){
			$(this).height(PrarchLecturer.closedHeight);
			$(this).click(function(){ PrarchLecturer.open(this); });
			$(this).hover(function(){
				$(this).addClass('prarch-item-hover');
			}, function(){
				$(this).removeClass('prarch-item-hover');
			})
		});
	},

	open: function(el){
		if($(el).hasClass(this.classItemOpened)) return false; 
		this.closeAll();
		$(el).addClass(this.classItemOpened);
		var maxHeight = 0;
		$(el).children('.prarch-box').each(function(){
			var boxHeight = $(this).height();
			if(boxHeight > maxHeight) maxHeight = boxHeight;
		});
		//openHeight = Math.ceil((maxHeight+10)/15)*15
		if(maxHeight > this.closedHeight){
			openHeight = maxHeight+18;
		} else {
			openHeight = maxHeight+2;
		}
		$(el).animate({height: openHeight+'px'}, 300);

		setTimeout(function() {
			var targetOffset = $(el).offset().top - 30;
			var currentScrollTop = $('html').scrollTop();
			if(!currentScrollTop) currentScrollTop = $('body').scrollTop();
			if(targetOffset < currentScrollTop){
				$('html,body').animate({scrollTop: targetOffset}, 500);
			}
		}, 300);
	},

	closeAll: function(){
		$('.'+this.classItemOpened).each(function(){
			PrarchLecturer.close(this);
		});
	},

	close: function(el){
   		$(el).animate({height: this.closedHeight+'px'}, 300).removeClass(this.classItemOpened);
	}

}