/**
 * @author john
 */
function tabManager() {
	this.menutabs = new Array();
	
	this.openUrl = function(title, url, system) {
		var tab = new YAHOO.widget.Tab({
			label: this.getTabLabel(title, system),
			content: this.getIframeCode(url),
			active: true
		});
		tab.url = url;
		tab.title = title;
		tab.sys = system;
		
		this.addTab(url, tab);
		
		return false;
	}
	
	this.addTab = function(id, tab) {
		if(this.menutabs[id]) {
			this.tabTo(id);
		} else {
			this.menutabs[id] = tab;
			tabView.addTab(tab);
			var tid = id;
			tab.addListener('click', function(e) {
				if(e.target.tagName == "SPAN") {
					if(e.target.id == "close") {
						TM.removeTab(tid);
					}
					
					if(e.target.id == "save") {
						TM.saveTab(TM.menutabs[tid]);
					}
					
					if(e.target.id == "clone") {
						window.open(tab.url);
					}
				}
			});
		}
		tab.id = id;
		prepareActions(tab);
		
		if(!tab.sys) {
			this.logTab(tab);
		}
	}
	
	this.tabTo = function(id) {
		tabView.set('activeIndex', tabView.getTabIndex(this.menutabs[id]));
	}
	
	this.removeTab = function(id) {
		this.clearTabProperties(id);
		tabView.removeTab(this.menutabs[id]);
		delete(this.menutabs[id]);
		map.checkResize();
	}
	
	this.clearTabProperties = function(id) {
		if(this.menutabs[id].parent) {
			for(var n in this.menutabs[id].parent.properties) {
				delete(this.menutabs[id].parent.properties[n]);
			}
			delete(this.menutabs[id].parent);
		}
	}
	
	this.getIframeCode = function(url) {
		return "<iframe width='100%' height='540' frameborder='0' src='" + url + "'></iframe>";
	}
	
	this.getTabLabel = function(title, system) {
		var output = '<table class="tabcontrol" cellpadding="0" cellspacing="0"><tr><td>' + title + '</td>';
		
		if(!system) {
			output += '<td><a title="Add to Itinerary" href="javascript: nothing()" style="background: none;"><span id="save"></span></a></td><td><a title="Clone this tab in a new window" href="javascript: nothing()" style="background: none;"><span id="clone"></span></a></td>';
		}
		
		output += '<td><a title="Close this tab" href="javascript: nothing()" style="background: none;"><span id="close"></span></a></td></tr></table>';
		
		return output;
	}
	
	this.saveTab = function(tab) {
		var tblob = '';
		
		if(tab.parent) {
			tblob += objToParams(tab.parent.properties) + "&actions=" + URLEncode(objToParams(tab.parent.parent.actions));
			tblob += "&type=" + URLEncode(tab.parent.parent.type);
		} else {
			tblob += "title=" + URLEncode(tab.title) + "&url=" + URLEncode(tab.url);
		}
		
		RM.lastReq = baseURL + "mygarage/savetab.php?" + tblob;
		var request = YAHOO.util.Connect.asyncRequest('GET', baseURL + "mygarage/savetab.php?" + tblob, {success: evaluator, failure: nothing});
	}
	
	this.logTab = function(tab) {
		var tblob = '';
		
		if(tab.parent) {
			tblob += objToParams(tab.parent.properties) + "&actions=" + URLEncode(objToParams(tab.parent.parent.actions));
			tblob += "&type=" + URLEncode(tab.parent.parent.type);
		} else {
			tblob += "title=" + URLEncode(tab.title) + "&url=" + URLEncode(tab.url);
		}
		
		RM.lastReq = baseURL + "mygarage/logtab.php?" + tblob;
		var request = YAHOO.util.Connect.asyncRequest('GET', baseURL + "mygarage/logtab.php?" + tblob, {success: nothing, failure: nothing});
	}
	
	this.doAction = function(action, tabid, title) {
		switch(action) {
			case "save":
				this.saveTab(this.menutabs[tabid]);
				break;
			default:
				var url = baseURL + "action.php?action=" + action + objToParams(this.menutabs[tabid].parent.properties);
				url += "&type=" + this.menutabs[tabid].parent.parent.type;
				this.openUrl(title, url, 1);
		}
	}
}

function openSavedTab(title, url, iObj) {
	if(iObj && iObj.properties.url) {
		url = iObj.properties.url;
	}
	
	if(url.substr(0,6) == 'go.php') {
		url = baseURL + url;
	}
	
	var tab = new YAHOO.widget.Tab({
		label: TM.getTabLabel(trunc(title, 23)),
		content: TM.getIframeCode(url),
		active: true
	});
	tab.url = url;
	tab.parent = iObj;
	TM.addTab("saved" + url, tab);
	
	return false;
}

TM = new tabManager();