<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebOffice.aspx.cs" Inherits="BeginScreen.WebOffice" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <title><%=filename %></title>

    <script language="javascript">
        var pfile = '<%=filename %>'; //word的文件名
        var varpath = decodeURI(window.location.pathname);
        var varhost = window.location.host;
        var strRoot, strSmartUrl;
        //if (varhost == '') {
        //    //本机地址	
        //    strRoot = varpath.substring(1, varpath.lastIndexOf('/') + 1);//取得打开、保存路径 
        //    if (strRoot == '') strRoot = varpath.substring(1, varpath.lastIndexOf('\\') + 1);
        //} else {
        //    //网络地址
        //    varpro = window.location.protocol;
        //    strRoot = varpro + "//" + varhost + "/" + varpath.substring(1, varpath.lastIndexOf('/') + 1);
        //    if (strRoot == '') strRoot = varpath.substring(1, varpath.lastIndexOf('\\') + 1);
        //}
        var strOpenUrl = "http://localhost:50853/office/"; //取得打开路径  
        var strSaveUrl; //取得保存路径和文件名
        strSmartUrl = "WebOffice://|Officectrl|" + window.location.href;//开启智能窗模式

        var WebOffice;
        function WebOpen() {
            var fileText = pfile.substring(pfile.lastIndexOf("."), pfile.length);
            var fileName = fileText.toLowerCase();

            WebOffice = document.getElementById('WebOffice');//获得控件对象

            if (fileName == ".ppt" || fileName == ".pptx") {
                if (WebOffice) setTimeout('openfile()', 5000);//等待控件初始化完毕，时间可以根据网络速度设定。
            } else {
                if (WebOffice) setTimeout('openfile()', 1000);//等待控件初始化完毕，时间可以根据网络速度设定。
            }
        }
        function openfile() {
            pfile = '<%=filename %>';
            strOpenUrl = strOpenUrl + pfile;
            strOpenUrl = encodeURI(strOpenUrl);
            setInterval(function () { readflag(); }, 500);
            WebOffice.Open(strOpenUrl, true, "Word.Document", "", "");

            var fileText = pfile.substring(pfile.lastIndexOf("."), pfile.length);
            var fileName = fileText.toLowerCase();

            if (fileName == ".ppt" || fileName == ".pptx") {
                //ppt加载完后自动播放
                var obj = document.getElementById('WebOffice').PowerAPP();
                obj.ActivePresentation.SlideShowSettings.Run();
            }
        }

        function SmartOpen() {
            location.href = strSmartUrl;
        }

        <%--function readflag() {
            var isClose = '<%=isClose %>';
            if (isClose == "0") {
                CloseYes();
            }

            var appnum = '<%=Application[fileid]%>';
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    strText = xmlhttp.responseText;
                    if (appnum < parseInt(strText)) {
                        WebOffice.Close();
                        window.opener = null; window.open('', '_self', ''); window.close();

                    }
                }

            }
            xmlhttp.open("GET", "application.aspx?fileid=<%=fileid%>", true);
            xmlhttp.send();
        }--%>

        var appnum = '<%=Application[fileid]%>';

        function readflag() {
            //alert(varhost+varpath);
            var isClose = '<%=isClose %>';
            //alert(isClose);
            if (isClose == "0") {
                CloseYes();
            }
            $.post("application.aspx", { "fileid": "<%=fileid%>" }, function (data) {
                //alert(appnum + "-" + data);
                strText = data;
                if (parseInt(appnum) < parseInt(strText)) {
                    WebOffice.Close();
                    window.opener = null; window.open('', '_self', ''); window.close();

                }
            });
        }

        function CloseYes() {
            try {
                self.opener = null;
                self.open('', '_self');
                self.close();
            } catch (e) {
                window.close();
            }
        }
    </script>
</head>
<body topmargin="0" leftmargin="0" onload="javascript:WebOpen();">
    <div style="text-align: left; font-size: 15px;">
        <input type="hidden" value="word文档一.doc" id="filename" name="filename" />
        <script language="javascript" src="js/webofficetip.js" charset="utf-8"></script>
    </div>
    <script language="javascript" src="js/webofficeocx.js" charset="utf-8"></script>
</body>
</html>

