function ie6_footer_fix()
{
    if (!$.browser.msie || $.browser.version >= 7)
        return;
        
    $('#search').css({
        position: 'static',
        display:  'none'
    }).css({
        position: 'absolute',
        display:  'block',
        bottom:   '121px',
        right:    '4%'
    });

    $('.footer').css({
        position: 'static',
        display:  'none'
    }).css({
        position: 'absolute',
        display:  'block',
        bottom:   '0',
        right:    '0',
        left:     '0'
    });
}

/******* gallery *******/
var gallery_index = 0;
var gallery_total = 0;

function gallery_pic(cl)
{
    $pic = $("#gallery_preloader ."+cl);
    if (!$pic.length)
        $pic = $('#gallery_preloader img').eq(0);
    gallery($pic);
}

function gallery($pic)
{
    $('body').append(
    '<div id="gal"><a class="close" title="Çàêðûòü" href="#">&nbsp;</a><div class="img"></div><div class="watermark"></div><a class="prev" href="#">ÏÐÅÄÛÄÓÙÀß</a><a class="next" href="#">ÑËÅÄÓÞÙÀß</a><div class="pager"></div><div class="descr"></div></div>'
    );
    
    $(document).pngFix();
    
    var $gal = $('#gal');
    var $big = $('#gallery_preloader img'); 
    
    //gallery_index = $big.index('.'+$pic.attr('class')) + 1;
    gallery_index = parseInt($pic.attr('class').replace('pic', ''));
    gallery_total = $big.length;
    
    if (gallery_total < 2)
        $('.prev, .next', $gal).remove();
    
    gallery_load($pic, $gal);
    
    $('.close', $gal).click(function(){
        popup_hide();
        return false;
    });
    
    $('.prev', $gal).click(function(){
        gallery_index--;
        if (gallery_index <= 0)
            gallery_index = gallery_total;
        gallery_load($big.eq(gallery_index - 1), $gal);    
        return false;    
    });
    
    $('.next', $gal).click(function(){
        gallery_index++;
        if (gallery_index > gallery_total)
            gallery_index = 1;
        gallery_load($big.eq(gallery_index - 1), $gal);    
        return false;
    });
}

function gallery_load($pic, $gal)
{
    img = new Image();
    img.src = $pic.attr('src');
    var height = $pic.attr('height') || $pic.height() || img.height;

    var scrollTop = self.pageYOffset || document.documentElement.scrollTop;
    var galTop = parseInt(($(window).height() - height - 48) / 2);
    if (galTop < 0)
        galTop = 0;
          
    $gal.animate({
        top: scrollTop + galTop + 'px'
    }, parseInt(height / 3));
    
    $('.img', $gal).animate({
        height: height + 'px'
    }, parseInt(height / 3), function(){
        $('.img', $gal).append($pic.clone().hide());
        if ($('img', $gal).length > 1) {
            $('img', $gal).eq(0).remove();
        }
        $('img', $gal).show();
        $('.next, .prev', $gal).height(
            height - 65
        );
        faderResize();
    });
    $('.pager', $gal).html(gallery_index+'/'+gallery_total);
    //$('.descr', $gal).html('' + $pic.next('span.desc').html());
}

/* startup */
$(document).ready(function(){
    $('.photos_trigger').toggle(function(){
        var $link = $(this);
        var perRow = Math.ceil($('.gal_photos').width() / 80);
        $('.gal_photos').animate({
            height: (80 * (Math.ceil($('img', '.gal_photos').length / perRow))) + 'px'
        }, 150, function(){
            ie6_footer_fix();
            $link.removeClass('i_down').addClass('i_up');
        });
        return false;
    }, function(){
        var $link = $(this);
        $('.gal_photos').animate({
            height: '80px'
        }, 150, function(){
            ie6_footer_fix();
            $link.removeClass('i_up').addClass('i_down');
        });
        return false;
    });

    $('.gal_photos img').css('cursor', 'pointer').click(function(){
        popup_show();
        var cl = $(this).attr('class');
        if (!$('#gallery_preloader').length) {
            $('body').append('<div id="gallery_preloader" style="display:none"></div>');
            $('#gallery_preloader').html(gallery_html);
        }
        gallery_pic(cl);
        return false;  
    });

});
