//基本信息页面
strBasicInfo = "../view.html";
//无基本信息页面
strNoBasicInfo = "ServerPage/NoInfo.html";
//编辑信息页面
strEditorInfo = "ServerPage/ShowEditorInfo.php";
//网址页面
strWeb = "ServerPage/inputurl.html";
//帮助
strHelp = "ServerPage/help.html";

document.writeln('<style type="text/css">.x-tree-node-icon{display:none;}</style>');


Ext.onReady( function() {
	Ext.BLANK_IMAGE_URL = "../ext-2.2/resources/images/default/s.gif";
	
	//
	var contentPanel = {id:'content-panel',region:'center',title:'走动|飞行|滑动|翻转|全景|跳转|水平|顶视|复位|碰撞|重力|信息',contentEl:'UnityContent',margins:'0 0 5 0'};
	
	//
	var treePanel = new Ext.tree.TreePanel({id:'tree-panel', region:'north', title:'设备',
											split:true, height:500, rootVisible:false,
											lines:true, singleExpand:true, animation:false,
											expanded:true,autoScroll:true,
											loader : new Ext.tree.TreeLoader({dataUrl:'ServerPage/GetNodes.php'}),
											root : new Ext.tree.AsyncTreeNode({id:"root", text:"建筑"})
											});
	
	var leftTabPanel = new Ext.TabPanel({id:'tab-panel', region:'north',height:500,
		 margins:'0 0 0 0', activeTab:0,enableTabScroll:true,
		 items :
			 [treePanel,
			  {title:'人员', autoLoad:{url:strNoBasicInfo}},
			  {title:'预案', autoLoad:{url:strNoBasicInfo}},
			  {title:'其他', autoLoad:{url:strNoBasicInfo}}]
		});
	//var root = new Ext.tree.TreeNode({id:"root", text:"物体", checked:true});
	//treePanel.setRootNode(root);
	//root.expand(true);
	
	 var ua = navigator.userAgent.toLowerCase();  
	   
	 var isStrict = document.compatMode == "CSS1Compat",  
	     isOpera = ua.indexOf("opera") > -1,  
	     isSafari = (/webkit|khtml/).test(ua),  
	     isSafari3 = isSafari && ua.indexOf('webkit/5') != -1,  
	     isIE = !isOpera && ua.indexOf("msie") > -1,  
	     isIE7 = !isOpera && ua.indexOf("msie 7") > -1,  
	     isGecko = !isSafari && ua.indexOf("gecko") > -1,  
	     isBorderBox = isIE && !isStrict,  
	     isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),  
	     isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),  
	     isAir = (ua.indexOf("adobeair") != -1),  
	     isLinux = (ua.indexOf("linux") != -1),  
	     isSecure = window.location.href.toLowerCase().indexOf("https") === 0; 
	
	var UnityId = isIE?"UnityObject":"UnityEmbed";
	     
	treePanel.on('checkchange',function(node){
		var index = node.id - 1;
		if(node.attributes.checked)		
			document.getElementById(UnityId).SendMessage("ListedObjectsController","ShowObject",""+index);
		else
			document.getElementById(UnityId).SendMessage("ListedObjectsController","HideObject",""+index);
		
		if(node.hasChildNodes())
		{
			node.eachChild(function(child){
				child.getUI().toggleCheck(node.attributes.checked);
				child.attributes.checked = node.attributes.checked;
				child.fireEvent('checkchange',child);
			})
		}
	});
	
	//
	var detailsPanel = {id:'details-panel', title:'WebGIS', region:'center',
						html: '<p class="details-info">WebGIS</p>'
					};
	
	//
	var tabPanel = new Ext.TabPanel({id:'tab-panel', region:'north', height:500,
									 margins:'0 0 0 0', activeTab:0,enableTabScroll:true,
									 items :
										 [
										  {title:'基本信息',autoScroll:true, autoLoad:{url:strNoBasicInfo}},
										  {title:'扩展信息', autoLoad:{url:strNoBasicInfo}},
										  {title:'操作指南', autoLoad:{url:strHelp}},
										  {title:'其他信息', autoLoad:{url:strNoBasicInfo}}]
									});
	
	var urlPanel = {id:'details-panel', title:'输入网址', region:'center',
			items:[{autoLoad:{url:strWeb}}]
		};

	
//	var urlPanel = new Ext.form.FormPanel({
//	    defaultType: 'textfield',
//	    labelAlign: 'right',
//	title: 'form',
//	labelWidth: 50,  
//	    frame: true,                          
//	    width: 220,                           
//	    items: [{                             
//	        fieldLabel: '文本框'             
//	    }],                                    
//	    buttons: [{                          
//	        text: '按钮'                       
//	    }]                                      
//	});
	
	//
	new Ext.Viewport({layout:'border',
					  items:
						  [{xtype:'box', region:'north',applyTo:'header',height:30},
						   {layout:'border', id:'layout-browser', region:'west', 
							  border:false, split:true, margins:'0 0 5 5', 
							  width:200, minSize:100, maxSize:500,
							  collapsible:true,
							  items:[leftTabPanel, detailsPanel]},
							  
							  contentPanel,
							  
							  {layout:'border', id:'layout', region:'east', 
								  border:false, split:true, margins:'0 3 5 0', 
								  width:250, minSize:100, maxSize:500,
								  collapsible:true,
								  items:[tabPanel, urlPanel]}
						  ],
					  renderTo:Ext.getBody()
					});
})

///////////////////////
//following code is community with unity3d
function UnitySelect(id){
	var basicInfoTab = Ext.getCmp('tab-panel').getItem(0);
	Ext.getCmp('tab-panel').setActiveTab(basicInfoTab);
	if( basicInfoTab )
	{
		basicInfoTab.getUpdater().update(strBasicInfo+"?id="+id);
		//basicInfoTab.getUpdater().update("ServerPage/ShowBasicInfo.php?id="+id+"&name="+name);
		//var index = id + 1870;
		//basicInfoTab.getUpdater().update("../view.html?quhao=1001&weihao="+index);
	}
	
//	var editorInfoTab = Ext.getCmp('tab-panel').getItem(1);
//	if( editorInfoTab)
//	{
//		editorInfoTab.getUpdater().update(strEditorInfo+"?id="+id+"&name="+name);
//	}
	var editorInfoTab = Ext.getCmp('tab-panel').getItem(1);
	if( editorInfoTab)
	{
		editorInfoTab.getUpdater().update("../edit.html?id="+id);
	}	
}

function UnityNoSelect(){
	var basicInfoTab = Ext.getCmp('tab-panel').getItem(0);
	Ext.getCmp('tab-panel').setActiveTab(basicInfoTab);
	if(basicInfoTab)
	{
		basicInfoTab.getUpdater().update(strNoBasicInfo);
	}
	
	var editorInfoTab = Ext.getCmp('tab-panel').getItem(1);
	if( editorInfoTab)
	{
		editorInfoTab.getUpdater().update(strNoBasicInfo);
	}
}

function AddTreeItem(id, name) {
	var tree = Ext.getCmp('tree-panel');
	tree.root.appendChild(new Ext.tree.TreeNode( {
		id :id,
		text :new String(name),
		leaf :true,
		checked :true,
		listeners : {
			"checkchange" : function(node) {
				if (node.attributes.checked)
				{
					document.getElementById("UnityObject").SendMessage("ListedObjectsController","ShowObject",""+node.id);
					//document.getElementById("UnityEmbed").SendMessage("ListedObjectsController","ShowObject",""+node.id);
				}
				else
				{
					document.getElementById("UnityObject").SendMessage("ListedObjectsController","HideObject",""+node.id);
					//document.getElementById("UnityEmbed").SendMessage("ListedObjectsController","HideObject",""+node.id);
				}
			}
		}
	}));
}

/////////////////////////////////////

//function UnitySelect(id,name){
//	var basicInfoTab = Ext.getCmp('tab-panel').getItem(0);
//	Ext.getCmp('tab-panel').setActiveTab(basicInfoTab);
//	if( basicInfoTab )
//	{
//		basicInfoTab.getUpdater().update("./ServerPage/ShowBasicInfo.php?id="+id+"&name="+name);
//	}
//	AddTreeItem(id,name);
//}
//
//function UnityNoSelect(){
//	var basicInfoTab = Ext.getCmp('tab-panel').getItem(0);
//	Ext.getCmp('tab-panel').setActiveTab(basicInfoTab);
//	if(basicInfoTab)
//	{
//		basicInfoTab.getUpdater().update("./ServerPage/NoInfo.html");
//	}
//}
//
//function AddTreeItem(id,name)
//{
//	var tree = Ext.getCmp('tree-panel');
//	tree.root.appendChild(new Ext.tree.TreeNode({id:id, text:name,leaf:true,checked:true,
//												listeners:{"checkchange":function(node){
//																							if(node.attributes.checked)
//																								alert(node.attributes.id);
//																						}				
//														}
//												}));
//}


// 
// //i f   u s e   d i s s e l e c t 
// f u n c t i o n   U n i t y N o S e l e c t ( )   { 
// 	 v a r   b a s i c I n f o T a b   =   E x t . g e t C m p ( ' t a b - p a n e l ' ) . g e t I t e m ( 0 ) ; 
// 	 E x t . g e t C m p ( ' t a b - p a n e l ' ) . s e t A c t i v e T a b ( b a s i c I n f o T a b ) ; 
// 
// 	 i f   ( b a s i c I n f o T a b )   { 
// 	 	 b a s i c I n f o T a b . g e t U p d a t e r ( ) . u p d a t e ( " . / S e r v e r P a g e / N o I n f o . h t m l " ) ; 
// 	 } 
//
// } 
// 
// //a d d   i t e m   t o   t r e e 
// f u n c t i o n   A d d T r e e I t e m ( i d ,   n a m e )   { 
// 	 v a r   t r e e   =   E x t . g e t C m p ( ' t r e e - p a n e l ' ) ; 
// 	 t r e e . r o o t . a p p e n d C h i l d ( n e w   E x t . t r e e . T r e e N o d e (   { 
// 	 	 i d   : " r o o t " , 
// 	 	 t e x t   : " t e s t " , 
// 	 	 c h e c k e d   : t r u e , 
// 	 } ) ) 
// } 
// //i f   u s e   s e l e c t e d   s o m e t h i n g   i n   u n i t y 
// f u n c t i o n   U n i t y S e l e c t ( i d ,   n a m e )   { 
// 	 v a r   b a s i c I n f o T a b   =   E x t . g e t C m p ( ' t a b - p a n e l ' ) . g e t I t e m ( 0 ) ; 
// 	 E x t . g e t C m p ( ' t a b - p a n e l ' ) . s e t A c t i v e T a b ( b a s i c I n f o T a b ) ; 
// 
// 	 i f   ( b a s i c I n f o T a b )   { 
// 	 	 b a s i c I n f o T a b . g e t U p d a t e r ( ) . u p d a t e ( " . / S e r v e r P a g e / S h o w B a s i c I n f o . p h p ? i d = "   +   i d   +   " & n a m e = "   +   n a m e ) ; 
// 	 } 
// 	 A d d T r e e I t e m ( i d ) ; 
// 	  a l e r t ( i d ) ; 
// } 

// 			//add the tree Panel
// 	 	 	 v a r   t r e e P a n e l   =   n e w   E x t . t r e e . T r e e P a n e l (   { 
// 	 	 	 	 i d :' t r e e - p a n e l ' , 
// 	 	 	 	 t i t l e : 'Node' , 
// 	 	 	 	 r e g i o n   : ' n o r t h ' , 
// 	 	 	 	 s p l i t:true , 
// 	 	 	 	 a u t o S c r o l l: t r u e , 
// 	 	 	 	 h e i g h t :5 0 0 , 
// 	 	 	 	 r o o t V i s i b l e :false, 
// 	 	 	 	l i n e s :false, 
// 	 	 	 	 s i n g l e E x p a n d :   t r u e , 
// 	 	 	 	 u s e A r r o w s   : t r u e , 
// 	 	 	 	a n i m a t i o n    : t r u e 
// 	 	 	 } ) ; 
//			//end 
// 	 	 	 
//// 	 	 	 t r e e P a n e l . o n ( ' c h e c k c h a n g e ' ,   f u n c t i o n ( n o d e ,   c h e c k e d )   {     
//// 	 	 	 	                                   n o d e . e x p a n d ( ) ;     
//// 	 	 	 	                                   n o d e . a t t r i b u t e s . c h e c k e d   =   c h e c k e d ;     
//// 	 	 	 	                                   n o d e . e a c h C h i l d ( f u n c t i o n ( c h i l d )   {     
//// 	 	 	 	                                           c h i l d . u i . t o g g l e C h e c k ( c h e c k e d ) ;     
//// 	 	 	 	                                           c h i l d . a t t r i b u t e s . c h e c k e d   =   c h e c k e d ;     
//// 	 	 	 	                                         c h i l d . f i r e E v e n t ( ' c h e c k c h a n g e ' ,   c h i l d ,   c h e c k e d ) ;     
//// 	 	 	 	                                   } ) ;     
//// 	 	 	 	                           } ,   t r e e P a n e l ) ; 
// 
//			//add root to the tree
// 	 	 	 v a r   r o o t   =   n e w   E x t . t r e e . T r e e N o d e (   { 
// 	 	 	 	 i d   : " r o o t " , 
// 	 	 	 	 t e x t   : "root" , 
// 	 	 	 	 c h e c k e d   : t r u e , 
// 	 	 	 } ) ; 
// 
// 	 	 	 t r e e P a n e l . s e t R o o t N o d e ( r o o t ) ; 
// 	 	 	  r o o t . e x p a n d ( t r u e ) ;
//			//end 
// 
//			//the detailsPanel
// 	 	 	 v a r   d e t a i l s P a n e l   =   { 
// 	 	 	 	 i d   : ' d e t a i l s - p a n e l ' , 
// 	 	 	 	 t i t l e   : 'details' , 
// 	 	 	 	 r e g i o n   : ' c e n t e r ' , 
// 	 	 	 	 h e i g h t   : 2 0 0 , 
// 	 	 	 	 b o d y S t y l e   : ' p a d d i n g - b o t t o m : 1 5 p x ; b a c k g r o u n d : # e e e ; ' , 
// 	 	 	 	 a u t o S c r o l l   : f a l s e , 
// 	 	 	 	 h t m l   : ' < p   c l a s s = " d e t a i l s - i n f o " > W h e n   y o u   s e l e c t   a   l a y o u t   f r o m   t h e   t r e e ,   a d d i t i o n a l   d e t a i l s   w i l l   d i s p l a y   h e r e . < / p > ' 
// 	 	 	 } ; 
//			//end
// 
//			//the tabPanel
// 	 	 	 v a r   t a b P a n e l   =   n e w   E x t . T a b P a n e l (   { 
// 	 	 	 	 i d   : ' t a b - p a n e l ' , 
// 	 	 	 	 r e g i o n   : ' e a s t ' , 
// 	 	 	 	 w i d t h   : 2 0 0 , 
// 	 	 	 	 s p l i t   : t r u e , 
// 	 	 	 	 m a r g i n s   : ' 0   5   5   0 ' , 
// 	 	 	 	 c m a r g i n s   : ' 3   3   3   3 ' , 
// 	 	 	 	 a c t i v e T a b   : 0 , 
// 	 	 	 	 i t e m s   :   [   { 
// 	 	 	 	 	 t i t l e   : 'Basic' ,
// 	 	 	 	 	 h t m l   : ' . / S e r v e r P a g e / N o I n f o . h t m l ' 
// 	 	 	 	 } ,   { 
// 	 	 	 	 	 t i t l e   : 'Editor' ,
// 	 	 	 	 	 h t m l   : ' . / S e r v e r P a g e / N o I n f o . h t m l ' 
// 	 	 	 	 }  ] 
// 	 	 	 } ) ; 
//			//end 
// 
//			//the viewport
// 	 	 	 n e w   E x t . V i e w p o r t (   { 
// 	 	 	 	 l a y o u t   : ' b o r d e r ' , 
// 	 	 	 	 t i t l e   : ' E x t   L a y o u t   B r o w s e r ' , 
// 	 	 	 	 i t e m s   :   [   { 
// 	 	 	 	 	 x t y p e   : ' b o x ' , 
// 	 	 	 	 	 r e g i o n   : ' n o r t h ' , 
// 	 	 	 	 	 a p p l y T o   : ' h e a d e r ' , 
// 	 	 	 	 	 h e i g h t   : 3 0 
// 	 	 	 	 } ,   { 
// 	 	 	 	 	 l a y o u t   : ' b o r d e r ' , 
// 	 	 	 	 	 i d   : ' l a y o u t - b r o w s e r ' , 
// 	 	 	 	 	 r e g i o n   : ' w e s t ' , 
// 	 	 	 	 	 b o r d e r   : f a l s e , 
// 	 	 	 	 	 s p l i t   : t r u e , 
// 	 	 	 	 	 m a r g i n s   : ' 0   0   5   5 ' , 
// 	 	 	 	 	 w i d t h   : 2 0 0 , 
// 	 	 	 	 	 m i n S i z e   : 1 0 0 , 
// 	 	 	 	 	 m a x S i z e   : 5 0 0 , 
// 	 	 	 	 	 i t e m s   :   [   t r e e P a n e l ,   d e t a i l s P a n e l   ] 
// 	 	 	 	 } ,   c o n t e n t P a n e l ,   t a b P a n e l   ] , 
// 	 	 	 	 r e n d e r T o   : E x t . g e t B o d y ( ) 
// 	 	 	 } ) ; 
//			//end
// 
// 	 	 } ) 
// 
// //i f   u s e   s e l e c t e d   s o m e t h i n g   i n   u n i t y 
// f u n c t i o n   U n i t y S e l e c t ( i d ,   n a m e )   { 
// 	 v a r   b a s i c I n f o T a b   =   E x t . g e t C m p ( ' t a b - p a n e l ' ) . g e t I t e m ( 0 ) ; 
// 	 E x t . g e t C m p ( ' t a b - p a n e l ' ) . s e t A c t i v e T a b ( b a s i c I n f o T a b ) ; 
// 
// 	 i f   ( b a s i c I n f o T a b )   { 
// 	 	 b a s i c I n f o T a b . g e t U p d a t e r ( ) . u p d a t e ( " . / S e r v e r P a g e / S h o w B a s i c I n f o . p h p ? i d = "   +   i d   +   " & n a m e = "   +   n a m e ) ; 
// 	 } 
// 	 A d d T r e e I t e m ( i d ) ; 
// 	  a l e r t ( i d ) ; 
// } 
// 
// //i f   u s e   d i s s e l e c t 
// f u n c t i o n   U n i t y N o S e l e c t ( )   { 
// 	 v a r   b a s i c I n f o T a b   =   E x t . g e t C m p ( ' t a b - p a n e l ' ) . g e t I t e m ( 0 ) ; 
// 	 E x t . g e t C m p ( ' t a b - p a n e l ' ) . s e t A c t i v e T a b ( b a s i c I n f o T a b ) ; 
// 
// 	 i f   ( b a s i c I n f o T a b )   { 
// 	 	 b a s i c I n f o T a b . g e t U p d a t e r ( ) . u p d a t e ( " . / S e r v e r P a g e / N o I n f o . h t m l " ) ; 
// 	 } 
//
// } 
// 
// //a d d   i t e m   t o   t r e e 
// f u n c t i o n   A d d T r e e I t e m ( i d ,   n a m e )   { 
// 	 v a r   t r e e   =   E x t . g e t C m p ( ' t r e e - p a n e l ' ) ; 
// 	 t r e e . r o o t . a p p e n d C h i l d ( n e w   E x t . t r e e . T r e e N o d e (   { 
// 	 	 i d   : " r o o t " , 
// 	 	 t e x t   : " t e s t " , 
// 	 	 c h e c k e d   : t r u e , 
// 	 } ) ) 
// } 

