/******* 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(){
            $link.removeClass('i_down').addClass('i_up');
        });
        return false;
    }, function(){
        var $link = $(this);
        $('.gal_photos').animate({
            height: '80px'
        }, 150, function(){
            $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;  
    });
	
	

});



/******* pupup faders *******/
function faderResize(){
    var height = $('#main').height();
    if ($('#gal').length) {
        var galheight = $('#gal').offset().top + $('#gal').height() + 20;
        if (galheight > height)
            height = galheight;
    } 
    $('#fader').height(height).width($('#main').width());
};

function popup_show()
{
    $('body').append('<div id="fader"></div>');
    faderResize();
    $('#fader').css({
        opacity: '0'
    }).animate({
        opacity: '0.8'
    }, 100).attr('title', 'Закрыть').click(function(){
        popup_hide();
    });
    $(window).bind('resize', faderResize);
}

function popup_hide()
{
    $('#fader').animate({
        opacity: '0'
    }, 100, function(){
        $(this).remove();
        $('#gal').remove();
    });
    
    $(window).unbind('resize', faderResize);
    return false;
}



