<?php
class TabHandler {
	
    var $TAB_CONTROL = array();
	var $idx = 0;	
	var $default = "";
	var $tabname = "tab_";
	var $firsttab = true;
	var $commonid = "";	

	function __construct ($id)
		{
		global $wb;	
		$this->commonid=$id;
		$this->tabname = $id."_";
		}

	function AddHTML ($html)
		{
		$tmp = array();
		$tmp['code']=$html;
		$tmp['tabmode'] = false;
		array_push ($this->TAB_CONTROL,$tmp);
		}

	function NewTabContent ($id)
		{
		$tmp = "\n</div>\n";
		$tmp .='<div id="'.$this->tabname.$id.'" class="tab_hidden">'."\n";
		return $tmp;
		}	

	function Add ($id,$caption,$class="dummy")
		{
		$tmp = array();
		$tmp['id'] = $this->tabname.$id;
		$tmp['caption'] = $caption;
		$tmp['tabmode'] = true;
		$listitem = file_get_contents(SENTRA_SCRIPT_CORE."tab_items.tpl");
		
		$listitem = str_replace("{{CLASS}}",$class,$listitem);
		$listitem = str_replace("{{CAPTION}}",$caption,$listitem);
		$listitem = str_replace("{{DEFAULT}}",$this->default,$listitem);
		$listitem = str_replace("{{IDX}}",$this->idx,$listitem);
		
		$this->idx++;
		
		if ($this->firsttab)
			{			
			$this->firsttab = false;
			$content = file_get_contents(SENTRA_SCRIPT_CORE."tabs_common.tpl");	
			$content .= file_get_contents(SENTRA_SCRIPT_CORE."script_template.tpl");
			$content .= '
			<div id="'.$this->commonid.'" class="tab_hidden">
			';
			$content = str_replace("{{ID}}",$this->commonid,$content);
			$tmp['code']=$content;
			$tmp['id']="";
			$listitem = str_replace("{{CURRENT_ID}}",$this->commonid,$listitem);
			$listitem = str_replace("{{ID}}",$this->commonid,$listitem);
			}
		else
			{
			$listitem = str_replace("{{ID}}",$this->commonid,$listitem);
			$listitem = str_replace("{{CURRENT_ID}}",$this->tabname.$id,$listitem);
			$content = '
			</div>
			<div id='.$this->tabname.$id.'" class="tab_hidden">
			';			
			$tmp['code']=$content;
			$tmp['id']=$id;
			}
		$tmp['list']= $listitem;
		array_push ($this->TAB_CONTROL,$tmp);		
		}
function CloseAll()
		{
		return "\n</div>\n";
		}

	function CloseTabs()
		{
		$tmp = array();
		$tmp['tabmode']=false;
		$tmp['code']='
		</div>
';		
		array_push ($this->TAB_CONTROL,$tmp);
		}

	function Dispose()
		{
		$this->TAB_CONTROL=array();
		}

	function GetAll()
		{
		$content = "";
		if (count($this->TAB_CONTROL) > 0)
			{
			for ($x=0;$x < count($this->TAB_CONTROL);$x++)
				{
				if ($x == 0)
					$content .= $this->GetListCode();
				else
					{
					$content .= $this->TAB_CONTROL[$x]['code'];
					}
				}
			}
		return ($content);
		}

	function GetListCode()
		{
		$content = "";
			if (count($this->TAB_CONTROL) > 0)
			{
			$list = ""; $main_tab = ""; $list_id = array(); $cont_id = array();
			for ($x=0;$x < count($this->TAB_CONTROL);$x++)
				{
				if ($this->TAB_CONTROL[$x]['tabmode'])
					{
					if ($x == 0)
						{								
						$content = $this->TAB_CONTROL[0]['code'];					
						$list .= $this->TAB_CONTROL[0]['list'];	
						array_push($list_id,'"str_'.$this->commonid.$this->TAB_CONTROL[$x]['id'].'"');
						array_push($cont_id,'"'.$this->commonid.$this->TAB_CONTROL[$x]['id'].'"');	
						}
					else
						{
						if ($this->TAB_CONTROL[$x]['tabmode'])
							$list .= $this->TAB_CONTROL[$x]['list'];
						array_push($list_id,'"str_'.$this->tabname.$this->TAB_CONTROL[$x]['id'].'"');
						array_push($cont_id,'"'.$this->tabname.$this->TAB_CONTROL[$x]['id'].'"');	
						}
								
					}
				}
			$content = str_replace("{{LID}}",implode(',',$list_id),$content);
			$content = str_replace("{{CID}}",implode(',',$cont_id),$content);
			$content = str_replace("{{TAB_LIST}}",$list,$content);			
			}
		return ($content);
		}

	function GetTabArray ()
		{
		return ($this->TAB_CONTROL);
		}

	function GetLastContent ()
		{
		return ($this->TAB_CONTROL[count($this->TAB_CONTROL)-1]['code']);
		}
}
?>