<!DOCTYPE html>
<html>
<head>
    <title>ONLYOFFICE Documents</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=IE8"/>
    <meta name="description" content="" />
    <meta name="keywords" content="" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">

    <style type="text/css">
        html {
            height: 100%;
        }

        body {
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }

        #wrap {
            position:absolute;
            left:0;
            top:0;
            right:0;
            bottom:0;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <div id="placeholder"></div>
    </div>

    <script type="text/javascript" src="api.js"></script>
    <script>
        (function() {

            // Url parameters

            var urlParams = getUrlParams(),
                cfg = getEditorConfig(urlParams),
                doc = getDocumentData(urlParams);

            // Document Editor

            var docEditor = new DocsAPI.DocEditor('placeholder', {
                type: urlParams['type'],
                width: '100%',
                height: '100%',
                documentType: urlParams['doctype'] || 'text',
                document: doc,
                editorConfig: cfg,
                events: {
                    'onReady': onDocEditorReady,
                    'onDocumentStateChange': onDocumentStateChange,
                    'onRequestEditRights': onRequestEditRights,
                    'onRequestHistory': onRequestHistory,
                    'onRequestHistoryData': onRequestHistoryData,
                    'onRequestEmailAddresses': onRequestEmailAddresses,
                    'onRequestStartMailMerge': onRequestStartMailMerge,
                    'onRequestHistoryClose': onRequestHistoryClose,
                    'onError': onError
                }
            });

            // Document Editor event handlers

            function onRequestEmailAddresses() {
                docEditor.setEmailAddresses({emailAddresses: ['aaa@mail.ru'], createEmailAccountUrl: 'http://ya.ru'});
            }

            function onRequestHistory() {
                docEditor.refreshHistory({
                    'currentVersion': 3,
                    'history': [
                    {
                        'user': {
                            id: '8952d4ee-e8a5-42bf-86f0-6cd77801ec15',
                            name: 'Татьяна Щербакова'
                        },
                        'changes': null,
                        'created': '1/18/2015 6:38 PM',
                        'version': 1,
                        'versionGroup': 1,
                        'key': 'wyX9AwRq_677SWKjhfk='
                    },
                    {
                        'user': {
                            id: '8952d4ee-e8a5-42bf-86f0-6cd77801ec15',
                            name: 'Татьяна Щербакова'
                        },
                        'changes': [
                            {
                                'user': {
                                    id: '8952d4ee-e8a5-42bf-86f0-6cd77801ec15',
                                    name: 'Татьяна Щербакова'
                                },
                                'created': '1/19/2015 6:30 PM'
                            },
                            {
                                'user': {
                                    'id': '8952d4ee-e8a5-42bf-11f0-6cd77801ec15',
                                    'name': 'Александр Трофимов'
                                },
                                'created': '1/19/2015 6:32 PM'
                            },
                            {
                                'user': {
                                    id: '8952d4ee-e8a5-42bf-86f0-6cd77801ec15',
                                    name: 'Татьяна Щербакова'
                                },
                                'created': '1/19/2015 6:38 PM'
                            }
                        ],
                        'created': '2/19/2015 6:38 PM',
                        'version': 2,
                        'versionGroup': 1,
                        'key': 'wyX9AwRq_677SWKjhfk='
                    },
                    {
                        'user': {
                            id: '895255ee-e8a5-42bf-86f0-6cd77801ec15',
                            name: 'Me'
                        },
                        'changes': null,
                        'created': '2/21/2015 6:38 PM',
                        'version': 3,
                        'versionGroup': 2,
                        'key': 'wyX9AwRq_677SWKjhfk='
                    },
                    {
                        'user': {
                            id: '8952d4ee-e8a5-42bf-11f0-6cd77801ec15',
                            name: 'Александр Трофимов'
                        },
                        'changes': null,
                        'created': '2/22/2015 6:37 PM',
                        'version': 4,
                        'versionGroup': 3,
                        'key': 'wyX9AwRq_677SWKjhfk='
                    },
                    {
                        'user': {
                            id: '8952d4ee-e8a5-42bf-11f0-6cd33801ec15',
                            name: 'Леонид Орлов'
                        },
                        'changes': null,
                        'created': '2/24/2015 6:29 PM',
                        'version': 5,
                        'versionGroup': 3,
                        'key': 'wyX9AwRq_677SWKjhfk='
                    }]
                });
            }

            function onRequestHistoryData(revision) {
                docEditor.setHistoryData(
                    {
                        'version': revision.data,
                        'url': 'http://isa2',
                        'urlDiff': 'http://isa2',
                        'changesUrl': 'http://isa2'
                    }
                );
            }

            function onRequestStartMailMerge() {
                docEditor.processMailMerge(true, 'some error message');
            }

            function onRequestHistoryClose() {
                // reload page
            }

            function onDocEditorReady(event) {
                if (event.target) {
                    //console.log('Ready! Editor: ', event.target);
                }
            }

            function onDocumentStateChange(event) {
                var isModified = event.data;
                //console.log(isModified);
            }

            function onRequestEditRights(event) {
                // occurs whenever the user tryes to enter edit mode
                docEditor.applyEditRights(true, "Someone is editing this document right now. Please try again later.");
            }

            function onError(event) {
                // critical error happened
                // examine event.data.errorCode and event.data.errorDescription for details
            }

            function onDownloadAs(event) {
                // return url of downloaded doc
                // console.log(event.data);
            }


            // helpers

            function getUrlParams() {
                var e,
                    a = /\+/g,  // Regex for replacing addition symbol with a space
                    r = /([^&=]+)=?([^&]*)/g,
                    d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
                    q = window.location.search.substring(1),
                    urlParams = {};

                while (e = r.exec(q))
                    urlParams[d(e[1])] = d(e[2]);

                return urlParams;
            }

            function getDocumentData(urlParams) {
                return {
                    key: urlParams["key"],
                    url: urlParams["url"] || '_offline_',
                    title: urlParams["title"],
                    fileType: urlParams["filetype"],
                    vkey: urlParams["vkey"],
                    permissions: {
                        edit: true,
                        download: true,
                        reader: true
                    }
                };
            }

            function getEditorConfig(urlParams) {
                return {
                    mode            : urlParams["mode"] || 'edit',
                    lang            : urlParams["lang"] || 'en',
                    canCoAuthoring  : true,
                    createUrl       : 'http://www.example.com/create',

                    user: {
                        id: urlParams["userid"] || 'uid-901', firstname: urlParams["userfname"] || 'Mitchell', lastname: urlParams["userlname"] || 'Hamish'
                    },
                    recent          : [
                        {title: 'Memory.docx',          url: 'http://onlyoffice.com',  folder: 'Document Editor'},
                        {title: 'Description.doc',      url: 'http://onlyoffice.com',  folder: 'Document Editor'},
                        {title: 'DocEditor_right.xsl',  url: 'http://onlyoffice.com',  folder: 'Spreadsheet Editor'},
                        {title: 'api.rtf',              url: 'http://onlyoffice.com',  folder: 'Unnamed folder'}
                    ],
//                    templates       : [
//                        {name: 'Contracts', icon: '../../api/documents/resources/templates/contracts.png', url: 'http://...'},
//                        {name: 'Letter',    icon: '../../api/documents/resources/templates/letter.png', url: 'http://...'},
//                        {name: 'List',      icon: '../../api/documents/resources/templates/list.png', url: 'http://...'},
//                        {name: 'Plan',      icon: '../../api/documents/resources/templates/plan.png', url: 'http://...'}
//                    ],
                    embedded        : {
                        embedUrl        : 'http://onlyoffice.com/embed',
                        fullscreenUrl   : 'http://onlyoffice.com/fullscreen',
                        saveUrl         : 'http://onlyoffice.com/download',
                        shareUrl        : 'http://tl.com/72b4la97',
                        toolbarDocked   : 'top'
                    }
                    ,customization: {
//                        logo: {
//                            image: 'https://dylnrgbh910l3.cloudfront.net/studio/tag/i8.8.237/skins/default/images/onlyoffice_logo/editor_logo_general.png', // default size 86 x 20
//                            imageEmbedded: 'https://d2hw9csky753gb.cloudfront.net/studio/tag/i8.8.237/skins/default/images/onlyoffice_logo/editor_embedded_logo.png', // default size 124 x 20
//                            url: 'http://...'
//                        },
//                        backgroundColor: '#ffffff',
//                        textColor: '#ff0000',
//                        customer: {
//                            name: 'SuperPuper',
//                            address: 'New-York, 125f-25',
//                            mail: 'support@gmail.com',
//                            www: 'www.superpuper.com',
//                            info: 'Some info',
//                            logo: 'https://img.imgsmail.ru/r/default/portal/0.1.29/logo.png' // default size 216 x 35
//                        },
//                        goback: {text: 'Go To London', url: 'http://...'},
                        about: true,
                        feedback: true
                    }
                };
            }

            // Mobile version

            function isMobile(){
                var prefixes = {
                        ios: 'i(?:Pad|Phone|Pod)(?:.*)CPU(?: iPhone)? OS ',
                        android: '(Android |HTC_|Silk/)',
                        blackberry: 'BlackBerry(?:.*)Version\/',
                        rimTablet: 'RIM Tablet OS ',
                        webos: '(?:webOS|hpwOS)\/',
                        bada: 'Bada\/'
                    },
                    i, prefix, match;

                for (i in prefixes){
                    if (prefixes.hasOwnProperty(i)) {
                        prefix = prefixes[i];

                        if (navigator.userAgent.match(new RegExp('(?:'+prefix+')([^\\s;]+)')))
                            return true;
                    }
                }

                return false;
            }

            var fixSize = function() {
                var wrapEl = document.getElementById('wrap');
                if (wrapEl){
                    wrapEl.style.height = screen.availHeight + 'px';
                    window.scrollTo(0, -1);
                    wrapEl.style.height = window.innerHeight + 'px';
                }
            };

            var fixIpadLandscapeIos7 = function() {
                if (navigator.userAgent.match(/iPad;.*CPU.*OS 7_\d/i)) {
                    var wrapEl = document.getElementById('wrap');
                    if (wrapEl){
                        wrapEl.style.position = "fixed";
                        wrapEl.style.bottom = 0;
                        wrapEl.style.width = "100%";
                    }
                }
            };

            if (isMobile()){
                window.addEventListener('load', fixSize);
                window.addEventListener('resize', fixSize);

                fixIpadLandscapeIos7();
            }

        })();
    </script>
</body>
</html>