var mainTabsJSON = new Array();
var loggedInUserName = null;

Ext.onReady(function(){
	Ext.Ajax.request({
      url: 'php/query.php',
      success: function(response, opts){
			renderMainTabs(response, opts);
      },
      failure: function(response,opts){
			Ext.Msg.alert('Error','A communication error has occurred.');
		},
      params: { 
      	operation: 'getMainTabs'
      }
	});	
});

function renderMainTabs(response, opts, activeTab){
	if(response){
		mainTabsJSON = Ext.util.JSON.decode(response.responseText);
	} else {
		maimTabsJSON[0] = new Object({mainTabs_title:'Error',mainTabs_content:'An error has occurred: ' + response.responseText});
	}

	var mainTabsItems = new Array();

	for(i=0; i<mainTabsJSON.length; i++){
		mainTabsItems[i] = new Object({
			title: mainTabsJSON[i].mainTabs_title,
			html: mainTabsJSON[i].mainTabs_content
		});
		if (!activeTab && mainTabsJSON[i].mainTabs_default == '1'){
			activeTab = i;
		};
	}

	if(Ext.ComponentMgr.get('maintabs')){
		Ext.ComponentMgr.get('maintabs').destroy();
	}

	var mainTabs = new Ext.TabPanel({
		id:'maintabs',
		width:750,
		defaults:{autoHeight: true},
		plain:true,
		enableTabScroll: true,
		activeTab: activeTab,
		bodyStyle: 'padding:5px',
		renderTo:'rendermaintabs',
		items: mainTabsItems
	});
}

function login(){
	var callLogin = function(){
		Ext.Ajax.request({
   			url: 'php/login.php',
			params: {
				userName: Ext.ComponentMgr.get('userNameField').getValue(),
				password: Ext.ComponentMgr.get('passwordField').getValue()
			},
   			success: function(response, opts){
					var head = document.getElementsByTagName("head")[0];
            	var js = document.createElement('script');
					js.setAttribute("type", "text/javascript");
					js.text = response.responseText;
					head.appendChild(js);
			},
			failure: function(response, opts){
				Ext.Msg.alert('Error','A communication error has occurred.');
			}
		});
	};

	var loginWindow = new Ext.Window({
		id: 'loginWindow',
		title:'Log In',
		defaults:{autoWidth: true},
		resizable: false,
		modal: true,
		layout:'form',
		border: false,
		items:[
			new Ext.form.TextField({
				id:'userNameField',
				width: 435,
				fieldLabel:'User Name'
			}),
			new Ext.form.TextField({
				id:'passwordField',
				width: 435,
				inputType:'password',
				fieldLabel:'Password'
			})
		],
		buttons:[{
			id:'loginButton',
			text:'Log In',
			handler: callLogin
		},{
			text:'Cancel',
			handler: function(){
				loginWindow.close();
			}
		}]
	});


	if (Ext.isIE){
		loginWindow.setWidth(267);
	}

	loginWindow.show(null, function(){
		Ext.ComponentMgr.get('userNameField').focus(true, 10);

		new Ext.KeyMap("userNameField", {
    			key: Ext.EventObject.ENTER,
    			fn: callLogin
		});

		new Ext.KeyMap("passwordField", {
	    		key: Ext.EventObject.ENTER,
    			fn: callLogin
		});
	});
}

var locationWindow;

function showLocationWindow(){
	if(navigator.appName == "Microsoft Internet Explorer") {
		window.location = 'http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=Cutting+Hall&sll=42.113057,-88.039622&sspn=0.006311,0.009656&g=150+E.+Wood+Street,+Palatine,+IL+60067&ie=UTF8&cid=7228883943715649505&ll=42.115543,-88.039134&spn=0.012622,0.019312&t=h&z=16&iwloc=A';
	} else {
		if(locationWindow == null){
			locationWindow = new Ext.Window({
				title:'Show Location',
				contentEl:'locationWindowContent',
				resizable: false,
				closeAction: 'hide'
			});
		}

		locationWindow.show();	
	}
}