<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/Header.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendMenu.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/KeywordSearchForm.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/Footer.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Openads.inc.php" );

	function getGlobalFrontendPage() {
		if (!isset( $GLOBALS["FrontendPage"] )) return false;
		return $GLOBALS["FrontendPage"];
	}

	function GlobalWarningMessage( $id , $str ) {
		$page = getGlobalFrontendPage();
		if (!$page) return;
		$page->warningMessage( $id , $str );
	} 

	class FrontendPage extends Widget {
		protected $oHeader;
		protected $oKeywordSearchForm;
		protected $oFrontendMenu;
		protected $oBanners;
		protected $oDeluxeBanner;
		protected $oContentBanner;
		protected $oFooter;
		protected $aErrorMessages = array();

		protected $bPrint = false;

		protected $sPageType = "default";
		protected $sExtraClass = "";

		protected function setPageType( $v ) { $this->sPageType = $v; }

		public function setPrint( $v ) { $this->bPrint = $v; }

		public function renderContent() {
			$filename = $this->getTemplateFilename( ".content" );
			return $this->renderFromTemplate( $filename );
		}

		public static function addTicker( $tickerinfo ) {
			$p = getGlobalFrontendPage();
			$p->setTickerInfo( $tickerinfo );
		}
		
		public function setTickerInfo( $tickerinfo ) {
			$this->aTickerParams[$tickerinfo]=true;
		}

		public function __construct( $id = "FrontendPage" , $parent = null , $pagetype = "default" ) {
			if (!isset( $GLOBALS["FrontendPage"] )) $GLOBALS["FrontendPage"]=$this;
			if (isset( $_SESSION["frontend_error_messages"] )) {
				$this->aErrorMessages = $_SESSION["frontend_error_messages"];
				unset( $_SESSION["frontend_error_messages"] );
			}
			FNTHPage::setPageFullInit( "jslangdefs" , "" ); // hack (elol legyen az asszociativ tombben a jslangdefs, hogy mindenki tudja hasznalni)
			FNTHPage::setPageFullInit( "jsvars" , "" ); // hack (elol legyen az asszociativ tombben a jslangdefs, hogy mindenki tudja hasznalni)

			// megsem kell gemius
			/*
			FNTHPage::setPageHead( "gemius" , "<!-- (C)2000-2010 Gemius SA - gemiusAudience / fordit.hu / Minden_oldal -->
<script type=\"text/javascript\">
<!--//--><![CDATA[//><!--
var pp_gemius_identifier = new String('B3LgHaOq23KAbHC3N4NA4ZQJ.Kk6LLBJ5Sf15ob3VKf.h7');
//--><!]]>

</script>
<script type=\"text/javascript\" src=\"/design/rp/js/xgemius.js\"></script>" );
			*/


			$this->addMediaDirectory( $GLOBALS["site_root"] , $GLOBALS["site_www"] , "design/rp/","pages/");
			parent::__construct( $id , $parent );
			$this->setPageType( $pagetype );

			$this->oHeader = new Header( "Header" , $this );
			switch ($GLOBALS["language"]) {
			default:
			case "hu":
				$layer_id = 22;
				$deluxe_id = 162;
				$content_id = 164;
				break;
			case "en":
				$layer_id = 46;
				$deluxe_id = 170;
				$content_id = 171;
				break;
			case "de":
				$layer_id = 180;
				$deluxe_id = 185;
				$content_id = 186;
				break;
			}
			$this->oLayerBanner = new Openads( "adserver_22" , $this , $layer_id );
			$this->oDeluxeBanner = new Openads( "adserver_162" , $this , $deluxe_id );	// felso2
			$this->oContentBanner = new Openads( "adserver_164" , $this , $content_id ); // felso3
			$this->oKeywordSearchForm = new KeywordSearchForm( "KeywordSearchForm" , $this );
			$this->oKeywordSearchForm->setGoogle( $GLOBALS["fo3_freetext_google"] );
			$this->oFrontendMenu = new FrontendMenu( "FrontendMenu" , $this , $this->sPageType );
			$this->oFooter = new Footer( "Footer" , $this );
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "pager_Acknowledge_message" );
			fo3_add_js_langdef( "pager_No_target_user_selected" );
			fo3_add_js_langdef( "pager_Send" );
			fo3_add_js_langdef( "pager_Cancel" );
		}
		
		protected function getGlobalInfo() {
			$allusers = new AllUsersModel();
			$alljobs = new AllJobsModel();
			$globalinfo = array();
			if (isset( $this->aP["first"])) {
				$globalinfo["t_numofagencies"]=$allusers->getActiveAgencies();
				$globalinfo["t_numoffreelancers"]=$allusers->getActiveFreelancers();
				$globalinfo["t_numoftranslationjobs"]=$alljobs->getActiveJobs();
			}
			$globalinfo["t_online_users"]=$allusers->getOnlineUsers();
			$globalinfo["t_online_members"]=$allusers->getOnlineUsers( 1 );
			return $globalinfo;
		}


		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			switch ($this->aP["ajaxevent"]) {
			case "target_user_autocomplete":
				require_once( $GLOBALS["site_root"] . "/includes/models/Member.inc.php" );
				$mm = new MemberModel();
				$p = array( "filter" => $this->aP["name_filter"] );
				$users = $mm->getActiveMemberUsers( $p );

				$user_ids=array();foreach ($users as $user) $user_ids[]=$user["user_id"];
				$retval = array();
				require_once( $GLOBALS["site_root"] . "/includes/models/AllUsers.inc.php" );
				$aum = new AllUsersModel();
				$onlines = $aum->getUsersOnlineState( $user_ids );
				foreach ($users as $user) {
					$user["is_online"]=$onlines[$user["user_id"]];
					$retval[]=$user;
				}
				FNTH_echo_json_response( $retval , 0 , "fo3_exit" );
			case "send_pager_message":
				$mum = new MyUserModel();
				if ($mum->isUserLoggedIn()) {
					require_once( $GLOBALS["site_root"] . "/includes/models/PagerMessages.inc.php" );
					$pm = new PagerMessagesModel();
					$retval = $pm->postMessage(
						$mum->getLoggedInUserId(),
						$this->aP["target_user_id"],
						$this->aP["message"] );
				} else {
					$retval = false;
				}
				FNTH_echo_json_response( $retval , 0 , "fo3_exit" );
			case "ack_pager_message":
				$mum = new MyUserModel();
				if ($mum->isUserLoggedIn()) {
					require_once( $GLOBALS["site_root"] . "/includes/models/PagerMessages.inc.php" );
					$pm = new PagerMessagesModel();
					$retval = $pm->acknowledgeMessage(
						$mum->getLoggedInUserId(),
						$this->aP["pager_message_id"] );
				} else {
					$retval = false;
				}
				FNTH_echo_json_response( $retval , 0 , "fo3_exit" );
				break;
			case "refresh_infopanel":
				$mum = new MyUserModel();
				$retval = array();
				// -- global info 
				$retval["globalinfo"]=$this->getGlobalInfo();
				// -- chat messages
				if (isset( $this->aTickerParams["chat_messages"] )) {
					require_once( $GLOBALS["site_root"] . "/includes/models/Chat.inc.php" );
					$chat_model = new ChatModel();
					$last_chat_message_id = (isset( $this->aP["last_chat_message_id"] )) ?
						$this->aP["last_chat_message_id"] : $chat_model->getLastChatMessageId();
					$chat_model->keepaliveUser( $mum->getLoggedinUserId() );
					$retval["chat_messages"]=$chat_model->getChatMessages( $last_chat_message_id );

					if (isset( $this->aP["get_chatroom_users"] )) {
						$retval["chatroom_users"]=array();
						$retval["chatroom_users"][1]=$chat_model->getChatroomMembers( 1 );
					}
					// $retval["stop_callback"]=true;
				}
				// -- poster messages
				require_once( $GLOBALS["site_root"] . "/includes/models/PagerMessages.inc.php" );
				$pm = new PagerMessagesModel();
				if ($mum->isUserLoggedIn()) {
					$retval["pager_messages"]=$pm->getUserUnacknowledgedMessages( $mum->getLoggedInUserId() );
				}
				FNTH_echo_json_response( $retval , 0 , "fo_exit" );
			}
			return parent::handleEvents();
		}

		protected function generateLanguageEditorMode() {
			if (!fo3_get_language_editor_mode()) return;
			if (!isset( $GLOBALS["translation_needed"])) return;
			if (empty( $GLOBALS["translation_needed"])) return;
			$retval = "";
			ksort( $GLOBALS["translation_needed"] );
			foreach ($GLOBALS["translation_needed"] as $k => $v) {
				$url = sprintf( "/admin/langdefs/edit.php?mode=modify&uid=%s" , $k );
				$retval.=sprintf( "<a href=\"%s\">%s</a><br/>" ,
					$url , $v );
			}
			if (empty( $retval )) return;
			$retval=sprintf( "<div class=\"langdef_editlinks\"><div class=\"hdr\">Langdefs:</div><div class=\"cnt\">%s</div></div>", $retval );
			FNTHPage::setPageBodyEnd( "language_editor_langdefs" , $retval );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->setParam( "bDebug" , FNTHPage::isDebugPage() );
			$this->generateLangDefs();
			$this->renderWidgets();

			$this->setParam( "bPrint" , $this->bPrint );

			if ($this->bPrint) {

			} else {
				FNTHPage::setpageBodyAttributes( "class" , "FrontendPage" );
			}
	
			$datas = array();
			$datas["refresh_time"]=(isset( $this->aG["refresh"] )) ? $this->aG["refresh"] : 1000;
			FNTHPage::addUniquePageFullInit(
				sprintf( "InitFrontendPage( '%s' , %s );\n" , $this->getId() , json_encode( $datas ) ) );
			$this->setParam( "errormessage" , $this->renderErrorMessage() );
			$jslang = fo3_render_js_langdefs();
			if ($jslang) FNTHPage::setPageFullInit( "jslangdefs" , $jslang );
			$jsvars = fo3_render_js_variables();
			if ($jsvars) FNTHPage::setPageFullInit( "jsvars" , $jsvars );
			$this->setParam( "debug_log_queries" , (FNTHPage::isDebugPage()) ? FNTH_db_renderlog() : "" );
			$this->setParam( "content" , $this->renderContent() );
			$this->setParam( "extra_class" , $this->sExtraClass );
			$this->generateLanguageEditorMode();
			$content = parent::render();
			// FNTHPage::$bCache = true;
			if (isset( $_SESSION["scrolltothis"] )) {
				FNTHPage::addUniquePageFullInit( sprintf( "$.scrollTo('#%s',800,{});" , $_SESSION["scrolltothis"] ) );
				unset( $_SESSION["scrolltothis"] );
			}

			FNTHPage::setPageHead( "meta_description_head",
				sprintf( "<meta name=\"description\" content=\"%s\" />" , htmlspecialchars( $GLOBALS["fo3_meta_description"] ) ) );
			FNTHPage::setPageHead( "meta_keywords_head",
				sprintf( "<meta name=\"keywords\" content=\"%s\" />" , htmlspecialchars( $GLOBALS["fo3_meta_keywords"] ) ) );
			FNTHPage::setPageTitle( $GLOBALS["fo3_page_title"] );

			return FNTHPage::renderPage( $content );
		}

		public function setErrorMessage( $id , $str ) {
			$this->aErrorMessages[$id]=$str;
		}

		public function setSessionErrorMessage( $id , $str ) {
			if (!isset( $_SESSION["frontend_error_messages"] ))
				$_SESSION["frontend_error_messages"] = array();
			$_SESSION["frontend_error_messages"][$id]=$str;
		}

		public function renderErrorMessage() {
			return implode( "<br/>" , $this->aErrorMessages );
		}

		public static function warningMessage( $id , $str ) {
			$GLOBALS["FrontendPage"]->setErrorMessage( $id , $str );
		}

		public static function nextPageWarningMessage( $id , $str ) {
			$GLOBALS["FrontendPage"]->setSessionErrorMessage( $id , $str );
		}
	}

?>
