var MENU_APPS = [];
var DIRECT_LEVEL = [0,0,0,0,0];
var CURRENT_PAGE_MASTER = "";

var HEADER_TOP = 
    '<div class="m-header__top">' +
					'<div class="m-container m-container--fluid m-container--full-height m-page__container">' +
						'<div class="m-stack m-stack--ver m-stack--desktop">' +
							'<!-- begin::Brand -->' +
							'<div class="m-stack__item m-brand m-stack__item--left">' +
								'<div class="m-stack m-stack--ver m-stack--general m-stack--inline">' +
									'<div class="m-stack__item m-stack__item--middle m-brand__logo">' +
										'<a href="index.html" class="m-brand__logo-wrapper">' +
											'<div alt="" id="logo" class="m-brand__logo-desktop"></div>' +
//											'<img alt="" src="assets/demo/media/img/logo/logo_mini.png" class="m-brand__logo-mobile" />' +
										'</a>' +
									'</div>' +
									'<div class="m-stack__item m-stack__item--middle m-brand__tools">' +
										'<!-- begin::Quick Actions-->' +
										'<div id="menu-dropdown" class="m-dropdown m-dropdown--inline m-dropdown--arrow m-dropdown--align-left m-dropdown--align-push" m-dropdown-toggle="click" aria-expanded="true">' +
											'<a href="#" id="formSelect" class="dropdown-toggle m-dropdown__toggle btn btn-outline-metal m-btn  m-btn--icon m-btn--pill">' +
												'<span></span>' +
											'</a>' +
											'<div class="m-dropdown__wrapper">' +
												'<span class="m-dropdown__arrow m-dropdown__arrow--left m-dropdown__arrow--adjust"></span>' +
												'<div class="m-dropdown__inner">' +
													'<div class="m-dropdown__body">' +
														'<div class="m-dropdown__content">' +
															'<ul class="m-nav" id="appsNav">' + 
																'<li class="m-nav__section m-nav__section--first m--hide">' +
																	'<span class="m-nav__section-text">Quick Menu</span>' +
																'</li>';

HEADER_TOP +=
															'</ul>' +
														'</div>' +
													'</div>' +
												'</div>' +
											'</div>' +
										'</div>' +
										'<!-- end::Quick Actions-->' +
										'<!-- begin::Responsive Header Menu Toggler-->' +
										'<a id="m_aside_header_menu_mobile_toggle" href="javascript:;" class="m-brand__icon m-brand__toggler m--visible-tablet-and-mobile-inline-block">' +
											'<span></span>' +
										'</a>' +
										'<!-- end::Responsive Header Menu Toggler-->' +
										'<!-- begin::Topbar Toggler-->' +
										'<a id="m_aside_header_topbar_mobile_toggle" href="javascript:;" class="m-brand__icon m--visible-tablet-and-mobile-inline-block">' +
											'<i class="flaticon-more"></i>' +
										'</a>' +
										'<!--end::Topbar Toggler-->' +
									'</div>' +
								'</div>' +
							'</div>' +
							'<!-- end::Brand -->' +
							'<!-- begin::Topbar -->' +
								'<div class="m-stack__item m-stack__item--right m-header-head" id="m_header_nav">' +
									'<div id="m_header_topbar" class="m-topbar  m-stack m-stack--ver m-stack--general">' +
													'<div class="m-stack__item m-stack__item--middle m-dropdown m-dropdown--arrow m-dropdown--large m-dropdown--mobile-full-width m-dropdown--align-right m-dropdown--skin-light m-header-search m-header-search--expandable m-header-search--skin-light" id="m_quicksearch" m-quicksearch-mode="default">' +
									'<!--BEGIN: Search Form -->' +
									'<form class="m-header-search__form">' +
										'<div class="m-header-search__wrapper">' +
											'<span class="m-header-search__icon-search" id="m_quicksearch_search">' +
												'<i class="flaticon-search"></i>' +
											'</span>' +
											'<span class="m-header-search__input-wrapper">' +
												'<input autocomplete="off" type="text" name="q" class="m-header-search__input" value="" placeholder="Search..." id="m_quicksearch_input">' +
											'</span>' +
											'<span class="m-header-search__icon-close" id="m_quicksearch_close">' +
												'<i class="la la-remove"></i>' +
											'</span>' +
											'<span class="m-header-search__icon-cancel" id="m_quicksearch_cancel">' +
												'<i class="la la-remove"></i>' +
											'</span>' +
										'</div>' +
									'</form>' +
									'<!--END: Search Form -->' +
									'<!--BEGIN: Search Results -->' +
									'<div class="m-dropdown__wrapper">' +
										'<div class="m-dropdown__arrow m-dropdown__arrow--center"></div>' +
										'<div class="m-dropdown__inner">' +
											'<div class="m-dropdown__body">' +
												'<div class="m-dropdown__scrollable m-scrollable" data-scrollable="true" data-height="300" data-mobile-height="200">' +
													'<div class="m-dropdown__content m-list-search m-list-search--skin-light">' +
													'</div>' +
												'</div>' +
											'</div>' +
										'</div>' +
									'</div>' +
									'<!--BEGIN: END Results -->' +
								'</div>' +
										'<div class="m-stack__item m-topbar__nav-wrapper">' +
											'<ul class="m-topbar__nav m-nav m-nav--inline">' +
											'<li class="m-nav__item m-nav__item--accent m-dropdown m-dropdown--large m-dropdown--arrow m-dropdown--align-center 	m-dropdown--mobile-full-width" m-dropdown-toggle="click" m-dropdown-persistent="1">' +
												'<a href="#" class="m-nav__link m-dropdown__toggle" id="m_topbar_notification_icon">' +
													'<span id= "alertBadge" class=""></span>' +
//													'<span id= "alertBadge" class="m-nav__link-badge m-badge m-badge--dot m-badge--dot-small m-badge--danger"></span>' +
													'<span class="m-nav__link-icon"><span class="m-nav__link-icon-wrapper"><i class="flaticon-alarm"></i></span></span>' +
												'</a>' +
												'<div class="m-dropdown__wrapper">' +
													'<span class="m-dropdown__arrow m-dropdown__arrow--center"></span>' +
													'<div class="m-dropdown__inner">' +
														'<div class="m-dropdown__header m--align-center">' +
															'<span class="m-dropdown__header-title">0 New</span>' +
															'<span class="m-dropdown__header-subtitle">User Notifications</span>' +
														'</div>' +
														'<div class="m-dropdown__body">' +
															'<div class="m-dropdown__content">' +
																'<ul class="nav nav-tabs m-tabs m-tabs-line m-tabs-line--brand" role="tablist">' +
																	'<li class="nav-item m-tabs__item">' +
																		'<a class="nav-link m-tabs__link active" data-toggle="tab" href="#topbar_notifications_notifications" role="tab">' +
																			'Alerts' +
																		'</a>' +
																	'</li>' +
																	'<li class="nav-item m-tabs__item">' +
																		'<a class="nav-link m-tabs__link" data-toggle="tab" href="#topbar_notifications_events" role="tab">Events</a>' +
																	'</li>' +
																	'<li class="nav-item m-tabs__item">' +
																		'<a class="nav-link m-tabs__link" data-toggle="tab" href="#topbar_notifications_logs" role="tab">Logs</a>' +
																	'</li>' +
																'</ul>' +
																'<div class="tab-content">' +
																	'<div class="tab-pane active" id="topbar_notifications_notifications" role="tabpanel">' +
																		'<div class="m-stack m-stack--ver m-stack--general" style="min-height: 180px;">' +
																			'<div class="m-stack__item m-stack__item--center m-stack__item--middle">' +
																				'<span class="">All caught up!<br>No new alerts.</span>' +
																			'</div>' +
																		'</div>' +
																	'</div>' +
																	'<div class="tab-pane" id="topbar_notifications_events" role="tabpanel">' +
																		'<div class="m-stack m-stack--ver m-stack--general" style="min-height: 180px;">' +
																			'<div class="m-stack__item m-stack__item--center m-stack__item--middle">' +
																				'<span class="">All caught up!<br>No new events.</span>' +
																			'</div>' +
																		'</div>' +
																	'</div>' +
																	'<div class="tab-pane" id="topbar_notifications_logs" role="tabpanel">' +
																		'<div class="m-stack m-stack--ver m-stack--general" style="min-height: 180px;">' +
																			'<div class="m-stack__item m-stack__item--center m-stack__item--middle">' +
																				'<span class="">All caught up!<br>No new logs.</span>' +
																			'</div>' +
																		'</div>' +
																	'</div>' +
																'</div>' +
															'</div>' +
														'</div>' +
													'</div>' +
												'</div>' +
											'</li>' +
											'<li class="m-nav__item m-dropdown m-dropdown--medium m-dropdown--arrow  m-dropdown--align-right m-dropdown--mobile-full-width m-dropdown--skin-light" m-dropdown-toggle="click">' +
												'<a href="#" class="m-nav__link m-dropdown__toggle">' +
													'<span class="m-topbar__userpic">' +
														'<img name="ThumbImage" src="" class="m--img-rounded m--marginless m--img-centered" alt="" />' +
													'</span>' +
													'<span class="m-nav__link-icon m-topbar__usericon  m--hide">' +
														'<span class="m-nav__link-icon-wrapper"><i class="flaticon-user-ok"></i></span>' +
													'</span>' +
													'<span class="m-topbar__username m--hide">Mark</span>' +
												'</a>' +
												'<div class="m-dropdown__wrapper">' +
													'<span class="m-dropdown__arrow m-dropdown__arrow--right m-dropdown__arrow--adjust"></span>' +
													'<div class="m-dropdown__inner">' +
														'<div class="m-dropdown__header m--align-center">' +
															'<div class="m-card-user m-card-user--skin-light">' +
																'<div class="m-card-user__pic">' +
																	'<img name="ThumbImage" src="" class="m--img-rounded m--marginless" alt="" />' +
																'</div>' +
																'<div class="m-card-user__details">' +
																	'<span class="m-card-user__nickname m--font-weight-500"></span>' +
																	'<span class="m-card-user__name m--font-weight-300"></span>' +
																'</div>' +
															'</div>' +
														'</div>' +
														'<div class="m-dropdown__body">' +
															'<div class="m-dropdown__content">' +
																'<ul class="m-nav m-nav--skin-light">' +
																	'<li class="m-nav__section m--hide">' +
																		'<span class="m-nav__section-text">Section</span>' +
																	'</li>' +
																	'<li class="m-nav__item">' +
																		'<a href="profile.html" class="m-nav__link">' +
																			'<i class="m-nav__link-icon flaticon-profile-1"></i>' +
																			'<span class="m-nav__link-title">' +
																				'<span class="m-nav__link-wrap">' +
																					'<span class="m-nav__link-text">My Profile</span>' +
//																					'<span class="m-nav__link-badge"><span class="m-badge m-badge--success">2</span></span>' +
																				'</span>' +
																			'</span>' +
																		'</a>' +
																	'</li>' +
																	'<li class="m-nav__item">' +
																		'<a href="lock.html" class="m-nav__link">' +
																			'<i class="m-nav__link-icon flaticon-lock"></i>' +
																			'<span class="m-nav__link-text">Lock Screen</span>' +
																		'</a>' +
																	'</li>' +
																	'<li class="m-nav__separator m-nav__separator--fit">' +
																	'</li>' +
																	'<li class="m-nav__item">' +
																		'<a href="http://osiris.monsanto.com/IT_Support/Osiris-Dashboard.html" target="_blank" class="m-nav__link">' +
																			'<i class="m-nav__link-icon flaticon-lifebuoy"></i>' +
																			'<span class="m-nav__link-text">Support</span>' +
																		'</a>' +
																	'</li>' +
																	'<li class="m-nav__separator m-nav__separator--fit">' +
																	'</li>' +
																	'<li class="m-nav__item">' +
																		'<a href="javascript:void(0);" onclick="Functions.Logout()" class="btn m-btn--pill    btn-secondary m-btn m-btn--custom m-btn--label-brand m-btn--bolder">Logout</a>' +
																	'</li>' +
																'</ul>' +
															'</div>' +
														'</div>' +
													'</div>' +
												'</div>' +
											'</li>' +
											'<li id="m_quick_sidebar_toggle" class="m-nav__item m-nav__item--info m-nav__item--qs">' +
												'<a href="#" class="m-nav__link m-dropdown__toggle">' +
													'<span class="m-nav__link-icon m-nav__link-icon-alt"><span class="m-nav__link-icon-wrapper"><i class="flaticon-grid-menu"></i></span></span>' +
												'</a>' +
											'</li>' +
										'</ul>' +
									'</div>' +
								'</div>' +
							'</div>' +
							'<!-- end::Topbar -->' +
						'</div>' +
					'</div>' +
				'</div>';

$("#m_header").html(HEADER_TOP + $("#m_header").html());

GetData.SingleSqlValue("id_Catalog, Name", "OS_Catalog", "Category = 34 AND CatActive = 1",true,true,function(ret) {
	
	MENU_APPS = Functions.sortByColumn($.map(ret, function( e ) { return [(e.id_Catalog + "|" + e.Name).split("|")];}),5);
	if (GetData.UrlParameter("page") != undefined) {
		DIRECT_LEVEL = [0,0,0,0,0];
		GetData.SingleSqlValue("id_Catalog, Name", "OS_Catalog", "Category = 35 AND CatActive = 1",true,true,function(ret) {
			var RET = ret;
			var CURRENT_LEVEL = RET[Functions.indexOfObject(RET,"id_Catalog",GetData.UrlParameter("page"))].Name.split("|")[4];
			var ID_LOOP = RET[Functions.indexOfObject(RET,"id_Catalog",GetData.UrlParameter("page"))].Name.split("|")[0];
			DIRECT_LEVEL[CURRENT_LEVEL - 1] = ID_LOOP;
			DIRECT_LEVEL[CURRENT_LEVEL] = GetData.UrlParameter('page');
			for (var i = CURRENT_LEVEL - 1; i > 0; i--) {
				DIRECT_LEVEL[i - 1] = RET[Functions.indexOfObject(RET,"id_Catalog",ID_LOOP)].Name.split("|")[0];
				ID_LOOP = DIRECT_LEVEL[i - 1];
			}
			SetData.localStorage('Current_Page', DIRECT_LEVEL[0]);
			CURRENT_PAGE_MASTER = GetData.localStorage('Current_Page');
			openPage(MENU_APPS);
		});
	} else {
		CURRENT_PAGE_MASTER = GetData.localStorage('Current_Page');
		openPage(MENU_APPS);
	}
});

function openPage(ret) {
	if (CURRENT_PAGE_MASTER == null) {
		window.location.href = "app_select.html";
	}
	$('#menu-dropdown > a > span').html(Functions.LanguageSelect(Functions.indexOfObject(ret,'singleArr',CURRENT_PAGE_MASTER,2),Functions.indexOfObject(ret,'singleArr',CURRENT_PAGE_MASTER,1)));
	for(var i = 0; i < ret.length; i++) {
	$('#appsNav').append('<li class="m-nav__item ' + (ret[i][0] == CURRENT_PAGE_MASTER ? 'back-aliceblue' : '') + '"><a href="javascript:void(0);" onclick="mainPageDropDownClick(' + ret[i][0] + ');" class="m-nav__link"><i class="m-nav__link-icon flaticon-share"></i><span class="m-nav__link-text">' + Functions.LanguageSelect(ret[i][2],ret[i][1]) + '</span></a></li>');
	}
	
	buildMenu();
	
	if ((Functions.isNullOrWhitespace(GetData.localStorage('Picture')) ? '' : GetData.localStorage('Picture')).length > 100) {
        $('[name="ThumbImage"]').attr('src', GetData.localStorage('Picture'));
    } else {
		$('[name="ThumbImage"]').attr('src', '/img/about.png');
	}
    $('.m-card-user__nickname').html(GetData.localStorage('NickName'));
    $('.m-card-user__name').html(GetData.localStorage('EmpName'));
}

function mainPageDropDownClick(FORM_ID) {
	$('#menu-dropdown > a > span').html(Functions.LanguageSelect(Functions.LanguageSelect(Functions.indexOfObject(MENU_APPS,'singleArr',FORM_ID.toString(),2),Functions.indexOfObject(MENU_APPS,'singleArr',FORM_ID.toString(),1))));
	SetData.localStorage('Current_Page', FORM_ID);
	$('.m-nav__item.back-aliceblue').removeClass('back-aliceblue');
	for(var l = 0; l < $('#appsNav').children().length; l++) {
		if ($($('#appsNav').children()[l]).find('span').html() == Functions.LanguageSelect(Functions.indexOfObject(MENU_APPS,'singleArr',FORM_ID.toString(),2),Functions.indexOfObject(MENU_APPS,'singleArr',FORM_ID.toString(),1))) {
			$($('#appsNav').children()[l]).addClass('back-aliceblue');
			$('#menu-dropdown > a > span').html(Functions.LanguageSelect(Functions.indexOfObject(MENU_APPS,'singleArr',FORM_ID.toString(),2),Functions.indexOfObject(MENU_APPS,'singleArr',FORM_ID.toString(),1)));
			break;
		}
	}
	buildMenu();
}