var LocationManager = function() {
	var dialog, showLink, te_id;
	
	return {
		init : function() {
			showLink = Ext.get('show-dialog-link');
		},
		
		showDialog : function() {
			if (!dialog) {
				var lm = document.createElement('div');
				lm.setAttribute('id','LocationManager');
				lm.style.visibility = 'hidden';
				lm.style.textAlign = 'left';				
				
				document.body.appendChild(lm);
							
				dialog = new Ext.LayoutDialog('LocationManager', {
					autoCreate: true,
					modal: true,
					width: 640,
					height: 480,
					shadow: true,
					minWidth: 700,
					minHeight: 480,
					collapsible: false,
					fixedcenter: true,
					proxyDrag: false,
					title: 'Location Manager',
					west: {
						split: true,
						initialSize: 200,
						minSize: 100,
						maxSize: 200,
						titlebar: true,
						margins: {left:5,right:0,bottom:5,top:5},
						collapsible: true,
						animate: true
					},
					center: {
						autoScroll: true,
						tabPosition: 'top',
						closeOnTab: true,
						alwaysShowTabs: true
					}
				});
				dialog.addKeyListener(27, dialog.hide, dialog);
				
				var layout = dialog.getLayout();
				layout.beginUpdate();
				var west = new Ext.ContentPanel('west', {id: 'location-pane', autoCreate: true, title: 'Locations', autoScroll: true, fitToFrame: true});
				var edittab = new Ext.ContentPanel('center', {id: 'ldetails', autoCreate: true, autoScroll:true, title: 'Edit'});
				var templatetab = new Ext.ContentPanel('center', {id: 'ltemplate', autoCreate: true, autoScroll:true, title: 'Template', background: true});
				var icontab = new Ext.ContentPanel('center', {id: 'licon', autoCreate: true, autoScroll:true, title: 'Icon', background: true});
				var eventstab = new Ext.ContentPanel(Ext.id(), {id: 'levents', autoCreate: true, autoScroll:true, title: 'Events', background: true});
				layout.add('west', west);
				layout.add('center', edittab);
				layout.add('center', templatetab);
				layout.add('center', icontab);
				layout.add('center', eventstab);
				
				var tree = new Ext.tree.TreePanel('location-pane', {
        			animate:true,         			
        			containerScroll: true,
					rootVisible:false,
					singleExpand: true
				});
				
    			// set the root node
    			var root = new Ext.tree.AsyncTreeNode({
        			text: 'Locations',
        			id:'source'
    			});				
    			tree.setRootNode(root);				
				
				var loc_types = events.events_getLocationTypes();
				var tnodes = new Array();
				
				for (i=0;i<loc_types.length;i++) {
					tnodes[loc_types[i]] = root.appendChild(new Ext.tree.TreeNode({text: loc_types[i], allowDrag: false}));
				}
				
				var locs = events.events_getLocations();
				
				var cnode;
				
				for (i=0;i<locs.length;i++) {
					cnode = new Ext.tree.TreeNode({text: locs[i].name, icon: locs[i].icon, allowDrag: false});
					cnode.loc_id = locs[i].id;
					tnodes[locs[i].type].appendChild(cnode);
					
				}
				
    			// render the tree
    			tree.render();
				
				var sm = tree.getSelectionModel();
				
				sm.on('selectionchange', function() {
					var n = sm.getSelectedNode();
					if(!n) {
						//alert('doh!');
					}
					if (n.loc_id > 0) {						
						var location = events.events_getLocationDetails(n.loc_id);
						editform.setValues(location);
						templateform.setValues(location);
						var oEditor = FCKeditorAPI.GetInstance(te_id) ;								
						oEditor.SetHTML(location.template);
					}
				});
				
    			root.expand();
				
            var editform = new Ext.form.Form({
               method: 'POST',
               labelWidth: 150,
               url: 'save-form.php'					
            });
 
            editform.add(
               new Ext.form.TextField({
                  fieldLabel: 'Location Name',
                  name: 'name',
                  width: '95%',
                  allowBlank:false}),
               new Ext.form.TextField({
                  fieldLabel: 'Minimum Level',
                  name: 'min_level',
                  width: '95%',
                  allowBlank:false}),
               new Ext.form.TextField({
                  fieldLabel: 'Max Participants',
                  name: 'max_participants',
                  width: '95%',
                  allowBlank:false}),
               new Ext.form.TextField({
                  fieldLabel: 'Default Rallypoint',
                  name: 'default_rally',
                  width: '95%',
                  allowBlank:false})
            );
				
				editform.fieldset(
					{legend:'Options:', hideLabels:true},
					new Ext.form.Checkbox({
						boxLabel:'BC Required',
						name:'bc'
					}),
					new Ext.form.Checkbox({
						boxLabel:'Attunement Required',
						name:'attune'
					}),
					new Ext.form.Checkbox({
						boxLabel:'Has Heroic Mode',
						name:'heroic'
					}),
					new Ext.form.Radio({
						boxLabel:'PuG Event',
						name:'pug'
					}),
					new Ext.form.Radio({
						boxLabel:'Official Event',
						name:'pug'
					})						
				);				
				
            editform.addButton('Save');
            
            editform.render('ldetails');
				
            var templateform = new Ext.form.Form({
               method: 'POST',
               labelWidth: 150,
					labelAlign: 'top',
               url: 'save-form.php'					
            });				
				
				var te_TA = new Ext.form.TextArea({
                  name: 'template',
                  width: '95%',
						grow: true,
						growMax: 50,
                  allowBlank:false});
				
				te_id = te_TA.getId();
				
				templateform.add(
               te_TA
				);				
				
				templateform.render('ltemplate');
				
				var sBasePath = '/_library/fckeditor/';
				
				var te = new FCKeditor( te_id ) ;
				te.BasePath	= sBasePath ;
				te.Config['CustomConfigurationsPath'] = '/events/_script/fckeditorconfig.js' ;
				te.ReplaceTextarea();
						
				layout.endUpdate();
			}
			dialog.show(showLink.dom);						
		},
		
		hideDialog : function() {
			dialog.hide(showLink.dom);		
		}
	};
}();
Ext.EventManager.onDocumentReady(LocationManager.init, LocationManager, true);
