/**
* @fileoverview This file contains the admin tab object.
* @author Sara Lin
* @version 0.1
*/

/**
* The AdminTab is currently not used.  It is designed to replace the sign in/ sign out functionalities of the User object.
* However, it's sub-objects, such as AdminTab.editHelp, is implemented and used for the current system.
* @namespace
*/
var AdminTab = {
	signedIn: false,
	signInHTML: null,
	/* the currently active page, can be general information, predefined lists, or photos */
	activePage: null,
	/* the tab object item uses */
	tab: null,
	/* the item edit tab content */
	tabContent: null,
	
	/** ignore */
	initialize: function() {
	},
	
	/** ignore */
	load: function(item) {
		var tabResults = Main.tabs.loadTab('admin','Admin',true,true);
		if (tabResults.madeNew) {
			var thisObj = this;
			this.tab = tabResults.tab;
			var url = base_url+'admin/showAdminTab';
			new Ajax.Request(url, {
				onSuccess: function(response) {
					thisObj.signInHTML = response.responseText;
					thisObj.tab.tabElem.innerHTML = thisObj.signInHTML;
					Event.observe('signInForm', "submit", thisObj.signIn);
				}
			});
		}
		Main.tabs.showTab('admin');
	},
	
	/** ignore */
	unload: function() {

	},
	
	/** ignore */
	_signIn: function(event) {
		var username = $('usr').value;
		var password = $('pwd').value;
		
		//hide all error messages
		$$('#signInForm .errorMsg span').each(function(e) {
			e.style.display='none';
		});
			
		if (username == "" || password == "") {
			$$('#signInForm .errorMsg .mandat')[0].style.display='';
		} 
		else {	
			//validate username and password in server
			var thisObj = this;
			
			//encode the url
			var url = base_url+'admin/signIn';
			var queryArray = new Array(username,sha1Hash(password));
			url = encodeURL(url, queryArray);
			
			new Ajax.Request(url, {
				onSuccess: function(response) {
					var json = response.responseText.evalJSON();
					if (json.signedIn == "good") {	
						thisObj.signedIn = true;
						//attach admin page event handlers
						thisObj.addPageEvents();
				
						//show all elements with class "signedIn"
						$$('.signedIn').each(function(el) {
							el.style.display='';
						});
						
						AdminTab.user.load(username, json.email);
						AdminTab.mapPage.load();
					}
					else if (json.signedIn == "bad") {
						$$('#signInForm .errorMsg .valid')[0].style.display='';
						$('pwd').value = "";
					}
					else {
					}
				}
			});
		}
	},

	/** ignore */
	prepareSignOut: function() {
		//hide the elements that shows only when user is logged in
		$$('.signedIn').each(function(el) {
			el.style.display='none';
		});
		//delete all elements that only shows when user is logged in
		$$('.admin').each(function(el) {
			el.parentNode.removeChild(el);
		});
		//show all original files before admin made changes
		$$('.original').each(function(el) {
			el.style.display='';
		});
		
		ItemTab.unloadEditTab();
		AdminTab.delPageEvents();
		AdminTab.user.unload();
	},
	
	/** ignore */
	_signOut: function() {
		// only sign out if we're good to go with coordinate editing
		if (this._turnOffCoordEditing(null)) {
			this.prepareSignOut();
		
			var thisObj = this;
			var url = base_url+'admin/signOut/';
			new Ajax.Request(url, {
				onSuccess: function(response) {
					thisObj.signedIn = false;
					thisObj.tab.tabElem.innerHTML = thisObj.signInHTML;
					Event.observe('signInForm', "submit", thisObj.signIn);
				}
			});
		}
	},
	
	/** ignore */
	addPageEvents: function() {
		Event.observe('mapLink', 'click', this.mapPage.load);
		Event.observe('listLink', 'click', this.listPage.load);		
	},
	
	/** ignore */
	delPageEvents: function() {
		Event.stopObserving('mapLink', 'click', this.mapPage.load);
		Event.stopObserving('listLink', 'click', this.listPage.load);
	},
	
	/** ignore */
	switchPage: function(selectedPage) {
		if (AdminTab.activePage != null) {
			$(AdminTab.activePage.body).hide();
			AdminTab.activePage.link.className = '';
		}
		$(selectedPage.body).show();
		selectedPage.link.className = 'active';
		AdminTab.activePage = selectedPage;
	}
}

AdminTab.user = {
	username: null,
	email: null,
	
	load: function(username, email) {
		this.username = username;
		this.email = email;
	},
	
	unload: function() {
		this.username = null;
		this.email = null;
	}
}