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

    this.pageLoading = function (cont) {
        var table = this.core.create('table', cont, 'loading');
        var tbody = this.core.create('tbody', table);
        var trImg = this.core.create('tr', tbody);
        var trMsg = this.core.create('tr', tbody);
        var tdImg = this.core.create('td', trImg, 'animation');
        var tdMsg = this.core.create('td', trMsg, 'message');

        var loading = this.core.create('div', tdImg, 'animation');
        this.core.write(tdMsg, 'Подождите, идёт загрузка...');
    }

    this.pageError = function (cont, msg, code) {

        var div = this.core.create('div', cont, 'error');

        if (code == undefined) {
            code = -1;
        }

        if (msg instanceof pfClientApiException) {
            code = msg.code;
            msg  = msg.message;

            this.core.write(this.core.create('div', div, 'title'), 'Ошибка GW');
        }
        else if (msg instanceof pfClientApiPhotofileException) {
            code = msg.code;
            msg  = msg.message;


            if (code == 540) {
                this.core.write(this.core.create('div', div, 'title'), 'Ошибочное обращение!');
                this.core.write(this.core.create('div', div, 'message'), 'Нет такого пользователя: '+this.core.main.currentUser);
                return false;
            }

            this.core.write(this.core.create('div', div, 'title'), 'Ошибка API');
        }
        else {
            this.core.write(this.core.create('div', div, 'title'), 'Произошла ошибка!');
        }

        this.core.write(this.core.create('div', div, 'message'), msg ? msg : 'Без комментариев');
        this.core.write(this.core.create('div', div, 'code'), 'Код ошибки: '+code);

        if (msg != undefined && msg != null && typeof(msg) == 'object') {
            if (msg.fileName != undefined)
                this.core.write(this.core.create('div', div, 'misc'), 'файл: '+msg.fileName);

            if (msg.lineNumber != undefined)
                this.core.write(this.core.create('div', div, 'misc'), 'строка: '+msg.lineNumber);

            if (msg.stack != undefined)
                this.core.write(this.core.create('div', div, 'misc'), 'стэк вызова: '+msg.stack);

            //IE
            if (msg.name != undefined)
                this.core.write(this.core.create('div', div, 'misc'), 'имя ошибки: '+msg.name);

            if (msg.message != undefined)
                this.core.write(this.core.create('div', div, 'misc'), 'сообщение: '+msg.message);

            if (msg.description != undefined)
                this.core.write(this.core.create('div', div, 'misc'), 'описание: '+msg.description);
        }

        /*
        if (typeof(msg) == 'object') {
            var key;

            for (key in msg) {
                this.core.write(this.core.create('div', div, 'misc'), key+' = '+msg[key]);
            }
        }
        */
    }

    this.pageLink = function (cont) {
        var self = this;
        var main = this.core.main;

        var title   = this.core.create('h1', cont);
        this.core.write(title, 'Сссылка');

        var cont    = this.core.create('div', cont, 'service_menu');


        this.core.write(cont, 'Ссылка на фотографию:');

        var contButton  = this.core.create('div', cont, 'buttonClose');
        var textareaLink= this.core.createInput('textarea', null, 'pf_link', null, null, contButton, null);
        textareaLink.id = this.getTextareaLinkId();

        textareaLink.value = 'http://pochta.local/index.php/photohosting/login@domain/large/albumId/photoId/';
        this.core.create('br', contButton);
        var buttonClose = this.core.createInput(null, 'button','pf_close', ' Закрыть ', null, contButton, 'button');

        buttonClose.onclick = function () {
            self.closePopup();
        }

        window.onresize = function () {
            self.pageLinkUpdatePosition();
        }

        window.onscroll = function () {
            self.pageLinkUpdatePosition();
        }
    }

    this.getTextareaLinkId = function () {
        return 'pfClient_textarea_link_id';
    }

    this.getPageLinkId = function () {
        return 'pfClient_page_link_id';
    }

    this.raisePopup = function (link) {
        this.core.dom(this.getTextareaLinkId()).value = link;
        this.pageLinkUpdatePosition();
        this.core.dom(this.getPageLinkId()).style.display = 'block';
    }

    this.closePopup = function () {
        this.core.dom(this.getPageLinkId()).style.display = 'none';
    }

    this.pageLinkUpdatePosition = function () {

        var div = this.core.dom(this.getPageLinkId());

        var deltaX  =  window.pageXOffset
                    || document.documentElement.scrollLeft
                    || document.body.scrollLeft
                    || 0;

        var deltaY  =  window.pageYOffset
                    || document.documentElement.scrollTop
                    || document.body.scrollTop
                    || 0;

        var width   = document.body.offsetWidth;

        var absDivWidth = 400;

        var divLeft = parseInt((parseInt(width) - 300)/2 - parseInt(absDivWidth)/2 + parseInt(deltaX)) + 300;
        var divTop  = parseInt(200 + parseInt(deltaY));

        div.style.left  = divLeft + 'px';
        div.style.top   = divTop  + 'px';

        var now = new Date();
    }

    this.debugPFApi = function (res) {
        if (res['debug'] == undefined || res['debug']['api'] == undefined) {
            this.debugMsg("Коммункаций с фотофайлом не было");
            return;
        }

        var debug = res['debug']['api'];
        this.debugMsg("Коммункаций с фотофайлом: " + debug.length);
        var j;

        for (j = 0; j < debug.length; j++) {
            this.debugPFApiCommunication(j + 1, debug[j]);
        }
    }

    this.debugPFApiCommunication = function (j, comm) {
        var div = this.core.create('div', this.core.main.contDebug, 'comm');
        this.core.write(this.core.create('div', div), 'N: '+j);
        this.core.write(this.core.create('div', div), 'дата: '+comm['date']);
        this.core.write(this.core.create('div', div), 'url: '+comm['url']);
        this.core.write(this.core.create('div', div), 'запрос: ');
        this.core.write(this.core.create('pre', div, 'xml'), comm['query']);
        this.core.write(this.core.create('div', div), 'ответ:');
        this.core.write(this.core.create('pre', div, 'xml'), comm['answer']);
    }

    this.debugMsg = function (msg) {
        this.core.write(this.core.create('div', this.core.main.contDebug, 'msg'), msg);
    }

    this.pageNoPhotos = function (cont, album) {
        if (this.core.main.isOwner) {
            this.core.write(
                this.core.create('div', cont, 'NoPhotos')
              , 'В данном альбоме нет фото. Воспользуйтесь формой загрузки, что бы добавить новое фото.'
            );

            var divUpload = this.core.create('div', cont, 'FastUpload');
            this.core.widgetUpload.pageUpload(divUpload, album);
        }
        else {
            this.core.write(
                this.core.create('div', cont, 'NoPhotos')
              , 'У пользователя нет фото в данном альбоме'
            );
        }
    }

    /*
    // DEPRICATED
    this.pageLoginForm = function (cont) {
        var form        = this.core.create('form', cont, 'pf_login_form');

        this.core.write(this.core.create('div', form, 'pf_div pf_title'), 'Скажи волшебное слово, милчеловек...');

        var divLogin    = this.core.create('div', form, 'pf_div pf_login');
        var divPasswd   = this.core.create('div', form, 'pf_div pf_password');
        var divOk       = this.core.create('div', form, 'pf_div pf_ok');

        this.core.write(divLogin , 'Логинъ: ');
        this.core.write(divPasswd, 'Пароль: ');

        var login   = this.core.createInput(null, null, 'pf_login', 'pusk_login', null, divLogin);
        var passwd  = this.core.createInput(null, 'password', 'pf_passwd', 'pusk_passwd', null, divPasswd);
        var ok      = this.core.createInput(null, 'button','pf_ok', ' Ня! ', null, divOk, 'button');
        var emul    = this.core.createInput(null, 'button','pf_emul', ' ЭмульНя! ', null, divOk, 'button');

        login.id = 'pf_login';
        passwd.id = 'pf_passwd';

        core = this.core;
        main = this.core.main;

        ok.onclick = function () {
            var login   = core.dom('pf_login').value;
            var passwd  = core.dom('pf_passwd').value;

            main.realRun(login, passwd);
        }

        emul.onclick = function () {
            main.model.loadExamples();
            main.api.sid = '9e834f30dab5f5dcfe0b8a03ce499514';
            //main.pageUpload();
            //main.pageAlbums();
            //main.pageEditAlbum(2313496);
            main.wrapper.pagePhotos(2313496);
            //main.pageLargePhotos(2313496, 1);
        }
    }
    */
}

