requirejs.config({
	waitSeconds: 150
});

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 pgm_models = new Object;
var pgm_views = new Object;
var pgm_key_maps = new Object;
var pgm_name_maps = new Object;

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 = [];
   packet.type = type;
   packet.range = ran;
   packet.msg = msg;
}

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

var info = null;
active_base = "tab_install_wizard";
ap.active_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: langs.getText("common","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);
      info.NO_OF_INSTALL_WIZARD_STEP = 9;
      if (info.SYSTEM_ID == "iPECS eMG80" || info.SYSTEM_ID == "iPECS eMG800") {
			if (info.BOARD_ID == 2) {
				$('.brand').css('padding','10px 84px 5px 34px');
				$('.brand img').attr('src','img/Vertical_summit_logo_main.png');
			} else if (info.BOARD_ID == 3) {
				$('.brand').css('padding','10px 147px 5px 34px');
				$('.brand img').attr('src','img/Aria_IP_100_logo_main.png');
			} else {
				$('.brand img').attr('src','img/iPECS_logo_main.png');
			}
      } else {
			$('.brand img').attr('src','img/iPECS_logo_main.png');
      }
      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");

					$('title').text(langs.getText("common","iw_title"));
					$('.brand').append(langs.getText("common","iw_title"));
					$('#account_logout').text(langs.getText("common","top_menu_exit"));
					$('#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"));
					$('#make_uploadpanel').text(langs.getText("mnt_menu","pgm51"));
					$('.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:"-")+")");
					
					$('#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:"-")+")");
					$('#menu_for_moblie_web #mob_make_upload').text(langs.getText("mnt_menu","pgm51"));
					$('#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_exit"));
					
					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")
						 }
					};
					require(['libs/websocket'], function (e) {
						debug_mode.flag && console.log("libs/websocket loaded");

						initWebSocket();
						if(typeof g_nation_list === "undefined") {
							require(['pgms/common_var'], function (i) {
							debug_mode.flag && console.log("pgms/common_var loaded");
							});
						}
						require(['pgms/pgm_view'], function (f) {
							debug_mode.flag && console.log("pgms/pgm_view loaded");
							require(['pgms/main_pgm'], function (g) {
								debug_mode.flag && console.log("pgms/main_pgm loaded");
								require(['pgms/file_upload_pgm'], function (h) {
									debug_mode.flag && console.log("pgms/file_upload_pgm loaded");
									$(document).trigger("load_fileuploadpgm");
								});
							});
						});
					});
				});
			});
      });
   });
}

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