 /*  Animated-Image-Box  by comcon1 & cranx */

 //Анимация ширины, высоты, центрирования.
 function imgPosition(height, width) {  
    
	var minWidth = 320; // Минимальная ширина блока
	var AnimSpeed = 3; // Коэфициент скорости анимации
	
	var cib = $('#cur-image-box');
	width = (width+40);
	if (width < minWidth) { width = minWidth; }
	var heightCash = $('.big-box').height();
	var widthCash = cib.width();
	var  WanimationTime = 0;
	var  HanimationTime = 0;
	var xoffset = Math.floor($(window).width()/2-width/2);
	
	if (widthCash > 0) { 
		WanimationTime = Math.abs((width - widthCash)*AnimSpeed);
		HanimationTime = Math.abs((height - heightCash)*AnimSpeed);
	}
	  
	$('.big-box').animate( {height: height + 'px'}, HanimationTime, function(){  
		cib.css('height', 'auto');
		cib.animate({
			width: width + 'px', 
			left: xoffset + 'px', 
			top: ($(window).scrollTop()+10) + "px"
			}, WanimationTime, function(){   
				$('#cur-image-box').css('visibility', 'visible');
				$('.big-box').css('backgroundImage', 'none');
				$('img#big-image').fadeIn(400); 
			});
	});		 
 } 
 
 $(document).ready(function() {
	
	var backg = $('.big-box').css('backgroundImage');

	// working with preview
	var inbox1 = $(".thumb-box");
    
	// цикл вставления маленьких картинок
	$("a.viewer-opener").each(function(i) {
		var curthu = $(this);
		inbox1.append('<div class="thumb-box-a"><a class="thumb-pic" href="'+
		curthu.attr('href')+'" title="'+curthu.attr('title')+
		'" class="thumb-pic">'+'<img src="'+$('#'+
		curthu.attr('id')+' img').attr('src')+'" alt="x"></a></div>');
    });
	
	inbox1.append('<div style="clear:left;">&nbsp;</div>');
	
	// pop-up viewer 
	$("a.open").click(function() {
	  
	    /* $("html" + ( ! $.browser.opera ? ",body" : "")).animate({scrollTop:0},"slow"); */
	   
		var pathimg = $(this).attr('href');
		var imgcomment = $(this).attr('title'); 
		var BodyWidthPadding = 0;
		var BodyHeightPadding = 0;
		
		//Затемнение страницы	
		var over = $('<div class="overlay"></div>');
		over.appendTo("body");
		$('.overlay').css('height', ($('body').height() + BodyHeightPadding) +'px');
		$('.overlay').css('width', ($('body').width() + BodyWidthPadding) +'px');
		  
		$('#cur-image-box').css('width', '0');
		$('#cur-image-box').css('height', '0');
		$('#cur-image-box').css('display', 'block');
		$('#cur-image-box').css('visibility', 'hidden');
		
		$('img#big-image').css('display', 'none');
		$("#cur-image-box .topdiv h1").html(imgcomment);
		$('img#big-image').attr('src', pathimg);
		
		 // выделение нужной миниатюры
		$('a.thumb-pic').each(function(i) {
			if ($(this).attr('href') == pathimg) {
			$("a.thumb-pic").removeClass('active');
			$(this).addClass('active');
			}   
		});
		
		return false;
	}); 
	
	//Переключение изображений
	$("a.thumb-pic").click(function() {
		if ($('img#big-image').css('display') == 'none' || $(this).hasClass('active')) { /* Отрицание */ }
		else {
		    $('.big-box').css('backgroundImage', backg);
			$('img#big-image').css('display', 'none');
			var sh = $(this).attr('href');
			var imgcom = $(this).attr('title');
			$("a.thumb-pic").removeClass('active');
			$(this).addClass('active');
			$("img#big-image").attr('src', sh);
			$("#cur-image-box .topdiv h1").html(imgcom);
		}
		return false;
    });  
		
    // max-width and max-height emulation for IE
	var wi = $(window).width() - 100;	
	var he = 1000;	
	
	$("img#big-image").load(function() {
		var thimg = $(this);
        thimg.removeAttr("width")	
			 .removeAttr("height")
             .css({ width: "", height: "" });
		if ((thimg.height() > he) && (thimg.width() > wi)) {
			if (thimg.height()/he > thimg.width()/wi) {
				thimg.css('height', he+'px');
			} else {
				thimg.css('width', wi+'px');
			}
		} else if (thimg.height() > he) {
				thimg.css('height', he+'px');
			} else if (thimg.width() > wi) {
				thimg.css('width', wi+'px');
			}
		var imgh = thimg.height();
		var imgw = thimg.width();
		imgPosition(imgh, imgw);
    });	

	// Закрытие простмотра
    $("#cur-image-box *:not(.thumb-box *)").click(function() {
		if ($('img#big-image').css('display') != 'none'){
			$('.overlay').remove();
			$('#cur-image-box').css('display','none');
			$('img#big-image').attr('src', '')
		}
		return false;     
    });
	
 });
