 Ext.namespace('northpoint');
 var home = 'welcome.html';
 var autoLoadPage = home;
 
 var pageContentId = 'pagecontent';
 var headerPage = 'header.html';
 
 var mainViewport = null;
 DEFAULT_DATASTORE_ERRORMSG = "We're sorry, but your request cannot be completed due to technical difficulties at this time.  Please refresh the page and try again.  If that does not resolve the issue, please contact the Northpoint Church of Christ: <a href='info@northpointcoc.com'>info@northpointcoc.com</a>";
 
 /**
  * This is the list of Help buttons that the user may click on to get help
  * content.  Set only one of the item's isDefault to true.  The helpId 
  * may be specified in any one of the tab's configs.  If the main help
  * button is clicked, and if the default button is not clicked, then
  * the tab that has focus is checked for a helpId reference.  If one exists,
  * that help page is loaded, otherwise the default help is loaded.
  We define this in a method because the handler is not yet defined.
  */
 northpoint.getHelpMenuConfig = function(){
	 return [{
			text: 'Website Usage',
			helpfile: '/northpointcoc/help/websiteUsageHelp.html',
			handler: northpoint.help.showHelp,
			helpId: 'websiteUsageHelp',
			isDefault: true
		},{
			text: 'Directions',
			helpfile: '/northpointcoc/help/directionsHelp.html',
			handler: northpoint.help.showHelp,
			helpId: 'directionsHelp'
		},{
			text: 'Articles',
			helpfile: '/northpointcoc/help/articlesHelp.html',
			handler: northpoint.help.showHelp,
			helpId: 'articlesMenuItem'
		},{
			text: 'Audio',
			helpfile: '/northpointcoc/help/audioHelp.html',
			handler: northpoint.help.showHelp,
			helpId: 'audioMenuItem'
		},{
			text: 'Video Sermons',
			helpfile: '/northpointcoc/help/videosermonsHelp.html',
			handler: northpoint.help.showHelp,
			helpId: 'videosermonsMenuItem'
		},{
			text: 'Stickam Sermons',
			helpfile: '/northpointcoc/help/stickamvideosermonsHelp.html',
			handler: northpoint.help.showHelp,
			helpId: 'stickamvideosermonsMenuItem'
		}];
};
 
 Ext.onReady(function(){
    mainViewport = new Ext.Viewport({
    	layout: 'border',
    	renderTo: 'pagebody',
    	id: 'mainViewPort',
    	items: [{
    		xtype: 'panel',
    		region: 'north',
    		height: 105,
    		autoLoad: headerPage,
    		split: true,
    		collapsible: true,
    		collapseMode: 'mini'
    	}, {
    		xtype: 'baseSiteNavigation',
    		region: 'west',
   			collapsible: true,
			items: [{
				title: 'Information',
				collapsed: true,
				items: [{
					html: "<a onclick='mainViewport.addContent({autoLoad: this.href, title: \"Welcome\"}); return false;' href='/northpointcoc/welcome.html'>Welcome</a>"
				},{
					html: "<a onclick='mainViewport.addContent({autoLoad: this.href, title: \"About Us\", closable: true}); return false;' href='/northpointcoc/aboutContent.html'>About Us</a>"
				}, {
					html: "<a onclick='mainViewport.addContent({autoLoad: this.href, title: \"Meeting Times\", closable: true}); return false;' href='/northpointcoc/meetingtimesContent.html'>Meeting Times</a>"
				}, {
					html: "<a onclick='try{northpoint.directions()}catch(ex){alert(ex.message)}; return false;' href='/northpointcoc/directionsContent.html'>Directions</a>"
				}]
			},{
				title: 'Study Tools',
				collapsed: true,
				items:[{
					html: "<a onclick='web.rss.rssfeeds(); return false;' href='#'>RSS Feeds</a>"
				}, {
					html: "<a onclick='web.rss.articles(); return false;' href='#'>Articles</a>"
				},{
					html: "<a onclick='web.rss.audio(); return false;' href='#'>Audio Sermons</a>"
				},{
					html: "<a onclick='web.rss.video(); return false;' href='#'>Video Sermons</a>"
				},{
					html: "<a onclick='web.rss.stickamvideo(); return false;' href='#'>Stickam Sermons</a>"
				},{
					html: "<a onclick='mainViewport.addContent({autoLoad: this.href, title: \"Live Broadcast\", closable: true}); return false;' href='/northpointcoc/livefeedContent.html'>Live Broadcast</a>"
				}]
			},/*{
				title: 'News',
				collapsed: true,
				items: [{
					html: "<a onclick='mainViewport.addContent({autoLoad: this.href, title: \"Gospel Meeting April, 2010\", closable: true}); return false;' href='/northpointcoc/GospelMeeting/2010/April2010.html'>Gospel Meeting April, 2010</a>"
				}]
			},*/{
				title: 'Extras',
				collapsed: true,
				items: [{
					html: "<a onclick='mainViewport.addContent({autoLoad: this.href, title: \"Internet Links\", closable: true}); return false;' href='/northpointcoc/linksContent.html'>Internet Links</a>"
				}]
			}]
    	},{
    		xtype: 'tabpanel',
    		region: 'center',
			id: 'contentTabs',
			activeTab: 0,
			items:[{
				title: 'Welcome',
				xtype: 'panel',
				autoScroll: true,
				autoLoad: autoLoadPage,			
				id: pageContentId
			}],
			tbar:[/*{
				xtype: 'tbfill'
			},*/
			{
				text: 'Help',
				xtype: 'tbsplit',
				//cls: 'x-btn-icon',
				//icon: '/northpointcoc/images/help.png',
				helpfile: '/northpointcoc/help/websiteUsageHelp.html',
				handler: northpoint.help.showHelp,				
				menu: northpoint.getHelpMenuConfig()
			}]
    	}],
    	
    	getContentTabById: function(id){
    		return this.findById(id);
    	},
    	addContent: function(itemConfig){
    		var isInserted = false;
    		var contentTab = this.getContentTabById('contentTabs');
    		var comp = contentTab.find("title", itemConfig.title);
    		if(comp === null || comp.length === 0){
    			if(itemConfig.autoScroll === undefined)
    				itemConfig.autoScroll = true;
    				
    			comp = contentTab.add(itemConfig);
    			isInserted = true;
    		}
    		else
    		{
    			comp = comp[0];
    		}
    		contentTab.setActiveTab(comp);
    		return isInserted;
    	},
    	setActiveTab: function(tab){
    		var contentTab = this.getContentTabById('contentTabs');
    		contentTab.setActiveTab(tab);
    	},
    	getActiveTab: function(){
    		return this.getContentTabById('contentTabs').getActiveTab();
    	},
		/**
		 * Adds a button to the center region's toolbar.
		 * If you need to remove the button, call the button's destroy method.
		 */
    	addToolbarButton: function(config){
    		var contentTab = this.getContentTabById('contentTabs');
    		var tbar = contentTab.getTopToolbar();
    		if(tbar){
    			return tbar.addButton(config);
    		}
    		return null;
    	}
    });
 });

northpoint.help = function(){
	var helpwindow = null;
	return{
		showHelp: function(btn){
			if(!helpwindow){
				helpwindow = new Ext.Window({
					autoScroll: true,
					closeAction: 'hide',
					height: 500,
					width: 600,
					title: 'Help Window'
				});
			}
			helpwindow.show();
			var url = null;
			if(btn.ownerId === undefined){
				// The generic help page was clicked.  Check what tab is clicked
				// and if it is owned by one of the help buttons.
				var currTab = mainViewport.getActiveTab();
				var menuList = northpoint.getHelpMenuConfig();
				if(currTab !== undefined && currTab.helpId !== undefined){
					// find the helpId in the list of help menus:
					for(var index = 0; index < menuList.length && url === null; index++){
						if(menuList[index].helpId === currTab.helpId){
							url = menuList[index].helpfile;
						}
					}
				}
			}

			if(url === null){
				url = btn.helpfile;
			}
			
			helpwindow.load({
				url: url,
				timeout: 15
			});
			
			// helpwindow.show();
			// northpoint.help.doLoad(btn.helpfile);
		},
		doLoad: function(file){
			// open new window and load file into it's content:
			
			Ext.get('helpbody').load({
				url: file
			});
		}
	};
}();
 
northpoint.directions = function(){
	mainViewport.addContent({
		xtype: 'panel',
		layout: 'border',
		title: "Directions",
		closable: true,
		helpId: 'directionsHelp',
		listeners:{
			activate: function(panel){
	       		// add the button to the panel's bbar:
	       		if(this.resetButton){
	       			this.resetButton.show();
	       		}
	       		else{
		       		var mapPanel = Ext.getCmp('mapPanel');
		       		this.resetButton = mainViewport.addToolbarButton({
						text: 'Reset Map',
						handler: mapPanel.onResetMap.createDelegate(mapPanel),
						itemId: 'resetMapId'
					});
		    				/*this.tbar = [{
						text: 'Reset',
						handler: this.onResetMap.createDelegate(this)
					}];*/
				}
	       		
	       	},
	       	deactivate: function(panel){
	       		this.resetButton.hide();
	       	},
	       	close: function(){
	       		this.resetButton.hide();
	       	},
	       	destroy: function(){
	       		this.resetButton.destroy();
	       	}		
		},
		items: [{
			xtype: 'web.gmappanel',
			region: 'center',
			id: 'mapPanel',
			directionsPanelId: 'directionsText',
			closable: true,
	        zoomLevel: 13,
	        gmapType: 'map',
	        mapConfOpts: ['enableScrollWheelZoom','enableDoubleClickZoom','enableDragging'],
	        mapControls: ['GSmallMapControl','GMapTypeControl','NonExistantControl'],
			mapOptions: {
			    googleBarOptions : {
			      style : "new"
			    }
			  },        
	        setCenter: {
				// geoCodeAddr: '5101 E University Dr., Denton, TX, 76208, USA',        
	        	lng: (-97.050384 - .005),
	        	lat: 33.238237
	            // marker: {title: 'Northpoint Church of Christ'}
	        },
	        listeners: {
	        	afterDirectionsLoaded: function(evnt){
	        		mainViewport.findById('directionsTextPanel').expand(true);
	        	}	        	
	        },
	        markers: [{
	            lng: -97.050384,
	            lat: 33.238237,
	            marker: {title: 'Northpoint Church of Christ', draggable:true},
	            listeners: {
	                click: function(e){
	                	var mapPanel = mainViewport.findById('mapPanel');
	                	mapPanel.openGetDirectionsWindow(e.y, e.x, "Northpoint Church of Christ");
	                }
	            }
	        }]// end markers config
	    },{
	    	xtype: 'panel',
	    	autoScroll: true,
	    	html: "<div id='directionsText'></div>",
	    	id: 'directionsTextPanel',
	    	region: 'east',
	    	split: true,
	    	title: 'Directions',
	    	collapsed: true,
	    	collapsible: true,
	    	width:300,
	    	tbar:[{
    			xtype: 'tbbutton',
    			cls: 'x-btn-icon',
    			icon: '/northpointcoc/images/printer16x16.png',
    			tooltip: 'Print',
    			// disabled: true,
    			handler: function(thisBtn){
    				var directionsDom = document.getElementById('directionsText');
    				if(directionsDom.innerHTML === ''){
    					return;
    				}
    				var mapPanel = mainViewport.findById('mapPanel');
					mapPanel.openCurrentDirectionsPrintPreviewWindow();
    			}
    		}]
	    }] // end items config
	});
}