var LoginShortcut = {};
LoginShortcut.Fader = new Utilities.UI.Fader();
LoginShortcut.Fader.Speed = 10;

LoginShortcut.ActiveLayer = null;
LoginShortcut.ShadowLayer = null;
LoginShortcut.IsHomePage = true;
LoginShortcut.LoginLink = null;
LoginShortcut.OnReloadPage = null;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.ShowLayer = function(Link){

	LoginShortcut.LoginLink = (Link != null) ? Link : document.getElementById("LoginLink");
	
	var HttpRequest = new CHttpRequest();
	HttpRequest.Additional = LoginShortcut.LoginLink;
	HttpRequest.AddProperty("AjaxType", "HTML");
	HttpRequest.AddProperty("RequestFile", "Passport.UI.LogIn");
	HttpRequest.AddProperty("MethodName", "XGetLayer");
	HttpRequest.AddProperty("Arguments[SubmitButtonHandlerCode]", "LoginShortcut.DoLogin()");
	HttpRequest.OnResponse = LoginShortcut.ShowLayer_Callback;
	HttpRequest.Send();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.ShowLayer_Callback = function(HttpRequest, state){
	var bIsIE6 = document.all && !window.XMLHttpRequest;
	//alert(bIsIE6);
	
	var Link = state;
	var arCoords = Utilities.DOM.GetCoordinates(Link);
	
	LoginShortcut.ShadowLayer = document.createElement("DIV");
	//LoginShortcut.ShadowLayer.style.display = "none";
	LoginShortcut.ShadowLayer.style.position = bIsIE6 ? "absolute" : "fixed";
	LoginShortcut.ShadowLayer.style.top = "0px";
	LoginShortcut.ShadowLayer.style.left = "0px";
	LoginShortcut.ShadowLayer.style.zIndex = 502;
	//LoginShortcut.ShadowLayer.style.width = "1600px";
	//LoginShortcut.ShadowLayer.style.height = "600px";
	LoginShortcut.ShadowLayer.style.width = "100%";
	LoginShortcut.ShadowLayer.style.height = "100%";
	LoginShortcut.ShadowLayer.style.backgroundColor = "#000000";
	LoginShortcut.ShadowLayer.tabIndex = -1;
	//LoginShortcut.ShadowLayer.onclick = alert;
	
	Controls.UI.Effects.SetOpacity(LoginShortcut.ShadowLayer, 50);
	
	var Layer = document.createElement("DIV");
	Layer.id = "LoginLayer";
	Layer.className = "LoginLayer";
	Layer.style.top = arCoords[1] + Link.offsetHeight +"px";
	Layer.style.left = (arCoords[0] + Link.offsetWidth - 350) + "px";
	Layer.style.position = bIsIE6 ? "absolute" : "fixed";
	Layer.style.zIndex = 503;
	
	Layer.style.width = "350px";
	//Controls.UI.Effects.SetOpacity(Layer, 100);
	
	var Container = document.createElement("DIV");
	Container.id = "LoginLayerContainers";
	Container.className = "LoginLayerContainer";
	
	Container.innerHTML = HttpRequest.responseText;
	
//	var oShadow = new Utilities.UI.LayerShadow();
//	oShadow.Object = Layer;
	
	Layer.appendChild(Container);
	
	LoginShortcut.ActiveLayer = Layer;
	//document.body.insertBefore(Layer, document.body.childNodes[0]);
	//document.body.insertBefore(LoginShortcut.ShadowLayer, Layer);
	//LoginShortcut.ShadowLayer.appendChild(Layer);
	document.body.insertBefore(LoginShortcut.ShadowLayer, document.body.childNodes[0]);
	document.body.insertBefore(Layer, document.body.childNodes[0]);
	
	if(bIsIE6){
		Utilities.DOM.ChangeComboboxesVisibility("hidden");
		//BodyTag.style.height = "100%";
		document.body.style.overflowY = "hidden";
	}
	//PauseTicker();
	//LoginShortcut.Fader.SetOpacity(Layer, 100);
	//LoginShortcut.Fader.Fade(Layer.id, 10, 1, LoginShortcut.Fader_CallBack);
	
	//oShadow.Render();
	
	Utilities.Events.RegisterEventHandler(Layer, "onclick", LoginShortcut.Layer_Click);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.Fader_CallBack = function(sID, nOpacity){
    PauseTicker();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.DoLogOut = function(link){
	LoginShortcut.LoginLink = link;
	var HttpRequest = new CHttpRequest();
	HttpRequest.AddProperty("AjaxType", "XML");
	HttpRequest.AddProperty("RequestFile", "Passport.UI.LogIn");
	HttpRequest.AddProperty("MethodName", "XLogOut");
	HttpRequest.onReturn = "LoginShortcut.DoLogOut_Callback";
	HttpRequest.Send();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.DoLogOut_Callback = function(HttpRequest, sAdditionalData){
	//window.location.reload();
	//PageLoader.Execute();
	LoginShortcut.RefreshLoginInfo();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.DoLogin = function(){
	//Utilities.Events.CancelBubble(pEvtObj);
	var EMailField = document.getElementById("Email");
	var PasswordField = document.getElementById("Password");
	var RememberIDField = document.getElementById("RememberID");
	var RememberPasswordField = document.getElementById("RememberPassword");
	var SecurityImageValueField = document.getElementById("SecurityImageValue");
	var AutoLoginField = document.getElementsByName("AutoLogin")[0];
	
	if(!Controls.DataValidation.CheckValue(EMailField, false)){
		return;
	}
	if(!Controls.DataValidation.CheckValue(PasswordField, false)){
		return;
	}
	if(!Controls.DataValidation.CheckValue(SecurityImageValueField, false)){
		return;
	}
	
	
	var HttpRequest = new CHttpRequest();
	HttpRequest.AddProperty("AjaxType", "XML");
	HttpRequest.AddProperty("RequestFile", "Passport.UI.LogIn");
	HttpRequest.AddProperty("MethodName", "XLogIn");
	HttpRequest.AddProperty("Arguments[Email]", EMailField.value);
	HttpRequest.AddProperty("Arguments[Password]", PasswordField.value);
	if(AutoLoginField.checked)
		HttpRequest.AddProperty("Arguments[AutoLogin]", "1");
	if(RememberIDField.checked)
		HttpRequest.AddProperty("Arguments[RememberID]", RememberIDField.value);
	if(RememberPasswordField.checked)
		HttpRequest.AddProperty("Arguments[RememberPassword]", RememberPasswordField.value);
	HttpRequest.AddProperty("Arguments[SecurityImageValue]", SecurityImageValueField.value);
	HttpRequest.OnResponse = LoginShortcut.DoLogin_Callback;
	HttpRequest.Send();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.DoLogin_Callback = function(HttpRequest, state){
	
	if(HttpRequest.responseXML.childNodes.length == 0){
		alert("There was a problem while logging in. Please try again later or visit the SeePassport Site.");
		return;
	}
	
	var ResponseNode = (document.all) ? HttpRequest.responseXML.childNodes[1] : HttpRequest.responseXML.childNodes[0];
	
	var ResultNode = (ResponseNode != null) ? ResponseNode.childNodes[0] : null;
	var MessageNode = (ResponseNode != null) ? ResponseNode.childNodes[1] : null;
	
	if(ResultNode == null || ResultNode.tagName != "result"){
		alert("There was a problem while logging in. Please try again later or visit the SeePassport Site.");
	}
	
	if(ResultNode.childNodes[0].nodeValue == "0"){
		alert(MessageNode.childNodes[0].nodeValue);
	}else{
		LoginShortcut.HideLayer();
		LoginShortcut.RefreshLoginInfo();
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.RefreshLoginInfo = function(){
	window.location.reload();
	
//	LoginShortcut.LoginLink.innerHTML = "(Retrieving Login Info, Please Wait...)";
//	LoginShortcut.LoginLink.title = LoginShortcut.LoginLink.innerHTML;
//	LoginShortcut.LoginLink.onclick = null;
//	
//	var HttpRequest = new CHttpRequest();
//	HttpRequest.AddProperty("AjaxType", "XML");
//	HttpRequest.AddProperty("RequestFile", "Controls.Header");
//	HttpRequest.AddProperty("MethodName", "XGetLoginInfo");
//	HttpRequest.OnResponse = LoginShortcut.RefreshLoginInfo_Callback;
//	HttpRequest.Send();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.RefreshLoginInfo_Callback = function(request, state){
	
	var isLoggedIn = request.responseXML.getElementsByTagName("IsLoggedIn")[0].childNodes[0].nodeValue == "1";
	var text = request.responseXML.getElementsByTagName("LoginText")[0].childNodes[0].nodeValue;
	var handler = request.responseXML.getElementsByTagName("LoginAction")[0].childNodes[0].nodeValue;
	
	document.getElementById("RegisterLinkCell").style.display = (isLoggedIn) ? "none" : "";
	document.getElementById("RegisterLinkCell").nextSibling.style.display = (isLoggedIn) ? "none" : "";
	
	LoginShortcut.LoginLink.innerHTML = text;
	LoginShortcut.LoginLink.title = LoginShortcut.LoginLink.innerHTML;
	LoginShortcut.LoginLink.onclick = new Function(handler);
	LoginShortcut.ReloadPage();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.ReloadPage = function(){
	if(LoginShortcut.IsHomePage)
		Controls.TemplateLoader.Load();
	
	var list = document.getElementById("ArticleList");
	if(list != null){
		list.Refresh();
		if(list.SelectedItem != null){
			SearchResults.DisplayView(list.GetValue(list.SelectedItem));
		}
	}
	if(LoginShortcut.OnReloadPage != null){
		LoginShortcut.OnReloadPage(this, null);
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.Layer_Click = function(pEvtObj){
	Utilities.Events.CancelBubble(pEvtObj);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.HideLayer = function(){
	if(LoginShortcut.ActiveLayer == null)
		return;
	Utilities.DOM.RemoveNode(LoginShortcut.ActiveLayer);
	Utilities.DOM.RemoveNode(LoginShortcut.ShadowLayer);
	LoginShortcut.ShadowLayer = null;
	LoginShortcut.ActiveLayer = null;
	var bIsIE6 = document.all && !window.XMLHttpRequest;
	if(bIsIE6){
		Utilities.DOM.ChangeComboboxesVisibility("visible");
		var BodyTag = document.body;
		BodyTag.style.overflowY = "";
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.Body_Click = function(pEvtObj){
	LoginShortcut.HideLayer();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LoginShortcut.Body_KeyPress = function(pEvtObj){
	if(pEvtObj.keyCode == 27)
		LoginShortcut.HideLayer();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Utilities.Events.RegisterEventHandler(document.documentElement, "onclick", LoginShortcut.Body_Click);
Utilities.Events.RegisterEventHandler(document.documentElement, "onkeypress", LoginShortcut.Body_KeyPress);

