( function() {
	var header = new Component("header");
	
	app.registerComponent(header);
	header.on( "load", function(){
		var self = this;
	    return header.render("template/header.html", "header", {})
	    .done(function(){
	    	//add the userName in header
	    	$("#welcome b").html(app.user.userId);
	    	$("#lastLoginTime #time").html(": "+app.user.lastLoginTime + ")");
	    	self.buttonEvent();
	    });
	});
	
	function closeWindow(){
		try{
			netscape.security.PrivilegeManageer.enablePrivilege("UniversalBrowserWrite");
		}
		catch(e){
			console.log(e);
		}
		top.open("", "_self");
		top.close();   
		window.location = app.EPWEB_CONTEXT_ROOT + "closeWindow.html";
	}
	
	function askLogout(){
		return dialog.show("<p><span msgid='msgConfirmLogout'>" + lang.getMessage('msgConfirmLogout') + "</span></p>", lang.getMessage('confirm'), lang.getMessage('buttonCacel')).done( function(btnName) {
			if (btnName === lang.getMessage('confirm')){
				
				//ajaxCall({url:app.EPRESTAPI_CONTEXT_ROOT + "access/logout", dataType: "text"}).always( function(){
					if (app.user.role.checkRole("hasAnyRole('WMD_Intranet')")) {
						addLogoutRecord(false);
						addAccessLogout();
						closeWindow();
					} else {
						addLogoutRecord(true);
						$("#popupDivBackground").show();
						$("#popupDivBackground").css('background','#EEE');
						app.mainComponent.showLoading(app.mainComponentDivId,true);
						ajaxCall({url: "/IDM/UI/Logout", dataType: "html"}).always( function(){
							closeWindow();						
						});
					}
					
				//});
			}
		});
	}
	
	function askExit(){
		return dialog.show("<p><span msgid='msgConfirmExit'>" + lang.getMessage('msgConfirmExit') + "</span></p>", lang.getMessage('confirm'), lang.getMessage('buttonCacel')).done(function (btnName) {
			if (btnName === lang.getMessage('confirm')) {
				ajaxCall({url:this.EPRESTAPI_CONTEXT_ROOT + "access/logout", dataType: "json"}).done( function(){
					closeWindow();
				});
			}
		});    	
	}
	
	header.buttonEvent = function() {

	    $("#exit").die().live("click", function(){
	    	askLogout();
	    	/*if (app.user.role.checkRole("hasAnyRole('WMD_Clients')")){
	    		askLogout();
	    	} else if (app.user.role.checkRole("hasAnyRole('WMD_RMs','WMD_CSOs','WMD_SUPER')")){
	    		dialog.show("<p><span msgid='msgConfirmExitLogout'>Are you sure you want to logout and exit?</span></p>", "Exit", "Logout", "Cancel")
	    		.done( function(btnName) {
	    			if (btnName === "Exit"){
	    				askExit();
	    			}
	    			else if (btnName === "Logout"){
	    	    		askLogout();
	     			}
	    		});
	    	} else {
				askExit();
	    	}*/
	    });
	    
	    /*$("#closeBtn").live("click", function(){
	        $("#intranetWarningOverlay").hide();
	        $("#intranetWarningOverlayBackground").hide();
	    });*/
	};
	window.header = header;
})();