        $(document).ready(function(){
            //for firefox with no firebug
            if (!window.console) console = {log: function() {}};
            
            /*var n = 5;
            for(var i=1; i<=n; i++) {
                $('#gallery').html($('#gallery').html() +
                        '\n<div class="imageitem" id="image'+ i +'">'+
                            '\n   <img id="imgimage'+ i +'" class="image" />'+
                        '\n</div>');
            }*/
            $(".image").each(
                    function(index){
                        var thumb = this;
                        $('#'+thumb.id).load(function(){
                            console.log('onLoadEnd ' + thumb.id);
                            $('#'+thumb.id).parent().removeClass('loading');
                            
                        });
                    }
            );
            $(".imageitem").each(
                    function(index){
                        var thumb = this;
                        console.log(this.id);
                        $( ($('#'+thumb.id).parent().children()[1]).children[1] ).bind('click', function(){
                            $.post('add-new-item.php', {deleteimage: thumb.id}, function(response) {
                                console.log('delete image ' + thumb.id + 'status: ' + response);
                            });
                            var imageinput = $('input[name=lx_'+ thumb.id +']');
                            imageinput.remove();
                            //$('#'+thumb.id + ' img').removeAttr('src');
                            $('#'+thumb.id+' img').attr('src', '/images/no_image.jpg');
                            //$('#'+thumb.id + ' img').hide();
                        });
                        var ubutton = $('#u'+thumb.id)  ;
                        var mydata = {
                            filesubmit: true
                        }
                        new AjaxUpload(ubutton, {
                            action: 'add-new-item.php',
                            data: mydata,
                            name: thumb.id,
                            onSubmit: function(file, extension) {
                               console.log('onSubmit ' + thumb.id);
                               $('#'+thumb.id).addClass('loading');
                               $('#'+thumb.id+' img').attr('src', '');
                               //$('#'+thumb.id+' img').hide();
                            },
                            onComplete: function(file, response) {
                                var res = eval('(' + response + ')');
                                console.log(res.filename);
                                console.log('error: ' + res.error)
                                console.log('onComplate ' + thumb.id);
                                $('#'+thumb.id+' img').attr('src', res.fullfilename);
                                //$('#'+thumb.id+' img').show();
                                
                                //<input type="hidden" name="lx_image1" value="{$data.lx_image1}" />
                                var lx_imageel = $('<input type="hidden"> </input>');
                                $('.adnew form').append(lx_imageel); 
                                lx_imageel.attr({
                                  name: 'lx_' + thumb.id,
                                  value: res.filename
                                });
                                console.log(lx_imageel);
                                
                            }
                        });
                        
                    }
            );
            
        });
        
