requirejs.config({
	waitSeconds: 150
});

var tab_history = [];
var pgm_tab_history = [];
pgm_tab_history[0] = null;
pgm_tab_history[1] = null;
var func_tab_history = [];
func_tab_history[0] = null;
func_tab_history[1] = null;
var mnt_tab_history = [];
mnt_tab_history[0] = null;
mnt_tab_history[1] = null;
//var sub_tab_history = [];
//sub_tab_history[0] = null;
//sub_tab_history[1] = null;
tab_history = pgm_tab_history;
var save_flag = false;
var hidden_flags = new Object;
var debug_mode = new Object;
if (localStorage.getItem("d_flag") == null) {
   localStorage.setItem("d_flag", false);
}
debug_mode.flag = eval(localStorage.getItem("d_flag"));
debug_mode.on = function () {
   debug_mode.flag = true;
   localStorage.setItem("d_flag", true);
}
debug_mode.off = function () {
   debug_mode.flag = false;
   localStorage.setItem("d_flag", false);
}

var ap = new Object;
ap.active_pgm = null;
ap.render_pgm = null;
ap.set_active_pgm = function (pgm) {
   ap.active_pgm = pgm;
   ap.render_pgm = pgm;
   $(document).trigger("change_active_pgm", pgm);
}
var active_base = null;
var active_tab = null;

var pgm_models = new Object;
var pgm_views = new Object;
var pgm_list = new Object;
var maint_list = new Object;
var select_list = new Object;
var select_menuList = new Object;
var pgm_key_maps = new Object;
var pgm_name_maps = new Object;

var pgm_dir_view = null;
var pgm_menuList_view = null;
var func_dir_view = null;
var func_menuList_view = null;
var mnt_dir_view = null;
var mnt_menuList_view = null;

var column_config = new Object;

var packet = new Object;
packet.main = 0;
packet.type = [];
packet.range = [];
packet.msg = "{}";
packet.setPacket = function (main, type, ran, msg) {
   packet.main = main;
   packet.type = type;
   packet.range = ran;
   packet.msg = msg;
}

Storage.prototype.setObject = function (key, value) {
   this.setItem(key, JSON.stringify(value));
}

Storage.prototype.getObject = function (key) {
   return this.getItem(key) && JSON.parse(this.getItem(key));
}

var $pem = null;
var $key = null;

var info = null;
active_base = "tab_pgm_base";
ap.active_pgm = "favorite_pgm";
var lang_common_type = "common";
var lang_error_msg_type = "error_msg";
var mnt_pgm_no_model = [45, 46, 47, 48, 49, 51, 56, 55, 89, 76, 77, 80, 82, 83, 782];      //attribute search will not require these pgms because these don't have model/js files. please add pgm number here that doesn't have model file.
var searchText = null;
var user_funclist = null;
requirejs.onError = function (err) {
   debug_mode.flag && console.log("Require Error : " + err.requireModules[0]);
   //debug_mode.flag && console.log("Retry...");
   skipConfirm_flag = true;
   $('<div />').html("Problem loading a web page occurred. Please refresh the page and try again.").dialog({
      modal: true,
      closeOnEscape: false,
      title: "Notification",
      open: function (event, ui) {
         $(".ui-dialog-titlebar-close").hide();
      },
      buttons: {
         Refresh: function () {
            location.href = location.href.replace(/#$/, '')
            $(this).dialog("destroy");
         }
      }
   });
};

function main_app() {
   require(['../system'], function (a) {
      debug_mode.flag && console.log("system loaded");

      info = sys_info[0];
      $pem = public_key;
      $key = RSA.getPublicKey($pem);
		if (info.SYSTEM_ID == "iPECS eMG80" || info.SYSTEM_ID == "iPECS eMG800") {
			if (info.BOARD_ID == 2) {
				if (info.SYSTEM_ID == "iPECS eMG800") {
					if (info.REDUN_STATUS.length) {
						$('.navbar-brand').css('padding','11px 34px 6px 20px');
					} else {
						$('.navbar-brand').css('padding','17px 34px 16px 20px');
					}
					$('.navbar-brand img').attr('src','img/Vertical_summit_800_main.png');
				} else {
					if (info.REDUN_STATUS.length) {
						$('.navbar-brand').css('padding','11px 85px 6px 20px');
					} else {
						$('.navbar-brand').css('padding','17px 85px 16px 20px');
					}
					$('.navbar-brand img').attr('src','img/Vertical_summit_logo_main.png');
				}
			} else if (info.BOARD_ID == 3) {
				if (info.SYSTEM_ID == "iPECS eMG800") {
					if (info.REDUN_STATUS.length) {
						$('.navbar-brand').css('padding','10px 148px 6px 20px');
					} else {
						$('.navbar-brand').css('padding','16px 148px 16px 20px');
					}
					$('.navbar-brand img').attr('src','img/Aria_IP_800_logo_main.png');
				} else {
					if (info.REDUN_STATUS.length) {
						$('.navbar-brand').css('padding','11px 148px 6px 20px');
					} else {
						$('.navbar-brand').css('padding','17px 148px 16px 20px');
					}
					$('.navbar-brand img').attr('src','img/Aria_IP_100_logo_main.png');
				}
			} else {
				$('.navbar-brand img').attr('src','img/iPECS_logo_main.png');
				if (info.NCODE == "TKM") {
					$('#navbar_sysid').append("OptiCon eMG");
				} else {
					$('#navbar_sysid').append(info.SYSTEM_ID);
				}
			}
		} else {
			$('.navbar-brand img').attr('src','img/iPECS_logo_main.png');
			$('#navbar_sysid').append(info.SYSTEM_ID);
		}

      if (localStorage.getItem('app_ver') == null)
         localStorage.setItem('app_ver', info.APP_VERSION);
		
      info.NO_OF_MAX_FUNCTION_STEP = 5;
      require(['user/common.lang'], function (b) {
			debug_mode.flag && console.log("user/common.lang loaded");
			require(['user/user.profile'], function (c) {
				debug_mode.flag && console.log("user/user.profile loaded");
				require(['user/common.profile'], function (d) {
					debug_mode.flag && console.log("user/common.profile loaded");
					
					if (info.REDUN_STATUS.length) {
						if (info.REDUN_STATUS == "MASTER") {
							$('#navbar_redun').append(" [" + langs.getText("common","txt_master") + "]");
						} else if (info.REDUN_STATUS == "SLAVE") {
							$('#navbar_redun').append(" [" + langs.getText("common","txt_slave") + "]");
						}
						if (info.REDUN_ACT == 1) {							
								$('#navbar_redun').addClass('navbar_redun_active');
						}						
					}
					
					if (info.SYSTEM_ID == "iPECS eMG80" || info.SYSTEM_ID == "iPECS eMG800") {
						if (info.BOARD_ID == 2) {
							if (info.SYSTEM_ID == "iPECS eMG800") {
								if (info.SITE_NAME) {
									$('title').text('Vertical Summit 800 (' + info.SITE_NAME + ')');
								} else {
									$('title').text("Vertical Summit 800 " + langs.getText("common","adm_title"));
								}
							} else {
								if (info.SITE_NAME) {
									$('title').text('Vertical Summit (' + info.SITE_NAME + ')');
								} else {
									$('title').text("Vertical Summit " + langs.getText("common","adm_title"));
								}
							}
						} else if (info.BOARD_ID == 3) {
							if (info.SYSTEM_ID == "iPECS eMG800") {
								if (info.SITE_NAME) {
									$('title').text('ARIA IP 800 (' + info.SITE_NAME + ')');
								} else {
									$('title').text("ARAI IP 800 " + langs.getText("common","adm_title"));
								}
							} else {
								if (info.SITE_NAME) {
									$('title').text('ARIA IP 100 (' + info.SITE_NAME + ')');
								} else {
									$('title').text("ARAI IP 100 " + langs.getText("common","adm_title"));
								}
							}
						} else {
							if (info.SITE_NAME) {
								if (info.NCODE == "TKM") {
									$('title').text('OptiCon eMG (' + info.SITE_NAME + ')');
								} else {
									$('title').text('iPECS (' + info.SITE_NAME + ')');
								}
							} else {
								if (info.NCODE == "TKM") {
									$('title').text("OptiCon eMG " + langs.getText("common","adm_title"));
								} else {
									$('title').text(info.SYSTEM_ID + " " + langs.getText("common","adm_title"));
								}
							}
						}
					} else {
						if (info.SITE_NAME) {
							$('title').text('iPECS (' + info.SITE_NAME + ')');
						} else {
							$('title').text(info.SYSTEM_ID + " " + langs.getText("common","adm_title"));
						}
					}
					$('#account_logout').text(langs.getText("common","top_menu_logout"));
					$('#menu_change_language a').text(langs.getText("common","top_menu_chlang"));
					$('#change_language #change_eng').text(langs.getText("common","opt_lang_eng"));
					$('#change_language #change_local').text(langs.getText("common","top_menu_chlang_loc"));
					$('#menu_admin').text(langs.getText("common","top_menu_admin"));
					$('#menu_mnt').text(langs.getText("common","top_menu_sysmnt"));
					$('#menu_pgm_base').text(langs.getText("common","left_menu_pgmbase"));
					$('#menu_function_base').text(langs.getText("common","left_menu_funcbase"));
					$('#search_pgm').attr('placeholder',langs.getText("common","left_menu_pgmsearch"));
					$('#tab_favorite_pgm').text(langs.getText("common","tab_pgm_favorpgm")).parent().attr("title",langs.getText("common","tab_pgm_favorpgm"));
					$('#tab_favorite_func').text(langs.getText("common","tab_func_favorfunc")).parent().attr("title",langs.getText("common","tab_func_favorfunc"));
					$('#tab_system_info').append(langs.getText("common","tab_sys_info") + "<img src='img/refresh_icon.png' class='icon_refresh' title='" + langs.getText("toolTip","tip_refreshtab") + "' />").parent().attr("title",langs.getText("common","tab_sys_info"));
					$('.scrl_left').attr('title',langs.getText("toolTip","tip_mvtoleft"));
					$('.scrl_right').attr('title',langs.getText("toolTip","tip_mvtoright"));
					$('.close_tab').attr('title',langs.getText("toolTip","tip_closealltabs"));
					$('.show_tab_list').attr('title',langs.getText("toolTip","tip_tablist"));
					$('.tab_list_pgm').append("<ul class='nav nav-list'><li class='tab_name'><a href='#favorite_pgm' data-toggle='tab'>" + langs.getText("common","tab_pgm_favorpgm") + "</a></li></ul>");
					$('.tab_list_func').append("<ul class='nav nav-list'><li class='tab_name'><a href='#favorite_func' data-toggle='tab'>" + langs.getText("common","tab_func_favorfunc") + "</a></li></ul>");
					$('.tab_list_mnt').append("<ul class='nav nav-list'><li class='tab_name'><a href='#pgm98' data-toggle='tab'>" + langs.getText("common","tab_sys_info") + "</a></li></ul>");
					$('.navbar-fixed-bottom .sw_version').text("["+langs.getText("common","info_version")+"] "+langs.getText("common","info_app")+"("+(info.APP_VERSION.match(/[A-Z]-\w+-\w+-\d+.\d+.\d+-\w+/)?(info.APP_VERSION.split('-')[2][0]+info.APP_VERSION.split('-')[3]):(info.APP_VERSION.match(/\w\.\w\w\w/)?info.APP_VERSION.match(/\w\.\w\w\w/):"-"))+"), "+langs.getText("common","info_boot")+"("+(info.BOOT_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)?(info.BOOT_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)):(info.BOOT_VERSION.match(/\w\.\w\w\w/)?info.BOOT_VERSION.match(/\w\.\w\w\w/):"-"))+"), "+langs.getText("common","info_kernel")+"("+(info.KERNEL_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)?(info.KERNEL_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)):(info.KERNEL_VERSION.match(/\w\.\w\w\w/)?info.KERNEL_VERSION.match(/\w\.\w\w\w/):"-"))+"), "+langs.getText("common","info_hw")+"("+(info.HW_ISSUE?info.HW_ISSUE:"-")+")"+(offline_mode?(", "+langs.getText("common","simple_database")+"("+(info.DB_VERSION?info.DB_VERSION:"-")+")"):""));
					$('.row-fluid .custom_span_menu .menu_blinker').attr('title',langs.getText("toolTip","hide_menu"));
					
					$('#menu_for_moblie_web #mob_title_ver').text(langs.getText("common","info_version"));
					$('#menu_for_moblie_web #mob_info_ver').text(langs.getText("common","info_app")+"("+(info.APP_VERSION.match(/[A-Z]-\w+-\w+-\d+.\d+.\d+-\w+/)?(info.APP_VERSION.split('-')[2][0]+info.APP_VERSION.split('-')[3]):(info.APP_VERSION.match(/\w\.\w\w\w/)?info.APP_VERSION.match(/\w\.\w\w\w/):"-"))+"), "+langs.getText("common","info_boot")+"("+(info.BOOT_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)?(info.BOOT_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)):(info.BOOT_VERSION.match(/\w\.\w\w\w/)?info.BOOT_VERSION.match(/\w\.\w\w\w/):"-"))+"), "+langs.getText("common","info_kernel")+"("+(info.KERNEL_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)?(info.KERNEL_VERSION.match(/[A-Za-z]+\d+.\d+.\d+/)):(info.KERNEL_VERSION.match(/\w\.\w\w\w/)?info.KERNEL_VERSION.match(/\w\.\w\w\w/):"-"))+"), "+langs.getText("common","info_hw")+"("+(info.HW_ISSUE?info.HW_ISSUE:"-")+")"+(offline_mode?(", "+langs.getText("common","simple_database")+"("+(info.DB_VERSION?info.DB_VERSION:"-")+")"):""));
					$('#menu_for_moblie_web #mob_menu_admin').text(langs.getText("common","top_menu_admin"));
					$('#menu_for_moblie_web #mob_menu_maint').text(langs.getText("common","top_menu_sysmnt"));
					$('#menu_for_moblie_web #mob_title_chlang').text(langs.getText("common","top_menu_chlang"));
					$('#menu_for_moblie_web #mob_chlang_eng').text(langs.getText("common","opt_lang_eng"));
					$('#menu_for_moblie_web #mob_chlang_local').text(langs.getText("common","top_menu_chlang_loc"));
					$('#menu_for_moblie_web #mob_logout').text(langs.getText("common","top_menu_logout"));
					
					window.locale = {
						 "fileupload": {
							  "error": langs.getText("error_msg","error"),
							  "start": langs.getText("common","start"),
							  "cancel": langs.getText("common","cancel"),
							  "reset": langs.getText("common","reset"),
							  "refresh": langs.getText("common","refresh"),
							  "destroy": langs.getText("common","clear"),
							  "logview": langs.getText("common","logview")
						 }
					};
					
					if (info.NEW_LICENSE_ENABLE) {
						$('#newlicense_status #lic_swmaint').attr('title',"("+langs.getText("common","license")+") "+langs.getText("common","sw_maint"));
						$('#newlicense_status #lic_demokey').attr('title',"("+langs.getText("common","license")+") "+langs.getText("common","demo_key"));
						$('#newlicense_status #lic_tnet_redun').attr('title',"("+langs.getText("common","license")+") T-Net LCM/"+langs.getText("common","redundancy"));
						$('#menu_for_moblie_web #mob_title_lic').text(langs.getText("common","lic_status"));
						$('#menu_for_moblie_web #mob_lic_swmaint').before(langs.getText("common","sw_maint"));
						$('#menu_for_moblie_web #mob_lic_demokey').before(langs.getText("common","demo_key"));
						$('#menu_for_moblie_web #mob_lic_tnet_redun').before("T-Net LCM/"+langs.getText("common","redundancy"));
						if (info.LIC_SWMAINT == 1) {
							$('#newlicense_status #lic_swmaint').css('background-color','#1DDB16');
							$('#menu_for_moblie_web #mob_lic_swmaint').css('background-color','#1DDB16');
						} else if (info.LIC_SWMAINT == 2) {
							$('#newlicense_status #lic_swmaint').css('background-color','yellow');
							$('#menu_for_moblie_web #mob_lic_swmaint').css('background-color','yellow');
						} else if (info.LIC_SWMAINT == 3) {
							$('#newlicense_status #lic_swmaint').css('background-color','red');
							$('#menu_for_moblie_web #mob_lic_swmaint').css('background-color','red');
						} else {
							$('#newlicense_status #lic_swmaint').css('background-color','#C7C8CA');
							$('#menu_for_moblie_web #mob_lic_swmaint').css('background-color','#C7C8CA');
						}
						
						if (info.LIC_DEMOKEY == 1) {
							$('#newlicense_status #lic_demokey').css('background-color','#1DDB16');
							$('#menu_for_moblie_web #mob_lic_demokey').css('background-color','#1DDB16');
						} else if (info.LIC_DEMOKEY == 2) {
							$('#newlicense_status #lic_demokey').css('background-color','yellow');
							$('#menu_for_moblie_web #mob_lic_demokey').css('background-color','yellow');
						} else if (info.LIC_DEMOKEY == 3) {
							$('#newlicense_status #lic_demokey').css('background-color','red');
							$('#menu_for_moblie_web #mob_lic_demokey').css('background-color','red');
						} else {
							$('#newlicense_status #lic_demokey').css('background-color','#C7C8CA');
							$('#menu_for_moblie_web #mob_lic_demokey').css('background-color','#C7C8CA');
						}
						
						if (info.LIC_TNET_REDUN == 1) {
							$('#newlicense_status #lic_tnet_redun').css('background-color','#1DDB16');
							$('#menu_for_moblie_web #mob_lic_tnet_redun').css('background-color','#1DDB16');
						} else if (info.LIC_TNET_REDUN == 2) {
							$('#newlicense_status #lic_tnet_redun').css('background-color','yellow');
							$('#menu_for_moblie_web #mob_lic_tnet_redun').css('background-color','yellow');
						} else if (info.LIC_TNET_REDUN == 3) {
							$('#newlicense_status #lic_tnet_redun').css('background-color','red');
							$('#menu_for_moblie_web #mob_lic_tnet_redun').css('background-color','red');
						} else {
							$('#newlicense_status #lic_tnet_redun').css('background-color','#C7C8CA');
							$('#menu_for_moblie_web #mob_lic_tnet_redun').css('background-color','#C7C8CA');
						}
					} else {
						$('#newlicense_status').hide();
						$('#mob_title_lic').parent().hide();
						$('#mob_lic_swmaint').parent().hide();
						$('#mob_lic_demokey').parent().hide();
						$('#mob_lic_tnet_redun').parent().hide();
						$('#list_for_moblie_web .divider:last()').hide();
					}
					
					if(info.DEFAULT_MAINT_ID && !offline_mode) {
						$('<div />').html(langs.getText("common","alert_changemaint")).dialog({
							modal: true,
							closeOnEscape: false,
							title: langs.getText("common","notification"),
							open: function (event, ui) {
								$(".ui-dialog-titlebar-close").hide();
							},
							buttons: [
								{
									text: langs.getText("common","simple_okay"),
									click: function () {
										$(this).dialog("destroy");
									}
								}
							]
						});
					}
					if(info.CHECK_LICENSE && !offline_mode) {
						var lic_reason;
						if(info.CHECK_LICENSE==1)
						{
							lic_reason=langs.getText("lic","swmnt");
						}
						else
						{
							lic_reason=langs.getText("lic","portover");
						}
						$('<div />').html(langs.getText("common","lic_alert")+" ("+lic_reason+")").dialog({
							modal: true,
							closeOnEscape: false,
							title: langs.getText("common","notification"),
							open: function (event, ui) {
								$(".ui-dialog-titlebar-close").hide();
							},
							buttons: [
								{
									text: langs.getText("common","simple_okay"), /*"name":'1030', "class": 'btn_link_to_otherpgm pgm1030',*/
									click: function() {
										$( this ).dialog( "close" );
									}
								}
							]
						});
					}
					require(['libs/websocket'], function (e) {
						debug_mode.flag && console.log("libs/websocket loaded");

						initWebSocket();
						require(['pgms/adm_leftmenu_view'], function (f) {
							debug_mode.flag && console.log("pgms/adm_leftmenu_view loaded");
							require(['pgms/pgm_view'], function (g) {
								debug_mode.flag && console.log("pgms/pgm_view loaded");
								require(['pgms/main_pgm'], function (h) {
									debug_mode.flag && console.log("pgms/main_pgm loaded");
									require(['pgms/favorite_pgm'], function (i) {
										debug_mode.flag && console.log("pgms/favorite_pgm loaded");

										make_favorite_pgm();

										require(['pgms/file_upload_pgm'], function (j) {
											debug_mode.flag && console.log("pgms/file_upload_pgm loaded");
											if(typeof g_nation_list === "undefined") {
												require(['pgms/common_var'], function (k) {
													debug_mode.flag && console.log("pgms/common_var loaded");
													allLoadComplete = true;
													debug_mode.flag && console.log("all load complete!!");
													$('#search_pgm').removeAttr('disabled');
													$('.loadingPane').removeClass('whitePane').hide();
												});
											} else {
												allLoadComplete = true;
												debug_mode.flag && console.log("all load complete!!");
												$('#search_pgm').removeAttr('disabled');
												$('.loadingPane').removeClass('whitePane').hide();
											}
										});
									});
								});
							});
						});
					});
				});
			});	
      });
   });
}

(function ($) {
   $("#tpl").load('templates/templates.html', main_app);
}(jQuery));
