/**
* @fileoverview This file contains the static object ItemTab. 
* @author Sara Lin
* @version 0.1
*/

/**
 * @namespace This is an ItemTab object that contains all functionalities and field members
 * associated with item tab.
 */
var ItemTab = {
	/** item currently on the itemTab */
	item: null,
	/** the edit link */
	editLink: null,
	/** the itemTab mode */
	edit: false,
	/** 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,
	
	/**
	* initialize the ItemTab, get the tab object from Main.tabs
	* @constructor
	*/
	initialize: function() {
		this.tab = Main.tabs.getTab('item');
	},
	
	/**
	* loads the item tab with the clicked item's information, update the ItemTab.item to the current item.
	* @param {object} item The clicked item of type Item
	*/
	load: function(item) {
		if (this.edit || this.item==null || this.item.id!=item.id) {
			if (this.item)
				this.item.highlight_hide(true);
			this.item = item;
			
			new Ajax.Request(base_url+'item/itemClick/'+item.id, {
				onSuccess: function(response) {
					ItemTab.unload();
					
					ItemTab.tab.tabElem.innerHTML = response.responseText;
					ItemTab.tabContent = $('itemContent');
					ItemTab.tab.labelElem.firstChild.innerHTML = '<span>'+item.name+'</span>';
					
					ItemTab.addPageEvents();
					ItemTab.loadGeneralPage(null);
					ItemTab.item.highlight_show(true);
					ItemTab.tab.mySystem.changeTab(ItemTab.tab,false);
					
					ItemTab.attachEditObserver();
					//attach event handlers to edit icon in item tab
					Event.observe('edit', 'click', ItemTab.editTab);
				}
			});
		}
		else {
			this.tab.mySystem.changeTab(this.tab,false);
		}
	},
	
	/**
	* unload the pages in ItemTab
	*/
	unload: function() {
		this.generalPage.unload();
		this.photosPage.unload();
		this.activePage == null;
		ItemEditTab.unload();
		ItemTab.edit = false;
		ItemTab.delPageEvents();
		if ($('edit'))
			Event.stopObserving($('edit'), 'click', this.editTab);
	},
	
	/**
	* attach event handler for editing item properties.
	*/
	attachEditObserver: function() {
		if (!$('edit'))
			return;
			
		if(Main.user.signedIn) {
			$('edit').style.display = '';
		}
		else {
			$('edit').style.display = 'none';
		}
	},
	
	/**
	* detach event handler for editing item properties.
	*/
	unloadEditTab: function() {
		if (ItemTab.edit) {
			ItemEditTab.unload(ItemTab.item);
			ItemTab.load(ItemTab.item);
			ItemTab.edit = false;
		}
	},
	
	/**
	* invoked by admin clicking on the edit option link to either show or hide the edit view by either loading or unloading the ItemEditTab.
	* @event
	*/
	editTab: function(event) {
		if (ItemTab.edit) {
			ItemEditTab.unload(ItemTab.item);
			ItemTab.load(ItemTab.item);
			ItemTab.edit = false;
		} else {
			ItemEditTab.load(ItemTab.item);
			ItemTab.edit = true;
		}
	},
	
	/** @ignore */
	tabNotify: function(tab) {
		thisObj = this;		
		tab.showFunc = function() {
			$('edit').style.display='';
		};
		tab.hideFunc = function() {
			$('edit').style.display='none';
		}
		tab.closeFunc = function() {
			$('edit').style.display='none';
		}
	},
	
	/** @ignore */
	addPageEvents: function() {
		if (this.tabContent!=null) {
			if ($('photosLink'))
				Event.observe('photosLink', 'click', this.loadPhotosPage);
			if ($('generalLink'))
				Event.observe('generalLink', 'click', this.loadGeneralPage);		
		}
	},
	
	/** @ignore */
	delPageEvents: function() {
		if (this.tabContent!=null) {
			if ($('photosLink'))
				Event.stopObserving('photosLink', 'click', this.loadPhotosPage);
			if ($('generalLink'))
				Event.stopObserving('generalLink', 'click', this.loadGeneralPage);
		}
	},
	
	/** hide all other pages; show the selected page 
	* @param {object} selectedPage
	*/
	switchPage: function(selectedPage) {
		if (ItemTab.activePage != null && ItemTab.activePage.body != null) {
			Element.hide(ItemTab.activePage.body);
			ItemTab.activePage.link.className = '';
		}
		Element.show(selectedPage.body);
		selectedPage.link.className = 'active';
		ItemTab.activePage = selectedPage;
	},
	
	/** 
	* load contents for the genearl edit page while hiding all others.  
	* @event
	*/
	loadGeneralPage: function(event) {
		if (ItemTab.generalPage.body == null)
			ItemTab.generalPage.load(ItemTab.item.id, ItemTab.switchPage, ItemTab.generalPage);
		else {
			ItemTab.switchPage(ItemTab.generalPage);
		}
	},
	
	/** 
	* load contents for the genearl edit page while hiding all others.  
	* @event
	*/
	loadPhotosPage: function(event) {
		if (ItemTab.photosPage.body == null)
			ItemTab.photosPage.load(ItemTab.item.id, ItemTab.switchPage, ItemTab.photosPage);
		else {
			ItemTab.switchPage(ItemTab.photosPage);
		}
	}
}

/**
* @namespace The general page (default page) of the ItemTab
*/
ItemTab.generalPage = {
	/** current page's link element */
	link: null,
	/** element that contains the content of the page */
	body: null,
	
	/**
	* loads the properties of the item asyncronously.
	* @constructor
	* @param {number} itemID the id of the item.
	*/
	load: function(itemID, funCallBack, funParams) {
		if (this.body==null) {
			thisObj = this;
			new Ajax.Request(base_url+'item/showDetailPage/'+itemID, {
				onSuccess: function(response) {
					new Insertion.Bottom($('itemContent'), response.responseText);
					thisObj.body = $('generalPage');
					thisObj.link = $('generalLink');
				
					if (funCallBack)
						funCallBack(funParams);
				}
			});
		}
	},
	
	/**
	* unloads the properties of the item and reset the general page fields.
	*/
	unload: function() {
		if (this.body!=null) {
			this.link=null;
			this.body=null;
		}
	}
}

/**
* @namespace The photos page  of the ItemTab
*/
ItemTab.photosPage = {
	/** current page's link element */
	link: null,
	/** element that contains the content of the page */
	body: null,
	
	/**
	* loads the properties of the item asyncronously.
	* @constructor
	* @param {number} itemID the id of the item.
	*/
	load: function(itemID, funCallBack, funParams) {
		if (this.body==null) {
			thisObj = this;
			new Ajax.Request(base_url+'item/showPhotosPage/'+itemID, {
				onSuccess: function(response) {
					new Insertion.Bottom($('itemContent'), response.responseText);
					thisObj.body = $('photosPage');
					thisObj.link = $('photosLink');
				
					if (funCallBack)
						funCallBack(funParams);
				}
			});
		}
	},
	
	/**
	* unloads the properties of the item and reset the general page fields.
	*/
	unload: function() {
		if (this.body!=null) {
			this.link=null;
			this.body=null;
		}
	}
}