var Home  = {};
Home._popupContainer = null;
Home._popupContainerBackground = null;
Home._currentCloseHandler = null;

Home.Window_Load = function(e){
	Home.CheckIE6Sizes();
}

Home.CheckOverflow = function(){
	var footer = document.getElementById("PageFooter");
	if(footer != null){
		footer.style.display = "none";
		var body = document.documentElement;
		var overflow = false;
		if(body.scrollHeight > body.clientHeight){
			footer.style.position = "static";
			overflow = true;
		}else{
			footer.style.position = "fixed";
		}
		footer.style.display = "";
	}
	Home.CheckIE6Sizes();
}

Home.CheckIE6Sizes = function(){
	var browser = Controls.BrowserHelper.GetBrowserName(true);
	if(browser != "IE6")
		return;
	
	var body = document.documentElement;
	var footer = document.getElementById("PageFooter");
	if(footer == null)
		return;
	footer.style.display = "none";
	var overflow = body.scrollHeight > body.clientHeight;
	footer.style.display = "";
	if(!overflow){
		var backgroundContainer = document.getElementById("CenterBackgroundContainerCell");
		var leftMenuContainer = document.getElementById("LeftMenuContainer");
		var header = document.getElementById("PageHeader");
		footer.style.position = "static";
		var height = body.clientHeight - footer.offsetHeight - header.offsetHeight;
		backgroundContainer.style.paddingBottom = "0px";
		backgroundContainer.style.height = height + "px";
		leftMenuContainer.style.height = backgroundContainer.style.height;
	}
}

Home._currentLoadingTextAlpha = 100;
Home._currentLoadingIntervalCookie = null;
Home._currentFadeDirection = -1;

Home.StartAnimating = function(){
	Home._currentLoadingTextAlpha = 100;
	Home._currentLoadingIntervalCookie = window.setInterval(Home.AnimateLoadingText, 50);
}

Home.AnimateLoadingText = function(){
	var loadingText = document.getElementById("LoadingText");
	if(Home._currentLoadingTextAlpha == 100)
		Home._currentFadeDirection = -1;
	else if(Home._currentLoadingTextAlpha == 0)
		Home._currentFadeDirection = 1;
		
	Home._currentLoadingTextAlpha += Home._currentFadeDirection * 10;
	Controls.UI.Effects.SetOpacity(loadingText, Home._currentLoadingTextAlpha);
}

Home.StopAnimating = function(){
	window.clearInterval(Home._currentLoadingIntervalCookie);
	Home._currentLoadingTextAlpha = 100;
}

Home.LoadTemplate = function(name, arguments, usePopup, onCloseHandler){
	var loader = new Controls.TemplateLoader();
	var template = new Controls.TemplateLoader.Template("TemplateLoader", "XHandleRequest",  "PublicControls", "TemplateContainer", Home.LoadTemplate_Callback, Home.TemplateRender_Callback);
	template.RequestType = "XML";
	template.CreateInstance = true;
	template.Arguments["Template"] = name;
	template.Arguments["UsePopupLayout"] = usePopup ? "1" : "0";
	if(arguments != null){
		for(var argName in arguments){
			template.Arguments[argName] = arguments[argName];
		}
	}
	loader.Add(template);
	
	if(!usePopup){
		var footer = document.getElementById("PageFooter");
		if(footer != null)
			footer.style.position = "fixed";
		var container = document.getElementById("TemplateContainer");
		container.innerHTML = "<div id='LoadingText' class='LoadingText'>Зарежда се...</div>";
		Home.StopAnimating();
		Home.StartAnimating();
		var indexContainer = document.getElementById("PageIndexContainer");
		if(indexContainer != null)
			indexContainer.innerHTML = "Зарежда се...";
	}
	
	if(typeof(onCloseHandler) == "function")
		Home._currentCloseHandler = onCloseHandler;
	else
		Home._currentCloseHandler = null;
	
	loader.Load();
}

Home.TemplateRender_Callback = function(sender, e){
	//	alert(e.Request.responseText);
	if(e.Request.responseXML.parseError != null && e.Request.responseXML.parseError.reason != "" && e.Request.responseXML.parseError.reason != null){
		var errorText = e.Request.responseXML.parseError.reason + ", line: "+ e.Request.responseXML.parseError.line +", linepos: "+ e.Request.responseXML.parseError.linepos;
		alert("Възникна грешка при заявката:\r\n\r\n"  + errorText + "\r\n\r\n"+ e.Request.responseText);
		return;
	}
	// check for expired session
	var error = e.Request.responseXML.getElementsByTagName("expiredSessionError");
	if(error != null && error.length > 0){
		alert("Сесията Ви е изтекла. Трява да се логнете отново.");
		document.location.href = "?";
		return;
	}
	// check errors
	var error = e.Request.responseXML.getElementsByTagName("error");
	if(error != null && error.length > 0){
		alert("ERROR: "+ error[0].childNodes[0].nodeValue);
		return;
	}
	
	var indexTag = e.Request.responseXML.getElementsByTagName("pageIndex");
	if(indexTag != null && indexTag.length > 0)
		var pageIndexCode = e.Request.responseXML.getElementsByTagName("pageIndex")[0].childNodes[0].nodeValue;
	var isPopupLayout = e.Request.responseXML.getElementsByTagName("popupLayout")[0].childNodes[0].nodeValue == "1";
	var templateCode = e.Request.responseXML.getElementsByTagName("template")[0].childNodes[0].nodeValue;
	var indexContainer = document.getElementById("PageIndexContainer");
	var templateContainer = document.getElementById("TemplateContainer");
	
	if(isPopupLayout){
		var div = Home.CreatePopupContainer();
		Controls.Ajax.LoadContent(div, templateCode);
		div.focus();
		// ignore the index
	}else{
		Home.StopAnimating();
		if(indexContainer != null)
			Controls.Ajax.LoadContent(indexContainer, pageIndexCode);
		Controls.Ajax.LoadContent(templateContainer, templateCode);
		Home.CheckOverflow();
	}
}

Home.CreatePopupContainer = function(){
	if(Home._popupContainer == null){
		
		Home._popupContainer = document.body.appendChild(document.createElement("DIV"));
		Home._popupContainer.id = "PopupContainer";
		Home._popupContainer.className = "PopupContainer";
		Controls.EventHelper.RegisterEventHandler(Home._popupContainer, "onkeydown", Home.Popup_KeyDown);
		
		Home._popupContainerBackground = document.body.appendChild(document.createElement("DIV"));
		Home._popupContainerBackground.id = "PopupContainerBackground";
		Home._popupContainerBackground.className = "PopupContainerBackground";
		Controls.UI.Effects.SetOpacity(Home._popupContainerBackground, 60);
		
		var browser = Controls.BrowserHelper.GetBrowserName(true);
		if(browser == "IE6"){
			Home._popupContainerBackground.style.position = "absolute";
			var body = document.documentElement;
			window.attachEvent("onscroll", Home.Body_Scroll);
			Home._popupContainerBackground.style.width = body.clientWidth;
			Home._popupContainerBackground.style.height = body.clientHeight;
			Home._popupContainer.style.position = "absolute";
		}
	}
	Home._popupContainer.style.visibility = "visible";
	Home._popupContainerBackground.style.visibility = "visible";
	return Home._popupContainer;
}

Home.Body_Scroll = function(e){
	if(Home._popupContainerBackground != null){
		Home._popupContainerBackground.style.top = document.documentElement.scrollTop;
		Home._popupContainer.style.top = (document.documentElement.clientHeight / 10) + document.documentElement.scrollTop;
	}
}

Home.LoadTemplate_Callback = function(sender, e){
	
}

Home.ClosePopup = function(){
	var e = new Object();
	e.Cancel = false;
	if(Home._currentCloseHandler != null)
		Home._currentCloseHandler(Home._popupContainer, e);
	if(!e.Cancel){
		if(Home._popupContainer != null){
			Home._popupContainer.style.visibility = "hidden";
			Home._popupContainerBackground.style.visibility = "hidden";
			Home._popupContainer.innerHTML = "";
		}else{
			window.close();
		}
	}
}

Home.PopupClose_Click = function(sender, e){
	Home.ClosePopup();
}

Home.Popup_KeyDown = function(e){
	if(e.keyCode == 27)
		Home.ClosePopup();
}