var GalImgPath = "images/gallery/";
var GalImgLPath = "/large/";
var GalImgTPath = "/thumb/";
var GalNamePath = '';
var MaxGalImgs = 21;
var CurrentGallery;
var CurrentCatName;
var GalLoadPage;
var GalLoadImg;
var GalShowed = 0;
var GalTimeoutId ;
var GalBigImg ;
var GalFadeEnd =1;


function GalShowBig(imgName){
    GalBigImg = imgName;
    GalFadeEnd = 0;
    $('#GalleryBigImg').fadeOut(600,function(){
        $('#GalleryBigImg').load(function (){
        $(this).fadeIn();
        GalFadeEnd = 1;
        }).attr('src', GalBigImg);
    });
}

function Galinit(CatName){
    clearTimeout (GalTimeoutId);
    
    if (CatName == ''){
        for(var i in GalImages){
            CatName = i;
            break;
        }
    }
    
    $("#GalCatsTd").find("a").each(function(i) {
        $(this).attr('class', $(this).attr('innerHTML').replace(/[^a-zA-Z0-9 _]/g,"-") == CatName ? 'gallery_cats_sel' : 'gallery_cats');
    });
    
    
    CurrentCatName = CatName;
    eval('var TmpGal=GalImages["'+CurrentCatName+'"]');

    CurrentGallery=new Array();
    var page=0;
    var havespace=0;
    var nr=0;
    for (var i=0;i<TmpGal.length;i++){
        if (i==0 || havespace==0){
            if (i!=0) CurrentGallery[page][nr++]='next';
            page++;
            CurrentGallery[page]=new Array();
            nr=0;
            havespace=MaxGalImgs;
            if (MaxGalImgs<TmpGal.length) {
                if (i!=0) CurrentGallery[page][nr++]='prev';
                havespace--;
                if (MaxGalImgs<(TmpGal.length-i+1)  && i!=0) havespace--;
            }
        }
        CurrentGallery[page][nr++]=TmpGal[i];
        havespace--;
    }
    GallShowPage(1);
}


function GalLoadThumb(){
    if (GalLoadImg < CurrentGallery[GalLoadPage].length){
        $(new Image()).load(function() {
            if (CurrentGallery[GalLoadPage][GalLoadImg] == undefined || CurrentGallery[GalLoadPage][GalLoadImg] == 'undefined' || CurrentGallery[GalLoadPage][GalLoadImg] == '') return;
            
            if (CurrentGallery[GalLoadPage][GalLoadImg] == 'prev'){
                $("#galleryList").append('<li><img class="GalThumb" src="images/gallery_prev.jpg" OnClick="GallShowPage('+(GalLoadPage-1)+');" /></li>');
            }else if (CurrentGallery[GalLoadPage][GalLoadImg] == 'next'){
                $("#galleryList").append('<li><img class="GalThumb" src="images/gallery_next.jpg" OnClick="GallShowPage('+(GalLoadPage+1)+');" /></li>');
            }else{
                $("#galleryList").append('<li><img class="GalThumb" src="'+GalImgPath+GalNamePath+CurrentCatName+GalImgTPath+CurrentGallery[GalLoadPage][GalLoadImg]+'" OnClick="'+"GalShowBig('"+GalImgPath+GalNamePath+CurrentCatName+GalImgLPath+CurrentGallery[GalLoadPage][GalLoadImg]+"');"+'" /></li>');
            }
            
            GalLoadImg ++;
            GalTimeoutId = setTimeout ( 'GalLoadThumb()', 10);
        }).attr('src', CurrentGallery[GalLoadPage][GalLoadImg] == 'prev' ? 'images/gallery_next.jpg' : (CurrentGallery[GalLoadPage][GalLoadImg] == 'next' ? 'images/gallery_next.jpg' : GalImgPath+GalNamePath+CurrentCatName+GalImgTPath+CurrentGallery[GalLoadPage][GalLoadImg]));
    }else{
        $('.GalThumb').opacityrollover({
            mouseOutOpacity:   1.0,
            mouseOverOpacity:  0.67,
            fadeSpeed:         'fast',
            exemptionSelector: '.selected'
        });
    }
}

function GallShowPage(Page){
    clearTimeout (GalTimeoutId);
    $("#galleryList").find("li").each(function(i) {
        $(this).remove();
    });

    GalShowed = 0;    
    GalLoadPage = Page;
    GalLoadImg = 0;
    
    if (CurrentGallery[Page] && CurrentGallery[Page][0] != undefined && CurrentGallery[Page][0] == ''){
        if (GalFadeEnd){
            $('#GalleryBigImg').attr('src', 'images/nogal.jpg');
        }else{
            if (GalBigImg != 'images/nogal.jpg') GalShowBig('images/nogal.jpg');
        }
        return;
    }else{
        GalShowBig(CurrentGallery[GalLoadPage][0] != 'prev' ? GalImgPath+GalNamePath+CurrentCatName+GalImgLPath+CurrentGallery[GalLoadPage][0] : GalImgPath+GalNamePath+CurrentCatName+GalImgLPath+CurrentGallery[GalLoadPage][1]);
    }
    GalLoadThumb();
}

