var tabView_enableDebug = true;

function tabView_clickTab(tab) {
	var clickedTabName = tab.getAttribute('name');
	var tabViewRoot = tabView_getRoot(tab);
	var tabs = tabView_getTabs(tabViewRoot);
	var views = tabView_getViews(tabViewRoot);
	debugPrint('clickedTabName='+clickedTabName+" tabs="+tabs+" views="+views, 'tabView');

	for(var i = 0; i < tabs.childNodes.length; i++) {
		if(tabs.childNodes[i].nodeType == Node.ELEMENT_NODE) {
			var tabName = tabs.childNodes[i].getAttribute('name');
			if(tabName) {
				var view = tabView_getView(views, tabName);
//				debugPrint('clickedTabName='+clickedTabName+" tabName="+tabName, 'tabView');
				if(tabName == clickedTabName) {
//					css_addClass(tabs.childNodes[i], 'tab');
					css_addClass(tabs.childNodes[i], 'active');
//					tabs.childNodes[i].className = "tab active";
//					view.className = "view active";
//					css_addClass(view, 'view');
					css_addClass(view, 'active');
				} else {
//					css_addClass(tabs.childNodes[i], 'tab');
					css_removeClass(tabs.childNodes[i], 'active');
//					css_addClass(view, 'view');
					css_removeClass(view, 'active');
				}
			}
		}
	}

	var dlg = dialog_getContainingDialog(tab);
	if(dlg && dlg.draggable) {
		debugPrint('dlg.draggable='+dlg.draggable, 'tabView');
		if(dlg && dlg.draggable) dlg.draggable.updatePosition();	// pitää huolen että frame näkyy kokonaan ruudulla (jos framen koko sattui muuttumaan tabin klikkauksen johdosta)
	}
}

function tabView_getFrame(el) {
	var frame = null;
	var safe = 16;
	while(el && safe--) {
		if(el.nodeType == Node.ELEMENT_NODE) {
			if(css_hasClass(el, 'frame') && css_hasClass(el, 'fixed')) {
				frame = el;
				break;
			}
		}
		el = el.parentNode;
	}
	debugPrint('tabView_getFrame='+frame, 'tabView');
	return frame;
}

function tabView_getRoot(el) {
	var root = null;
	var safe = 256;
	while(el && safe--) {
		if(el.nodeType == Node.ELEMENT_NODE) {
			if(css_hasClass(el,'tabView')) {
				root = el;
				break;
			}
		}
		el = el.parentNode;
	}
	return root;
}

function tabView_getTabs(root) {
	var tabs = null;
	if(root && root.childNodes) {
		for(var i = 0; i < root.childNodes.length; i++) {
			var check = root.childNodes[i];
			if(check.nodeType == Node.ELEMENT_NODE) {
				if(css_hasClass(check,'tabs')) {
					tabs = check;
					break;
				} else {
					if(check.hasChildNodes() && tabView_getTabs(check)) {
						tabs = tabView_getTabs(check);
						break;
					}
				}
			}
		}
	}
	return tabs;
}

function tabView_getViews(root) {
	var views = null;
	if(root && root.childNodes) {
		for(var i = 0; i < root.childNodes.length; i++) {
			var check = root.childNodes[i];
			if(check.nodeType == Node.ELEMENT_NODE) {
				if(css_hasClass(check,'views')) {
					views = check;
					break;
				} else {
					if(check.hasChildNodes() && tabView_getViews(check)) {
						views = tabView_getViews(check);
						break;
					}
				}
			}
		}
	}
	return views;
}

function tabView_getView(views, viewName) {
	var view = null;
	if(views && views.childNodes) {
		for(var i = 0; i < views.childNodes.length; i++) {
			var checkView = views.childNodes[i];
			if(checkView.nodeType == Node.ELEMENT_NODE) {
				var checkViewName = checkView.getAttribute('name');
				if(checkViewName == viewName) {
					view = checkView;
					break;
				}
			}
		}
	}
	return view;
}

