requirejs.config({
	waitSeconds: 150
});

var tab_history = [];
tab_history[0] = null;
tab_history[1] = null;
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);
}

//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 pgm_dir_view = null;
var pgm_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;
}

var info = null;
active_base = "tab_stn_base";
ap.active_pgm = "stninfo_pgm";
var lang_common_type = "common";
var lang_error_msg_type = "error_msg";
var searchText = 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(['../station'], function (a) {
      debug_mode.flag && console.log("station.js loaded");

      info = stn_info[0];
      if (info.SYSTEM_ID == "iPECS eMG80" || info.SYSTEM_ID == "iPECS eMG100" || info.SYSTEM_ID == "iPECS eMG800") {
			if (info.BOARD_ID == 2) {
				if (info.SYSTEM_ID == "iPECS eMG800") {
					$('.navbar-brand').css('padding','17px 34px 16px 20px');
					$('.navbar-brand img').attr('src','img/Vertical_summit_800_main.png');
				} 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") {
					$('.navbar-brand').css('padding','16px 148px 16px 20px');
					$('.navbar-brand img').attr('src','img/Aria_IP_800_logo_main.png');
				} else {
					$('.navbar-brand').css('padding','17px 148px 16px 20px');
					$('.navbar-brand img').attr('src','img/Aria_IP_100_logo_main.png');
				}
			} else {
				if (info.NCODE == "CHN") {
					$('.navbar-brand img').remove();
					if (info.SYSTEM_ID == "iPECS eMG80" || info.SYSTEM_ID == "iPECS eMG100") {
						$('.navbar-brand').css('padding','15px 215px 15px 20px');
					} else if (info.SYSTEM_ID == "iPECS eMG800") {
						$('.navbar-brand').css('padding','15px 204px 15px 20px');
					}
					$('.navbar-brand').css('font-size','20px').css('color','#58585a');
				} else {
					$('.navbar-brand img').attr('src','img/iPECS_logo_main.png');
				}

				if (info.NCODE == "TKM") {
					$('.navbar-brand').append("OptiCon eMG");
				} else {
					if (info.NCODE == "CHN") {
						$('.navbar-brand').append(info.SYSTEM_ID.replace('iPECS ',''));
					} else {
						$('.navbar-brand').append(info.SYSTEM_ID);
					}
				}
			}
      } else {
			if (info.NCODE == "CHN") {
				$('.navbar-brand img').remove();
				if (info.SYSTEM_ID == "UCP100" || info.SYSTEM_ID == "UCP600") {
					$('.navbar-brand').css('padding','15px 205px 15px 20px');
				} else if (info.SYSTEM_ID == "UCP2400") {
					$('.navbar-brand').css('padding','15px 194px 15px 20px');
				} else if (info.SYSTEM_ID == "vUCP") {
					$('.navbar-brand').css('padding','15px 227px 15px 20px');
				}
				$('.navbar-brand').css('font-size','20px').css('color','#58585a');
				$('.navbar-brand').append(info.SYSTEM_ID.replace('iPECS ',''));
			} else {
				$('.navbar-brand img').attr('src','img/iPECS_logo_main.png');
				$('.navbar-brand').append(info.SYSTEM_ID);
			}
      }
      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.SITE_NAME) {
						$('title').text('STN ' + info.STN_NUM + ' @ ' + info.SITE_NAME);
					} else {
						if (info.SYSTEM_ID == "iPECS eMG80" || info.SYSTEM_ID == "iPECS eMG100" || info.SYSTEM_ID == "iPECS eMG800") {
							if (info.BOARD_ID == 2) {
								if (info.SYSTEM_ID == "iPECS eMG800") {
									$('title').text("Vertical Summit 800 " + langs.getText("common","stn_title"));
								} else {
									$('title').text("Vertical Summit " + langs.getText("common","stn_title"));
								}
							} else if (info.BOARD_ID == 3) {
								if (info.SYSTEM_ID == "iPECS eMG800") {
									$('title').text("ARIA IP 800 " + langs.getText("common","stn_title"));
								} else {
									$('title').text("ARIA IP 100 " + langs.getText("common","stn_title"));
								}
							} else {
								if (info.NCODE == "TKM") {
									$('title').text("OptiCon eMG " + langs.getText("common","stn_title"));
								} else {
									if (info.NCODE == "CHN") {
										$('title').text(langs.getText("common","stn_title"));
									} else {
										$('title').text("iPECS " + langs.getText("common","stn_title"));
									}
								}
							}
						} else {
							if (info.NCODE == "CHN") {
								$('title').text(langs.getText("common","stn_title"));
							} else {
								$('title').text("iPECS " + langs.getText("common","stn_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"));
					$('#btn_to_user_portal').text(langs.getText("common","up_title"));
					$('#tab_station_info').text(langs.getText("common","tab_stn_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_stn').append("<ul class='nav nav-list'><li class='tab_name'><a href='#favorite_pgm' data-toggle='tab'>" + langs.getText("common","tab_stn_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/):"-"))+")");

					$('#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/):"-"))+")");
					$('#menu_for_moblie_web #mob_user_portal').text(langs.getText("common","up_title"));
					$('#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"));
					require(['libs/websocket'], function (e) {
						debug_mode.flag && console.log("libs/websocket loaded");

						initWebSocket();
						require(['pgms/stn_leftmenu_view'], function (f) {
							debug_mode.flag && console.log("pgms/stn_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");
									if(typeof g_nation_list === "undefined") {
										require(['pgms/common_var'], function (i) {
											debug_mode.flag && console.log("pgms/common_var loaded");
											allLoadComplete = true;
											debug_mode.flag && console.log("all load complete!!");
											$('.loadingPane').removeClass('whitePane').hide();
										});
									} else {
										allLoadComplete = true;
										debug_mode.flag && console.log("all load complete!!");
										$('.loadingPane').removeClass('whitePane').hide();
									}
									make_stninfo_pgm();
								});
							});
						});
					});
				});
			});
      });
   });
}

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