window.pfClientPhotosShowTable = function (widget, album) {
    this.widget     = widget;

    this.itemWidth  = 120;
    this.epp        = album.photos.length;

    this.album      = album;
    this.items      = album.photos;
    this.page       = 1;
    this.pages      = 1;

    this.pageHref = function (page) {
        throw "method can't be used";
    }

    this.drawItem = function (cont, item, itemIdx) {
        this.widget.widgetPhotos.drawShowPhoto(cont, item, itemIdx, this.album.id);
    }
}

window.pfClientPhotosTable = function (widget, album, page) {
    this.widget     = widget;

    this.itemWidth  = 138;
    this.epp        = 8;
    this.forceCols  = 4;

    this.album      = album;
    this.items      = album.photos;
    this.page       = page;
    this.pages      = 0;

    this.selected           = [];

    this.pageHref = function (page) {
        return 'javascript:'+this.widget.main.name+'.pageSmallPhotos('+album.id+','+page+');';
    }

    this.drawItem = function (cont, item, idx) {
        this.selected[this.selected.length] = item;
        this.widget.widgetPhotos.drawSmallPhoto(cont, item, idx, this.album.id, widget.main.isOwner);
    }
}

window.pfClientWidgetPhotos = function (core) {
    this.core = core;

    this.checkboxes         = [];
    this.currentAlbum       = null;
    this.currentPhotoIdx    = -1;
    this.currentPage        = 1;

    this.showLargePhoto = function () {
        var self            = this;
        var photoImg        = this.core.dom(this.getLargePhotoImgId());
        var photo           = this.currentAlbum.photos[this.currentPhotoIdx];
        photoImg.title      = photo.title;
        photoImg.src        = photo.urlMedium;
        photoImg.onclick    = function() {self.showNextLargePhoto()}
    }

    this.showNextLargePhoto = function () {
        this.currentPhotoIdx = this.currentPhotoIdx + 1 < this.currentAlbum.photos.length ? this.currentPhotoIdx + 1 : 0;
        this.showLargePhoto();
    }

    this.showPrevLargePhoto = function () {
        this.currentPhotoIdx = this.currentPhotoIdx > 0 ? this.currentPhotoIdx - 1 : this.currentAlbum.photos.length - 1
        this.showLargePhoto();
    }


    this.pageLargePhotos  = function (cont, album, photoIdx) {
        if (album.photos.length == 0) {
            throw "Album does not contain photos";
        }

        if (album.photos[photoIdx] == undefined) {
            throw "Album does not contain photo with idx = "+photoIdx;
        }

        this.currentAlbum       = album;
        this.currentPhotoIdx    = photoIdx;

        var photo = album.photos[photoIdx];

        var table   = this.core.create('table'   , cont, 'photo');
        var tbody   = this.core.create('tbody'   , table);
        var headTr  = this.core.create('tr'      , tbody);
        var showTr  = this.core.create('tr'      , tbody);
        var photoTr = this.core.create('tr'      , tbody);
        var act1Tr  = this.core.create('tr'      , tbody);

        var self = this;

        // head with nav
        var headTd          = this.core.create('td', headTr);
        var buttonAlbums    = this.core.createInput(null, 'button', null, 'к списку Альбомов', null, headTd, 'button');
        buttonAlbums.onclick= function () { self.core.main.pageAlbums(); }

        this.core.write(headTd, ' ');

        var buttonPhotos     = this.core.createInput(null, 'button', null, 'Маленькие фотки', null, headTd, 'button');
        buttonPhotos.onclick = function () { self.core.main.pageSmallPhotos(album.id); }

        if (self.core.main.isOwner) {
            this.core.write(headTd, ' ');

            var buttonAdd       = this.core.createInput(null, 'button', null, 'добавить фото', null, headTd, 'button');
            buttonAdd.onclick   = function () {
                self.core.main.pageUpload(album.id);
            };
        }

        // preview show
        var showTd          = this.core.create('td'  , showTr, 'show');
        var showDiv         = this.core.create('div' , showTd, 'show');
        showDiv.style.width = this.core.main.cont.offsetWidth - 25 + 'px';
        var table           = new pfClientPhotosShowTable(this.core, album);
        this.core.drawTable(showDiv, table, album.photos, album.photos.length, 1, 1);

        // large photo image
        var photoTd         = this.core.create('td', photoTr, 'bigdot');

        var photoImg        = this.core.create('img', photoTd);
        photoImg.id         = this.getLargePhotoImgId();
        photoImg.alt        = 'фото';
        this.showLargePhoto();

        // large photo controls 1
        var act1Td          = this.core.create('td', act1Tr, 'photo_icons');

        var tableAct1       = this.core.create('table', act1Td, 'actions');
        var tbodyAct1       = this.core.create('tbody', tableAct1);
        var trAct1          = this.core.create('tr', tbodyAct1, 'actions');
        var tdAct1Left      = this.core.create('td', trAct1, 'act_left');
        var tdAct1Center    = this.core.create('td', trAct1, 'act_center');
        var tdAct1Right     = this.core.create('td', trAct1, 'act_right');

        var tableIcons1 = this.core.create('table', tdAct1Center, 'divicon largeicon');
        var tbodyIcons1 = this.core.create('tbody', tableIcons1);
        var trIcons1    = this.core.create('tr', tbodyIcons1);

        this.core.widgetIcons.iconLargePrev         (tdAct1Left);

        if (self.core.main.isOwner) {

            this.core.widgetIcons.iconLargeUnrotate     (this.core.create('td', trIcons1), photo, function () {
                self.core.main.api.callPhotoRotateAntiClockwise(photo.id, function(answer) {
                    self.core.main.model.clearPhotos(album.id);
                    self.core.main.wrapper.pagePhotosLarge(album.id, photo.id);
                });
            });

            this.core.widgetIcons.iconLargeRotate       (this.core.create('td', trIcons1), photo, function () {
                self.core.main.api.callPhotoRotateClockwise(photo.id, function(answer) {
                    self.core.main.model.clearPhotos(album.id);
                    self.core.main.wrapper.pagePhotosLarge(album.id, photo.id);
                });
            });
        }

        //this.core.widgetIcons.iconLargeFull         (this.core.create('td', trIcons1), photo);

        if (self.core.main.isOwner) {
            this.core.widgetIcons.iconLargeDelete       (this.core.create('td', trIcons1), photo, function() {
                if (confirm("Вы уверены?")) {
                    self.core.main.api.callPhotoDelete(album.id, photo.id, function(answer) {
                        self.core.main.model.clearPhotos(answer.albumId);
                        self.core.main.wrapper.pagePhotosLarge(answer.albumId);
                    });
                }
            });
        }

        //this.core.widgetIcons.iconLargeSendLink     (this.core.create('td', trIcons1), photo);
        this.core.widgetIcons.iconLargeTakeLink     (this.core.create('td', trIcons1), photo, function () {
            self.core.widgetEtc.raisePopup(self.core.main.linker.linkLargePhotos(album['id'], photo['id']));
        });
        //this.core.widgetIcons.iconLargeCard         (this.core.create('td', trIcons1), photo);

        this.core.widgetIcons.iconLargeNext         (tdAct1Right);
    }

    this.showLargePhotoBlock = function (albumId, photoIdx) {
        var cont    = this.core.dom(this.getLargePhotoDivId());
        var display = cont.style.display;
        cont.style.display = 'none';
        this.core.clear(cont);

        this.blockLargePhoto(cont, albumId, photoIdx);

        cont.style.display = display;
    }

    this.blockLargePhoto  = function (cont, albumId, photoIdx) {
        var self    = this;
        var rootDiv = this.core.create('div', cont, 'LargePhotoView');

        var table   = this.core.create('table'  , rootDiv)
        var tbody   = this.core.create('tbody'  , table)
        var tr      = this.core.create('tr'     , tbody)

        var tdImg   = this.core.create('td'     , tr, 'Photo');
        var tdInfo  = this.core.create('td'     , tr, 'Info');

        var photos  = this.core.main.model.getPhotos(albumId);
        var photo   = photos[photoIdx];

        var divTitle    = this.core.create('div'    , tdInfo, 'Title');
        this.core.write(divTitle, this.core.splitLongWordsLazy(photo.title));

        if (photo.desc != undefined && photo.desc != null && photo.desc) {
            var divDesc     = this.core.create('div'    , tdInfo, 'Desc');
            this.core.write(divDesc,  this.core.splitLongWordsLazy(photo.desc));
        }

        if (this.core.main.isOwner) {
            var divAction   = this.core.create('div'    , tdInfo, 'Action');
            this.core.write(divAction   , 'Редактировать');
            divAction.onclick = function() {
                var title = prompt ("Новое название фото", photo.title);

                if (title != undefined && title != null && title != "" && title != photo.title) {
                    self.core.main.api.callPhotoChangeTitle(albumId, photo.id, title, function(answer) {
                        self.core.main.model.clearPhotos(albumId);
                        self.core.main.wrapper.pagePhotos(albumId);
                    });
                }
            }
        }

        var outerCont = cont;
        var img     = this.core.create('img', tdImg);
        img.alt     = photo.title;
        img.src     = photo.urlMedium;
        img.onclick = function () {
            self.core.clear(outerCont);
        }
    }

    this.pageSmallPhotos  = function (cont, album, page) {
        var self = this;

        this.currentAlbum       = album;
        this.currentPhotoIdx    = 0;
        this.currnetPage        = page;

        var divPhoto = this.core.create('div', cont);
        divPhoto.id = this.getLargePhotoDivId();

        // header

        /* TODO REMOVE
        // album box

        var albumTable  = this.core.create('table'   , cont          , 'album_info'  );
        var albumTBody  = this.core.create('tbody'   , albumTable                    );
        var albumTr     = this.core.create('tr'      , albumTBody    , 'album_info'  );
        var albumBoxTd  = this.core.create('td'      , albumTr       , 'cover'       );
        var albumAct1Td = this.core.create('td'      , albumTr       , 'albumlinks'  );
        var albumAct2Td = this.core.create('td'      , albumTr       , 'act2'        );

        this.core.widgetAlbums.drawAlbumBox(albumBoxTd, album, false);

        // album actions via links

        if (self.core.main.isOwner) {
            var linkProp    = this.core.createA('#prop'  , 'Свойства альбома', 'blue', albumAct1Td);

            linkProp.onclick = function () {
                self.core.main.pageEditAlbum(album.id);
            }

            this.core.create('br', albumAct1Td);
            var linkDel     = this.core.createA('#del'   , 'Удалить альбом'  , 'blue', albumAct1Td);
            linkDel.onclick = function () {
                if (confirm('Вы действительно хотите удалить альбом?')) {
                    self.core.main.api.callAlbumDelete(album.id, function (answer) {
                        self.core.main.model.deleteAlbum(answer.albumId);
                        self.core.main.pageAlbums();
                    });
                }
            }
        }
        */

        /*
        this.core.create('br', albumAct1Td);
        var linkSend    = this.core.createA('#send'  , 'Отправить ссылку', 'blue', albumAct1Td);
        this.core.create('br', albumAct1Td);
        var linkGetUrl  = this.core.createA('#url'   , 'Получить ссылку' , 'blue', albumAct1Td);
        */

        // album actions via buttons

        /* TODO REMOVE
        var buttonAlbums = this.core.createInput(null, 'button', null, 'к списку Альбомов', null, this.core.create('div', albumAct2Td), 'button');
        var self = this;
        buttonAlbums.onclick = function () { self.core.main.pageAlbums(); }

        if (self.core.main.isOwner) {
            var buttonAdd = this.core.createInput(null, 'button', null, 'добавить фото', null, this.core.create('div', albumAct2Td), 'button');
            buttonAdd.onclick = function () {
                self.core.main.pageUpload(album.id);
            }
        }

        var albumTitle = this.core.create('strong', this.core.create('div', albumAct2Td));
        this.core.write(albumTitle, 'Альбом: '+this.core.splitLongWordsLazy(album.title));

        var genreDiv = this.core.create('div', albumAct2Td);
        this.core.write(this.core.create('strong', genreDiv), 'Жанр: ');
        this.core.write(genreDiv, this.core.main.model.getGenre(album.genreId).name);

        var descDiv = this.core.create('div', albumAct2Td);
        this.core.write(this.core.create('strong', descDiv), 'Описание: ');
        this.core.write(descDiv, album.desc ? this.core.splitLongWordsLazy(album.desc) : 'нет описания');


        if (album.photos == undefined || album.photos == null || !album.photos.length) {
            this.core.write(this.core.create('div', cont, 'photosNo'), 'В данном альбоме нет фотографий');
            return;
        }
        */

        /* TODO REMOVE

        // select all
        if (self.core.main.isOwner) {
            var selectAllDiv = this.core.create('div', cont);
            var selectAllCheckbox = this.core.createInput(null, 'checkbox', 'selectAll', 1, null, selectAllDiv);
            this.core.write(selectAllDiv, 'выбрать все фото альбома');

            selectAllCheckbox.id = this.getAllPhotosCheckboxId();
            selectAllCheckbox.onchange = function () {
                self.checkAllPhotosOnChange(this);
            }

            selectAllCheckbox.onclick = function () {
                self.checkAllPhotosOnChange(this);
            }
        }
        */


        // photos list
        var listDiv = this.core.create('div', cont, 'photosList');
        var navDiv  = this.core.create('div', cont, 'nav');

        var table   = new pfClientPhotosTable(this.core, album, page);

        this.core.drawRubberTable(listDiv, table, this.core.main.cont.offsetWidth);
        this.core.drawNav(navDiv, table);

        this.checkboxes = table.selected;


        // footer
        this.core.create('a', cont).name = 'bottom';


        /* TODO REMOVE
        if (self.core.main.isOwner) {

            var form            = this.core.create('form', cont, 'groupAction');
            form.id             = this.getGroupFormId();
            form.onsubmit       = function () {return false;};

            var actionDiv       = this.core.create('div', form);

            var totalDiv        = this.core.create('div', actionDiv);
            this.core.write(totalDiv, 'Выбрано ');
            var totalSpan       = this.core.create('strong', totalDiv);
            this.core.write(totalSpan, '0');
            totalSpan.id        = this.getTotalNumContId();
            this.core.write(totalDiv, ' фото');

            this.core.write(this.core.create('strong', actionDiv), 'Выбранные фото:');

            var delDiv          = this.core.create('div', actionDiv);
            var delRadio        = this.core.createInput (null, 'radio', 'groupAction', 'del', null, delDiv);
            delRadio.id         = this.getDelRadioId();
            this.core.write(delDiv, 'Удалить');

            //var blockDiv        = this.core.create('div', actionDiv);
            //var blockRadio      = this.core.createInput (null, 'radio', 'groupAction', 'block', null, blockDiv);
            //blockRadio.id       = this.getBlockRadioId();
            //this.core.write(blockDiv, 'Запретить установку комментариев');

            var moveDiv         = this.core.create('div', actionDiv);
            var moveRadio       = this.core.createInput (null, 'radio', 'groupAction', 'move', null, moveDiv);
            moveRadio.id        = this.getMoveRadioId();
            this.core.write(moveDiv, 'Переместить в альбом ');

            var albumsSelect    = this.core.create('select', moveDiv);
            albumsSelect.name   = 'albumId';
            albumsSelect.id     = this.getGroupToAlbumId();

            var albumsList      = this.core.main.model.getAllAlbums();
            var idx             = 0;
            var idxLim          = albumsList.length;
            
            var hideMoveOption  = true;

            for (idx = 0; idx < idxLim; idx++) {

                if (albumsList[idx].id == album.id) {
                    continue;
                }

                var option      = this.core.create('option', albumsSelect);
                option.value    = albumsList[idx].id
                this.core.write(option, albumsList[idx].title);

                hideMoveOption  = false;
            }

            if (hideMoveOption) {
                moveDiv.style.display = 'none';
            }

            var buttonDiv       = this.core.create('div', actionDiv);
            var buttonDo        = this.core.createInput(null, 'button', 'doit', 'выполнить', null, buttonDiv, 'button');
            buttonDo.id         = this.getDoButtonId();

            this.updateGroupActionForm();

        } // isOwner
        */

        if (self.core.main.isOwner && !this.core.main.drawPfSelect) {
            var divUpload = this.core.create('div', cont, 'FastUpload');
            this.core.widgetUpload.pageUpload(divUpload, album);
        } // isOwner
    }

    this.drawShowPhoto = function (cont, photo, photoIdx, albumId) {
        var self    = this;
        var table   = this.core.create('table', cont  , 'photo_show');
        var tbody   = this.core.create('tbody', table               );
        var trImg   = this.core.create('tr'   , tbody               );
        var tdImg   = this.core.create('td'   , trImg , 'photo_show');

        var pictureImg      = this.core.create('img', tdImg);
        pictureImg.title    = photo.title;
        pictureImg.alt      = 'фото';
        pictureImg.src      = photo.urlSmall;
        pictureImg.onclick  = function () {
            self.currentPhotoIdx = photoIdx;
            self.showLargePhoto();
            return false;
        }
    }


    this.linkLargePhotosPage = function (albumId, photoIdx) {
        return 'javascript:'+this.core.main.name+'.pageLargePhotos('+albumId+','+photoIdx+');';
    }

    this.drawSmallPhoto = function (cont, photo, photoIdx, albumId, isOwner) {
        var self        = this;



        /* TODO REMOVE
        var iconsTable      = this.core.create('table'  , tdAct, 'divicon');
        var iconsTbody      = this.core.create('tbody'  , iconsTable);
        var iconsTr         = this.core.create('tr'     , iconsTbody);
        */

        if (this.core.main.drawPfSelect == 1)
        {
            var table   = this.core.create('table' , cont    , 'photo_table'   );
            var tbody   = this.core.create('tbody' , table                     );
    
            var trImg   = this.core.create('tr'    , tbody                     );
            var tdImg   = this.core.create('td'    , trImg   , 'picture'       );

            var trAct   = this.core.create('tr'    , tbody                     );
            var tdAct   = this.core.create('td'    , trAct   , 'bott2'         );            
            
            var trInfo  = this.core.create('tr'    , tbody                     );
            var tdInfo  = this.core.create('td'    , trInfo  , 'middle2'       );
    
            // photo's picture
            var pictureImg      = this.core.create('img', tdImg);
            pictureImg.alt      = 'фото';
            pictureImg.title    = photo.title;
            pictureImg.src      = photo.urlSmall;
                        
            var selectInput = this.core.createInput('input', 'radio', 'select_photo', photo.urlSmall, false, tdAct);
            //this.core.write(selectInput, '');
            
            /*pictureImg.onclick  = function () {
                selectInput.checked = true;
                //self.showLargePhotoBlock(albumId, photoIdx);
            }*/
        }
        else 
        {
            var form        = this.core.create('form'  , cont);
            form.name       = this.getPhotoCheckboxFormName();
            form.action     = '';
            form.onsubmit   = function () {return false;}            
            
            var table   = this.core.create('table' , form    , 'photo_table'   );
            var tbody   = this.core.create('tbody' , table                     );
    
            var trImg   = this.core.create('tr'    , tbody                     );
            var tdImg   = this.core.create('td'    , trImg   , 'picture'       );
    
            var trInfo  = this.core.create('tr'    , tbody                     );
            var tdInfo  = this.core.create('td'    , trInfo  , 'middle2'       );
    
            var trAct   = this.core.create('tr'    , tbody                     );
            var tdAct   = this.core.create('td'    , trAct   , 'bott2'         );
    
            // photo's picture
            var pictureImg      = this.core.create('img', tdImg);
            pictureImg.alt      = 'фото';
            pictureImg.title    = photo.title;
            pictureImg.src      = photo.urlSmall;
                    
            pictureImg.onclick  = function () {
                self.showLargePhotoBlock(albumId, photoIdx);
            }
            // photo's controls
            if (isOwner) {
                // edit
                var spanEdit = this.core.create('span', tdAct, 'Action');
                this.core.write(spanEdit, 'Редактировать')
                spanEdit.onclick = function () {
                    var title = prompt ("Новое название фото", photo.title);
    
                    if (title != undefined && title != null && title != "" && title != photo.title) {
                        self.core.main.api.callPhotoChangeTitle(albumId, photo.id, title, function(answer) {
                            self.core.main.model.clearPhotos(albumId);
                            self.core.main.wrapper.pagePhotos(albumId);
                        });
                    }
                }
    
                // delim
                this.core.write(tdAct, '/');
    
                // delete
                var spanDelete = this.core.create('span', tdAct, 'Action');
                this.core.write(spanDelete, 'Удалить')
                spanDelete.onclick = function () {
                    if (confirm("Вы точно хотите удалить фото?")) {
                        self.core.main.api.callPhotoDelete(albumId, photo.id, function(answer) {
                            self.core.main.model.clearPhotos(answer.albumId);
                            self.core.main.wrapper.pagePhotos(answer.albumId);
                        });
                    }
                };
    
                
                /* TODO REMOVE
                this.core.widgetIcons.iconLeft10   (this.core.create('td', iconsTr), photo, function () {
                    self.core.main.api.callPhotoChangePosition(albumId, photoIdx, -10, function(answer) {
                        self.core.main.model.clearPhotos(albumId);
                        self.core.main.wrapper.pagePhotos(albumId);
                    });
                });
    
                this.core.widgetIcons.iconLeft     (this.core.create('td', iconsTr), photo, function () {
                    self.core.main.api.callPhotoChangePosition(albumId, photoIdx, -1, function(answer) {
                        self.core.main.model.clearPhotos(albumId);
                        self.core.main.wrapper.pagePhotos(albumId);
                    });
                });
    
                this.core.widgetIcons.iconEdit     (this.core.create('td', iconsTr), photo, function () {
                    var title = prompt ("Новое название фото", photo.title);
    
                    if (title != undefined && title != null && title != "" && title != photo.title) {
                        self.core.main.api.callPhotoChangeTitle(albumId, photo.id, title, function(answer) {
                            self.core.main.model.clearPhotos(albumId);
                            self.core.main.wrapper.pagePhotos(albumId);
                        });
                    }
                });
    
                this.core.widgetIcons.iconDelete   (this.core.create('td', iconsTr), photo, function () {
                    if (confirm("Вы уверены?")) {
                        self.core.main.api.callPhotoDelete(albumId, photo.id, function(answer) {
                            self.core.main.model.clearPhotos(answer.albumId);
                            self.core.main.wrapper.pagePhotos(answer.albumId);
                        });
                    }
                });
    
                this.core.widgetIcons.iconRight    (this.core.create('td', iconsTr), photo, function () {
                    self.core.main.api.callPhotoChangePosition(albumId, photoIdx, 1, function(answer) {
                        self.core.main.model.clearPhotos(albumId);
                        self.core.main.wrapper.pagePhotos(albumId);
                    });
                });
    
                this.core.widgetIcons.iconRight10  (this.core.create('td', iconsTr), photo, function () {
                    self.core.main.api.callPhotoChangePosition(albumId, photoIdx, 10, function(answer) {
                        self.core.main.model.clearPhotos(albumId);
                        self.core.main.wrapper.pagePhotos(albumId);
                    });
                });
                */
    
    
                /* TODO REMOVE
                // photo's name+select
                var photoCheckbox   = this.core.createInput(null, 'checkbox', 'action_elements[]', photo.id, null, tdInfo);
                photoCheckbox.id    = this.getPhotoCheckboxId(photo.id);
                photoCheckbox.onchange = function () {
                    self.checkOnePhotoOnChange(this);
                }
    
                photoCheckbox.onclick = function () {
                    self.checkOnePhotoOnChange(this);
                }
                */
    
            } // isOwner            
        }        

        this.core.write(this.core.create('strong', tdInfo), this.core.splitLongWords(photo.title));
    }
     

    this.getPhotoCheckboxId = function (photoId) {
        return this.core.main.name+'_photo_checkbox_'+photoId;
    }

    this.getAllPhotosCheckboxId = function (photoId) {
        return this.core.main.name+'_photo_checkbox_all';
    }

    this.getPhotoCheckboxFormName = function () {
        return this.core.main.name+'_photo_checkbox_form';
    }

    this.getTotalNumContId = function () {
        return this.core.main.name+'_total_photos';
    }

    this.getDoButtonId = function () {
        return this.core.main.name+'_doit';
    }

    this.getGroupFormId = function () {
        return this.core.main.name+'_gropuform_id';
    }

    this.getLargePhotoImgId = function () {
        return this.core.main.name+'_large_photo_img';
    }

    this.getLargePhotoDivId = function () {
        return this.core.main.name+'_large_photo_div';
    }


    this.getDelRadioId      = function () {
        return this.core.main.name+'_del_radio_id';
    }

    this.getBlockRadioId    = function () {
        return this.core.main.name+'_block_radio_id';
    }

    this.getMoveRadioId     = function () {
        return this.core.main.name+'_move_radio_id';
    }

    this.getGroupToAlbumId = function () {
        return this.core.main.name+'_group_to_album_id';
    }

    this.checkAllPhotosOnChange = function (checkbox) {

        var j, jlim, checked = checkbox.checked;

        for (j = 0, jlim = this.checkboxes.length; j < jlim; j++) {
            var box = this.core.dom(this.getPhotoCheckboxId(this.checkboxes[j].id));
            box.checked = checked;
        }

        this.updateGroupActionForm();
    }

    this.checkOnePhotoOnChange = function (checkbox) {

        var j, jlim, checked = true;

        for (j = 0, jlim = this.checkboxes.length; j < jlim; j++) {
            var box = this.core.dom(this.getPhotoCheckboxId(this.checkboxes[j].id));
            checked = checked && box.checked;
        }

        this.core.dom(this.getAllPhotosCheckboxId()).checked = checked;

        this.updateGroupActionForm();
    }

    this.makeGropuAction = function () {
        var self    = this;

        var photos  = [];
        var j       = 0;
        var jlim    = 0;

        for (j = 0, jlim = this.checkboxes.length; j < jlim; j++) {
            if (this.core.dom(this.getPhotoCheckboxId(this.checkboxes[j].id)).checked) {
                photos[photos.length] = this.checkboxes[j].id;
            }
        }
                
        if (this.core.dom(this.getDelRadioId()).checked) {
            if (confirm('Вы уверены, что хотите удалить выбранные фото?')) {
                this.core.main.api.callPhotoDelete(this.currentAlbum.id, photos, function(answer) {
                    self.core.main.model.clearPhotos(answer.albumId);
                    self.core.main.wrapper.pagePhotos(answer.albumId);
                });
            }
        }
        else if (this.core.dom(this.getMoveRadioId()).checked) {
            var fromId  = this.currentAlbum.id;
            var toId    = this.core.dom(this.getGroupToAlbumId()).value;

            if (confirm('Вы уверены, что хотите переместить выбранные фото в другой альбом?')) {
                this.core.main.api.callPhotoMove(fromId, toId, photos, function(answer) {
                    self.core.main.model.clearPhotos(answer.fromId);
                    self.core.main.model.clearPhotos(answer.toId);
                    self.core.main.wrapper.pagePhotos(answer.fromId);
                });
            }
        }
        else {
            alert('Необходимо выбрать, что делать с выбранными фото');
        }
    }

    this.updateGroupActionForm = function () {
        var self = this;

        var j, jlim, selected = 0;

        for (j = 0, jlim = this.checkboxes.length; j < jlim; j++) {
            if (this.core.dom(this.getPhotoCheckboxId(this.checkboxes[j].id)).checked) {
                selected++ ;
            }
        }

        var totCont = this.core.dom(this.getTotalNumContId());
        this.core.clear(totCont);
        this.core.write(totCont, '' + selected);


        var buttonDo = this.core.dom(this.getDoButtonId());
        if (selected > 0) {
            buttonDo.onclick   = function () {self.makeGropuAction()};
        }
        else {
            buttonDo.onclick   = function () { alert('Для начала нужно выбрать фото') }
        }
    }
}

