                        function showNext(curr){  
                            document.getElementById('foto').src = '';                            
                            $(".left-arrow").css("display","none").css("visibility","hidden");
                            $(".right-arrow").css("display","none").css("visibility","hidden");
                            $("#loader").css("display","block");
                            if(curr=="ahead"){
                                pointer++;                    
                            }
                            if(curr=="back"){
                                pointer--;
                            }
                            if(typeof(curr)=="number"){
                                pointer = curr;
                            }
                            if(pointer<0){
                                pointer = count_fotos;
                            }
                            if(pointer>count_fotos){
                                pointer = 0;
                            }
                            //alert(pointer);
                            //document.getElementById('foto').style.display = 'none'; 
                            document.getElementById('foto').style.width = 'auto'; // чтобы после ресайза больших маленькие отображались корректно
                            document.getElementById('foto').style.height = 'auto';
                            //document.getElementById('foto').src = foto_gal[pointer][0];
                            /*var im = new Image();
                            im.src = foto_gal[pointer][0];
                            im.onload = function(){
                                document.getElementById('foto').src = foto_gal[pointer][0];
                                //im.destroy;
                            }*/
                            $("#foto").css({"visibility":"hidden","marginLeft":0,"marginTop":0}).attr({src: foto_gal[pointer][0],title: foto_gal[pointer][1],alt: foto_gal[pointer][1]}); 
                            document.getElementById('this_pointer').innerHTML = parseInt(pointer+1);                           
                            document.getElementById('backgr_galery').style.display = 'block';
                            $("#backgr_galery").css("opacity",0.9);
                            document.getElementById('galery').style.display = 'block';                            
                        }
                        function hideGalery() {
                            document.getElementById('backgr_galery').style.display='none';
                            document.getElementById('galery').style.display='none';
                        }
                        function changeBoxSize(th) {                          
                            //сначала уменьшаем до нужных размеров                                                                             
                            changeImageSize("#"+th.id,650,650);                             
                            var width_i = -parseInt(th.width/2);
                            var height_i = -parseInt(th.height/2);
                            //alert(th.width+":"+width_i+"-"+th.height+":"+height_i);                                                       
                            $("#"+th.id).animate({ marginLeft:width_i, marginTop:height_i}, 100);                             
                            //var p = $("#"+th.id).offset();
                            //alert(p.left+'|'+p.top);
                            if ($("#"+th.id).css("marginLeft")>=0) {
                                $("#"+th.id).animate({ marginLeft:-300}, 1); 
                            }
                            if ($("#"+th.id).css("marginTop")>=0) {
                                $("#"+th.id).animate({marginTop:-300}, 1); 
                            } /**/                                                       
                            document.getElementById('count').style.top = height_i+"px";         // надпись сверу справа: № фото/всего
                            $("#"+th.id).css("visibility","visible");
                             $(".left-arrow").animate({ marginLeft:width_i, marginTop:height_i}, 100); 
                             $(".right-arrow").animate({ marginLeft:width_i, marginTop:height_i}, 100);
                            $(".left-arrow").css('display','block').css("visibility","visible");
                            $(".right-arrow").css('display','block').css("visibility","visible");
                             $("#loader").css("display","none");
                            //$(".left-arrow").css("marginLeft",width_i).css("marginTop",height_i); 
                            //$(".right-arrow").css("marginLeft",width_i).css("marginTop",height_i);
                        }
                        function changeImageSize(th,maxWidth,maxHeight) {
                            $(th).each(function() {
                                var ratio = 0; 
                                var width_r = $(this).width();    
                                var height_r = $(this).height(); 
                                
                                if(width_r > maxWidth){
                                    ratio = maxWidth / width_r;  
                                    $(this).css("width", maxWidth); 
                                    $(this).css("height", height_r * ratio);  
                                    height_r = height_r * ratio;  
                                    width_r = width_r * ratio;    
                                }
                               
                                if(height_r > maxHeight){
                                    ratio = maxHeight / height_r; 
                                    $(this).css("height", maxHeight);  
                                    $(this).css("width", width_r * ratio);   
                                    width_r = width_r * ratio;    
                                }                                
                                $(this).css("display","block");                                    
                            });                            
                        }
