var GalleryBox_maxWidth = 900;
var GalleryBox_maxHeight = 900;
var GalleryBox_minMargin = 50;
var Galleries = new Object();
var Gallery_started = false;
var preload_running = false;

Gallery = Class.create();
Gallery.prototype = {
    initialize: function() {
        if (!document.getElementsByTagName){return;}
        var divs = document.getElementsByTagName('div');
        for (var i=0; i<divs.length; i++) {
            if (hasClassName(divs[i],'gallery')){
                var linkObjs = divs[i].getElementsByTagName('a');
                for (var ii=0; ii<linkObjs.length; ii++) {
                    if (linkObjs[ii].className!='galleryprintlink'){
                        linkObjs[ii].onclick = function () {
                            myGallery.start(this); return false;
                        }
                    }
                }
            }
        }
        objTemp = document.createElement("div");
        objTemp.id = 'GalleryOverlay';
        objTemp.style.visibility = 'hidden';
        objTemp.onclick = function() { myGallery.end(); return false;}
        objGalleryOverlay = document.body.appendChild(objTemp);

        objTemp = document.createElement("div");
        objTemp.id = 'GalleryBox';
        objTemp.style.display = 'none';
        objGalleryBox = document.body.appendChild(objTemp);

        objTemp = document.createElement('div');
        objTemp.id = 'GalleryBoxTitle';
        objGalleryBoxTitle = objGalleryBox.appendChild(objTemp);
        
        objTemp = document.createElement('a');
        objTemp.id = 'GalleryPrint';
        objTemp.href = '#';
        objTemp.target = '_blank';
        objTemp.title = 'Drucken';
        objTemp.innerHTML = '<img src="' + GalleryPicPrint + '" alt="Drucken" />';
        objGalleryPrint = objGalleryBox.appendChild(objTemp);
        
        objTemp = document.createElement('a');
        objTemp.id = 'GalleryClose';
        objTemp.href = '#';
        objTemp.onclick = function() { myGallery.end(); return false;}
        objTemp.innerHTML = 'x';
        objGalleryClose = objGalleryBox.appendChild(objTemp);
        
        objTemp = document.createElement('div');
        objTemp.id = 'GalleryBoxNav';
        objTemp.style.visibility = 'hidden';
        objGalleryBoxNav = objGalleryBox.appendChild(objTemp);
        
        objTemp = document.createElement('a');
        objTemp.id = 'GalleryBoxNavFirst';
        objTemp.href = '#';
        objTemp.onclick = function () {myGallery.navFirst(); return false;}
        objTemp.innerHTML = 'Anfang';
        objGalleryBoxNavFirst = objGalleryBoxNav.appendChild(objTemp);

        objTemp = document.createElement('a');
        objTemp.id = 'GalleryBoxNavBack';
        objTemp.href = '#';
        objTemp.onclick = function () {myGallery.navBack(); return false;}
        objTemp.innerHTML = 'zurück';
        objGalleryBoxNavBack = objGalleryBoxNav.appendChild(objTemp);

        objTemp = document.createElement('a');
        objTemp.id = 'GalleryBoxNavNext';
        objTemp.href = '#';
        objTemp.onclick = function () {myGallery.navNext(); return false;}
        objTemp.innerHTML = 'weiter';
        objGalleryBoxNavNext = objGalleryBoxNav.appendChild(objTemp);

        objTemp = document.createElement('a');
        objTemp.id = 'GalleryBoxNavLast';
        objTemp.href = '#';
        objTemp.onclick = function () {myGallery.navLast(); return false;}
        objTemp.innerHTML = 'Ende';
        objGalleryBoxNavLast = objGalleryBoxNav.appendChild(objTemp);

        objTemp = document.createElement('div');
        objTemp.id = 'GalleryBoxEntityLoad';
        objGalleryBoxEntityLoad = objGalleryBox.appendChild(objTemp);
        
        objTemp = document.createElement('img');
        objTemp.src = GalleryPicLoading;
        objTemp.alt = 'lade';
        objGalleryBoxEntityLoad.appendChild(objTemp)
        

        objTemp = document.createElement('div');
        objTemp.id = 'GalleryBoxEntity';
        objTemp.style.visibility = 'hidden';
        objGalleryBoxEntity = objGalleryBox.appendChild(objTemp);
        
        objTemp = document.createElement('div');
        objTemp.id = 'GalleryBoxContainerPicture';
        objGalleryBoxContainerPicture = objGalleryBoxEntity.appendChild(objTemp);
        
        objTemp = document.createElement('div');
        objTemp.id = 'GalleryBoxEntityText';
        objGalleryBoxEntityText = objGalleryBoxEntity.appendChild(objTemp);

        GalleryOverlayEffect = $('GalleryOverlay').effect('opacity');
        GalleryOverlayEffect.hide();
        
        GalleryBoxEntityEffect = $('GalleryBoxEntity').effect('opacity');

    },
    start: function(element) {
        hideSelectBoxes();
        GalleryOverlayEffect.custom(0,0.8);
        GalleryBoxEntityEffect.hide();
        gallery_id = element.rel;
        if (!Galleries[gallery_id]){
            Galleries[gallery_id] = new Object();
            Galleries[gallery_id].obj = document.getElementById('gallery_' + gallery_id);
            Galleries[gallery_id].pictures = new Array();
            Galleries[gallery_id].pictures_loaded = new Object();
            var anchors = Galleries[gallery_id].obj.getElementsByTagName('a');
            for (var i=0;i<anchors.length;i++){
                if (anchors[i].className!='galleryprintlink'){
                    Galleries[gallery_id].pictures.push(anchors[i].id);
                }
            }
        }
        picsToHide = objGalleryBoxContainerPicture.getElementsByTagName('img');
        for (var i=0;i<picsToHide.length;i++){
            picsToHide[i].style.visibility = 'hidden';
        }
        Galleries[gallery_id].active_id = element.id;
        Galleries[gallery_id].active_index = null;
        for (var i=0;i<Galleries[gallery_id].pictures.length;i++){
            if (Galleries[gallery_id].active_id==Galleries[gallery_id].pictures[i]){
                Galleries[gallery_id].active_index = i;
            }
        }
        myGallery.onResize(true);
        myGallery.navInit();
        objGalleryBoxTitle.innerHTML = 'Galerie';
        objGalleryBoxEntityLoad.style.visibility = 'visible';
        objGalleryBox.style.display = 'block';
        myGallery.preload(Galleries[gallery_id].active_index);
        myGallery.showFirst();
        Gallery_started = true;
    },
    showFirst: function () {
        loaded_picture_id = myGallery.getLoadedPictureId();
        if (Galleries[gallery_id].pictures_loaded[loaded_picture_id] && Galleries[gallery_id].pictures_loaded[loaded_picture_id].loaded) {
            myGallery.picture_change(loaded_picture_id,Galleries[gallery_id].active_index);
            if (Galleries[gallery_id].pictures.length>1){
                objGalleryBoxNav.style.visibility = 'visible';
            }
            myGallery.preloadFirst();
            if (xIE4Up){
                busywaiter = window.setInterval(function () {
                    if (!preload_running){
                        window.clearInterval(busywaiter);
                        myGallery.preloadPrevious();
                        busywaiter2 = window.setInterval(function () {
                            if (!preload_running){
                                window.clearInterval(busywaiter2);
                                myGallery.preloadNext();
                                busywaiter3 = window.setInterval(function () {
                                    if (!preload_running){
                                        window.clearInterval(busywaiter3);
                                        myGallery.preloadLast();
                                    }
                                }, 100);
                            }
                        }, 100);
                    }
                }, 100);
            }else{
                myGallery.preloadPrevious();
                myGallery.preloadNext();
                myGallery.preloadLast();
            }
        }
        else {
            window.setTimeout(myGallery.showFirst,100);
        }
    },
    getLoadedPictureId: function (picture_index) {
        if (isNaN(picture_index)){
            return 'GalleryBoxPicture_' + Galleries[gallery_id].active_id;
        } else {
            return 'GalleryBoxPicture_' + Galleries[gallery_id].pictures[picture_index];
        }
    },
    onResize: function(initGallery) {
        bodyHeight = document.body.offsetHeight;
        bodyWidth = document.body.offsetWidth;
        if (bodyHeight-(GalleryBox_minMargin*2)>GalleryBox_maxHeight){
            objGalleryBox.style.top = (bodyHeight-GalleryBox_maxHeight)/2 + 'px';
            objGalleryBox.style.height = GalleryBox_maxHeight + 'px';
            objGalleryBox.style.bottom = 'auto';
        }
        else {
            objGalleryBox.style.top = GalleryBox_minMargin + 'px';
            objGalleryBox.style.height = (bodyHeight-(GalleryBox_minMargin*2)) + 'px';
            objGalleryBox.style.bottom = 'auto';
        }
        if (bodyWidth-(GalleryBox_minMargin*2)>GalleryBox_maxWidth){
            objGalleryBox.style.left = (bodyWidth-GalleryBox_maxWidth)/2 + 'px';
            objGalleryBox.style.width = GalleryBox_maxWidth + 'px';
            objGalleryBox.style.right = 'auto';
        }
        else {
            objGalleryBox.style.left = GalleryBox_minMargin + 'px';
            objGalleryBox.style.width = (bodyWidth-(GalleryBox_minMargin*2)) + 'px';
            objGalleryBox.style.right = 'auto';
        }
        if (!initGallery){
            myGallery.entityResize();
        }
    },
    entityResize: function() {
        posTop = objGalleryBoxNav.offsetTop + objGalleryBoxNav.offsetHeight + 10;
        posBottom = objGalleryBoxEntityText.offsetHeight;
        if (posBottom==0){
            posBottom = 20;
        }
        objGalleryBoxContainerPicture.style.top = posTop + 'px';
        objGalleryBoxContainerPicture.style.height = (objGalleryBox.offsetHeight - posTop - posBottom) + 'px';
        avail_width = objGalleryBoxContainerPicture.offsetWidth-15;
        avail_height = objGalleryBoxContainerPicture.offsetHeight-15;
        resizing_picture_id = myGallery.getLoadedPictureId();
        current_picture = Galleries[gallery_id].pictures_loaded[resizing_picture_id];
        if (current_picture && current_picture.loaded){
            if (current_picture.realwidth<=avail_width && current_picture.realheight<=avail_height){
                current_picture.obj.style.width = 'auto';
                current_picture.obj.style.height = 'auto';
                use_width = current_picture.realwidth;
                use_height = current_picture.realheight;
            }else{
                pc_width = avail_width/current_picture.realwidth;
                pc_height = avail_height/current_picture.realheight;
                if (pc_width<1 && pc_width<pc_height){ 
                    use_pc = pc_width;
                }else{
                    use_pc = pc_height;
                }
                use_width = Math.ceil(current_picture.realwidth*use_pc);
                use_height = Math.ceil(current_picture.realheight*use_pc);
                current_picture.obj.style.width = use_width + 'px';
                current_picture.obj.style.height = 'auto';
            }
            use_top = Math.ceil(((avail_height-use_height)/2))+5;
            use_left = Math.ceil(((avail_width-use_width)/2))+5;
            current_picture.obj.style.top = use_top + 'px';
            current_picture.obj.style.left = use_left + 'px';
        }
        else{
            window.setTimeout(myGallery.entityResize, 100)
        }
    },
    preloadFirst: function () {
        if (Galleries[gallery_id].active_index>0){
            myGallery.preload(0);
        }
    },
    preloadPrevious: function () {
        if (Galleries[gallery_id].active_index>1){
            previous_index = Galleries[gallery_id].active_index-1;
            myGallery.preload(previous_index);
        }
    },
    preloadNext: function () {
        if (Galleries[gallery_id].active_index<Galleries[gallery_id].pictures.length-2){
            next_index = Galleries[gallery_id].active_index+1;
            myGallery.preload(next_index);
        }
    },
    preloadLast: function () {
        if (Galleries[gallery_id].active_index<Galleries[gallery_id].pictures.length-1){
            myGallery.preload(Galleries[gallery_id].pictures.length-1);
        }
    },
    preload: function (picture_index) {
        picture_id = Galleries[gallery_id].pictures[picture_index];
        loading_picture_id = 'GalleryBoxPicture_' + picture_id;
        if (!Galleries[gallery_id].pictures_loaded[loading_picture_id]){
            linkObj = document.getElementById(picture_id);
            Galleries[gallery_id].pictures_loaded[loading_picture_id] = new Object();
            Galleries[gallery_id].pictures_loaded[loading_picture_id].loaded = false;
            Galleries[gallery_id].pictures_loaded[loading_picture_id].title = null;
            if (linkObj.title.length>0){
                Galleries[gallery_id].pictures_loaded[loading_picture_id].title = linkObj.title;
            }
            Galleries[gallery_id].pictures_loaded[loading_picture_id].description = null;
            objDescription = document.getElementById('description_' + picture_id);
            if (objDescription){
                Galleries[gallery_id].pictures_loaded[loading_picture_id].description = objDescription.innerHTML;
            }
            Galleries[gallery_id].pictures_loaded[loading_picture_id].realwidth = Math.ceil(document.getElementById('width_' + picture_id).innerHTML);
            Galleries[gallery_id].pictures_loaded[loading_picture_id].realheight = Math.ceil(document.getElementById('height_' + picture_id).innerHTML);
            objTemp = document.createElement('img');
            objTemp.id = loading_picture_id;
            objTemp.src = linkObj.href;
            objTemp.alt = '';
            objTemp.style.visibility = 'hidden';
            if (xIE4Up){
                preload_running = true;
                obj_preload_pic = objGalleryBoxContainerPicture.insertBefore(objTemp);
                Galleries[gallery_id].preloadRunner = window.setInterval(function () {
                    if (!isNaN(obj_preload_pic.offsetWidth) && obj_preload_pic.offsetWidth == Galleries[gallery_id].pictures_loaded[loading_picture_id].realwidth && !isNaN(obj_preload_pic.offsetHeight) && obj_preload_pic.offsetHeight == Galleries[gallery_id].pictures_loaded[loading_picture_id].realheight){
                        window.clearInterval(Galleries[gallery_id].preloadRunner);
                        myGallery.picture_onload(obj_preload_pic);
                        preload_running = false;
                    }
                }, 100);
            } else {
                obj_preload_pic = objGalleryBoxContainerPicture.appendChild(objTemp);
                obj_preload_pic.onload = function (event) {myGallery.picture_onload(this);};
            }
        }
    },
    picture_onload: function (obj) {
        Galleries[gallery_id].pictures_loaded[obj.id].obj = obj;
        Galleries[gallery_id].pictures_loaded[obj.id].loaded = true;
    },
    navInit: function () {
        if (Galleries[gallery_id].active_index>0){
            objGalleryBoxNavFirst.className = '';
            objGalleryBoxNavBack.className = '';
        }else{
            objGalleryBoxNavFirst.className = 'disabled';
            objGalleryBoxNavBack.className = 'disabled';
        }
        if (Galleries[gallery_id].active_index<Galleries[gallery_id].pictures.length-1){
            objGalleryBoxNavLast.className = '';
            objGalleryBoxNavNext.className = '';
        }else{
            objGalleryBoxNavLast.className = 'disabled';
            objGalleryBoxNavNext.className = 'disabled';
        }
        if (Galleries[gallery_id].pictures.length==1){
            objGalleryBoxNav.style.visibility = 'hidden';
        }
    },
    navFirst: function() {
        if (Galleries[gallery_id].active_index>0){
            loaded_picture_id = myGallery.getLoadedPictureId(0);
            if (Galleries[gallery_id].pictures_loaded[loaded_picture_id] && Galleries[gallery_id].pictures_loaded[loaded_picture_id].loaded){
                myGallery.picture_change(loaded_picture_id,0);
                myGallery.preloadNext();
            }else{
                window.setTimeout(myGallery.navFirst,100);
            }
        }
    },
    navBack: function() {
        if (Galleries[gallery_id].active_index>0){
            picture_index = Galleries[gallery_id].active_index-1;
            loaded_picture_id = myGallery.getLoadedPictureId(picture_index);
            if (Galleries[gallery_id].pictures_loaded[loaded_picture_id] && Galleries[gallery_id].pictures_loaded[loaded_picture_id].loaded){
                myGallery.picture_change(loaded_picture_id,Galleries[gallery_id].active_index-1);
                myGallery.preloadPrevious();
            }else{
                window.setTimeout(myGallery.navBack,100);
            }
        }
    },
    navNext: function() {
        if (Galleries[gallery_id].active_index<Galleries[gallery_id].pictures.length-1){
            picture_index = Galleries[gallery_id].active_index+1;
            loaded_picture_id = myGallery.getLoadedPictureId(picture_index);
            if (Galleries[gallery_id].pictures_loaded[loaded_picture_id] && Galleries[gallery_id].pictures_loaded[loaded_picture_id].loaded){
                myGallery.picture_change(loaded_picture_id,Galleries[gallery_id].active_index+1);
                myGallery.preloadNext();
            }else{
                window.setTimeout(myGallery.navNext,100);
            }
        }
    },
    navLast: function() {
        if (Galleries[gallery_id].active_index<Galleries[gallery_id].pictures.length-1){
            picture_index = Galleries[gallery_id].pictures.length-1;
            loaded_picture_id = myGallery.getLoadedPictureId(picture_index);
            if (Galleries[gallery_id].pictures_loaded[loaded_picture_id] && Galleries[gallery_id].pictures_loaded[loaded_picture_id].loaded){
                myGallery.picture_change(loaded_picture_id,Galleries[gallery_id].pictures.length-1);
                myGallery.preloadPrevious();
            }else{
                window.setTimeout(myGallery.navLast,100);
            }
        }
    },
    picture_change: function(loaded_picture_id,new_active_index) {
        GalleryBoxEntityEffect.hide();
        old_index = Galleries[gallery_id].active_index;
        if (!isNaN(old_index) && old_index !=new_active_index){
            old_loaded_picture_id = myGallery.getLoadedPictureId(old_index);
            Galleries[gallery_id].pictures_loaded[old_loaded_picture_id].obj.style.visibility = 'hidden';
        }
        objGalleryBoxEntityLoad.visibility = 'visible';
        Galleries[gallery_id].active_index = new_active_index;
        Galleries[gallery_id].active_id = Galleries[gallery_id].pictures[Galleries[gallery_id].active_index];
        objGalleryBoxEntityText.innerHTML = '';
        if (Galleries[gallery_id].pictures_loaded[loaded_picture_id].title){
            objGalleryBoxEntityText.innerHTML += '<div id="GalleryBoxEntityTitle">' + Galleries[gallery_id].pictures_loaded[loaded_picture_id].title + '</div>';
        }
        if (Galleries[gallery_id].pictures_loaded[loaded_picture_id].description){
            objGalleryBoxEntityText.innerHTML += '<div id="GalleryBoxEntityDescription">' + Galleries[gallery_id].pictures_loaded[loaded_picture_id].description + '</div>';
        }
        myGallery.entityResize();
        Galleries[gallery_id].pictures_loaded[loaded_picture_id].obj.style.visibility = 'visible';
        href_string = Galleries[gallery_id].pictures_loaded[loaded_picture_id].obj.src
        href_splitted = href_string.split('/');
        new_href_string = href_string.replace(href_splitted[href_splitted.length-1],'') + 'gallery_printpic_ger.html';
        objGalleryPrint.href = new_href_string;
        objGalleryBoxEntityLoad.style.visibility = 'hidden';
        GalleryBoxEntityEffect.custom(0,1);
        myGallery.navInit();
    },
    end: function() {
        objGalleryBoxEntity.style.visibility = 'hidden';
        objGalleryBox.style.display = 'none';
        GalleryOverlayEffect.custom(0.8,0);
        showSelectBoxes();
        Gallery_started = false;
    }
}

gallery_oldonload = window.onload;
window.onload = function(){
    myGallery = new Gallery();
    if (gallery_oldonload){
        gallery_oldonload();
    }
}