<?php

	class FNTHWidgetTop extends FNTHWidget {

		public function __construct( $id , $parent = NULL ) {
			parent::__construct( $id , $parent );
			// $this->bDesignCache = false;
		}

		protected function dcGetDesignCacheFilename( $postfix , $www = true ) {
			$retval = ($www) ? $GLOBALS["site_www"] : $GLOBALS["site_root"];
			$retval.="/templates_c/";
			switch ($postfix) {
			case ".css":$retval.="css_";break;
			case ".js":$retval.="js_";break;
			}
			$retval.=$this->sDesignCachePrefix.$postfix;
			return $retval;
		}

		protected function storeInheritedDesignFilesCache() {
			if (!$this->oParent) return;
			return parent::storeInheritedDesignFilesCache();
		}

		protected function loadAllInheritedDesignFilesFromCache() {
			if (!$this->oParent) return false;

			$retval = parent::loadAllInheritedDesignFilesFromCache();
			if (!$retval) return $retval;
			// post up to parent
			if (!$this->oParent) return $retval;

			if (!empty( $this->aDesignCacheCSS ))
				foreach ($this->aDesignCacheCSS as $k => $v)
					$this->oTopParent->addDesignFileToCache(
						$v["postfix"],
						$v["rfilename"],
						$v["wfilename"],
						$v["callback"],
						$v["callback_www"] );
			if (!empty( $this->aDesignCacheJS ))
				foreach ($this->aDesignCacheJS as $k => $v)
					$this->oTopParent->addDesignFileToCache(
						$v["postfix"],
						$v["rfilename"],
						$v["wfilename"],
						$v["callback"],
						$v["callback_www"] );
			if (!empty( $this->aDesignCacheHead ))
				foreach ($this->aDesignCacheHead as $k => $v)
					$this->oTopParent->addDesignFileToCache(
						$v["postfix"],
						$v["rfilename"],
						$v["wfilename"],
						$v["callback"],
						$v["callback_www"] );
			return true;
		}

		protected function dcGenerateDesignFiles( ) {
			$all_js = "";
			$all_css = "";
			$debug_css = "";
			if (!empty( $this->aDesignCacheJS )) {
				foreach ($this->aDesignCacheJS as $f) {
					if (!file_exists( $f["rfilename"] )) continue;
					$all_js.=file_get_contents( $f["rfilename"] );
				}
			}
			if (!empty( $this->aDesignCacheCSS )) {
				foreach ($this->aDesignCacheCSS as $f) {
					$debug_css.=sprintf( "%d %s\n" , file_exists( $f["rfilename"] ) , $f["rfilename"] );
					if (!file_exists( $f["rfilename"] )) continue;
					$all_css.=file_get_contents( $f["rfilename"] );
				}
			}

			$all_css = "/* \n " . $debug_css . "*/\n" . $all_css;
			$css_file = $this->dcGetDesignCacheFilename( ".css" , false );
			file_put_contents( $css_file , $all_css );
			$js_file = $this->dcGetDesignCacheFilename( ".js" , false );
			file_put_contents( $js_file , $all_js );
		}

		protected function dcGetDesignCachePrefix() {
			$cache_keys = array_merge( array_keys( $this->aDesignCacheJS ) , array_keys( $this->aDesignCacheCSS ) );
			asort( $cache_keys );
			return md5( implode( "," , $cache_keys ) );
		}

		protected function designCacheToPage() { 
			// nem taroljuk
		}

		protected function addDesignFileToCache( $postfix , $rfilename , $wfilename , $callback , $callback_www ) {
			if (isset( $this->oParent)) {
				parent::addDesignFileToCache( $postfix , $rfilename , $wfilename , $callback , $callback_www );
				$this->oTopParent->addDesignFileToCache( $postfix , $rfilename , $wfilename , $callback , $callback_www );
				return;
			}
			parent::addDesignFileToCache( $postfix , $rfilename , $wfilename , $callback , $callback_www );
		}	

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->renderWidgets();
			$this->loadAllInheritedDesignFiles();

			if (!isset( $this->oParent )) {
				$this->sDesignCachePrefix = $this->dcGetDesignCachePrefix();
				if (!file_exists( $this->dcGetDesignCacheFilename( ".css" , false )) || isset( $_GET["FNTH_regen_template"] )) {
					$this->dcGenerateDesignFiles();
				}
				FNTHPage::setPageCSSHead( get_class( $this ) . "_css" ,
					$this->dcGetDesignCacheFilename( ".css" ) );
				FNTHPage::setPageJSHead( get_class( $this ) . "_js" ,
					$this->dcGetDesignCacheFilename( ".js" ) );
			}
			$css_attrs = FNTH_render_css_attributes( $this->oCSSAttrs );
			if (!empty($css_attrs)) $this->setAttribute( "style" , $css_attrs );
			$this->setParam( "attrs" , FNTH_render_html_attributes( $this->oAttrs ) );
			$filename = $this->getTemplateFilename();
			$this->sRendered = $this->renderFromTemplate( $filename );
			return $this->sRendered;
		}

	}


?>
