page_images = [];
$(document).ready(function(){
	//console.log('ready');
	var image_number = 0;
	/*$('div.more2').each(function(){
		this.image_number = image_number;
		image_number ++;
	});*/

	if ( (pageType != 'supra') && (pageType != 'toasting')) {
		$('div.more2').click(function(){
			page_images = topic_images[this.getAttribute('topic_id')];
			showPopunder(0);
			return false;
		});


		var machvi_images = [];
		var machvi_contents = [];
		$('.machvi_wrapper .machvi_content').each(function(){
			machvi_contents.push(this.innerHTML);
		});
		$('.machvi_wrapper .machvi_image').each(function(){
			machvi_images.push(image_big(this.src));
		});
		for (var i = 0; i < machvi_images.length; i++) {
			page_images[i] = [machvi_images[i], machvi_contents[i]];
		}
	}

	/*
	var image_number = 0;
	$('.machvi_wrapper .moreimages').each(function(){
		this.image_number = image_number;
		image_number ++;
	});
	image_number = 0;
	$('.machvi_wrapper').each(function(){
		this.image_number = image_number;
		image_number ++;
	});*/


	//console.dir(page_images);

	if (typeof topic_cur == 'undefined') {
		topic_cur = '';
	}
	/*$('a.showimages').css('display', 'block');
	$('a.showimages').click(function(){
		page_images = topic_images[this.getAttribute('topic_id')];
		//showPopunder(0);
		return false;
	});*/

	$('.machvi_wrapper').each(function(){
		this.machvi_open = false;
		this.machvi_selected = false;
	});
	$('.machvi_wrapper .handle').css('display', 'block');
	$('.machvi_wrapper .moreimages').click(function(){
		console.dir(topic_images[this.getAttribute('topic_id')]);
		page_images = topic_images[this.getAttribute('topic_id')];
		showPopunder(0);
	});
	$('.machvi_wrapper a').click(function(event){
		event.preventDefault();
	});

	$('.machvi_wrapper').hover(
		function() {
			//console.log(this.getAttribute('topic_id'));
			if (this.getAttribute('topic_id') == topic_cur) return;
			machvi_open(this.getAttribute('topic_id'));
		},
		function() {

			if (this.getAttribute('topic_id') == topic_cur) return;
			if (this.machvi_open) machvi_close(this.getAttribute('topic_id'));
		}
	).click(
		function() {
			//debugger;
			if ( (pageType != 'supra') && (pageType != 'toasting') ) {
				topic_select(this.getAttribute('topic_id'));
			}
		}
	);
	$('.toasting_image').click(function() {
		showPopunder(this.image_number);
	});
	$('.moreimages').click(function(){
		var wrapper = $(this).find()
	});
	if (typeof autoSelect != 'undefined') {
		topic_select(autoSelect);
	}
});


function topic_select(topic_id) {

	if (topic_cur == topic_id) return;

	machvi_select(topic_id);
	//////

	topic_content_show(topic_id);

	topic_cur = topic_id;

	var el = $('#machvi_' + topic_id);
	//console.dir(el.find('.machvi_content span'));
	el.find('.machvi_title').stopAll().animate({top:-15}, 300, function(){ el.find('.samk').css('display', 'block');  });
	el.find('.machvi_content span').css('borderBottom', 'none');

	if (typeof topic_bg != 'undefined') {
		$('#cave-bottom').css({
			backgroundImage:  'url(' + topic_bg[topic_id][0] + ')'
			//width: topic_bg[topic_id][1] + 'px',
			//height: topic_bg[topic_id][2] + 'px'
		});

		var s = document.createElement('div');
		s.style.height = topic_bg[topic_id][2].toString() + 'px';
		document.getElementById('cave-bottom-space').innerHTML = '';
		document.getElementById('cave-bottom-space').appendChild(s);


		//document.getElementById('cave-bottom-space').style.marginBottom =  parseInt(300) + 'px';
		//document.getElementById('cave-bottom-space').style.marginBottom =  '0px';
		//document.getElementById('cave-bottom-space').style.marginBottom =  parseInt(300) +'px';
		//alert(topic_bg[topic_id][2].toString() + 'px');
		/*$('#cave-bottom-space').css({
			height: topic_bg[topic_id][2].toString() + 'px'
		});*/
	}
}
function machvi_open(topic_id) {
	var el = $('#machvi_' + topic_id);
	var t = topic_id + ' ---  ' + el;
	var t2 = document.getElementById('machvi_' + topic_id);
	var t3 = document.getElementById('machvi_svaneti');
	//alert(t3);
	//return;
	//debugger;
	if (el[0].machvi_open) return;
	el[0].machvi_open = true;
	el.find('.machvi_content span').css('borderBottom', '1px dashed white');
}

function machvi_close(topic_id) {

	var el = $('#machvi_' + topic_id);
	//alert(1);
	if ( ! el[0].machvi_open) return;
	//alert(2);
	el[0].machvi_open = false;
	el.find('.machvi_content span').css('borderBottom', 'none');
	/*el.find('.machvi_title').stopAll().animate({top:-6}, 300);

	el.find('.samk').css('display', 'none');*/
}

function machvi_select(topic_id) {
	if (topic_cur != '') {
		//alert('deselect ' + topic_cur);
		machvi_deselect(topic_cur);
	}
	machvi_open(topic_id);
}

function machvi_deselect(topic_id) {
	machvi_close(topic_id);
	var el = $('#machvi_' + topic_id);
	el.find('.samk').css('display', 'none');
	el.find('.machvi_title').stopAll().animate({top:-25}, 300);
	el.find('.machvi_content span').css('borderBottom', 'none');
}



function topic_content_show(topic_id) {
	if (pageType == 'toasting') return;

	if (topic_cur != '') {
		//console.dir($('#topic_' + topic_cur));
		$('#topic_' + topic_cur)[0].style.display = 'none';
	}
	////
	var el = $('#topic_' + topic_id);
	//console.log(topic_id);
	//console.log(el);
	if (el.css('display') != 'block') el.css('display', 'block');
}
