requirejs.config({
	waitSeconds: 150
});

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);
}

//active_pgm = null;
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 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;
}

var info = null;
active_base = "tab_user_portal_base";
ap.active_pgm = "pgmup001";
var lang_common_type = "up_common";
var lang_error_msg_type = "up_error_msg";

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(['../station'], function (a) {
      debug_mode.flag && console.log("station.js loaded");

      info = stn_info[0];
      
      $pem = public_key;
      $key = RSA.getPublicKey($pem);
      
		if (localStorage.getItem("g_acdstngrpnum["+info.STN_NUM+"]") == null) {
			localStorage.setItem("g_acdstngrpnum["+info.STN_NUM+"]", "");
		}
		if (localStorage.getItem("g_acdoffreason["+info.STN_NUM+"]") == null) {
			localStorage.setItem("g_acdoffreason["+info.STN_NUM+"]", "");
		}
      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","up_title"));
					
					if (info.STN_NAME) {
						$('.up_brand').text(info.STN_NAME + " (" + langs.getText("common","simple_sta") + " " + info.STN_NUM + ")");
					} else {
						$('.up_brand').text(langs.getText("common","simple_sta") + " " + info.STN_NUM);
					}
					require(['libs/websocket'], function (e) {
						debug_mode.flag && console.log("libs/websocket loaded");

						initWebSocket();
						require(['pgms/up_menu_view'], function (f) {
							debug_mode.flag && console.log("pgms/up_menu_view loaded");
							require(['pgms/up_morebtn_view'], function (g) {
								debug_mode.flag && console.log("pgms/up_morebtn_view loaded");
								require(['pgms/up_pgm_view'], function (h) {
									debug_mode.flag && console.log("pgms/up_pgm_view loaded");
									require(['pgms/up_hotkey_view'], function (i) {
										debug_mode.flag && console.log("pgms/up_hotkey_view loaded");
										require(['pgms/up_pgm'], function (j) {
											debug_mode.flag && console.log("pgms/up_pgm loaded");
											allLoadComplete = true;
											debug_mode.flag && console.log("all load complete!!");
										});
									});
								});
							});
						});
					});
				});
			});
      });
   });
}

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