<?php

class TopView extends Slot {

	var $ModuleID = 1;
	var $SectionID = 2;
	var $city = 0;

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

	function init() {
		global $REGISTRY;
		if(!empty($REGISTRY['Page']['id'])) {
			$this->Action->processEvent($this->SectionID,'open',array('id' => $REGISTRY['Page']['id']),$this);
			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);
				}
			}
		}
		else $this->PreparedTemplates['contents'] = array();
		$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']==2) $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']==2) $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);
			}
		}
		else {
			foreach($this->PreparedTemplates['contents'] as $value)
				if($value['type']==2) $this->TempFillEls['describe'][] = array('value' => $value['value']);
		}
	}
}
?>