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

	// включаем shadowbox
	var Shadowbox_options = {
	resizeDuration: 0.2,
	fadeDuration: 0.2,
	viewportPadding: 10,
	handleOversize: 'drag',
	overlayOpacity: 0.5,
	viewportPadding: 5,
	autoDimensions: true,
		animate: false
	};

	list = $$(document.links);
	list = list.filter(function(item){
	//if (item.hasClass('no')) return false;
	return item.href.test(/\.(jpe?g|png|gif|swf)$/)
	});
	if (list && list.length) {
		Shadowbox.setup(list, Shadowbox_options);
	}

	new SmoothScroll({
	onStart: function() {
		if (this.oldanchor) {
			this.oldanchor.removeClass('anchored');
		}
		this.oldanchor = $(this.anchor).addClass('anchored');
	}
	});


	$$('#hmenu li').addEvent('click', function(e) {
	if (e.target.tagName != 'A') location = this.getLast().href;
	});



/*-------------------------------------------------------------------------*/
// Список альбомов сверху
/*-------------------------------------------------------------------------*/
(function() {
	var h2_active = null;
	var icat = $('catalog');
	var h2_first = null;
	var h2_last = null;

	icat.getElements('h2').each(function(h2) {
		if (!h2_first) h2_first = h2;

		h2.textBody = h2.getNext();
		if (!h2.textBody || h2.textBody.tagName != 'DIV') h2.textBody = null;

		h2.ael = h2.getFirst();

		if (h2_last) {
			h2.inject(h2_last, 'after');
		} else {
			h2.inject(icat, 'top');
		}

		if (h2.textBody) h2.textBody.setStyle('display', 'none').addClass('injs');

		h2_last = h2;

		if (h2.hasClass('current')) h2_active = h2;

		h2.addEvent('click', function(e) {

			if (e) {
				if (this.ael) {
					location = this.ael.href;
					return true;
				}
				if (e.target.tagName == 'A') return true;
			}

			h2_active.removeClass('current');
			if (h2_active.textBody) h2_active.textBody.setStyle('display', 'none');
			this.addClass('current');
			if (this.textBody) this.textBody.setStyle('display', '');

			if (this == h2_first) {
				icat.ug1.className = 'ug_al';
			} else {
				icat.ug1.className = 'ug_l';
			}
			if (this == h2_last) {
				icat.ug2.className = 'ug_ar';
			} else {
				icat.ug2.className = 'ug_r';
			}

			h2_active = this;

		});

	});

	if (!h2_active) {
		h2_active = h2_first;
	}
	icat.ug1 = new Element('div', {'class': 'ug_al'}).inject(h2_first, 'before');
	icat.ug2 = new Element('div', {'class': 'ug_r'}).inject(h2_last, 'after');
	h2_active.fireEvent('click');

})();



/*-------------------------------------------------------------------------*/
// Кнопки в тексте
/*-------------------------------------------------------------------------*/
	var itb = $('textbuttons');
	$$('#text h2').each(function(h2) {
		h2.set('html', '<span>' + h2.get('html') + '</span><b><i></i></b>');
		h2.txt = h2.getNext();
		h2.txt.setStyle('display', 'none');
		h2.inject(itb);

		itb.active = h2;

		h2.addEvent('click', function() {
			itb.active.removeClass('active');
			itb.active.txt.setStyle('display', 'none');

			this.addClass('active');
			this.txt.setStyle('display', '');
			itb.arrow.inject(this, 'before');
			itb.active = this;
		});
	});

	if (itb && itb.active) {
		itb.active = itb.getFirst();
		itb.arrow = new Element('i', {'class': 'arrow'}).inject(itb.active, 'before');
		itb.active.fireEvent('click');

		$$('#text .body').setStyle('min-height', itb.clientHeight);
		if (navigator.userAgent.match(/MSIE 6/)) $$('#text .body').setStyle('height', itb.clientHeight);
	}



/*-------------------------------------------------------------------------*/
// Галерея
/*-------------------------------------------------------------------------*/
	if (ifullpic = $('fullpic')) {

		// скролл для списка альбомов
		var iabody = $('abody');
		var active_album = iabody.getElement('li.active');

		var ascroller = new Scroller(iabody, {
			area: 130,
			velocity: 0.05,
			horizontal: false
		});
		ascroller.start();
		new Fx.Scroll(iabody).toElement(active_album);

		var thumbs_total = 0;
		var thumb_number = 1;
		var thumb_first = null;

		var thumb_height = 74;

		$$('#picturelist a').each(function(item) {
			var pic = item.getFirst();
			item.pic = pic;
			item.hoverpic = new Asset.image(pic.src.replace(/\/thumb_2/, '/thumb_1'));
			item.normalpic = new Asset.image(pic.src);

			item.num = thumb_number++;
			if (item.num == 1) {
				thumb_first = item;
				thumb_first.set('tween', {
					duration: 500,
					transition: 'sine:out'
				});
			}
			thumbs_total++;

			item.fullpic = new Asset.image(item.href);

			item.removeEvents('click');

			item.addEvent('mouseenter', function(e) {
				this.pic.src = this.hoverpic.src;

			}).addEvent('mouseleave', function(e) {
				if (ifullpic.activeItem != this) this.pic.src = this.normalpic.src;

			}).addEvent('click', function(e) {
				ifullpic.src = this.fullpic.src;
				var oldActive = ifullpic.activeItem;
				ifullpic.activeItem = this;
				this.fireEvent('mouseenter');
				this.addClass('active');
				if (oldActive) {
					oldActive.fireEvent('mouseleave');
					oldActive.removeClass('active');
				}
				if (e) e.stop();

				// двигаем
				if (thumbs_total > 5) {
					if (this.num <= 3) {
						thumb_first.tween('margin-top', 1);

					} else if (this.num + 2 > thumbs_total) {
						var offst = thumbs_total - 5;
						thumb_first.tween('margin-top', -offst * thumb_height);

					} else if (thumbs_total != 4) {
						var offst = this.num - 3;
						thumb_first.tween('margin-top', -offst * thumb_height);
					}
				}

			});

			if (!ifullpic.activeItem) {
				item.fireEvent('click');
			}

		});

		var topButton = $('picturelist').getFirst();
		var bottomButton = $('picturelist').getLast();

		$$('#picturelist .tbutton, #picturelist .bbutton').addEvent('mousedown', function(e) {
			this.addClass('down');

		}).addEvent('mouseleave', function(e) {
			this.removeClass('down');

		});

		topButton.addEvent('mouseup', function(e) {
			this.removeClass('down');
			try {
				if (prv = ifullpic.activeItem.getPrevious()) prv.fireEvent('click');
			} catch (ex) {}
		});
		bottomButton.addEvent('mouseup', function(e) {
			this.removeClass('down');
			try {
				if (nxt = ifullpic.activeItem.getNext()) nxt.fireEvent('click');
			} catch (ex) {}
		});

		// buttons
		var keycatcher = Browser.Engine.trident ? document.body : window;
		$(keycatcher).addEvent('keydown', function(e) {
			if (e.control) {
				if (e.key == 'up') {
					topButton.fireEvent('mouseup');
					e.stop();
				}
				else if (e.key == 'down') {
					bottomButton.fireEvent('mouseup');
					e.stop();
				}
			}
		});



	}


/*-------------------------------------------------------------------------*/
// Главная
/*-------------------------------------------------------------------------*/
	if (imainpic = $('mainpic')) {
		var mbuttons = imainpic.getLast().getChildren();
		var piclink = $('mainpic_link');
		var piclink_img = piclink.getFirst();
		var picname = imainpic.getFirst();

		mbuttons.each(function(item) {
			item.imgname = item.title;
			item.bigimg = new Asset.image('/images/u/gallery/' + item.get('rel'));
			item.addEvent('click', function(e) {
				piclink.href = this.href;
				piclink_img.src = this.bigimg.src;
				picname.innerHTML = this.imgname;
				e.stop();
			}).addEvent('dblclick', function(e) {
				location = this.href;
			});

		});
	}

	if (!navigator.userAgent.match(/MSIE 6/)) new Tips('*[title]',{className: 'atip'});

//////////



});
