/* 
	IFRAME TAB NAVIGATION 
	$Id: framenav.js,v 1.12 2007/03/22 23:49:27 mok Exp $
	* keeps track of all the tab highlights and associated iframe
*/

FrameNavigation = Class.create();

Object.extend(FrameNavigation, new amicus.StateManager("amicus.gui.save",1));


// define object
FrameNavigation.prototype = {
	
	// required using prototype library (needs to have this method)
	initialize : function(navId,frameId,height) {
		
		
		this.id = navId;
		this.frameId = $(frameId);
		
		var url = window.location.toString().split('section:');
		
		this.queryParam = url[1];
				
		// * loops through all LI's and A's and create tabs for them
		
		this.tabs = $A($(navId).getElementsByTagName('ul')[0].getElementsByTagName('li')).collect(this.createTab);
		
		// adding an "handleTabClick" method to each tab		
		this.tabs.each(function(tab) {
			Event.observe(tab.control, "click", this.handleTabClick.bindAsEventListener(this));
		}.bind(this));
		
		// observe the iframe to control tab highlight
		Event.observe(this.frameId, "load", this.handleFrameEvent.bindAsEventListener(this));
	
		this.loadSelectedTab();			
		
	},
		
	// watches the mouse event and gets the href of the link 
	handleTabClick	:	function(evt) {
		var tabLocation = Event.element(evt).href;
		this.highlightTab(tabLocation);
		this.setSelectedTab(tabLocation);
	},
	
	// highlight a tab if the target href of link matches the location of the iframe html file
	highlightTab : 	function (tabLocation) {
		
		tabLocation = tabLocation.toString();	
		
		this.tabs.each(function(tab){
			if(tabLocation.indexOf('?') > -1 ) {
				tab.element.className = (tab.location == tabLocation)? 'selected' : '';
			}
		}.bind(this));
	},
	
	// sets the currentTabTarget of the tab to the target of the current highlighted tab
	setSelectedTab :	function(tabLocation) {
		return (this.currentTabTarget = tabLocation);
	},
	
	loadSelectedTab :	function() {
		// var savedTab = FrameNavigation.getCookieParam(this.id);	// get cookie 
		// if query param (with "!") set default state of tabs (whatever HREF is set to in iframe include)
		/* if(this.queryParam && this.queryParam.indexOf('!') > -1) {
			return;
		} else  */
		/* if cookie
		if(savedTab) {
			this.highlightTab(savedTab);
			if (this.getFrameLocation() == savedTab) { return; }
			this.setFrameLocation(savedTab);
		} else */
		// if query param (with no "!")
		if(this.queryParam) {
			this.setTabFromQuery(this.queryParam);
		} 
		//get whatever frameLocation and highlight matching tab
		else {		
			selectedLoc = this.getFrameLocation();
			this.highlightTab(selectedLoc);
		}
	},
	
	// if url query
	setTabFromQuery : function(query) {
		
		/* if query matches the ID of a tab, set the tab highlight to matching tab id */
		var notab = this.tabs.detect(function(tab) {
			currentTabTarget =  tab.location;															
			return tab.elementID == query;					
		});
		
		/* if tab query doesn't match anything */
		if(!notab) { return; }
		
		// set highlighted tab to match the tab location
		this.highlightTab(currentTabTarget);	
		
		// if there's a cookie that matches current frame location, override the query
		// var savedTab = FrameNavigation.getCookieParam(this.id);		
		// if (this.getFrameLocation() == savedTab) { return; }
		
		this.setFrameLocation(currentTabTarget);
	},

	
	// get current frame location
	getFrameLocation : function() {
		return this.frameId.contentWindow.location;
	},
	
	// set current frame loc to current tab target
	setFrameLocation : function (currentTabTarget) {
		this.frameId.contentWindow.location = currentTabTarget; 
	},
	
	// watches frame load event
	handleFrameEvent	: function(evt) {
		
		// setting the pathname location of the iframe window
		var iframeLoc = this.getFrameLocation();	
		// highlight the tab with matching frame location href
		this.highlightTab(iframeLoc);		
		
		// set href of current href if set from cookie or not
		var iframeHref = this.frameId.contentWindow.location.href;	
		this.frameLocation = iframeHref || '';
				
	},
	
	// keep track of list elements and their links
	createTab : function (li) {
		var a = li.getElementsByTagName('a')[0];
		//var a = li.getElementsByTagName('a')[0];
		return { 
			element 	: 	li,
			elementID	:		li.id,
			control		: 	a,
			location 	: 	a.href
		};
	}
};