var stand_left, n = $("#img_cont > a").length, summ_f=0;
var prev_width1,width1;
var position;
var rese=0;
var monoshirin=0;

function position_imgs() {
	var width2,temp3,position2,position3;

	position = $("#img_cont > a:first").position();

	summ_f=0;
	summ_f+=($("#img_cont > a:first").width());

	temp3=parseInt($("#img_c").width()/$("#img_cont > a:first").width());
	monoshirin=parseInt( ($("#img_c").width()-$("#img_cont > a:first").width()*temp3) / (temp3-1) );

	for (i=1;i<n;i++) {
		summ_f+=($("#img_cont > a:eq("+i+")").width());
	}

	if (summ_f<$("#img_c").width()) {
		width2=parseInt( ($("#img_c").width()-summ_f) /(n-1) );
	} else {
		width2=parseInt( ($("#img_c").width()-$("#img_cont > a:first").width()*temp3) / (temp3-1) );
	}

	if (rese==1) {
		$("#img_cont > a").css( {'top' : '0px', 'left': '0px'});
	}

	for (i=1;i<n;i++) {
		position2 = $("#img_cont > a:eq("+i+")").position();
		position3 = $("#img_cont > a:eq("+(i-1)+")").position();
		$("#img_cont > a:eq("+i+")").css( {'top' : position.top-position2.top+"px",
														'left' : ( position3.left-position2.left+$("#img_cont > a:eq("+(i-1)+")").width()+width2 )+"px"});
	}
}

$(document).ready(function() {
	position = $("#img_cont>a").clone().appendTo('#img_cont');
	n = $("#img_cont > a").length;
	position_imgs();	
	
	position = $("#img_cont").position();
	stand_left=position.left;
	prev_width1=$("#img_c").width();
	width1=prev_width1;
	rese=1;
	$("#downloading").css ( {'display' : 'none'});

	$(window).resize (function() {
		prev_width1=width1;
		width1=$("#img_c").width();
		if (prev_width1!=width1) {
			position_imgs();
			if (summ_f>=$("#img_c").width()) {
				$("#img_cont").css ({'left' : '0px'});
			}
			position = $("#img_cont").position();
			stand_left=position.left;
		}
	});
});

function toright() {
	var position = $("#img_cont").position();
	var shift = $("#img_cont > a:first").width()+monoshirin;

	$("#img_cont > a").each(function(){
		if($(this).offset().left+$(this).width() < $("#img_c").offset().left){
			var l = parseInt($(this).css('left'));
			l = isNaN(l) ? 0 : l;
			$(this).css('left', l+n*shift);
		}
	});

	$("#img_cont").animate ({'left': '-='+shift}, 500);
}

function toleft() {
	var position = $("#img_cont").position();
	var shift = $("#img_cont > a:first").width()+monoshirin;

	$("#img_cont > a").each(function(){
		if($(this).offset().left > $("#img_c").offset().left+$("#img_c").width()){
			var l = parseInt($(this).css('left'));
			l = isNaN(l) ? 0 : l;
			$(this).css('left', l-n*shift);
		}
	});

	$("#img_cont").animate ({'left': '+='+shift}, 500);
}

