<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/TodoList.inc.php" );

	require_once( $GLOBALS["site_root"] . "/includes/urlgen/MemberSearch.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/RequestQuote.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/TranslatorJobs.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/News.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/Forum.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/Article.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/Blueboard.inc.php" );

	class FrontendMenu extends Widget {

		protected function generateMenuFromArray( $a , $level = 0 ) {
			$retval = "";
			$len = count( $a );
			for ($i=0;$i<$len;$i++) {
				$menuitem = $a[$i];
				$li="<li";
				// classes
				$classes=array();
				$classes[]=sprintf( "lev%d" , $level + 1 );
				if ($i==0) $classes[]="FM_first";
				if ($i+1==$len) $classes[]="FM_last";
				if (isset( $menuitem["extra_class"] )) $classes[]=$menuitem["extra_class"];

				if (count( $classes )) $li.=sprintf( " class=\"%s\"" , implode( " " , $classes ) );
				// styles
				$styles=array();
				if (isset( $menuitem["width"] )) $styles[]=sprintf( "width:%dpx;" , $menuitem["width"] );
				if (count( $styles )) $li.=sprintf( " style=\"%s\"" , implode( " " , $styles ) );
				$li.=">";
				$li.=sprintf( "<a href=\"%s\" title=\"%s\">%s</a>",
					$menuitem["url"],
					$menuitem["title"],
					$menuitem["title"] );
				if (isset( $menuitem["submenu"] ))
					$li.=sprintf( "<ul>%s</ul>",$this->generateMenuFromArray( $menuitem["submenu"] , $level + 1 ) );
				$li.="</li>";
				if ($i+1<$len && !$level) {
					$li.="<li class=\"sep\"><img src=\"/images/blank.gif\" alt=\"blank\" /></li>";
				}
				$retval.=$li;
			}
			return $retval;
		}

		protected function generateFrontendMenu() {
			$menu = array();
			$menu[] = array(
				"pagetype" => "translators",
				"title" => __("menu_Translators","attr"),
				"url" => MemberSearchURL::renderURL(),
				"width" => __("_width_menu_Translators","attr" ) );
			$menu[] = array(
				"pagetype" => "request_a_quote",
				"title" => __("menu_Request_a_quote","attr"),
				"url" => RequestQuoteURL::renderURL(),
				"width" => __("_width_menu_Request_a_quote","attr" ) );
			$menu[] = array(
				"pagetype" => "knowledge_base",
				"title" => __("menu_Knowledge_base_guide","attr"),
				"url" => ArticleURL::renderURLFromArticleName( "about_translation" ),
				"width" => __("_width_menu_Knowledge_base_guide","attr" ) );
			/*
			$menu[] = array(
				"pagetype" => "translation_jobs",
				"title" => __("menu_Translation_jobs","attr" ),
				"url" => TranslatorJobsURL::renderURL(),
				"width" => __("_width_menu_Translation_jobs","attr" ) );
			$menu[] = array(
				"pagetype" => "discounts",
				"title" => __("menu_Discounts","attr" ),
				"url" => "/cikkek/kedvezmenyek/",	// FIXME ide majd kell a hungarian-translationos megfeleloje is
				"width" => __("_width_menu_Discounts","attr" ));
			*/
			if ($GLOBALS["language"]=="hu") {
				$menu[] = array(
					"pagetype" => "news",
					"title" => __("menu_News","attr"),
					"url" => NewsURL::renderURL(),
					"width" => __("_width_menu_News","attr" ));
			}
			/*
			$menu[] = array(
				"pagetype" => "forum",
				"title" => __("menu_Forum","attr"),
				"url" => ForumURL::renderURL(),
				"width" => __("_width_menu_Forum","attr") );
			*/
			return $menu;
		}

		protected function generateMessagesSubMenu() {
			$menu = array();
			$menu[] = array(
				"pagetype" => "messages",
				"title" => __("Messages_Inbox","attr" ),
				"url" => "/uzenetek/",
				);
			$menu[] = array(
				"pagetype" => "messages",
				"title" => __("Messages_Sent","attr" ),
				"url" => "/uzenetek/",
				);
			$menu[] = array(
				"pagetype" => "chat",
				"title" => __("Chat","attr" ),
				"url" => "/chat/",
				);
			$menu[] = array(
				"pagetype" => "pager",
				"title" => __("Pager","attr" ),
				"url" => "javascript:PagerMessageForm();"
				);
			return $menu;
		}

		protected function generateMemberMenu() {
			$this->my_member_model = new MyMemberModel();
			$this->member_model = new MemberModel();
			$this->my_todo_model = new TodoListModel();	

			$usermenu = array();
			$usermenu[] = array(
				"pagetype" => "my_member_my_profile",
				"title" => __("menu_My_member_My_profile","attr"),
				"url" => MyMemberURL::renderMyProfileURL(),
				"width" => __("_width_menu_My_member_My_profile","attr" ),
				"submenu" => $this->my_member_model->getMenu() );

			$todocount = $this->my_todo_model->getUserTodoListCount( $this->my_member_model->getLoggedInUserId() );
			$menuitem = array();
			$menuitem["pagetype"]="my_user_status";
			$menuitem["title"]=__("menu_My_user_Status","attr");
			$menuitem["url"]=MyMemberURL::renderMyStatusURL();
			$menuitem["width"]=__("_width_menu_My_user_Status","attr");
			if ($todocount) {
				$menuitem["extra_class"]="menuitemred";
				$menuitem["title"].=sprintf( " (%d)" , $todocount );
			}

			$usermenu[] = $menuitem;

			$usermenu[] = array(
				"pagetype" => "translation_jobs",
				"title" => __("menu_Translation_jobs","attr" ),
				"url" => TranslatorJobsURL::renderURL(),
				"width" => __("_width_menu_Translation_jobs","attr" ) );
			/*
			$usermenu[] = array(
				"pagetype" => "my_member_messages",
				"title" => __("menu_My_member_Messages","attr"),
				"url" => MyMemberURL::renderMyMessagesURL(),
				"width" => __("_width_menu_My_member_Messages","attr"),
				"submenu" => $this->generateMessagesSubMenu() );
			*/
			$usermenu[] = array(
				"pagetype" => "my_user_quote_requests",
				"title" => __("menu_My_member_Quote_requests","attr"),
				"url" => MyMemberURL::renderMyQuoteRequestsURL(),
				"width" => __("_width_menu_My_member_Quote_requests","attr") );
			$usermenu[] = array(
				"pagetype" => "my_member_quotes",
				"title" => __("menu_My_member_Quotes","attr"),
				"url" => MyMemberURL::renderMyQuotesURL(),
				"width" => __("_width_menu_My_member_Quotes","attr") );
			$usermenu[] = array(
				"pagetype" => "my_member_blueboard",
				"title" => __("menu_My_member_Blueboard","attr"),
				"url" => MyUserURL::renderFeedbackURL(),
				"width" => __("_width_menu_My_member_Blueboard","attr") );
			$member = $this->member_model->getMemberFromMemberId( $this->my_member_model->getLoggedinMemberId( ) );
			if ($member["is_forditmail"]=="t") {
				$usermenu[] = array(
					"pagetype" => "my_member_webmail",
					"title" => __("menu_My_member_Webmail","attr"),
					"url" => "/forditmail/",
					"width" => __("_width_menu_My_member_Webmail","attr" ) );
			}
			return $usermenu;
		}

		protected function generateUserMenu() {
			$usermenu = array();
			$usermenu[] = array(
				"pagetype" => "my_user_my_profile",
				"title" => __("menu_My_user_My_profile","attr"),
				"url" => MyUserURL::renderMyProfileURL(),
				"width" => __("_width_menu_My_user_My_profile","attr" ) );
			/*
			$usermenu[] = array(
				"pagetype" => "my_user_status",
				"title" => __("menu_My_user_Status","attr"),
				"url" => "/",
				"width" => __("_width_menu_My_user_Status","attr" ) );
			$usermenu[] = array(
				"pagetype" => "my_user_favourite_translators",
				"title" => __("menu_My_user_Favourite_Translators","attr"),
				"url" => "/",
				"width" => __("_width_menu_My_user_Favourite_Translators","attr" ) );
			*/
			$usermenu[] = array(
				"pagetype" => "my_user_quote_requests",
				"title" => __("menu_My_user_Quote_requests","attr"),
				"url" => MyUserURL::renderMyQuoteRequestsURL(),
				"width" => __("_width_menu_My_user_Quote_requests","attr") );
			/*
			$usermenu[] = array(
				"pagetype" => "my_user_active_quote_requests",
				"title" => __("menu_My_user_Active_quote_requests","attr"),
				"url" => "/",
				"width" => __("_width_menu_My_user_Active_quote_requests","attr") );
			$usermenu[] = array(
				"pagetype" => "my_user_coupon",
				"title" => __("menu_My_user_Coupon","attr"),
				"url" => "/",
				"width" => __("_width_menu_My_user_Coupon","attr") );
			*/

			$usermenu[] = array(
				"pagetype" => "my_user_feedback",
				"title" => __("menu_My_user_Feedback","attr"),
				"url" => MyUserURL::renderFeedbackURL() ,
				"width" => __("_width_menu_My_user_Feedback","attr") );
			return $usermenu;
		}

		public function __construct( $id = "FrontendMenu" , $parent = null , $pagetype = "default" ) {
			parent::__construct( $id , $parent );
			$this->sPageType = $pagetype;
			$this->iButtons = 0;
			$this->oButtons = array();
		}

		protected function renderMenus() {
			$menu = $this->generateFrontendMenu();	
			$this->setParam( "general_menu" , $this->generateMenuFromArray( $menu ) );

			$user = new MyUserModel();
			$is_user_logged_in = $user->isUserLoggedin();
			$this->setParam( "is_user_logged_in" , $is_user_logged_in);
			if ($is_user_logged_in) {
				$member_id = $user->getUserMember( $user->getLoggedinUserId() );
				// $member_id = false;
				$usermenu = ($member_id) ? $this->generateMemberMenu() : $this->generateUserMenu();
				$this->setParam( "user_menu" , $this->generateMenuFromArray( $usermenu ) );
			}
		}

		public function handleEvents() {
			// FUCKED UP! (no child events will be called)
			// csunya dolog ide betenni a constructorba illo cuccokat, de hat...
			// szukseg torvenyt bont, es nehogy hamarabb fusson le ez, mint a logine
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			if ($this->aP["ajaxevent"]!="refresh_frontendmenu") return parent::handleEvents();
			$this->renderMenus();
			$buttons=sprintf( "<div class=\"menu\" id=\"%s_general_menu\">" , $this->getId() );
			$buttons.=sprintf( "<ul class=\"sf-menu mainmenu general\" id=\"%s_general_menu_ul\">%s</ul>",
				$this->getId() , $this->getParam( "general_menu" ) );
			$buttons.="</div>";
			
			$user = new MyUserModel();
			if ($user->isUserLoggedin()) {
				$buttons.=sprintf( "<div class=\"menu\" id=\"%s_user_menu\">" , $this->getId() );
				$buttons.=sprintf( "<ul class=\"sf-menu mainmenu user\" id=\"%s_user_menu_ul\">%s</ul>",
					$this->getId() , $this->getParam( "user_menu" ) );
				$buttons.="</div>";
			}
			$json = array();
			$json["htmlsource"]=$buttons;
			$this->renderWidgets();
			FNTH_echo_json_response( $json , 0 , "fo3_exit" );
			return parent::handleEvents();
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->renderMenus();
			FNTHPage::setPageJSHead( "superfish_js" , ( $GLOBALS["site_www"]."/design/rp/js/superfish.js" ) );
			FNTHPage::setPageCSSHead( "superfish_css" , ( $GLOBALS["site_www"]."/design/rp/css/superfish.css" ) );
			FNTHPage::addUniquePageFullInit( sprintf( "InitFrontendMenu('%s');\n" , $this->getId() ) );
			// FNTHPage::addUniquePageFullInit( sprintf( "RefreshFrontendMenu('%s');" , $this->getId() ) );	// just for test
			return parent::render();
		}

	}

?>
