var current_teaser = 0;
var num_teasers = 0;
var _teaserTimeout = 10000;
$(document).ready(function(){

	selectThumb();

	$("#teaser ul li").each(function(){
		if ($("#teaser ul li").index(this) != current_teaser){
			$(this).hide();
		}
		num_teasers++;
	});

	setTimeout('getNextTeaser()', _teaserTimeout);

	$(".move .next").bind('click', function(e){
		loadImage();
		var data = {'class':'Image_Gallery', 'action':'get_next', 'image_id':current_image, 'gallery_id':gallery_id};
		$.post('/action.php', $.param(data), function(xml){
			if ($('Response', xml).find('more').text() == 1){
				$(".move .next").hide();
			}
			$(".move .prev").show();
			setImage($('Response', xml).find('src').text());
			current_image = $('Response', xml).find('id').text();
		}, 'xml');

		e.preventDefault();
	});

	$(".move .prev").bind('click', function(e){
		loadImage();
		var data = {'class':'Image_Gallery', 'action':'get_prev', 'image_id':current_image, 'gallery_id':gallery_id};
		$.post('/action.php', $.param(data), function(xml){
			if ($('Response', xml).find('more').text() == 1){
				$(".move .prev").hide();
			}
			$(".move .next").show();
			setImage($('Response', xml).find('src').text());
			current_image = $('Response', xml).find('id').text();
		}, 'xml');

		e.preventDefault();
	});

	$(".image_thumbnail").bind('click', function(e){
		loadImage();
		var id = $(this).attr('href').substr(1);
		var data = {'class':'Image', 'action':'get_image_path', 'image_id':id};
		current_image = id;

		if (_last_image == current_image){
			$(".move .next").hide();
		} else {
			$(".move .next").show();
		}

		if (_first_image == current_image){
			$(".move .prev").hide();
		} else {
			$(".move .prev").show();
		}

		$.post('/action.php', $.param(data), function(html){
			setImage(html);
		}, 'html');

		e.preventDefault();
	});

	$(".text_image .image a").lightBox({fixedNavigation:true});

	$(".text_image .thumbnail a").bind('mouseover', function(e){
		$(".text_image .image").hide();
		$("#image_" + $(this).attr('href').substring(1)).show();
	});

});

function loadImage(){
	$("#gallery_image img").css({
		'filter':'alpha(opacity=25)',
		'-moz-opacity':'.25',
		'opacity':'.25'
	});
	$("#gallery_loader").show();
}

function setImage(src){
	$("#gallery_image").find('img:last').attr('src', src).load(function(){
		$("#gallery_image img").css({
			'filter':'alpha(opacity=100)',
			'-moz-opacity':'1',
			'opacity':'1'
		});
		$("#gallery_loader").hide();
		selectThumb();
	});
}

function selectThumb(){
	$(".image_thumbnail").each(function(e){
		if ($(this).attr('href') == '#' + current_image){
			$(this).addClass('selected');
		} else {
			$(this).removeClass('selected');
		}
	});
}

function getNextTeaser(){
	if (num_teasers > 1){
		$("#teaser > ul > li.li_" + current_teaser).fadeOut(2000);
		if (current_teaser == parseInt(num_teasers)-1){
			current_teaser = -1;
		}
		$("#teaser > ul > li.li_" + ++current_teaser).fadeIn(2000);

		setTimeout('getNextTeaser()', _teaserTimeout);
	}
}