/*
 * Ext JS Library 2.0 Beta 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
SRTA = {};
SRTA.window = {};
SRTA.editor = {};

Ext.onReady(function(){
//Ext.QuickTips.init();
	SRTA.window.render = function(){ 
	        // tabs for the center
		intro = '<div style="width:95%;padding-left:10px;">';
		intro += "<div class='format-header'>Introduction & HowTo</div>";
		intro += "<div class='format-content'><b>This is not the complete SRTA candidate's manual.</b>  The intention of this document is to give an <b>overview</b> of the SRTA examination and process.  The full document will be made available for download upon <a class='linkclass' target='_new' href='https://brighttrac.com/register'>candidate online registration.</a>";
		intro += '</div>';
		intro += "<div class='format-subheader'>Candidates are required to read the entire 2008 Candidate's Manual.</div>";
		intro += "<div class='format-content' style='padding-top:20px;'>To get started browsing the overview, slimply click \"2008 Dental Examination Overview\" and expand the topics.  Once you click on topic and new tab will open here (over what you're reading now).</div>";
		intro += "<div class='format-content'>";
		intro += 'When you are finished reviewing the overview, click the "X" in the top left corner of this window to return';
		intro += "</div>";
		intro += '</div>';
		intro += '</div>';
		SRTA.window.tabs = new Ext.TabPanel({
			region: 'center',
			margins:'3 3 3 0', 
			activeTab: 0,
			enableTabScroll:true,
			defaults:{autoScroll:true},
			items:[{
				title: 'Introduction & HowTo',
				html: intro,
				closable:false
			}]
		});
	
		var Tree = Ext.tree;
		var tree = new Tree.TreePanel({
			autoScroll:true,
			animate:true,	
			border: false,
			containerScroll: true, 
			loader: new Tree.TreeLoader({
				dataUrl:'./nodes.php'
			})
		});
	
		// set the root node
		var root = new Tree.AsyncTreeNode({
			text: '2008 Dental Examination Overview',
			draggable:false,
			id:'./output'
		});
	
		tree.setRootNode(root);
		// render the tree
		//root.expand();
	
	        // Panel for the west
		SRTA.window.nav = new Ext.Panel({
			title: 'Navigation',
			region: 'west',
			split: true,
			width: 250,
			collapsible: true,
			margins:'3 0 3 3',
			cmargins:'3 3 3 3',
			autoScroll: true,
			items: [tree]
	        });
	
	        SRTA.window.win = new Ext.Window({
			text: '2008 Candidate Manual Overview',
			closable:true,
			width:800,
			height:450,
			//border:false,
			plain:true,
			layout: 'border',
			items: [SRTA.window.nav, SRTA.window.tabs]
	        });
	
	        SRTA.window.win.show(this);
	
		tree.on('click', function(node, e){
			if(!node.isLeaf())
				node.reload();
			else{
				SRTA.tmp = node;
				var tabs = SRTA.window.tabs;
				e.stopEvent();
				curTab = tabs.getComponent(node.id);
				if(curTab)
					tabs.setActiveTab(curTab);
				else{
					Ext.Ajax.request({
						url: 'get-content.php',
						success: function(resp){
							tabs.add({
								title: node.text,
								id: node.id,
								html: '<div style="width:95%;padding-left:10px;">'+resp.responseText+'</div>',
								closable:true
							}).show();
						},
						failure: function() {alert('could not load leaf');},
						params: { id: node.id }
					});
				}
			}//if
		});
	
	}//render
	intro = "<div class='format-header'>Introduction & HowTo: for this online document</div>"
	SRTA.editor.render = function(){ 
	        // tabs for the center
		SRTA.editor.tabs = new Ext.TabPanel({
			region: 'center',
			margins:'3 3 3 0', 
			activeTab: 0,
			enableTabScroll:true,
			defaults:{autoScroll:true},
			items:[{
				title: 'General Information',
				html: intro,
				closable:false
			}]
		});
	
		var Tree = Ext.tree;
		var tree = new Tree.TreePanel({
			autoScroll:true,
			animate:true,	
			border: false,
			containerScroll: true, 
			loader: new Tree.TreeLoader({
				dataUrl:'./nodes.php'
			})
		});
	
		// set the root node
		var root = new Tree.AsyncTreeNode({
	      title: '2008 SRTA Candidate Manual',
			draggable:false,
			id:'./output'
		});
	
		tree.setRootNode(root);
		// render the tree
		//root.expand();
	
	        // Panel for the west
		SRTA.editor.nav = new Ext.Panel({
			title: 'Navigation',
			region: 'west',
			split: true,
			width: 200,
			collapsible: true,
			margins:'3 0 3 3',
			cmargins:'3 3 3 3',
			autoScroll: true,
			items: [tree]
	        });
	
	        SRTA.editor.win = new Ext.Window({
	      		title: '2008 SRTA Candidate Manual',
	            closable:false,
	            width:800,
	            height:650,
	            //border:false,
	            plain:true,
	            layout: 'border',
	            items: [SRTA.editor.nav, SRTA.editor.tabs]
	        });
	
	        SRTA.editor.win.show(this);
	
		tree.on('click', function(node, e){
			if(node.isLeaf()){
				e.stopEvent();
				SRTA.editor.formpanel = new Ext.form.FormPanel({
					baseCls: 'x-plain',
					labelWidth: 55,
					url:'save-node.php',
					method: 'post',
					defaultType: 'textfield',
					items: [{
						fieldLabel: 'File Name',
						name: 'file',
						anchor:'95%'  
					},{
						fieldLabel: 'Information',
						name: 'information',
						xtype: 'htmleditor',
						anchor: '95%',
						height: 500
					}]
				});

				SRTA.editor.formpanel.addButton('Save', function(){
					SRTA.editor.formpanel.form.submit({
						url: './save-node.php',
						success: function(){ SRTA.editor.window.hide();},
						failure: function(){ alert('f');}
					});
				}, SRTA.editor.formpanel);

				SRTA.editor.window = new Ext.Window({
					title: 'Edit Information',
					width: 900,
					height:650,
					closable: true,
					minWidth: 300,
					minHeight: 200,
					layout: 'fit',
					bodyStyle:'padding:5px;',
					items: SRTA.editor.formpanel
				});
				SRTA.editor.window.show();
			}//if
		});
	
	}//render

	//SRTA.window.render();

	//SRTA.editor.render();
	
});

