<?php

class RightView extends Slot {

	var $ModuleID = 1;
	var $SectionID = 2;
	var $module = 1;
	var $item = 1;
	var $menu_id = 0;
	var $city = 0;

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {
		global $REGISTRY;
		$this->Action->processEvent($this->SectionID,'open_menu',array('module' => $REGISTRY['Page']['id'],'type' => 6),$this);
		$this->Action->processEvent($this->SectionID,'open',array('id' => $REGISTRY['Page']['id']),$this);
		if(!empty($this->PreparedTemplates['_error']) || !$this->PreparedTemplates['is_describe']) header('Location: '.$this->Home.$this->SID);
		if($this->PreparedTemplates['type']['id']==9) {
			$this->city = 1;
			if(!empty($this->LocalTarget[1])) {
				$this->Action->processEvent($this->SectionID,'city',array('name' => $this->LocalTarget[1]),$this);
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		$this->TempFillEls['describe'] = array();
		if($this->city) {
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'city');
			if(empty($this->PreparedTemplates)) {
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
				foreach($this->PreparedTemplates['contents'] as $v) if($v['type']==6) $this->TempFillEls['describe'][] = array('value' => $v['value']);
			}
			else {
				$city = $this->PreparedTemplates[0];
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
				foreach($this->PreparedTemplates['contents'] as $v) if($v['type']==6) $template = $v['value'];
				foreach($city as $key => $value) {
					switch($key) {
						case 'state': $template = preg_replace('({city.'.$key.'})',$value['name'],$template); break;
						case 'area_codes': $template = preg_replace('({city.'.$key.'})',eregi_replace('{|}','',$value),$template); break;
						default: $template = preg_replace('({city.'.$key.'})',$value,$template); break;
					}
					unset($city[$key]);
				}
				$this->TempFillEls['describe'][] = array('value' => $template);
			}
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open_menu');
			$this->setTargetArray($this->PreparedTemplates,0.1);
			if(!empty($this->TempFillEls['menu'][0])) {
				$menu = $this->Db->SelectArray(array('menu'),'title,link',array('type' => 6,'id' => $this->menu_id));
				$this->TempFillEls['menu'][0]['name'] = !empty($menu[0]['link'])? makeLink($menu[0]['link'],$menu[0]['title'],array(),'','style="text-decoration:none"') : $menu[0]['title'];
			}
		}
		else {
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open_menu');
			$this->setTargetArray($this->PreparedTemplates,0.1);
			if(!empty($this->TempFillEls['menu'][0])) {
				$menu = $this->Db->SelectArray(array('menu'),'title,link',array('type' => 6,'id' => $this->menu_id));
				$this->TempFillEls['menu'][0]['name'] = !empty($menu[0]['link'])? makeLink($menu[0]['link'],$menu[0]['title'],array(),'','style="text-decoration:none"') : $menu[0]['title'];
			}
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
			$this->TempFillEls['describe'] = array();
			foreach($this->PreparedTemplates['contents'] as $value)
				if($value['type']==6) $this->TempFillEls['describe'][0] = array('value' => $value['value']);
		}
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open_menu');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'city');
	}

	function setTargetArray($array = array(),$level = 1,$subitems = array()) {
		$i = 0;
		foreach($array as $key => $value) {
			$this->menu_id = $value['ownerid'];
			$this->TempFillEls['menu'][0]['items'][$key] = array(
				'id' => $key,
				'url' => $value['path']=='/'.implode('/',$this->LocalTarget)? trim($value['label']) : makeLink($value['path'],trim($value['label']),array(),$value['anchor'],($value['target']? 'target="_blank"' : 'target="_top"')),
			);
			if(!empty($value['subcat'])) $this->setTargetArray($value['subcat'],$level+1);
			$i++;
		}
	}
}
?>