
var galleryContainerWidth = 0;

var isPhotoListLoading = false;

var photoItemMargin = 10;
var photoItemPadding = 10;
var photoItemTextHeight = 20;
var photoItemUrlHeight = 20;

var itemsPerPage = 25;
var actualPage = 1;

function checkGalleryContainerDimensions() {
    
    
    var actualWidth = $('#galleryContainer').width();
    var actualHeight = $('#galleryContainer').height();
    
    if( galleryContainerWidth == actualWidth ) { return; }
    
    var itemWidth = thumbWidth + 2*(photoItemMargin+photoItemPadding);
    var itemHeight = thumbHeight + 2*(photoItemMargin+photoItemPadding);
    
    if( photoShowText ) {
            itemHeight += photoItemTextHeight;
    }
    
    if( photoShowUrl ) {
            itemHeight += photoItemUrlHeight;
    }

    var itemRowsCount = Math.floor( actualWidth / itemWidth );
    var itemColsCount = Math.floor( actualHeight / itemHeight );
    
    if( ( itemRowsCount * itemColsCount ) != itemsPerPage  ) {
        
        galleryContainerWidth = actualWidth;
        
        $('#corner').html( itemRowsCount + " x " + itemColsCount + "<br />itemWidth: " + itemWidth + "<br />actualWidth: " + actualWidth);
        
        itemsPerPage = itemRowsCount * itemColsCount;
        
        loadPhotoList();
    }
}


function loadPhotoList(page) {
    
    if( page != undefined ) {
        
            actualPage = page;
    }
    
    if( isPhotoListLoading ) { return; }

    isPhotoListLoading = true;

    $('#galleryContainer').append( '<div id="ajaxLoader"></div>' );
    
    $.post("/ajax/gallery", {"galleryId": galleryId, 'itemsPerPage' : itemsPerPage, 'actualPage' : actualPage},
    
            function(data){
                
                    $("#galleryContainer").html( data );
                    
                    $('.photoList').each( function() {

                            $(this).find('.url, .text').css('width', $(this).find('.photo').css('width') );
                    });
                    
                    cufonInit();
                    
                    isPhotoListLoading = false;
            }
            
    );
        
}

function cufonFancyboxInit() {
    
    Cufon.replace('#fancybox-title', {fontFamily: 'NewsGothCnEu', fontSize: '16px'});
}

function cufonInit() {
    
    Cufon.replace('#navi a, #contact, #content, #breadCrumbs, .pagination', {fontFamily: 'NewsGothCnEu'});
 }

$(function() {
   
    cufonInit();
    
    if( $('#galleryContainer').size() ) {
        
            checkGalleryContainerDimensions();

            setInterval( 'checkGalleryContainerDimensions()', 1000 );
    }
        
    //$('#navi').fadeIn('slow');
});
