photos_counter = $('.resize_photo').length;

framework_language = new Array;
framework_language['click'] = translator('enlarge');
framework_language['click_next'] = translator('enlarge_info');
framework_language['loading'] = translator('loading');

function loadImage(img_link, alt)
{

	$container = $('#photo_container');

	var img = new Image();
	$(img).load(function()
	{
		$img = $(this);

		obj_top = $(window).height()/2+$(window).scrollTop();
		$container.css('top', obj_top);
		$('#overlay_loader').css('top', obj_top);
 

		if(this.height > $(window).height())
		{

			this.width = Math.round(this.width*($(window).height()-20)/this.height);
			this.height = $(window).height()-20;
		}
		else if(this.width>640) 
		{
			this.height = Math.round(this.height*640/this.width)
			this.width=640;
		}


 
		$img.css({'display':'none'});
 
		marginTop = Math.round((this.height)/-2);
		marginLeft = Math.round((this.width)/-2);

		$overlay.animate({'marginLeft': marginLeft, 'marginTop': marginTop, 'width': this.width+10, 'height': this.height+10});

		
		$container.animate({'width': this.width+2, 'height': this.height+2}, function() {
			$('#overlay_loader').fadeOut('fast', function() {
				$img.fadeIn(
					
				function()
				{
					if(alt!='' && alt!='/')
					{
						$container.append('<div class="alt">'+alt+'</div>');
						alt_height = '+=';
						alt_height += ($('.alt', $container).height()+8);
						$container.animate({'height': alt_height});
						$overlay.animate({'height': alt_height});
						$('.alt', $container).show();
						
					}
				}
					)});}).append(img);
				
				
		}).attr('src', img_link);

		
		
 
		$(img).click(function() {
			if(photos_counter==1)
			closeOverlay();
			else
			$('#photo_container img').fadeOut('normal', showNextImage);
		});
}

function showNextImage()
{
	$('#photo_container img, #photo_container .alt').remove();
	$('#overlay_loader').fadeIn();
 
	if(photos_active<photos_counter-1)
	++photos_active
	else
	photos_active=0;
 
	img_link = $('.resize_photo:eq('+photos_active+') img').attr('src').replace('_sm', '').replace('png', 'jpg');
	alt = $('.resize_photo:eq('+photos_active+') img').attr('alt');
	loadImage(img_link, alt);
	
}
 
thumb_label = '<div class="click bg_black_07">'+framework_language['click']+'</div>';

$('.resize_photo').click(function() {
	photos_active = $(this).index('.resize_photo');
	img_link = $('img', this).attr('src').replace('_sm', '').replace('png', 'jpg');
	
	obj_top = $(window).height()/2+$(window).scrollTop();
	alt = $('img', this).attr('alt');
			
	$overlay = overlay();
	$overlay.append('<div id="photo_container" class="overlay_object"></div>');
 
	$('body').prepend('<div id="overlay_loader" class="overlay_object"><img src="/_media/loader2.gif" alt="'+framework_language['loading']+'"></div>');
	
 
	$('#overlay_loader').css('top', obj_top).fadeIn('fast', function() {$('#photo_container').css({'top': obj_top}).show(); loadImage(img_link, alt);});
 
 
	if(photos_counter > 1)
	$('#photo_container').prepend('<div class="click bg_black_07">'+framework_language['click_next']+'</div>');
	
	$('#overlay_close').bind('click', closeOverlay);
	
	return false;
});
 
if(photos_counter > 1)
{
	$('#photo_container').live('mouseenter', function() {$('.click', this).show() } );
	$('#photo_container').live('mouseleave', function() {$('.click', this).hide() } );
}
 
$('.resize_photo').mouseenter(function(target) {
	if($('.click', this).length==0)
	{
		$(this).prepend(thumb_label);
		$('.click', this).hide().stop().fadeIn();
	}
});
 
$('.resize_photo').mouseleave(function() {
	$('.click', this).stop().fadeOut('normal', function() {$(this).remove()});
});
 

