window.pfClientAlbumsTable = function (widget, albumsList, page) {
    this.widget     = widget;

    this.itemWidth  = 124;
    this.epp        = 10;

    this.items      = albumsList;
    this.page       = page;
    this.pages      = 0;

    this.pageHref = function (page) {
        return 'javascript:'+this.widget.main.name+'.pageAlbums('+page+')';
    }

    this.drawItem = function (cont, item) {
        this.widget.widgetAlbums.drawAlbum(cont, item);
    }
}

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

    this.pageAlbums = function (cont, albumsList, page) {


        if (this.core.main.isOwner) {
            var ctrlDiv   = this.core.create('div', cont, 'albumsCtrl');
            var buttonAdd = this.core.createInput(null, 'button', null, 'Новый альбом', null, ctrlDiv, 'button');
            var self = this;
            buttonAdd.onclick   = function () {
                self.core.main.pageUpload();
            };
        }

        var listDiv = this.core.create('div', cont, 'albumsList');
        var navDiv  = this.core.create('div', cont, 'nav'       );

        var table   = new pfClientAlbumsTable(this.core, albumsList, page)

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


    this.makeAlbumLink  = function (album) {
        return 'javascript:'+this.core.main.name+'.wrapper.pagePhotos('+album.id+');';
    }

    this.drawAlbum = function (cont, album) {
        this.drawAlbumBox(cont, album, true);
    }

    this.drawAlbumBox = function (cont, album, footer) {
        var div   = this.core.create('div', null, 'albumbox');

        var table = this.core.create('table', div, 'photobox');
        var tbody = this.core.create('tbody', table);

        var topTr = this.core.create('tr', tbody);
        var botTr = this.core.create('tr', tbody);

        var pictureTd   = this.core.create('td', topTr, 'picture' );
        var quantityTd  = this.core.create('td', botTr, 'quantity');

        var albumHref   = this.core.create('a', pictureTd);
        albumHref.href  = this.makeAlbumLink(album);
        albumHref.title = album.title;

        if (album.accentUrl) {
            var img     = this.core.create('img', albumHref);
            img.src     = album.accentUrl;
            img.title   = album.title;
            img.alt     = 'альбом';
        }
        else {
            var noimg   = this.core.create('div', albumHref, 'noimg');
        }

        var photonameDiv = this.core.create('div', quantityTd, 'photoname');
        this.core.write(photonameDiv, album.photosCount+' фото');

        if (footer) {
            var footerCont  = this.core.create('div', div, 'photoname2');
            var titleHref   = this.core.create('a' , footerCont, 'bluelink'  );
            titleHref.href  = this.makeAlbumLink(album);
            this.core.write(titleHref, this.core.splitLongWords(album.title, 0, 100));
        }

        cont.appendChild(div);
    }

    this.submitAlbumEditForm = function () {
        var form = this.core.dom(this.getAlbumEditFormId());
        var self = this;

        //check for title
        if (form.title.value == '') {
            alert('Введите название альбома');
            return false;
        }

        //check for pwd
        if (form.pwd1.value != form.pwd2.value) {
            alert('Оба введёных пароля должны совпадать');
            return false;
        }

        this.core.main.api.callAlbumUpdate(
            form.albumId.value
          , form.title.value
          , form.desc.value
          , this.core.dom(this.core.widgetUpload.getGenresSelectId()).value
          , form.pwdChanged.value ? form.pwd1.value : null

          // callback
          , function (answer) {
                self.core.main.model.updateAlbum(answer);
                self.core.main.wrapper.pagePhotos(answer.id);
            }
        );
    }

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

        var form    = this.core.create('form' , cont)
        var table   = this.core.create('table', form);
        var tbody   = this.core.create('tbody', table);

        form.id     = this.getAlbumEditFormId();
        form.name   = this.getAlbumEditFormName();

        var inputAlbumId  = this.core.createInput('input', 'hidden', 'albumId', null, null, form);
        inputAlbumId.value = album.id;

        var pwd = this.generateFakePwd(album);

        this.core.widgetUpload.drawTitleTr(tbody, album.title);
        this.core.widgetUpload.drawGenreTr(tbody, album.genreId);
        this.core.widgetUpload.drawDescTr(tbody, album.desc);
        this.core.widgetUpload.drawPwd1Tr(tbody, pwd);
        this.core.widgetUpload.drawPwd2Tr(tbody, pwd);

        // footer
        var trFoot      = this.core.create('tr', tbody);
        var tdFoot      = this.core.create('td', trFoot, 'footer');
        tdFoot.colSpan  = 2;

        var buttonSubmit        = this.core.createInput('input', 'button', 'change', 'Изменить', null, tdFoot, 'button');

        buttonSubmit.onclick    = function () {
            self.submitAlbumEditForm();
        }

        var buttonCancel        = this.core.createInput('input', 'button', 'cancel', 'Отменить', null, tdFoot, 'button');
        buttonCancel.onclick    = function () {
            self.core.main.wrapper.pagePhotos(album.id);
        }
    }

    this.generateFakePwd = function (album) {
        if (!album.isPrivate) {
            return '';
        }

        var j, pwd = '', letter = 'j';

        for (j = 0; j < album.pwdLength; j++) {
            pwd = pwd + letter;
        }

        return pwd;
    }

    this.getAlbumEditFormName = function () {
        return this.core.main.name+'_album_edit_form';
    }

    this.getAlbumEditFormId = function () {
        return this.core.main.name+'_album_edit_form_id';
    }

}

