Shadowbox.init({
	language: 'ru',
	adapter: 'mootools',
	skipSetup: true
});
window.addEvent('domready', function() {

// hrush
$(document.body).addEvent('mousemove', function(e) {
	var lft = parseInt(0 + 469 * (e.page.x / window.cursize));
	window.imhrush1.setStyle('left', lft);
	window.imhrush3.setStyle('left', lft);
});
window.addEvent('resize', function(e) {
	window.cursize = window.getSize().x;
});
window.fireEvent('resize');
window.imhrush1 = $('hrush1');
window.imhrush3 = $('hrush3');

// H1
try {
var h1_cont_width = (h1_cont = $('h1').getFirst()).clientWidth;
var h1_width = $('h1').clientWidth - 35;
if (h1_cont_width > h1_width) {
	var font_size = parseInt(h1_cont.getStyle('font-size'));
	var new_font_size = parseInt(font_size * h1_width / h1_cont_width);
	h1_cont.setStyle('font-size', new_font_size);

	//recheck
	h1_cont_width = (h1_cont = $('h1').getFirst()).clientWidth;
	while (h1_cont_width > h1_width) {
		new_font_size--;
		h1_cont.setStyle('font-size', new_font_size);
		h1_cont_width = (h1_cont = $('h1').getFirst()).clientWidth;
	}
}

} catch(e) {}


$('topmenu').addEvent('mouseenter', function() {
	$('top').addClass('inmenu');
	$('top').timer = $clear($('top').timer);
}).addEvent('mouseleave', function() {
	if ( !$('top').hasClass('inchangepane') || !$('panes') ) {
		$('top').timer = (function(){
			$('top').removeClass('inmenu');
		}).delay(600);
	}
});

// menu
$$('#topmenu ul.l2').each(function(ul) {

	var parent_li = ul.getParent();

	if ($('panes')) {
		parent_li.addEvent('mouseenter', function() {
			$('panes').addClass('inmenu');
		}).addEvent('mouseleave', function() {
			$('panes').removeClass('inmenu');
		});
	}

	ul.getLast().addClass('last');

	ul.set('morph', {
		duration: 'short'
	});

	ul.initHeight = ul.getSize().y;
	ul.setStyle('height', 0);

	parent_li.addEvent('mouseenter', function() {
		$clear(parent_li.leaveFunction);
		$clear($('top').timer);
		if (parent_li.hovered) return false;
		parent_li.hovered = true;
		ul.setStyle('opacity', 0);
		ul.morph({
			height: ul.initHeight,
			opacity: 1
		});
		try {
			$('panes').className = 'inmenu';
		} catch(ex) {}

	}).addEvent('mouseleave', function() {
		parent_li.leaveFunction = (function(){
			parent_li.hovered = false;
			ul.morph({
				height: 0,
				opacity: 0
			})

		}).delay(200);
	})

});


// h2
var current_h = 1;
$$('#content h2').each(function(item) {
	if (current_h == 4) return false;
	item.addClass('hn' + current_h);
	current_h++;
});

/**
 * Main page
 */
if ($(document.body).hasClass('main')) {
	var panescol = $('panes').getChildren();

	var curpane_n = Cookie.read('lastpane');
	if (!curpane_n) curpane_n = 0;
	var curpane = panescol[curpane_n];

	if (curpane_n != 0) {
		panescol[0].setStyle('visibility', 'hidden');
		curpane.setStyle('visibility', 'visible');
	}

	$('s' + curpane.id).addClass('active');

	for (var i = 0; i < panescol.length; i++) {
		panescol[i].n = i;
	}

	var changePane = function(to) {
		$('top').addClass('inchangepane');
		if (to == null) {
			curpane_n++;
			if (curpane_n == panescol.length) curpane_n = 0;
			var tween_duration = 'long';
		} else {
			if (curpane_n == to) return false;
			curpane_n = to;
			var tween_duration = 'short';
		}
		Cookie.write('lastpane', curpane_n);
		pane_to = panescol[curpane_n];

		pane_to.setStyles({
			zIndex: 1,
			opacity: 0,
			visibility: 'visible'
		});

		changePane.processing = true;

		pane_to.set('tween', {
			duration: tween_duration,
			onComplete: function() {
				$('top').removeClass('inchangepane');
				curpane.setStyle('visibility', 'hidden');
				$('s' + curpane.id).removeClass('active');
				this.element.setStyle('z-index', null);
				curpane = this.element;
				$('s' + curpane.id).addClass('active');
				changePane.processing = false;
			}
		});
		pane_to.tween('opacity', 1);
		pane_to.getFirst().set('morph', {duration: tween_duration});
		pane_to.getFirst().morph({
			'margin-top': [4, 0]
			//'font-size': [5, 14]
		});
		window.lastPane = pane_to;

	}

	var lastTimer = null;
	$$('#panes_select li').addEvent('mouseenter', function() {
		var el = this;
		$clear(lastTimer);
		lastTimer = (function(){
			$clear(changePane.timer);
			changePane($(el.id.match(/s(.*)/)[1]).n);
		}).delay(200);
	}).addEvent('click', function() {
		var testa = this.getElement('a');
		if (testa) {
			if (testa.target == '_blank') {
				open(testa.href, '_blank');
			} else {
				location = testa.href;
			}
		}
	}).setStyle('cursor', 'pointer');

	$('panes_select').addEvent('mouseleave', function() {
		$clear(lastTimer);
		$clear(changePane.timer);
		changePane.timer = changePane.periodical(5000);
	});

	$$('.pane .description').addEvent('mouseleave', function() {
		$clear(changePane.timer);
		changePane.timer = changePane.periodical(5000);
	}).addEvent('mouseenter', function() {
		$clear(changePane.timer);
	});

	changePane.timer = changePane.periodical(5000);

}

// галерея
$$('.pictures_list .item, .album_item, .event_item').each(function(item) {
	item.img = item.getElement('img');
	item.img.initWidth = parseInt(item.img.get('width'));
	item.img.initHeight = parseInt(item.img.get('height'));

	item.img.set('morph', {duration: 100});

	var widthEnlarge = 10;
	var heightEnlarge = 10;

	var leftOffset = -parseInt(widthEnlarge / 2);
	var topOffset = -parseInt(heightEnlarge / 2);

	if (item.img.initWidth < 108) leftOffset = 0;

	item.addEvent('mouseenter', function() {

		this.img.setStyle('border-color', '#fff');
		this.img.morph({
			width: [this.img.initWidth, this.img.initWidth + widthEnlarge],
			height:[this.img.initHeight, this.img.initHeight + heightEnlarge],
			top: topOffset,
			marginBottom: -heightEnlarge,
			borderColor: '#b90000'
		});

	}).addEvent('mouseleave', function() {

		item = this;
		(function(){item.img.morph({
			width: [item.img.initWidth + widthEnlarge, item.img.initWidth],
			height: [item.img.initHeight + heightEnlarge, item.img.initHeight],
			top: 0,
			left: 0,
			marginBottom: 0,
			borderColor: '#fff'
		});}).delay(100);

	})




});


if (1 || !navigator.userAgent.match(/MSIE 6/)) {
	// включаем shadowbox
	var Shadowbox_options = {
	  resizeDuration: 0.2,
	  fadeDuration: 0.2,
	  viewportPadding: 10,
	  //handleOversize: 'none',
	  overlayOpacity: 0.5,
	  viewportPadding: 5,
	  autoDimensions: true,
		animate: false,
		onFinish: function() {
		window.fireEvent.delay(10, window, 'resize');
		}
	};

	list = $$('a');
	list = list.filter(function(item){
		if (item.className == 'no') return false;
	  return item.href.test(/\.(jpe?g|png|gif)$/)
	});

	if (list && list.length) {
		Shadowbox.setup(list, Shadowbox_options);
	}

	smscroll = new SmoothScroll();

	new Tips('*[title]',{className: 'atip'});
}



});

