
var sf = new Object();

sf.system = {
	init : function() {
		EGW.system.key.init();
		
		EGW.ui.translation.onUITranslate.bind("singleframe", sf.system.translate);
		EGW.ui.translation.loadwords("singleframe", function(){
			sf.system.loadhome();

			EGW.res.loadResource('menu', function() {

				sf.system.buildmenu();

				EGW.user.systeminit({
					callback: function() {
						EGW.user.onRightsChange.bind("singleframe", sf.user.applyRights);
						EGW.ui.translation.onUITranslate.fire();
					}
				});

				$('#option_register').click(function() {
					sf.user.loadregistration();
				});
				$('#option_login').click(function() {
					sf.user.loadlogin();
				});
				EGW.user.onLogin.bind("switchcaption", function() {
					sf.user.switchcaption();
				});
				EGW.user.onLogout.bind("switchcaption", function() {
					sf.user.switchcaption();
				});
			});
		});
		

		//languages
		$("#lang_zh").click(function(){
			EGW.ui.translation.switchLanguage("zh");
		});
		$("#lang_ru").click(function(){
			EGW.ui.translation.switchLanguage("ru");
		});
		$("#lang_pt").click(function(){
			EGW.ui.translation.switchLanguage("pt");
		});
		$("#lang_it").click(function(){
			EGW.ui.translation.switchLanguage("it");
		});
		$("#lang_es").click(function(){
			EGW.ui.translation.switchLanguage("es");
		});
		$("#lang_fr").click(function(){
			EGW.ui.translation.switchLanguage("fr");
		});
		$("#lang_de").click(function(){
			EGW.ui.translation.switchLanguage("de");
		});
		$("#lang_en").click(function(){
			EGW.ui.translation.switchLanguage("en");
		});
	},
	
	translate : function(){
		$("#option_register").html(EGW.ui.translation.translate('register'));
		$("#option_login").html(EGW.ui.translation.translate('login'));
		$("#option_welcome").html(EGW.ui.translation.translate('welcome')+"!");
		sf.user.switchcaption();
	},
	
	setModuleName: function(module) {
		if (typeof module == "string") {
			$("#h_modulename").html(module);
		} else {
			$("#h_modulename").html(EGW.ui.translation.translate(module.fullName));
			//$("#h_moduleicon").html("<img scr='"+baseurl+"modules/"+module.shortName+"/images/large_"+module.shortName+".png' alt='' height='100%' width='100%'/>");
			$("#h_moduleicon").css("background-image", "url("+baseurl+"modules/"+module.shortName+"/images/large_"+module.shortName+".png)");
		}
	},

	loadhome: function() {
		if (!sf.system.loading.home) {
			sf.system.loading.home = true;
			$('#h_iconbar').children().remove();
			$('#container').children().remove();
			$('#container').loadModule({
				name: 'home',
				headerbar: $('#h_iconbar'),
				textSelection: false,
				callback: function(mod) {
					sf.system.setModuleName(mod);
					mod.onRenameWindowTitle.bind("singleframe", sf.system.setModuleName);
					mod.onOpenWritings.bind("singleframe", sf.system.loadwritings);
					mod.onOpenOnlinebooks.bind("singleframe", sf.system.loadonlinebooks);
					sf.system.loading.home = false;
					sf.system.checkBrowserCompatibility(mod);
				}
			});
		}
	},
	checkBrowserCompatibility: function(mod){
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			EGW.res.loadResource("msgbox", "$.msgbox", function() {
				$.msgbox({
					title : "Information",
					message : "You are using Internet Explorer. <BR>Please have a look at our compatibility screen.",
					width: "450",
					cancel : false,
					ok_fn : function() {
						mod.openCompatibilityView();
					},
					escape_fn : function() {
						mod.openCompatibilityView();
					}
				});
			});
		}
	},	
	loadwritings: function() {
		if (!sf.system.loading.writings) {
			sf.system.loading.writings = true;
			$('#h_iconbar').children().remove();
			$('#container').children().remove();
			$('#container').loadModule({
				name: 'writings',
				headerbar: $('#h_iconbar'),
				callback: function(mod) {
					sf.system.setModuleName(mod);
					mod.onRenameWindowTitle.bind("singleframe", sf.system.setModuleName);
					sf.system.loading.writings = false;
				}
			});
		}
	},
	loadonlinebooks: function() {
		if (!sf.system.loading.onlinebooks) {
			sf.system.loading.onlinebooks = true;
			$('#h_iconbar').children().remove();
			$('#container').children().remove();
			$('#container').loadModule({
				name: "onlinebooks",
				headerbar: $('#h_iconbar'),
				callback: function(mod) {
					sf.system.setModuleName(mod);
					mod.onRenameWindowTitle.bind("singleframe", sf.system.setModuleName);
					sf.system.loading.onlinebooks = false;
				}
			});
		}
	},

	buildmenu: function() {
		var mainmenu = [{
			'caption': 'Home',
			'captionref': function() {
				sf.system.loadhome();
			},
			translate: function(){
				return 'Home';
			}
		},{
			'caption': 'Writings',
			'captionref': function() {
				sf.system.loadwritings();
			}

		},{
			'caption': 'Online Books',
			'captionref': function() {
				sf.system.loadonlinebooks();
			},
                        translate: function(){
				return 'online_books';
			}

		}];
		
		if (EGW.user.hasright("paralinker.view")){
			mainmenu.push({
				'caption': 'Para Linker',
				'captionref': function() {
					if (!sf.system.loading.paralinker) {
						sf.system.loading.paralinker = true;
						$('#h_iconbar').children().remove();
						$('#container').children().remove();
						$('#container').loadModule({
							name: "paralinker",
							callback: function(mod) {
								sf.system.setModuleName(mod);
								mod.onRenameWindowTitle.bind("singleframe", sf.system.setModuleName);
								sf.system.loading.paralinker = false;
							}
						});
					}
				}
			});
		}
		
		
		//if (EGW.user.hasright("rightsmanagement.view")){
		if (false){
			mainmenu.push({
				'caption': 'Rights Management',
				'captionref': function() {
					if (!sf.system.loading.rightsmanagement) {
						sf.system.loading.rightsmanagement = true;
						$('#h_iconbar').children().remove();
						$('#container').children().remove();
						$('#container').loadModule({
							name: "rightsmanagement",
							callback: function(mod) {
								sf.system.setModuleName(mod);
								mod.onRenameWindowTitle.bind("singleframe", sf.system.setModuleName);
								sf.system.loading.rightsmanagement = false;
							}
						});
					}
				}
			});
		}
		
		mainmenu.push({
			'caption': 'About',
			'captionref': function() {
				if (!sf.system.loading.about) {
					sf.system.loading.about = true;
					$('#h_iconbar').children().remove();
					$('#container').children().remove();
					$('#container').loadModule({
						name: 'contact',
						callback: function(mod) {
							sf.system.setModuleName(mod);
							mod.onRenameWindowTitle.bind("singleframe", sf.system.setModuleName);
							sf.system.loading.about = false;
						}
					});
				}
			}
		});
		
		
			
		$('#h_menu').menu({
			design: "singleframe",
			initStatus: mainmenu
		});
	},
	loading: {
		home: false,
		writings: false,
		onlinebooks: false,
		paralinker: false,
		about: false
	}
}

sf.user = {
	loadregistration : function() {
		EGW.user.loadregistration();
	},
	loadlogin : function() {
		if (EGW.user.loggedIn) {
			EGW.user.logout();
		} else {
			EGW.user.loadlogin();
		}
	},
	pinheader : function() {
		if ($('#option_pinheader').attr("title")=="Hide") {
			wtf.system.hideheader();
		} else {
			wtf.system.showheader();
		}
	},
	switchcaption : function() {
		if (EGW.user.loggedIn) {
			$('#option_login').text(EGW.ui.translation.translate('logout'));
			$('#option_welcome').text(EGW.ui.translation.translate('welcome') + " " + EGW.user.name + "!");
		} else {
			$('#option_login').text(EGW.ui.translation.translate('login'));
			$('#option_welcome').text(EGW.ui.translation.translate('welcome')+"!");
		}
	},
	applyRights: function() {
		sf.system.buildmenu();
	}
//,
//  pinrightside : function() {
//    if ($('#option_pinrightside').attr("title")=="Hide") {
//      //wtf.system.hiderightside();
//      InvisibleMode.hidePropertiesEditor();
//    } else {
//      wtf.system.showrightside();
//    }
//  },
//  loadhome : function() {
//    wt.desktop.load();
//  },
//  loadbooklist : function() {
//    var settings = new wt.books.settingsclass();
//    wt.books.openbooklist(settings);
//  },
//  loadfaq : function() {
//    wtf.menu.update(2);
//    wt.faq.show();
//  },
//  loadcontact : function() {
//    wtf.menu.update(3);
//    wt.contact.show();
//  }
}
