using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using COE.Components;
using COE.Data;
using MyApp.DBTable.tour;

namespace MYAPP.Manage.Project.Design
{
    public partial class Export_TourXml : MyApp.Web.UI.PublicPage
    {
        public string _Tour_ID = "", _Scene_ID = null, _Map_ID = null,_Html5=null;
        public int _Scene_Count = 0;
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Scene_ID = Request["Scene_ID"].Trim();
            }
            catch (Exception)
            {
                _Scene_ID = "0";
            }
            try
            {
                _Html5 = Request.QueryString["Html5"].Trim();
            }
            catch
            {
                _Html5 = "auto";
            }

            if (_Scene_ID == "") { _Scene_ID = "0"; }
            try
            {
                _Map_ID = Request["Map_ID"].Trim();
            }
            catch (Exception)
            {
                _Map_ID = "0";
            }

            if (_Map_ID == "") { _Map_ID = "0"; }
            try
            {
                _Tour_ID = Request["Tour_ID"];
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            ExportXml(base.action, _Tour_ID);
        }

        
        public void ExportXml(string action, string _Tour_ID)
        {
            XmlDocument xmldoc;
            XmlDocument ex_xmldoc = new XmlDocument();//漫游文档
            XmlDocument kr_xmldoc = new XmlDocument();//krpano基础调用

            try
            {
                Directory.CreateDirectory(Server.MapPath(String.Format("/project/{0}/ETXml", _Tour_ID)));
            }
            catch (Exception) { }

            try
            {
                xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));

                #region KRPano
                XmlElement elem_krpano = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/krpano");
                //SetElemValue(elem_krpano, "logkey","false");
                #endregion

                #region //生成自定义地图的场景标注
                ExportCustomMapXML();
                #endregion

                #region //生成场景列表
                ExportSceneListXML(xmldoc);
                #endregion

                ex_xmldoc.InnerXml = xmldoc.SelectSingleNode("/Project/krpano").OuterXml;
                kr_xmldoc.InnerXml = xmldoc.SelectSingleNode("/Project/krpano").OuterXml;

                ((XmlElement)xmldoc.SelectSingleNode("/Project")).SetAttribute("SceneCount", _Scene_Count.ToString());
                ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project").OuterXml;

                ex_xmldoc.SelectSingleNode("/krpano/Project").InnerText = "";

                #region 皮肤处理
                string skin = ((XmlElement)xmldoc.SelectSingleNode("/Project")).Attributes["Skin"].Value;
                if (skin.Trim() != "")
                {
                    try
                    {
                        XmlDocument xml_skin = MyApp.Components.XML.XmlDocument.Load(String.Format("/project/{0}/Skin/{1}", _Tour_ID, skin));

                        ex_xmldoc.ChildNodes[0].InnerXml += xml_skin.SelectSingleNode("/Skin").InnerXml;
                    }
                    catch (Exception) { }
                }
                #endregion


                #region 
                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/area").OuterXml;
                }
                catch (Exception) { }
                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/display").OuterXml;
                }
                catch (Exception) { }
                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/control").OuterXml;
                }
                catch (Exception) { }
                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/cursors").OuterXml;
                }
                catch (Exception) { }
                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/autorotate").OuterXml;
                }
                catch (Exception) { }
                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/progress").OuterXml;
                }
                catch (Exception) { }
                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/Plugins").InnerXml;
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/Actions").InnerXml;
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/Includes").InnerXml;
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/TextStyles").InnerXml;
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/Datas").InnerXml.Replace("&lt;", "<").Replace("&gt;", ">");
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/contextmenu").OuterXml;
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/Skin").InnerXml;
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/events").OuterXml;
                }
                catch (Exception) { }

                try
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/MProject").OuterXml;
                }
                catch (Exception) { }
                #endregion 


                try
                {
                    if (Convert.ToBoolean(MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/Project", "GoogleMapTF").ToLower()))
                    {
                        xmldoc.SelectSingleNode("/Project/GoogleMap/plugin").InnerXml += GetGoogleSpot(xmldoc);
                        ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/GoogleMap").InnerXml;
                    }
                }
                catch (Exception ex) { Response.Write(ex.ToString()); }

                #region   背景音乐
                XmlElement bksound = (XmlElement)xmldoc.SelectSingleNode("/Project/BKSound");
                string str_sound = "";
                XmlElement ac_elem_bksound = ex_xmldoc.CreateElement("action");
                ac_elem_bksound.SetAttribute("name", "_playtourbksound");

                if (bksound.GetAttribute("mp3file") != "")
                {
                    //str_sound = string.Format("playsound2D({0}, {1}, {2}, {3}, {4}, {5});", bksound.GetAttribute("TourSoundID"), bksound.GetAttribute("mp3file"), bksound.GetAttribute("volume"), bksound.GetAttribute("pan"), bksound.GetAttribute("loops"), bksound.GetAttribute("oncomplete"));
                    //str_sound = string.Format("if(get(device.html5)==true,playsound({0}, {1}, {4}, {5}),playsound2D({0}, {1}, {2}, {3}, {4}, {5}));", bksound.GetAttribute("TourSoundID"), bksound.GetAttribute("mp3file"), bksound.GetAttribute("volume"), bksound.GetAttribute("pan"), bksound.GetAttribute("loops"), bksound.GetAttribute("oncomplete"));
                    str_sound = string.Format(" set(ii,get(device.html5));if(ii==false,playsound2D({0}, {1}, {2}, {3}, {4}, {5}),playsound({0}, {1}, {4}, {5}));", bksound.GetAttribute("TourSoundID"), bksound.GetAttribute("mp3file"), bksound.GetAttribute("volume"), bksound.GetAttribute("pan"), bksound.GetAttribute("loops"), bksound.GetAttribute("oncomplete"));
                    //str_sound = string.Format("playsound({0}, {1}, {4}, {5});", bksound.GetAttribute("TourSoundID"), bksound.GetAttribute("mp3file"), bksound.GetAttribute("volume"), bksound.GetAttribute("pan"), bksound.GetAttribute("loops"), bksound.GetAttribute("oncomplete"));
                }
                else
                {
                    str_sound = "//";
                }

                ac_elem_bksound.InnerText = str_sound.Trim();

                ex_xmldoc.SelectSingleNode("/krpano").AppendChild(ac_elem_bksound);
                //try
                //{
                //    ex_xmldoc.ChildNodes[0].InnerXml += xmldoc.SelectSingleNode("/Project/BKSound").OuterXml;
                //}
                //catch (Exception) { }
                #endregion


                //<plugin name="dblclick" 
                //url="dblclick.swf" alturl="dblclick.js"
                //keep="true"
                //ondblclick="switch(fullscreen)"
                ///>

                #region dblclick --双击全屏
                //XmlElement exp_dblclick = ex_xmldoc.CreateElement("plugin");

                //exp_dblclick.SetAttribute("name", "dblclick");
                //exp_dblclick.SetAttribute("url", "%SWFPATH%/plugins/dblclick.swf");
                //exp_dblclick.SetAttribute("alturl", "%SWFPATH%/plugins/dblclick.js");
                //exp_dblclick.SetAttribute("keep", "true");
                //exp_dblclick.SetAttribute("ondblclick", "switch(fullscreen)");
                //ex_xmldoc.SelectSingleNode("/krpano").AppendChild(exp_dblclick);

                #endregion


                #region //地图处理
                XmlElement hide_all_map = ex_xmldoc.CreateElement("action");
                XmlElement hidenallmap_radar = ex_xmldoc.CreateElement("action");
                hide_all_map.SetAttribute("name", "_hideallmap");
                hidenallmap_radar.SetAttribute("name", "_hidenallmap_radar");
                XmlNodeList xmlnodelist_map = xmldoc.SelectNodes("/Project/Maps/Map");
                foreach (XmlElement xmlele_map in xmlnodelist_map)
                {
                    #region  hidenallmap_radar  隐藏所有radar动作
                    hidenallmap_radar.InnerXml += String.Format("set(plugin[radar_{0}].visible,false);set(plugin[spot_active_{0}].visible,false);", xmlele_map.GetAttribute("name"));
                    #endregion

                    #region //地图
                    XmlElement exp_xmlele_map = ex_xmldoc.CreateElement("plugin");
                    foreach (XmlAttribute xa_map in xmlele_map.Attributes)
                    {
                        if (!xa_map.Name.StartsWith("radar_"))
                        {
                            exp_xmlele_map.SetAttribute(xa_map.Name, xa_map.Value);
                        }
                    }
                    ex_xmldoc.SelectSingleNode("/krpano").AppendChild(exp_xmlele_map);
                    #endregion

                    #region radar
                    XmlElement exp_radar = ex_xmldoc.CreateElement("plugin");

                    exp_radar.SetAttribute("name", "radar_" + xmlele_map.GetAttribute("name"));

                    exp_radar.SetAttribute("keep", "true");
                    exp_radar.SetAttribute("x", "0");
                    exp_radar.SetAttribute("y", "0");
                    exp_radar.SetAttribute("align", "lefttop");
                    exp_radar.SetAttribute("url", "%SWFPATH%/plugins/radar.swf");
                    exp_radar.SetAttribute("alturl", "%SWFPATH%/plugins/radar.js");
                    exp_radar.SetAttribute("heading", "0");
                    exp_radar.SetAttribute("parent", exp_xmlele_map.GetAttribute("name"));
                    exp_radar.SetAttribute("edge", "center");

                    foreach (XmlAttribute xa_map in xmlele_map.Attributes)
                    {
                        if (xa_map.Name.StartsWith("radar_"))
                        {
                            exp_radar.SetAttribute(xa_map.Name.Replace("radar_", ""), xa_map.Value);
                        }
                    }
                    ex_xmldoc.SelectSingleNode("/krpano").AppendChild(exp_radar);
                    #endregion

                    #region spot_active
                    XmlElement spot_active = ex_xmldoc.CreateElement("plugin");

                    spot_active.SetAttribute("name", "spot_active_" + xmlele_map.GetAttribute("name"));
                    spot_active.SetAttribute("preload", "true");
                    spot_active.SetAttribute("keep", "true");
                    spot_active.SetAttribute("align", "lefttop");
                    spot_active.SetAttribute("edge", "center");
                    spot_active.SetAttribute("zorder", "4");
                    spot_active.SetAttribute("url", xmlele_map.GetAttribute("icon_active"));
                    spot_active.SetAttribute("parent", xmlele_map.GetAttribute("name"));

                    ex_xmldoc.SelectSingleNode("/krpano").AppendChild(spot_active);
                    #endregion

                    #region ac_show
                    XmlElement ac_show = ex_xmldoc.CreateElement("action");

                    ac_show.SetAttribute("name", "_showmap_" + xmlele_map.GetAttribute("name"));
                    ac_show.InnerXml = xmlele_map.GetAttribute("ac_show").Replace("{map_name}", xmlele_map.GetAttribute("name"));

                    ex_xmldoc.SelectSingleNode("/krpano").AppendChild(ac_show);
                    #endregion

                    #region ac_hidden
                    XmlElement ac_hide = ex_xmldoc.CreateElement("action");

                    ac_hide.SetAttribute("name", "_hidemap_" + xmlele_map.GetAttribute("name"));
                    ac_hide.InnerXml = xmlele_map.GetAttribute("ac_hidden").Replace("{map_name}", xmlele_map.GetAttribute("name"));

                    ex_xmldoc.SelectSingleNode("/krpano").AppendChild(ac_hide);

                    hide_all_map.InnerXml += ac_hide.InnerXml;
                    #endregion

                    #region spot
                    foreach (XmlElement xmlele_point in xmlele_map.ChildNodes)
                    {
                        XmlElement exp_point = ex_xmldoc.CreateElement("plugin");
                        exp_point.SetAttribute("name", "spot_" + xmlele_point.GetAttribute("Scene_ID"));

                        foreach (XmlAttribute xa_point in xmlele_point.Attributes)
                        {
                            //if (xa_point.Name.ToLower() == xa_point.Name)
                            //{
                            exp_point.SetAttribute(xa_point.Name, xa_point.Value);
                            //}
                        }

                        exp_point.SetAttribute("name", "spot_" + xmlele_point.GetAttribute("Scene_ID"));
                        exp_point.SetAttribute("preload", "true");
                        exp_point.SetAttribute("keep", "true");
                        exp_point.SetAttribute("parent", exp_xmlele_map.GetAttribute("name"));
                        try
                        {
                            exp_point.SetAttribute("zorder", (Convert.ToInt32(exp_xmlele_map.GetAttribute("zorder")) + 1).ToString());
                        }
                        catch (Exception)
                        {
                            exp_point.SetAttribute("zorder", "2");
                        }
                        exp_point.SetAttribute("align", "lefttop");
                        exp_point.SetAttribute("edge", "center");
                        exp_point.SetAttribute("x", (Convert.ToDecimal(exp_point.GetAttribute("x")) + Convert.ToDecimal(xmlele_map.GetAttribute("x_offset"))).ToString());
                        exp_point.SetAttribute("y", (Convert.ToDecimal(exp_point.GetAttribute("y")) + Convert.ToDecimal(xmlele_map.GetAttribute("y_offset"))).ToString());

                        exp_point.SetAttribute("url", exp_point.GetAttribute("icon_init").Replace("{icon_init}", xmlele_map.GetAttribute("icon_init")));
                        exp_point.SetAttribute("heading", GetHeading(xmlele_map.GetAttribute("heading"), xmlele_point.GetAttribute("Scene_ID")));

                        exp_point.SetAttribute("onclick", exp_point.GetAttribute("onclick").Replace("{Scene_ID}", xmlele_point.GetAttribute("Scene_ID")));
                        exp_point.SetAttribute("onhover", exp_point.GetAttribute("onhover").Replace("{hint}", xmlele_point.GetAttribute("hint")));

                        exp_point.SetAttribute("icon_init", exp_point.GetAttribute("icon_init").Replace("{icon_init}", xmlele_map.GetAttribute("icon_init")));
                        exp_point.SetAttribute("icon_visit", exp_point.GetAttribute("icon_visit").Replace("{icon_visit}", xmlele_map.GetAttribute("icon_visit")));
                        exp_point.SetAttribute("icon_onhover", exp_point.GetAttribute("icon_onhover").Replace("{icon_onhover}", xmlele_map.GetAttribute("icon_onhover")));
                        exp_point.SetAttribute("icon_active", exp_point.GetAttribute("icon_active").Replace("{icon_active}", xmlele_map.GetAttribute("icon_active")));

                        ex_xmldoc.SelectSingleNode("/krpano").AppendChild(exp_point);
                    }
                    #endregion
                }
                hidenallmap_radar.InnerXml += String.Format("set(plugin[%1].visible,true);set(plugin[%1].visible,true);");
                ex_xmldoc.SelectSingleNode("/krpano").AppendChild(hidenallmap_radar);
                ex_xmldoc.SelectSingleNode("/krpano").AppendChild(hide_all_map);
                #endregion
                
                    string _DefScene_ID = "";
                
                    if ((_Scene_ID == "0"))
                    {
                        _DefScene_ID = ((XmlElement)xmldoc.GetElementsByTagName("Project")[0]).GetAttribute("DefScene_ID");
                    }
                    else
                    {
                        _DefScene_ID = _Scene_ID;
                    }

                    if (_Scene_ID == "0")
                    {

                        string temp_Map_ID = ((XmlElement)xmldoc.GetElementsByTagName("Project")[0]).GetAttribute("DefMap_ID");

                        if (temp_Map_ID != "" && _Map_ID == "0")
                        {
                            _Map_ID = temp_Map_ID;
                        }
                        else
                        {

                        }

                    }
                    if (_Map_ID != "0")
                    {
                        ex_xmldoc.ChildNodes[0].InnerXml += String.Format("<include url=\"GigaMap/{0}/scene.xml\" />", _Map_ID);
                    }
                    else
                    {
                        if (_DefScene_ID != "0")
                        {
                            ex_xmldoc.ChildNodes[0].InnerXml += "<include url=\"scene/{DefScene_ID}/scene.xml\" />";
                        }
                    }

                ex_xmldoc.ChildNodes[0].InnerXml += "<include url=\"krroot.xml\" />";//添加住引用

                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/tour_template.aspx", _Tour_ID)), "<%@ Page Language=\"C#\" ResponseEncoding=\"utf-8\" %>" + ex_xmldoc.OuterXml.Replace("{DefScene_ID}", "<%=Request[\"Scene_ID\"]%>"));//生成漫游场景模板配置文件

                ex_xmldoc.InnerXml = ex_xmldoc.InnerXml.Replace("{DefScene_ID}", _DefScene_ID);//替换掉默认场景


                #region //XSLT 模板解析模块

                string file_etconfig = Server.MapPath(String.Format("/Project/{0}/templates/ET.config", _Tour_ID));
                if (File.Exists(file_etconfig))
                {
                    XmlDocument et_config_xmldoc = new XmlDocument();//模板解析配置文件
                    et_config_xmldoc.Load(file_etconfig);

                    XmlNodeList xmlnodelist_et_config = et_config_xmldoc.SelectNodes("/ET/Item");
                    foreach (XmlElement xmlele_et_config in xmlnodelist_et_config)
                    {
                        //this.parent.Items.Add(new ListItem("插件: " + xmlele_plugin.GetAttribute("name"), xmlele_plugin.GetAttribute("name")));
                        string outstringl = COE.Components.MyFun.InvokeXmlAndXsl(String.Format("/Project/{0}/{1}", _Tour_ID, xmlele_et_config.GetAttribute("xml")), String.Format("/Project/{0}/{1}", _Tour_ID, xmlele_et_config.GetAttribute("xslt"))).Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
                        MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/{1}", _Tour_ID, xmlele_et_config.GetAttribute("to"))), outstringl);
                        if (xmlele_et_config.GetAttribute("includetf").ToLower() == "true") //如果是include 包含类型,则添加到漫游引用中
                        {
                            ex_xmldoc.ChildNodes[0].InnerXml += String.Format("<include name=\"{0}\" url=\"{1}\" />", xmlele_et_config.GetAttribute("name"), xmlele_et_config.GetAttribute("includeurl"));
                        }
                    }
                }
                #endregion


                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/tour.xml", _Tour_ID)), ex_xmldoc.OuterXml);//生成默认漫游配置文件

                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/krroot.xml", _Tour_ID)), kr_xmldoc.OuterXml);//生成KRPanoG配置文件

                //MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/include/SceneList.xml", _Tour_ID)), COE.Components.MyFun.InvokeXmlAndXsl(String.Format("/Project/{0}/ETXml/SceneList.xml", _Tour_ID), String.Format("/Project/{0}/templates/SceneList.xslt", _Tour_ID)));
                if (action == "vxml")
                {
                    ex_xmldoc.ChildNodes[0].InnerXml += "<action name=\"_onloaded\">js(initload());</action>";
                    Response.Write(ex_xmldoc.OuterXml);
                }
                else
                    if (action == "gxml")
                    {
                        ex_xmldoc.ChildNodes[0].InnerXml += "<include url=\"%HTMLPATH%sz.xml\"/>";
                        ex_xmldoc.ChildNodes[0].InnerXml += "<action name=\"_onloaded\">js(initload());</action>";
                        Response.Write(ex_xmldoc.OuterXml);
                    }
                    else
                        if (action == "xml")
                        {
                            Response.Write(ex_xmldoc.OuterXml);
                        }
                        else
                            if (action == "Publish")
                            {
                                #region 导出发布设置
                                StringBuilder _filelist = new StringBuilder();
                                XmlElement elem_Publish = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Publish");
                                string _arg = "-bf -xml=tour.xml -o=tour.swf ";

                                if (elem_Publish.GetAttribute("SceneXMLTF").Trim().ToLower() == "true")
                                {
                                    DataRow[] list_dr = MyApp.Components.KRPano.Project.GetDirectorys(String.Format("{0}/Scene", _Tour_ID), "");
                                    foreach (DataRow dr in list_dr)
                                    {
                                        _filelist.AppendLine(string.Format("scene/{0}/scene.xml|scene/{0}/scene.xml", dr["Name"].ToString()));
                                    }
                                }
                                //_filelist.AppendLine("Include/SceneList.xml|Include/SceneList.xml");
                                _filelist.AppendLine("krroot.xml|krroot.xml");

                                using (StreamWriter sw = new StreamWriter(MyApp.Components.File.FileManage.GetRootDir(String.Format("/Project/{0}/filelist.txt", _Tour_ID))))
                                {//保存场景列表文件
                                    sw.Write(_filelist.ToString());
                                    sw.Close();
                                }
                                _arg += string.Format(" -filelist={0} ", MyApp.Components.File.FileManage.GetRootDir(String.Format("/Project/{0}/filelist.txt", _Tour_ID))); ;
                                
                                if (elem_Publish.GetAttribute("DomainTF").Trim().ToLower() == "true")
                                {

                                    string[] _list= elem_Publish.GetAttribute("DomainList").Split(new char[] { '\n', '\r'} );
                                    foreach (string dr in _list)
                                    {
                                       if (dr.Trim()!="")
                                       {
                                         _arg += string.Format(" -domain={0} ",dr.Trim());
                                       }
                                    }
                                }
                                if (elem_Publish.GetAttribute("ExpiredDateTF").Trim().ToLower() == "true")
                                {
                                    _arg += string.Format(" -expire={0} ", elem_Publish.GetAttribute("ExpiredDate"));

                                    if (elem_Publish.GetAttribute("ExpiredURL").Trim().ToLower() != "")
                                    {
                                        _arg += string.Format(" -expiredurl={0} ", elem_Publish.GetAttribute("ExpiredURL"));
                                    }
                                }

                                if (elem_Publish.GetAttribute("OfflineTF").Trim().ToLower() == "true")
                                {
                                    _arg += string.Format(" -nolu ");
                                }
                              
                                COE.Components.MyFun.ChangeNameExport();
                                
                                Process process = new Process();            
                                try
                                {
                                    //_arg += string.Format(" -fpexe=krpano1.exe ");
                                    //_arg = "-o=tour.swf ";
                                    process.StartInfo.FileName = MyApp.Components.File.FileManage.GetRootDir("\\pano\\kprotectcl.exe");
                                    process.StartInfo.Arguments = _arg;


                                    process.StartInfo.WorkingDirectory = MyApp.Components.File.FileManage.GetRootDir(String.Format("/Project/{0}", _Tour_ID));
                                    process.Start();

                                    process.WaitForExit();

                                    try//删除所有临时文件
                                    {
                                        ////File.Delete(MyApp.Components.File.FileManage.GetRootDir(String.Format("/Project/{0}/tour.xml", _Tour_ID)));
                                        File.Delete(MyApp.Components.File.FileManage.GetRootDir(String.Format("/Project/{0}/filelist.txt", _Tour_ID)));
                                        ////File.Delete(MyApp.Components.File.FileManage.GetRootDir(String.Format("/Project/{0}/krroot.xml", _Tour_ID)));
                                    }
                                    catch (Exception) { } COE.Components.MyFun.ReturnNameExport();
                                    Response.Write("<script language=javascript>alert('生成成功!!!');history.back()</script>");
                                }
                                catch (Exception )
                                {
                                    COE.Components.MyFun.ReturnNameExport();
                                    Response.Write("<script language=javascript>alert('生成错误!!!');history.back()</script>");
                                }
                                #endregion 发布导出设置
                            }
                            else
                                if (action == "alert")
                                {
                                    Response.Write("<script language=javascript>alert('ExportXML OK!!!');history.back()</script>");
                                }
                                else
                                    if (action == "")
                                    {
                                        if ((Request.QueryString["navurlx"] != "") && (Request.QueryString["navurlx"] != null))
                                        {
                                            Response.Redirect(COE.Components.Encrypt.DecryptString(Request.QueryString["navurlx"]), true);
                                        }
                                        else
                                        {
                                            if ((Request.QueryString["navurl"] != "") && (Request.QueryString["navurl"] != null))
                                            {
                                                Response.Redirect(String.Format("/project/{1}/{0}", Request.QueryString["navurl"], _Tour_ID), true);
                                            }
                                            else
                                            {
                                                Response.Redirect(String.Format("/project/{0}/view_tour__{1}.aspx", _Tour_ID, _Html5), true);//__
                                                //Response.Redirect(String.Format("~/pano/view_tour.aspx?tour_id={0}&html5={1}", _Tour_ID, _Html5));                                                
                                            }
                                        }
                                    }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
                // Response.Write("<script language=javascript>alert('错误!!!');history.back()</script>");
            }
        }

        public void ExportCustomMapXML()//生成自定义地图用的场景标注配置XML文件
        {
            XmlDocument ex_custommapdoc = new XmlDocument();//自定义地图场景标注文档
            ex_custommapdoc.LoadXml("<CustomerMap><SceneMarker/></CustomerMap>");
            DataRow[] list_dr = MyApp.Components.KRPano.Project.GetDirectorys(String.Format("{0}/Scene", _Tour_ID), "");

            foreach (DataRow dr in list_dr)
            {
                XmlDocument xmlscenedoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, dr["Name"]);

                if (MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/CustomMap", "enable").Trim() == "True")
                {
                    //li.Add(new ListItem(dr["Name"].ToString() + ":" + GetElemValueByPath(_Tour_ID, dr["Name"], "Name"), dr["Name"].ToString()));
                    XmlElement xmlele = ex_custommapdoc.CreateElement("item");
                    xmlele.SetAttribute("sceneid", dr["Name"].ToString());
                    xmlele.SetAttribute("name", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano", "Name"));
                    xmlele.SetAttribute("lng", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/CustomMap", "lng"));
                    xmlele.SetAttribute("lat", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/CustomMap", "lat"));
                    xmlele.SetAttribute("zoom", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/CustomMap", "zoom"));
                    ex_custommapdoc.SelectSingleNode("/CustomerMap/SceneMarker").AppendChild(xmlele);
                }

            }
            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/ETXml/CustomerMap.xml", _Tour_ID)), ex_custommapdoc.OuterXml);
            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/CustomerMap.xml", _Tour_ID)), ex_custommapdoc.OuterXml);
        }

        byte CTOByte(string str)
        {
            if (str.ToLower() == "true")
            {
                return Convert.ToByte(1);
            }
            else
            {
                return Convert.ToByte(0);
            }
        }

        public void ExportSceneListXML(XmlDocument xmldoc)//生成场景列表及配置文件
        {
            bool ExportLonLat = false;
            if (MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/Project", "ExportLonLat") == "True")
            {
                ExportLonLat = true;
            }

            XmlDocument ex_scenelistdoc = new XmlDocument();
            string str_type = "";
            try
            {
                str_type = xmldoc.SelectSingleNode("/Project/Scene/Type").InnerXml;
            }
            catch (Exception) { }
            ex_scenelistdoc.LoadXml("<Tour><Type>" + str_type + "</Type><Scene/></Tour>");
            DataRow[] list_dr = MyApp.Components.KRPano.Project.GetDirectorys(String.Format("{0}/Scene", _Tour_ID), "");

            #region 发布的时候备注

            //LonLat ll = new LonLat();
            //Database db = new Database();
            //db.Open();
            //db.RunCommand_ExecuteNonQuery(String.Format("DELETE FROM [tour].[Scene_Info] where Tour_ID={0}", _Tour_ID));

            //Scene_Info vi = new Scene_Info(db);
            //if (vi.Select(String.Format("ID={0}", 0)))
            //{
            //    vi.StartAppend();
            //}
            //if (ExportLonLat)
            //{
            //    ll.Open();
            //}
            #endregion
            _Scene_Count = list_dr.Length;
            foreach (DataRow dr in list_dr)
            {
                XmlDocument xmlscenedoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, dr["Name"]);

                XmlElement xmlele = ex_scenelistdoc.CreateElement("sceneitem");
                xmlele.SetAttribute("tourid", _Tour_ID.ToString());
                xmlele.SetAttribute("sceneid", dr["Name"].ToString());
                xmlele.SetAttribute("type", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/SSet", "TypeID"));
                xmlele.SetAttribute("name", "sl_" + dr["Name"].ToString());
                xmlele.SetAttribute("title", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano", "Name"));
                xmlele.SetAttribute("key", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/SSet", "Key"));
                xmlele.SetAttribute("SearchTF", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/SSet", "SearchTF"));
                xmlele.SetAttribute("Date", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano", "Date"));
                xmlele.SetAttribute("HotspotAutoTF", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano", "HotspotAutoTF"));
                xmlele.SetAttribute("HotspotAutoByTF", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano", "HotspotAutoByTF"));
                xmlele.SetAttribute("GoogleMapTF", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/SSet", "GoogleMapTF"));
                xmlele.SetAttribute("Prev_Scene_ID", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/SSet", "Prev_Scene_ID"));
                xmlele.SetAttribute("Next_Scene_ID", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/SSet", "Next_Scene_ID"));
                xmlele.SetAttribute("Intro", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/SSet", "Intro"));
                xmlele.SetAttribute("lng", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/Google", "lng"));
                xmlele.SetAttribute("lat", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/Google", "lat"));
                xmlele.SetAttribute("zoom", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/Google", "zoom"));
                xmlele.SetAttribute("heading", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/Google", "heading"));
                xmlele.SetAttribute("height", MyApp.Components.KRPano.Scene.GetValue(xmlscenedoc, "/krpano/Google", "height"));

                #region 发布的时候备注
                //if (ExportLonLat)
                //{
                //    if (xmlele.GetAttribute("lng") != "")
                //    {
                //        ll.getll(double.Parse(xmlele.GetAttribute("lng")), double.Parse(xmlele.GetAttribute("lat")));
                //        xmlele.SetAttribute("_lng", ll._lon.ToString());
                //        xmlele.SetAttribute("_lat", ll._lat.ToString());
                //    }
                //}
                //#endregion

                //#region //发布的时候备注
                //vi.SetAppend();
                //vi.Tour_ID = Convert.ToInt64(_Tour_ID);
                //vi.Scene_ID = Convert.ToInt64(xmlele.GetAttribute("sceneid"));
                //vi.Type = Convert.ToString(xmlele.GetAttribute("type"));
                //vi.Name = Convert.ToString(xmlele.GetAttribute("name"));
                //vi.Title = Convert.ToString(xmlele.GetAttribute("title"));
                //vi.Key = Convert.ToString(xmlele.GetAttribute("key"));
                //try
                //{
                //    vi.SearchTF = CTOByte(xmlele.GetAttribute("SearchTF"));
                //}
                //catch (Exception) { }
                //vi.Date = Convert.ToDateTime(xmlele.GetAttribute("Date"));
                //try
                //{
                //    vi.HotspotAutoTF = CTOByte(xmlele.GetAttribute("HotspotAutoTF"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.HotspotAutoByTF = CTOByte(xmlele.GetAttribute("HotspotAutoByTF"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.GoogleMapTF = CTOByte(xmlele.GetAttribute("GoogleMapTF"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.Prev_Scene_ID = Convert.ToInt64(xmlele.GetAttribute("Prev_Scene_ID"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.Next_Scene_ID = Convert.ToInt64(xmlele.GetAttribute("Next_Scene_ID"));
                //}
                //catch (Exception) { }
                //vi.Intro = Convert.ToString(xmlele.GetAttribute("Intro"));

                //try
                //{
                //    vi.lng = Convert.ToDecimal(xmlele.GetAttribute("lng"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.lat = Convert.ToDecimal(xmlele.GetAttribute("lat"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.zoom = Convert.ToDecimal(xmlele.GetAttribute("zoom"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.heading = Convert.ToDecimal(xmlele.GetAttribute("heading"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi.height = Convert.ToDecimal(xmlele.GetAttribute("height"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi._lng = Convert.ToDecimal(xmlele.GetAttribute("_lng"));
                //}
                //catch (Exception) { }
                //try
                //{
                //    vi._lat = Convert.ToDecimal(xmlele.GetAttribute("_lat"));
                //}
                //catch (Exception) { }

                //vi.xml = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, vi.Scene_ID);
                //vi.AddAppend();
                #endregion

                ex_scenelistdoc.SelectSingleNode("/Tour/Scene").AppendChild(xmlele);
            }

            //#region 发布的时候备注
            //if (ExportLonLat)
            //{
            //    ll.Close();
            //}

            //vi.UpdateAppend();
            //db.Close();
            //#endregion
            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/ETXml/SceneList.xml", _Tour_ID)), ex_scenelistdoc.OuterXml);
            //MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/ETXml/IncludeSceneList.xml", _Tour_ID)), ex_scenelistdoc.SelectSingleNode("/Tour/Scene").InnerXml);
        }
        public string GetGoogleSpot(XmlDocument pl_xmldoc)//生成Google地图用的标注点spot节点
        {
            string pl_name = MyApp.Components.KRPano.Scene.GetValue(pl_xmldoc, "/Project/GoogleMap/plugin", "name");
            XmlDocument ret_xmldoc = new XmlDocument();
            ret_xmldoc.LoadXml("<google/>");
            DataRow[] list_dr = MyApp.Components.KRPano.Project.GetDirectorys(String.Format("{0}/Scene", _Tour_ID), "");
            foreach (DataRow dr in list_dr)
            {
                XmlDocument xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, dr["Name"]);


                if (Convert.ToBoolean(MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano/Google", "GoogleMapTF")))
                {

                    XmlElement elem_item = ret_xmldoc.CreateElement("spot");

                    elem_item.SetAttribute("name", String.Format("gg_spot_{0}", dr["Name"]));
                    elem_item.SetAttribute("scene_id", dr["Name"].ToString());
                    elem_item.SetAttribute("scene_name", MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano", "Name"));
                    elem_item.SetAttribute("scene_hit", MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano", "Name"));
                    elem_item.SetAttribute("lat", MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano/Google", "lat"));
                    elem_item.SetAttribute("lng", MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano/Google", "lng"));
                    elem_item.SetAttribute("zoom", MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano/Google", "zoom"));
                    try
                    {
                        elem_item.SetAttribute("heading", (-90 + Convert.ToDecimal(MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano/Google", "heading"))).ToString());
                    }
                    catch (Exception) { }
                    elem_item.SetAttribute("height", MyApp.Components.KRPano.Scene.GetValue(xmldoc, "/krpano/Google", "height"));


                    elem_item.SetAttribute("onhover", MyApp.Components.KRPano.Scene.GetValue(pl_xmldoc, "/Project/GoogleMap/plugin/spot_set", "onhover").Replace("{Scene_ID}", dr["Name"].ToString()).Replace("{spot_name}", elem_item.GetAttribute("name")).Replace("{plugin_name}", pl_name));
                    elem_item.SetAttribute("onover", MyApp.Components.KRPano.Scene.GetValue(pl_xmldoc, "/Project/GoogleMap/plugin/spot_set", "onover").Replace("{Scene_ID}", dr["Name"].ToString()).Replace("{spot_name}", elem_item.GetAttribute("name")).Replace("{plugin_name}", pl_name));
                    elem_item.SetAttribute("onout", MyApp.Components.KRPano.Scene.GetValue(pl_xmldoc, "/Project/GoogleMap/plugin/spot_set", "onout").Replace("{Scene_ID}", dr["Name"].ToString()).Replace("{spot_name}", elem_item.GetAttribute("name")).Replace("{plugin_name}", pl_name));
                    elem_item.SetAttribute("onclick", MyApp.Components.KRPano.Scene.GetValue(pl_xmldoc, "/Project/GoogleMap/plugin/spot_set", "onclick").Replace("{Scene_ID}", dr["Name"].ToString()).Replace("{spot_name}", elem_item.GetAttribute("name")).Replace("{plugin_name}", pl_name));
                    ret_xmldoc.SelectSingleNode("/google").AppendChild(elem_item);
                }
            }
            return ret_xmldoc.ChildNodes[0].InnerXml;
        }

        public string GetHeading(string map_heading, string _Scene_ID)
        {
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID);
                XmlElement elem_Google = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/Google");
                return (Convert.ToDecimal(map_heading) + Convert.ToDecimal(elem_Google.GetAttribute("heading")) - 90).ToString();
            }
            catch (Exception) { return "0"; }
        }
    }
}
