<?php

class TabFolder extends View {



	protected $package = 'core-containers';
	protected $idRequired = true;



	public function __construct($id) {
		parent::__construct($id);
		$this->inner = new TabView($id . TabView::SUFFIX);
	}



	public function getInnerView() {
		return $this->inner;
	}



	public function selectTab($id) {
		foreach ($this as $child) {
			$child->unselect();
		}
		if (!isset($this->children[$id])) {
			throw new Exception("no such tab $id");
		}
		$this->children[$id]->select();
	}



	public function getSelectedTab() {
		$selected = null;
		foreach ($this as $child) {
			if ($child->isSelected()) {
				$selected = $child;
				break;
			}
		}
		if (!$selected and !empty($this->children)) {
			$selected = current($this->children);
			$selected->select();
		}
		return $selected;
	}



	public function __toString() {
		$innerId = $this->inner->getId();
		$all = array();
		foreach ($this as $child) {
			$all[] = "'" . $child->getId() . "'";
		}
		$tabs = '[' . implode(', ', $all) . ']';

		foreach ($this as $child) {
			$a = Action::buildAction($child->action);
			$actionString = $a->getActionString();
			$args = '[' . implode(', ', $a->getArgs()) . ']';
			$child->addListener(new ClickListener(new Action(
				$this->package . '/TabFolderCtrl/switchTab', array("'$innerId'", "'$actionString'", $args)
			)));
			$child->addListener(new ClickListener(new Behaviour(
				"TabFolder.switchTab($tabs, '{$child->getId()}', '{$child->class}-selected', '{$child->class}')"
			)));
		}

		$st = $this->getSelectedTab();
		$this->inner->add($st->getView());
		return parent::__toString();
	}



} // class TabFolder

?>