$(document).ready(function(){
        var next_text ="";
        var prev_text ="";
        var close_text = "";
        switch($('#main_section').attr('class')){
                case 'en':
                        next_text = "next";
                        prev_text = "previous";
                        close_text = "close";
                        break;
                case 'de':
                        next_text = "nächste";
                        prev_text = "vorherige";
                        close_text = "schließen";
                        break;
                case 'ru':
                        next_text = "Следующие";
                        prev_text = "Предыдущие";
                        close_text = "Закрыть";
                        break;
                default:
                        next_text = "następne";
                        prev_text = "poprzednie";
                        close_text = "zamknij";
        }
        $('.gallery-big-img').hide();
        
        var gal=$('.gallery').children().children('.gi');
        $('object').attr('wmode','transparent');
        $('.gallery-img').click(function(){
            var current_gal = $(this).parent('.gi').children('.gallery-img');
            var current_img_index = $(current_gal).index($(this));
            var current_gal_length = $(current_gal).length;
            
            
            $('body').append('<div id="block-site"></div>');
            $('#block-site').css('height',$(document).height());
       
            $('body').append('<div id="gallery-big" class="rnd"><div class="rndtop">'+
                             '<div class="rndtop1"><div class="rndtop2"></div>'+
                             '</div></div><div class="rnd1"><div class="rnd2">'+
                             '<div class="rnd3 big-gallery-content"><div id="close-big-gallery"><a href="#">'+close_text+'</a></div>'+
                             '<div class="img-container">'+
                             '</div><div class="big-gallery-prev"><a>&laquo '+prev_text+'</a></div><div class="big-gallery-next"><a>'+next_text+' &raquo</a></div></div></div>'+
                             '</div><div class="rndbot"><div class="rndbot1">'+
                             '<div class="rndbot2"></div></div></div></div>');
            $('#gallery-big .img-container').append($(this).children('.gallery-big-img').clone());
            $('#gallery-big .gallery-big-img').show();
            $('.big-gallery-next').css({ 'text-align':'right', 'right':'35px' });
            if (current_img_index == current_gal_length-1){
                $('.big-gallery-next').hide();
            }
            if (current_img_index == 0){
                $('.big-gallery-prev').hide();
            }
           // $('.img-container').css('margin','auto');
            
            var w = parseInt($('#gallery-big .rnd1').width());
            w=w-40;
            //var h = parseInt($('#gallery-big .rnd1').height());
            //alert(h);
            $('#gallery-big').css({ top: '10%' , left : '25%'});
            $('#gallery-big .rndtop2').css('width',w+'px');
            $('#gallery-big .rndbot2').css('width',w+'px');

            function center_img(){
                
                w=$(current_gal[current_img_index]).children('.gallery-big-img').width();
               // alert(w);
                w = parseInt((550-w)/2);
                h=$(current_gal[current_img_index]).children('.gallery-big-img').height();
                h = parseInt((520-h)/2);
               // alert('lewo:'+w+';gora:'+h);
                $('.img-container').css({
                    'top':h+'px',
                    'left':w+'px'
                    });
            }
            center_img();
            $('#close-big-gallery').click(function(){
                $('#block-site').remove();
                $('#gallery-big').remove();
                });
            
            $('.big-gallery-prev').click(function(){
                if (current_img_index > 0 )
                {
                    $('.big-gallery-next').show();
                    $('.img-container img').remove();
                    current_img_index--;
                    $('#gallery-big .img-container').append($(current_gal[current_img_index]).children('.gallery-big-img').clone());
                    $('#gallery-big .gallery-big-img').show();
                    if (current_img_index == 0){
                        $(this).hide();
                    }
                }
                center_img();
                });
            
            $('.big-gallery-next').click(function(){
                if (current_img_index < current_gal_length-1 )
                {
                    $('.big-gallery-prev').show();
                    $('.img-container img').remove();
                    current_img_index++;
                    $('#gallery-big .img-container').append($(current_gal[current_img_index]).children('.gallery-big-img').clone());
                    $('#gallery-big .gallery-big-img').show();
                    if (current_img_index == current_gal_length-1){
                        $(this).hide();
                    }
                }
                center_img();
                });
            
    
            });

        
        $('.gallery-img').children('.gallery-small-img').css({'opacity':'0.5','filter':'alpha(opacity=50)'});
        
        $('.gallery-img').children('.gallery-small-img').hover(function()
        {
            $(this).css({'opacity':'1','filter':'alpha(opacity=100)'});
        },function()
        {
            $(this).css({'opacity':'0.5','filter':'alpha(opacity=50)'});
        });
        $('.gallery').css({position:'relative'});
        $('.gi').each(function(){
            var gal_images = $(this).children('.gallery-img');
            var gallery_length = 54*gal_images.length;
            $(this).css({position:'relative',width:gallery_length+'px',left:'0px'});
            if ( gal_images.length < 7 ){
                $(this).parent('.gal-content').css('width',gal_images.length*50+'px');
                $(this).parent('.gal-content').parent('.gallery').css('width',40+gal_images.length*54+'px');
            }else{
                $(this).parent('.gal-content').css('width','380px');
            }
            });
        
        $('.next-img').css({position:'absolute',top:'0px',right:'0px'});
        $('.gal-content').css({position:'relative',display:'block',height:'73px',overflow:'hidden'});
        $('.gallery-img').each(function(){
            var image_index = $(this).parent().children().index(this);
            var position = 54 * image_index;
            $(this).css({position:'absolute',top:'0px',left:position+'px'});
            });
        $('.next-img').mouseover(function(){
            if ($(this).parent().children('.gal-content').children('.gi').children().length > 7){ 
                var gallery_position = parseInt($(this).parent().children('.gal-content').children('.gi').css('left'));
                var gallery_width = parseInt($(this).parent().children('.gal-content').children('.gi').css('width'));
                var next_img = $(this);
                var move_left = setInterval(function(){
                    
                    if(gallery_width+gallery_position > 380 ){
                    //alert(gallery_position+";");
                    gallery_position -= 1;
                    $(next_img).parent('.gallery').children('.gal-content').children('.gi').animate({left:gallery_position+'px'},1);
                    }                    
                    },1);
                $(this).mouseout(function(){
                    clearInterval(move_left);
                    });
                //alert(gallery_width);

                }
            });
        $('.prev-img').mouseover(function(){
            if ($(this).parent().children('.gal-content').children('.gi').children().length > 7){ 
                var gallery_position = parseInt($(this).parent().children('.gal-content').children('.gi').css('left'));
                var prev_img = $(this);
                var move_right = setInterval(function(){
                if(gallery_position < 0 ){
                gallery_position += 1;
                $(prev_img).parent('.gallery').children('.gal-content').children('.gi').animate({left:gallery_position+'px'},1);
                }
                },1);
            $(this).mouseout(function(){
                clearInterval(move_right);
                });
            }
            });
        });

