function objectWA(str) {
	document.write(str);
}

function galleryInit() {
	gallery_items = $$('.gallery a');
	if (gallery_items.length) {
		gallery_items.each(function(item){
			item.addEvent('click', galleryItemClick.bindWithEvent(item));
		});
		if (!$('image_area')) {
			var image_area_o = new Element('div', {'id': 'image_area_o'});
			var image_area_m = new Element('div', {'id': 'image_area_m'});
			var image_area_i = new Element('div', {'id': 'image_area_i'});
			image_area_o.injectBefore($('subpage_nav'));
			image_area_m.injectInside(image_area_o);
			image_area_i.injectInside(image_area_m);
			var image_controls = new Element('div', {'id': 'image_controls'});
			image_controls.injectBefore($('subpage_nav'));
			var controlImgPrev = new Element('img', {'src': './images/control_img_prev.gif', 'width': '16', 'height': '16', 'class': 'control_img_prev'});
			var controlImgNext = new Element('img', {'src': './images/control_img_next.gif', 'width': '16', 'height': '16', 'class': 'control_img_next'});
			controlImgNext.injectInside(image_controls);
			controlImgPrev.injectInside(image_controls);
			controlImgNext.addEvent('click', galleryNext);
			controlImgPrev.addEvent('click', galleryPrevious);
		}
		current_item = gallery_items[0];
		if (!$$('.gallery')[0].hasClass('inactive')) {
			galleryShowCurrentItem();
		}
	}
}

function galleryItemClick(evt) {
	evt.preventDefault();
	try {
		current_item.removeClass('selected');
	} catch (e) {}
	current_item = evt.target;
	galleryShowCurrentItem();
}

function galleryNext() {
	if (current_item) {
		current_item.removeClass('selected');
		var index = gallery_items.indexOf(current_item);
		if (index == gallery_items.length - 1) {
			current_item = gallery_items[0];
		} else {
			current_item = gallery_items[index + 1];
		}
		galleryShowCurrentItem();
	}
}

function galleryPrevious() {
	if (current_item) {
		current_item.removeClass('selected');
		var index = gallery_items.indexOf(current_item);
		if (index == 0) {
			current_item = gallery_items[gallery_items.length - 1];
		} else {
			current_item = gallery_items[index - 1];
		}
		galleryShowCurrentItem();
	}
}

function galleryShowCurrentItem() {
	$('image_area_i').empty();
	var img = new Element('img', {src: current_item.href});
	img.injectInside($('image_area_i'));
	current_item.addClass('selected');
}

function galleryImageLoad() {
	var imgSize = this.getSize().size;
	var imgAreaSize = $('image_area').getSize().size;
	this.setStyle('margin-top', Math.round((imgAreaSize.y - imgSize.y)/2) + 'px');
}

window.addEvent('domready', galleryInit);
