function class_gallery(){
    var images = new Array();
    var position;
    var userid;
    
    this.init = function(images_array,user){ 
        images = images_array;
        userid = user;
        position = 0;
        display_gallery();
    }
    
    function display_gallery(){
        var gallery_top = document.createElement('div');
        gallery_top.id='gallery_top';
        gallery_top.className='bildergalerie_auswahl';
        document.getElementById('gallery').appendChild(gallery_top);
        
        //Linker Pfeil
        
            var left_arrow = document.createElement("div");
            left_arrow.className="bildergalerie_navi";
            
            var left_img = new Image();
            left_img.className='';
            left_img.src='img/pfeil_links_sw.png';
            left_img.onclick=my_gallery.to_left;
            
            if(images.length<=5){
                left_img.style.visibility = 'hidden';
            }
                
            left_arrow.appendChild(left_img);
            document.getElementById('gallery_top').appendChild(left_arrow);
        
        //Vorschaubilder
        for(var i=0;i<images.length;i++){
            var preview = document.createElement('div');
            preview.className='bildergalerie_klein';
            
            var img = new Image();
            img.id='preview_' + i;
            img.className='';
            img.src='sites/galerie_bild_ausgeben.php?i=' + images[i];
            img.onclick=my_gallery.to_main;
            
            preview.appendChild(img);
            document.getElementById('gallery_top').appendChild(preview);
        }
        
        for (var i=images.length;i<5;i++){
            var preview = document.createElement('div');
            preview.className='bildergalerie_klein';
            var dummy = document.createElement('div');
            preview.appendChild(dummy);
            document.getElementById('gallery_top').appendChild(preview);
        }
        
        //Rechter Pfeil
        if(images.length>5){
            var right_arrow = document.createElement("div");
            right_arrow.className='bildergalerie_navi';
            
            var right_img = new Image();
            right_img.className='';
            right_img.src='img/pfeil_rechts_sw.png';
            right_img.onclick=my_gallery.to_right;
            
            right_arrow.appendChild(right_img);
            document.getElementById('gallery_top').appendChild(right_arrow);
        }
        //Grosses Image
        var gallery_detail = document.createElement('div');
        gallery_detail.id='gallery_detail';
        gallery_top.className='bildergalerie_detail';
        document.getElementById('gallery').appendChild(gallery_detail);
        
        var gallery_big = document.createElement('div');
        gallery_big.id='gallery_big';
        gallery_big.className='bildergalerie_gross';
        gallery_detail.appendChild(gallery_big);
        
        var gallery_image = document.createElement('div');
        gallery_image.id='gallery_image';
        gallery_image.className='imgouterfl';
        gallery_big.appendChild(gallery_image);
        
        var main_image = new Image();
        main_image.id='main';
        main_image.src='sites/galerie_main.php?i=' + images[0];
        gallery_image.appendChild(main_image);
        
        //Bildtitel und co        
        var imglr = document.createElement('div');
        imglr.id = 'imglr';
        imglr.className = 'imglr';
        document.getElementById('gallery_image').appendChild(imglr);
        
        var subbar = document.createElement('div');
        subbar.id = 'subbar';
        subbar.className = 'subbar';
        document.getElementById('imglr').appendChild(subbar);
        
        var subtitle = document.createElement('div');
        subtitle.id = 'subtitle';
        subtitle.className = 'subtitle';
        document.getElementById('subbar').appendChild(subtitle);
        
        request_imagedata(0);
        
        //Kommentare
        
    }
    
    this.to_right = function(){
        if ((position+5)<=(images.length-1)){
            for(var i=0;i<4;i++){
                document.getElementById('preview_' + i).src=document.getElementById('preview_' + (i+1)).src;
            }
            document.getElementById('preview_4').src='sites/galerie_bild_ausgeben.php?i=' + images[(position+5)];
            position++;
            //alert(position);
        }
    }
    
    this.to_left = function(){
        if (position>0){
            for(var i=4;i>0;i--){
                document.getElementById('preview_' + i).src=document.getElementById('preview_' + (i-1)).src;
            }
            position--;
            document.getElementById('preview_0').src='sites/galerie_bild_ausgeben.php?i=' + images[position];
            
            //alert(position);
        }
    }
    
    this.to_main = function(){
        //alert(this.parentNode.parentNode.innerHTML);
        for(var i=0;i<document.getElementById('gallery_top').childNodes.length;i++){
            var my_obj = document.getElementById('gallery_top').childNodes[i].childNodes[0];
            //alert(this.parentNode.parentNode.id);
            if (my_obj.id==this.id){
                var bild_position = i;
            }
        }
        //alert(this.parentNode.parentNode.id);
        //alert(bild_position);
        
        document.getElementById('main').src = 'sites/galerie_main.php?i=' + images[(bild_position+position-1)];
        
        request_imagedata((bild_position+position-1));
        
        //comments((bild_position+position-1));
    }
    
    function request_imagedata(imageid){
        if (window.XMLHttpRequest) {
            http = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
           http = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (http != null) {
           http.open("GET", "sites/galerie_main_data.php?i=" + images[imageid], true);
           http.onreadystatechange = ausgeben;
           http.send(null);
        }

        function ausgeben() {
           if (http.readyState == 4) {
                var daten = http.responseText;
                daten = eval("(" + daten + ")");
                
                document.getElementById('subtitle').innerHTML = daten[0] + '<br>' + daten[2] + '<br>' + daten[3] + '<br>' + daten[1];
                /*
                comments(imageid);
                if (userid!=0){
                    comment_add_box(imageid);
                }
               */
           }
        }
    }
    
    function comments(imageid){
        var comments_array;
        if (window.XMLHttpRequest) {
            http = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
           http = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (http != null) {
           http.open("GET", "sites/comments.php?i=" + images[imageid], true);
           http.onreadystatechange = holen;
           http.send(null);
        }
        
        function holen() {
           if (http.readyState == 4) {
                var daten = http.responseText;
                comments_array = eval(daten);
                //alert(comments_array);
                
                if (document.getElementById('nocomment')){
                    document.getElementById('subbar').removeChild(document.getElementById('nocomment'));
                }
                
                if(comments_array[0]=="0"){
                    
                    var nocomment = document.createElement('div');
                    nocomment.id = 'nocomment';
                    nocomment.innerHTML = '<i>Noch keine Kommentare vorhanden</i>';
                    document.getElementById('subbar').appendChild(nocomment);
                    
                }else{
                    if (document.getElementById('comments_box')){
                        for(var i=0;i<document.getElementById('comments_box').childNodes.length;i++){
                        document.getElementById('comments_box').removeChild(document.getElementById('comments_box').childNodes[i]);
                    }
                    }else{
                        var comments_box = document.createElement('div');
                        comments_box.id = 'comments_box';
                        document.getElementById('subbar').appendChild(comments_box);
                    }
                    
                    for (var i=0;i<comments_array.length;i++){
                            
                        var comment = document.createElement('div');
                        comment.id = 'comment_' + i;
                        document.getElementById('comments_box').appendChild(comment);
                        
                        var comment_person = document.createElement('div');
                        comment_person.id = 'comment_person_' + i;
                        comment_person.innerHTML = comments_array[i].name + ' ' + comments_array[i].secondname;
                        document.getElementById('comment_' + i).appendChild(comment_person);
                        
                        var comment_text = document.createElement('div');
                        comment_text.id = 'comment_text_' + i;
                        comment_text.innerHTML = comments_array[i].comment;
                        document.getElementById('comment_' + i).appendChild(comment_text);  
                    }
                }
            }    
        }
    }
    
    function comment_add_box(imageid){
        if (document.getElementById('new_comment')){
            document.getElementById('subbar').removeChild(document.getElementById('new_comment'));
        }
            
        var new_comment = document.createElement('div');
        new_comment.id = 'new_comment';
        document.getElementById('subbar').appendChild(new_comment);
        
        var comment_form = document.createElement('form');
        comment_form.id = 'comment_form';
        comment_form.method = 'post';
        document.getElementById('new_comment').appendChild(comment_form);
        
        var form_hidden = document.createElement('input');
        form_hidden.type = 'hidden';
        form_hidden.value = userid;
        document.getElementById('new_comment').appendChild(form_hidden);
        
        var write_comment = document.createElement('div');
        write_comment.innerHTML = 'Kommentar schreiben';
        document.getElementById('new_comment').appendChild(write_comment);
        
        var textarea = document.createElement('textarea');
        textarea.id = 'comment_textarea';
        textarea.name = 'comment';
        textarea.cols = '50';
        textarea.rows = '3';
        textarea.size = '50';
        textarea.className = 'defbreite';
        document.getElementById('new_comment').appendChild(textarea);
        
        var submit_button = document.createElement('input');
        submit_button.value = 'eintragen';
        submit_button.type = 'button';
        submit_button.className = 'button';
        submit_button.onclick = comment_add;
        document.getElementById('new_comment').appendChild(submit_button);
        
        function comment_add(){
            var http = null;
            if (window.XMLHttpRequest) {
               http = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
               http = new ActiveXObject("Microsoft.XMLHTTP");
            }
            if (http != null) {
               http.open("POST", "sites/comment_add.php?u=' + userid + '&i=' + images[imageid];", true);
               //http.onreadystatechange = ausgeben;
               http.setRequestHeader(
                  "Content-Type",
                  "application/x-www-form-urlencoded");
               http.send("u=" + userid + "&i=" + images[imageid] + "&c=" + document.getElementById('comment_textarea').value);
            }
            comments(imageid);
            
        }
    }
}
