/// <reference path="Extjs_Intellisense.js" />
/// <reference path="src/data/XmlStore.js" />
/// <reference path="addjs/Base.js" />
/// <reference path="addjs/XmlTreeLoader.js" />




//ptzAndAlarmForm,_PtzFormPanel,_AlarmFormPanel
function OnExpandPtz(p)
{
//    if(!_PtzFormPanel.collapsed || !_AlarmFormPanel.collapsed)
//    {
//        ptzAndAlarmForm.setHeight(350);
//    }
    ResizeLeftTreePlan(); 
}


//ptzAndAlarmForm,_PtzFormPanel,_AlarmFormPanel
function OnCollapsePtz(p)
{//collapsed,
//    if(_PtzFormPanel.collapsed && _AlarmFormPanel.collapsed)
//    {
//        ptzAndAlarmForm.setHeight(48);
//    }
    ResizeLeftTreePlan();
}

function ChangeCssStyle(treewidth)
{
    if(document && document.styleSheets  && document.styleSheets.length>0)
    {
        var oStyleSheet=document.styleSheets[0];
        for(var idx=0;idx<=2;idx++)
        {
            try
            {
                var oRule=oStyleSheet.rules[idx+1];
                oRule.style.width=(treewidth-idx*22-80);//64);
            }catch(eee){}
        }
    }
}

 function ResizeLeftTreePlan()
 {
    if(treeViewPlan!=null)
    {
        var grid=GetAlarmResultGrid();
        if(treeViewPlan.getWidth()>5)
        {
            grid.setWidth(treeViewPlan.getWidth()-5);
        }
        if(treeViewPlan.getHeight()>20)
        {
            grid.setHeight(treeViewPlan.getHeight()-20); 
        }
        if(_PtzFormPanel!=null && _AlarmFormPanel!=null)
        {
            if(_PtzFormPanel.collapsed && _AlarmFormPanel.collapsed)
            {
                ptzAndAlarmForm.setHeight(48);
            }else
            {
                ptzAndAlarmForm.setHeight(350);
            }
        }        
        var realtree=GetRelaTimeTree();
        if(realtree!=null)
        {
            realtree.setHeight(treeViewPlan.getHeight()-ptzAndAlarmForm.getHeight()-25); 
            ChangeCssStyle(ptzAndAlarmForm.getWidth());            
        }
        //GetRelaTimeTree();   
    }
 }
 

function GetTablPanel()
{
    if(showSearhPlan==null)
    {//右边
           showSearhPlan = new Ext.TabPanel({
                selectRealObj:null,
                selectPlayBackOjb:null,
                region: 'center', // a center region is ALWAYS required for border layout
                deferredRender: false,
                activeTab: 0,     // first tab initially active
                items: [
                    GetRealPlayPanel()
                    ,GetSearchFilePanel()
                    ,GetModifyPassPanel()
                    ,GetFullFillChangePanel()
                    ,GetHelpPanle()
                    ,GetExitPanel()
                ]
                ,listeners:
                {
                    collapse:function(obj)
                    {
                        var msg="";  
                    },
                    deactivate:function(obj)
                    {
                        var msg="";  
                    },
                    close:function(obj)
                    {
                        var msg="";
                    },
                    resize:function(eventObj,htmlel,obj)
                    {  
                        var gridwidth=eventObj.getWidth(false)-3;
                        var width=gridwidth-20;
                        var height=eventObj.getHeight(false);                        
                        var grid;
                        grid=GetSearchResultGrid();
                        grid.setWidth(gridwidth-10);
                        if(height-535>=250)
                        {
                            grid.setHeight(height-535);
                        }else
                        {
                            grid.setHeight(250);
                        }
                        grid=GetSearchPlayBackForm();
                        grid.setWidth(gridwidth-10);
                        if(gridwidth>520)
                        {
                            $("downloadpathdiv").style.width=gridwidth-520;
                        }else
                        {
                            $("downloadpathdiv").style.width=gridwidth-40;
                        }
                    }
                    ,beforetabchange:function(thisplan,newTab,currentTab)
                    {
                        if(newTab==GetExitPanel())
                        {
                            if(!window.confirm(GetDefaultLang("lang_exittipmsg","是否要退出登录并返回到登录界面？"),GetDefaultLang("lang_exityesno","是否要退出")))
                            {
                                return false;
                            }
                            try
                            {
                                ExitIE();
                                var urlfile=top.location.toString();
                                if(urlfile.replace("language=","")==urlfile)
                                {
                                    var langtype="zh_cn";
                                    if(IsEnglish())
                                    {
                                        langtype="en_us";
                                    }
                                    if(urlfile.replace("?","")==urlfile)
                                    {
                                        urlfile=urlfile+"?language="+langtype;
                                    }else
                                    {
                                        urlfile=urlfile+"&language="+langtype;
                                    }
                                    top.location=urlfile;
                                }else
                                {
                                    top.location.reload();
                                }
                            }catch(eee){}
                            return false;
                        }
                        
                        if(newTab==GetFullFillChangePanel())
                        {
                            try
                            {
                                var playlist="player1,player2,player3,player4,replay1,replay2";
                                var plls=playlist.split(",");
                                WriteWebLog("更改满屏显示状态");
                                try
                                {
                                    var value=1;
                                    var plobj=GetPlayerById(plls[0]);
                                    if(plobj.GetFullFillStatus())
                                    {
                                        value=0;
                                    }
                                    for(var idx=0;idx<plls.length;idx++)
                                    {
                                        try
                                        {
                                            var plobj=GetPlayerById(plls[idx]);
                                            plobj.SetFullFillStatus(value);
                                        }catch(eee){}
                                    }
                                }catch(ee){}                        
                            }catch(eee){}
                            return false;
                        }    
                                     
                                                
                        if(newTab==currentTab)
                        {
                            return false;
                        }
                        try
                        {
                            var playform=GetPlayBackForm();
                            if(playform.isVisible())
                            {
                                //newTab=currentTab;//不切换
                                oldAlert(GetDefaultLang("lang_closeplaybackfirst","请先关闭[录像文件回放]窗口再进行切换"),GetDefaultLang("lang_errortype110","错误操作"));
                                return false;
                            }
//                          playform.hide();
//                          $("replay").Stop();
                        }catch(eev){}
                        try
                        {
                            GetPlayerById("player1div").focus();
                        }catch(ee){}
                        try
                        {
                            GetPlayerById("selectdevid").focus();
                        }catch(ee){}
                    }
                    ,tabchange :function(eventObj,objseltab)
                    {
                        var gridwidth=eventObj.getWidth(false)-3;
                        var width=gridwidth-20;
                        var height=eventObj.getHeight(false);
                        var grid;
                        grid=GetSearchResultGrid();
                        grid.setWidth(gridwidth-10);
                        if(height-535>250)
                        {
                            grid.setHeight(height-535);
                        }else
                        {                        
                            grid.setHeight(250);
                        }                        
                        grid=GetSearchPlayBackForm();
                        grid.setWidth(gridwidth-10);
                    }
                }
               
            });
     }
     
     
     
     return showSearhPlan;
} 



function ShowTreePlan(vp)
{ 
    ReloadTree();
    //Ext.state.Manager.setProvider(new Ext.state.CookieProvider());  
    var searhplan=GetTablPanel();
    if(treeViewPlan==null)
    {//总体结构 
    
        Ext.QuickTips.init();
        Ext.QuickTips.register({
            target: 'tooltipdiv',
            hideDelay :0,
            showDelay :0
        });
        var qtipobj=Ext.QuickTips.getQuickTip();       
        qtipobj.on("hide",function(thisobj){
            nowallowShowAlarm=false;    
            try
            {
                if($("qtipframe").style.display!="none")
                {
                    $("qtipframe").style.display="none";
                }
            }catch(eee){}
        });
        
        qtipobj.on("move",function(thisobj,x,y)
        {
            try
            {
                nowallowShowAlarm=true;
                var obj=$("qtipframe");
                obj.style.left=x;
                obj.style.top=y;
                obj.style.width=thisobj.getWidth();//body.dom.offsetWidth;
                obj.style.height=thisobj.getHeight();//thisobj.body.dom.offsetHeight;
                obj.width=thisobj.getWidth();//thisobj.body.dom.offsetWidth;
                obj.height=thisobj.getHeight();//thisobj.body.dom.offsetHeight;
                if(obj.style.display!="block")
                {
                    obj.style.display="block";
                }
            }catch(eeee){}
        });
        qtipobj.on("show",function(thisobj,x,y)
        {
            try
            {
                nowallowShowAlarm=true;
            }catch(eeee){}
        });
         
        treeViewPlan = new Ext.Viewport({
            layout: 'border',
            items: [
            {
                region: 'west',
                id: 'realtime', // see Ext.getCmp() below
                split: true,
                width: 180,
                minSize: 160,
                maxSize: 320,
                collapsible: true,
                margins: '0 0 0 5',
                //layout:'column', //{ type: 'accordion' //,animate: true // 逐步变化效果 },
                items:[                
                {
                    layout:'accordion',
                    //region: 'north',
                    id:'realtimetree',
                    contentEl: 'west',
                    border: false
                    ,items:[GetRelaTimeTree()]
                    ,autoScroll: false
                }
                ,ptzAndAlarmForm=new Ext.Panel({
                    //region: 'south',
                    layout:'accordion',
                    height:350 ,
                    items:
                    [
                        _PtzFormPanel=new Ext.Panel({
                            id:'PTZControltree'
                            ,title:GetDefaultLang("lang_ptzcontroltitle",'云台控制')
                            ,border:false
                            ,contentEl:'ptzcontrol'
                            ,height:270
                            ,autoScroll: true
                            ,collapsed:true
                            ,listeners:
                            {
                                collapse:function(p)
                                {
                                    OnCollapsePtz(p);
                                },
                                expand:function(p)
                                {
                                    OnExpandPtz(p);
                                }
                            }
                        })
                        ,
                        _AlarmFormPanel=new Ext.Panel({
                            //id:'alarminfo',
                            title: '<div id="alarmtitlehead" >'+GetDefaultLang("lang_alarmformtitle","报警信息")+'</div>'
                            ,items:[GetAlarmResultGrid()]
                            ,border: false
                            ,height:270
                            ,collapsed:true
                            ,listeners:
                            {
                                collapse:function(p)
                                {
                                    OnCollapsePtz(p);
                                },
                                expand:function(p,e,o,v,d)
                                {
                                        OnExpandPtz(p);
                                }
                                ,beforeexpand:function(p,em)
                                {
                                    if(have_AlarmMngRight)
                                    {
                                        return true;
                                    }else
                                    {
                                        oldAlert(GetDefaultLang("lang_noalarmrighterror","您没有报警管理权限！"));
                                        return false;
                                    }
                                }
                            }
                        })
                    ]//items
                })
                ]
            },
            showSearhPlan
            ],
            listeners:
            {
                resize:function()
                { 
                    ResizeLeftTreePlan();
                }
                ,show:function()
                {  
                    ResizeLeftTreePlan();
                }
            }
        });
    }
    
    
    
    try
    {//  修改密码输入框限制   
        $("mdpassword").setAttribute("maxLength",31);
        $("mdnewpass1").setAttribute("maxLength",31);
        $("mdnewpass2").setAttribute("maxLength",31);
    }catch(eee){}
    
    
    
    return treeViewPlan;
}


