<?php
	class unitsFactory extends mainFactory{
		var $factory;
		var $unitSelected;
		var $unitPath;
		
		var $uipath;
		var $jspath;
		var $varpath;
		var $uilayout;
		var $mgrpath;
		
		public function __construct() {
		 $this->factory=mainFactory::getInstance();
		 
		}
		public function setUnit($unitName) {
			$this->unitSelected=$unitName;
			if (DEV_MODE==true) {
				$this->unitPath=DIR_UNITS.$unitName.DIRECTORY_SEPARATOR;
				$this->uipath=$this->unitPath."ui".DIRECTORY_SEPARATOR;
				$this->jspath=$this->unitPath."js".DIRECTORY_SEPARATOR;
				$this->varpath=$this->unitPath."var".DIRECTORY_SEPARATOR;
				$this->uilayout=$this->unitPath."layout".DIRECTORY_SEPARATOR;
				$this->mgrpath=$this->unitPath."manager".DIRECTORY_SEPARATOR;
			} else {
				$this->unitPath=DIR_UNITS.DIRECTORY_SEPARATOR;
				$this->uipath=$this->unitPath;
				$this->jspath=$this->unitPath;
				$this->varpath=$this->unitPath;
				$this->uilayout=$this->unitPath;
				$this->mgrpath=$this->unitPath;
			}
			return true;
		}
		public function load($directorName="index") {
			$className=strtolower($this->unitSelected);
			$classFile=$this->unitPath.$this->unitSelected.".director.php";
			
			if (!isset($this->vars[$className])) {
				if (!$this->loadCode($classFile,$className)) die();
				$this->vars[$className]=& instantiate_class(new $className());
				if (method_exists($this->vars[$className],$directorName)) {
					$this->vars[$className]->$directorName();
					return true;
				} else {
					echo "Invalid request";
					die();
				}
				
			}
		}
		public function loadReference($unitname) {
			$className=strtolower($unitname);
			$classFile=DIR_UNITS.$className.DIRECTORY_SEPARATOR.$className.".director.php";
			if (!DEV_MODE) $classFile=DIR_UNITS.$className.".director.php";
			if (!isset($this->vars[$className])) {
				if (!$this->loadCode($classFile,$className)) return false;
				$this->vars[$className]=& instantiate_class(new $className());
			}
			return true;
		}
		public function loadManager($managerName,$functionName="index",$path="") {
			$className=strtolower($managerName);
			if ($path=="") $path=$this->mgrpath.$this->unitSelected;
			$classFile=$path.".manager.".$className.".php";
			
			if (!isset($this->vars[$className])) {
				if (!$this->loadCode($classFile,$className)) die();
				$this->vars[$className]=& instantiate_class(new $className());
				if (method_exists($this->vars[$className],$functionName)) $this->vars[$className]->$functionName();
				return true;
			}
			return false;
		}
		public function loadUI($uilist) {
			$this->factory->UI=$uilist;
			return true;
		}
		public function loadUILayout($layoutname) {
			$this->loadMultiCode($this->uilayout,"layout.",$layoutname);
		}
		public function loadJS($jslist) {
			$this->factory->JS=$jslist;
			return true;
		}
		
		public function loadJSVar($jsvar) {
			$this->factory->JSVar=$jsvar;
			return true;
		}
		public function loadVar($jsvar) {
			$this->factory->JSVar=$jsvar;
			return true;
		}
		public function loadContentUI() {
			if ($this->factory->UI != "")
				$this->loadMultiCode($this->uipath,$this->unitSelected.".ui.",$this->factory->UI);
		}
		public function loadContentShared($sharedname) {
			if (DEV_MODE==true) {
				$f=DIR_UNITS."shared".DIRECTORY_SEPARATOR."shared.".$sharedname.".php";
				$this->loadMultiCode(DIR_UNITS."shared".DIRECTORY_SEPARATOR,"shared.",$sharedname);
			} else {
				$f=DIR_UNITS.DIRECTORY_SEPARATOR."shared.".$sharedname.".php";
				$this->loadMultiCode(DIR_UNITS.DIRECTORY_SEPARATOR,"shared.",$sharedname);
			}
		}
		public function loadContentJS() {
			if ($this->factory->JS != "") {
				$this->loadMultiCode($this->jspath,$this->unitSelected.".",$this->factory->JS,true,"js");
			}
		}
		public function loadContentJSVar() {
			if ($this->factory->JSVar != "") {
				$this->loadMultiCode($this->varpath,$this->unitSelected.".var.",$this->factory->JSVar);
			}
		
		}
	}
?>