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_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";

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") {
					$('.brand').css('padding','17px 84px 16px 28px');
					$('.brand img').attr('src','img/Vertical_summit_800_login.png');
				} else {
					$('.brand').css('padding','17px 84px 16px 34px');
					$('.brand img').attr('src','img/Vertical_summit_logo_main.png');
				}
			} else if (info.BOARD_ID == 3) {
				$('.brand').css('padding','17px 147px 16px 34px');
				$('.brand img').attr('src','img/Aria_IP_100_logo_main.png');
			} else {
				$('.brand img').attr('src','img/iPECS_logo_main.png');
				if (info.NCODE == "TKM") {
					$('.brand').append("OptiCon eMG");
				} else {
					$('.brand').append(info.SYSTEM_ID);
				}
			}
		} else {
			$('.brand img').attr('src','img/iPECS_logo_main.png');
			$('.brand').append(info.SYSTEM_ID);
		}
      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.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.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) {
						$('<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: {
								Ok: function () {
									$(this).dialog("destroy");
								}
							}
						});
					}
					if(info.CHECK_LICENSE) {
						$('<div />').html(langs.getText("common","lic_alert")+" ("+info.CHECK_LICENSE+")").dialog({
							modal: true,
							closeOnEscape: false,
							title: langs.getText("common","notification"),
							open: function (event, ui) {
								$(".ui-dialog-titlebar-close").hide();
							},
							buttons: [ { text: "Ok", /*"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");
												});
											}
										});
									});
								});
							});
						});
					});
				});
			});	
      });
   });
}

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