﻿/*
	Util.Web.Controls
	Dependencies: Util, JQuery
	Used by NavInvalidMessage.ascx
			SubMenuPopUpCtl.ascx
*/
Util.ensureNamespacePath("Util.Web.Controls");
/*=======================================================================*/

/*-------------------------------------------------------------*/

	Util.Web.Controls.NavInvalidMessage_Init = function(aUniqueIDPrefix, aAnc) {
		//var aAnc = $("a[href*='ProgramNotAvailable']")[0];
		var aPUEle = $("div[id$='"+aUniqueIDPrefix+"puMain']")[0];
		var aPU = new Util.PopUp.cPopUp(new Object(), aPUEle, aAnc, {mModal:true,mEnableShowToggle:true});
		//aPU.mEleForLoc = null;
		$(aPUEle).find("a[href$='?OK']").click(function(event) {
			aPU.hide();
			return false;
		});
	}
	
/*-------------------------------------------------------------*/

	Util.Web.Controls.SubMenuPopUpCtl_Init = function(aUniqueIDPrefix, aAncURL) {
		//Util.consoleInfo(aUniqueIDPrefix + ", " + aAncURL);
		//If aPUAnc is null, this will work fine. The PU is created and is invisible.
		var aPUAnc = $("a[href$='" + aAncURL + "']")[0];
		var aPUReportsSubMenu = $("div[id='" + aUniqueIDPrefix + "puSubMenuPopCtl']")[0];
		var aPUReportsSubMenuPopUp = new Util.PopUp.cPopUp(new Object(), aPUReportsSubMenu, aPUAnc,
			{mEnableShowToggle:true});
	}

/*=======================================================================*/
/*
	aProps on Init input (others set manually):
	mTimeout: seconds, <= 0, no timeout
	mKeepAliveDisplay: see eKeepAliveDisplays
	mKeepAliveCntnrDisp: css display types
*/
	Util.Web.Controls.KeepAlive_Init = function(aRootClientID, aProps) {
		var aRootEle = document.getElementById(aRootClientID);
		aRootEle.mProps = aProps;
		aRootEle.mProps.mKeepAliveInError = false;
		aRootEle.mKeepAliveStatusObj = null;

		Util.Web.Controls.KeepAlive_vdateDisplay(aRootEle);
		Util.Web.Controls.KeepAlive_startTimer(aRootEle, aRootEle.mProps.mTimeout);
	}
	
/*-------------------------------------------------------------*/

	Util.Web.Controls.KeepAlive_startTimer = function(aRootEle, aTimeout) {
		if (aTimeout <= 0)
			return;
		aRootEle.mTimer = setTimeout("Util.Web.Controls.KeepAlive_onTimeout(" +
					"'" + aRootEle.id + "')",
				aTimeout * 1000);
	}
	
/*-------------------------------------------------------------*/

	Util.Web.Controls.KeepAlive_onTimeout = function(aRootClientID) {
		var aRootEle = document.getElementById(aRootClientID);

		/*Util.consoleInfo("Timeout: " + aRootEle.id);
		Util.Web.Controls.KeepAlive_startTimer(aRootEle, aRootEle.mProps.mTimeout);*/
		var aLoader = new Net.cContentLoader("?u="+Util.uniqueURLValue() +
				"&Svc=" + aRootEle.id + "&Cmd=keepAliveTimer",
				Util.Web.Controls.KeepAlive_keepAliveTimerComplete,
				Util.Web.Controls.KeepAlive_keepAliveTimerError);
		aLoader.mRootEle = aRootEle;
		aLoader.loadData();
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.KeepAlive_keepAliveTimerComplete = function() {
		var aLoader = this;
		var aRootEle = aLoader.mRootEle;

		try {
			var aXMLDoc = $.parseXML(aLoader.mRequest.responseText);
			var aResult = $(aXMLDoc).find("Status").text();
			//Util.consoleInfo(aResult);
			if (aResult == "Ok") {
				Util.Web.Controls.KeepAlive_vdateDisplay(aRootEle);

				aRootEle.mProps.mTimeout = parseInt($(aXMLDoc).find("KeepAliveParams>Timeout").text());
				//Util.consoleInfo("*** "+aRootEle.mProps.mTimeout);
				Util.Web.Controls.KeepAlive_startTimer(aRootEle, aRootEle.mProps.mTimeout);
			}
			else {
				//window.open("http://localhost:1390/InfoRAP20/Home/SignIn.aspx?email=ward.winer@me.gatech.edu&pass=LTTWT", "_blank");
				Util.Web.Controls.KeepAlive_keepAliveTimerError.call(aLoader);
			}
		}
		catch (aErr) {
			Util.Web.Controls.KeepAlive_keepAliveTimerError.call(aLoader);
		}
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.KeepAlive_keepAliveTimerError = function() {
		var aLoader = this;
		var aRootEle = aLoader.mRootEle;

		aRootEle.mProps.mKeepAliveInError = true;
		Util.Web.Controls.KeepAlive_vdateDisplay(aRootEle);
		//Util.Web.Controls.KeepAlive_startTimer(aRootEle, aRootEle.mProps.mTimeout);
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.KeepAlive_vdateDisplay = function(aRootEle) {
		if (!aRootEle.mProps.mKeepAliveDisplay ||
				aRootEle.mProps.mKeepAliveDisplay == "None") {
			//default display is none
			return;
		}
		
		var aCntnrEle = $(aRootEle).find("div.id_KeepAliveDispCntnr")[0];
		if (!aCntnrEle)
			return;
		$(aCntnrEle).css("display", (!aRootEle.mProps.mKeepAliveCntnrDisp ? "block" : aRootEle.mProps.mKeepAliveCntnrDisp));
		
		if (!aRootEle.mKeepAliveStatusObj) {
			aRootEle.mKeepAliveStatusObj = new Object();
			aRootEle.mKeepAliveStatusObj.mCount = -1;
			aRootEle.mKeepAliveStatusObj.mCountMax = 4; //default
		}

		aRootEle.mKeepAliveStatusObj.mCount += 1;
		if (aRootEle.mKeepAliveStatusObj.mCount >= aRootEle.mKeepAliveStatusObj.mCountMax)
			aRootEle.mKeepAliveStatusObj.mCount = 0;

		if (aRootEle.mProps.mKeepAliveDisplay == "Count") {
			aRootEle.mKeepAliveStatusObj.mCountMax = aRootEle.mKeepAliveStatusObj.mCount + 2;
			var aTxtStr = String(aRootEle.mKeepAliveStatusObj.mCount);
			$(aCntnrEle).text(aTxtStr);
			if (aRootEle.mProps.mKeepAliveInError) {
				$(aCntnrEle).css("color", "red");
			}
		}
		else if (aRootEle.mProps.mKeepAliveDisplay == "ASCIIAsterisk") {
			var aTxtStr = "";
			for (var i = 1; i <= aRootEle.mKeepAliveStatusObj.mCount; i++)
				aTxtStr += "*";

			$(aCntnrEle).text(aTxtStr);
			if (aRootEle.mProps.mKeepAliveInError) {
				$(aCntnrEle).css("color", "red");
			}
		}
		else if (aRootEle.mProps.mKeepAliveDisplay == "ImgFillIn") {
			aRootEle.mKeepAliveStatusObj.mCountMax = 4;

			var aImgs = $(aCntnrEle).find(">img");
			if (aImgs.length == 0) {
				//no images, go get them
				for (var i = 0; i < 4; i++) {
					aImg = document.createElement("img");
					aImg.src = Util.applNameFromLocation() +
									"/wwwLibs/Util_Web/Images/" +
									"KeepAlive_FillIn_" + i + ".gif";
					aImg.border = "0";
					aImg.style.display = "none";
					aCntnrEle.appendChild(aImg);
				}
			}

			$(aCntnrEle).find(">img").css("display", "none");
			if (aRootEle.mProps.mKeepAliveInError) {
				var aImg = $(aCntnrEle).find(">img")[0];
				if (aImg) {
					$(aImg).attr("src",
						$(aImg).attr("src").replace("KeepAlive_FillIn_0", "KeepAlive_FillIn_Er_3"));
					$(aImg).css("display", "inline");
				}
			}
			else {
				$(aCntnrEle).find(">img[src$='" +
						"KeepAlive_FillIn_" + aRootEle.mKeepAliveStatusObj.mCount + ".gif" + "']").css("display", "inline");
			}
		}
	}

/*=======================================================================*/
/*
	aProps on Init input (others set manually):
	* mEndSlotCount, mMaxSlotCount, mRowCount
	* mCurRowNum, mRowsPerPage
*/
	Util.Web.Controls.cPaginationCtl = function(aRootEle, aProps) {
		this.mRootEle = aRootEle;
		this.mProps = aProps;
		this.mVDated = null;

		var aParamsPUEle = $(this.mRootEle).find("div[id$='puParams']")[0];
		this.mEditParamsPU = new Util.PopUp.cPopUp(this, aParamsPUEle,
				null,
				{mModal: true, mEscapeable:true});

		/*-----------------------*/

		this.vdateCalc();
		this.vdateRender();

		var aThis = this;
		$(this.mRootEle).find("a[href$='Previous']").click(function() {
			aThis.test();
			return false;
		});

		$(this.mEditParamsPU.mPopUpEle).find("a[href$='Ok']").click(function() {
			aThis.resetParams();
			return false;
		});
		Util.passKeyToFunc($(this.mEditParamsPU.mPopUpEle).find("input[type='text']")[0],
				{13:0},
				function() {
			aThis.resetParams();
		});

		//Util.consoleInfo("id: " + this.mProps.mMaxSlotCount);
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.vdateCalc = function() {
		if (!this.mVDated)
			this.mVDated = new Object();
		this.mVDated.mFreeSlotCount = this.mProps.mMaxSlotCount - (this.mProps.mEndSlotCount * 2);
		this.mVDated.mCtrSlot = Math.floor((this.mVDated.mFreeSlotCount-1) / 2) + this.mProps.mEndSlotCount;

		this.mVDated.mPageCount = this.mProps.mRowCount <= 0 ? 1 :
				Math.floor((this.mProps.mRowCount-1)/this.mProps.mRowsPerPage) + 1;
		this.mVDated.mCurPageNum = Math.floor(this.mProps.mCurRowNum/this.mProps.mRowsPerPage);

		if (!this.mVDated.mBreak)
			this.mVDated.mBreak = new Object();
		if (this.mVDated.mPageCount > this.mProps.mMaxSlotCount) {
			var aBreakLeft = this.mVDated.mCurPageNum > this.mVDated.mCtrSlot;
			var aBreakRight = this.mVDated.mPageCount-this.mVDated.mCurPageNum > this.mProps.mMaxSlotCount-this.mVDated.mCtrSlot;
			if (aBreakLeft && aBreakRight) {
				this.mVDated.mBreak.mLeft = this.mVDated.mCurPageNum - (this.mVDated.mCtrSlot-(this.mProps.mEndSlotCount+1));
				this.mVDated.mBreak.mRight = this.mVDated.mCurPageNum +
						((this.mProps.mMaxSlotCount-this.mProps.mEndSlotCount-2) - this.mVDated.mCtrSlot);
			}
			else if (aBreakRight) {
				this.mVDated.mBreak.mRight = this.mProps.mMaxSlotCount-this.mProps.mEndSlotCount-2;
			}
			else if (aBreakLeft) {
				this.mVDated.mBreak.mLeft = this.mVDated.mPageCount-1 - (this.mProps.mMaxSlotCount - this.mProps.mEndSlotCount-2);
			}
		}
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.createSlotCntnrEle = function(aParCntnrEle) {
		var aCntnrEle = document.createElement("div");
		$(aCntnrEle).css({width:"2.25em",float:"left","margin-right":"-1px",border:"1px solid #909829"/*,
				"background-color": "#EDD8AC"*/});
		if (aParCntnrEle)
			$(aParCntnrEle).append(aCntnrEle);
		return aCntnrEle;
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.vdateRender = function() {
		var a$RowsLabel = $(this.mRootEle).find(".id_RowCount");
		a$RowsLabel.text(a$RowsLabel.text().replace("{0}", this.mProps.mRowCount));
		
		var a$RowsLabel = $(this.mRootEle).find(".id_RowsPerPage");
		a$RowsLabel.text(a$RowsLabel.text().replace("{0}", this.mProps.mRowsPerPage));

		var a$SlotsCntnr = $(this.mRootEle).find(".id_CntnrSlots");
		a$SlotsCntnr.empty();

		for (var aPgNum = 0; aPgNum < this.mVDated.mPageCount; aPgNum++) {
			var aCntnrEle = this.createSlotCntnrEle();
			a$SlotsCntnr.append(aCntnrEle);
			$(aCntnrEle).text((aPgNum+1).toString());

			if (aPgNum == this.mVDated.mCurPageNum) {
				$(aCntnrEle).css({"font-weight":"bold"})
			}
			else {
				$(aCntnrEle).addClass("id_CntnrPgNum")
			}

			if (aPgNum == (this.mProps.mEndSlotCount-1) &&
					this.mVDated.mBreak.mLeft) {
				var aBreakEle = this.createSlotCntnrEle();
				a$SlotsCntnr.append(aBreakEle);
				$(aBreakEle).text("...")
					.css({border:"none",cursor: "default"});
				aPgNum = this.mVDated.mBreak.mLeft - 1; //loop increments it...
			}
			else if (this.mVDated.mBreak.mRight &&
					aPgNum == this.mVDated.mBreak.mRight) {
				var aBreakEle = this.createSlotCntnrEle();
				a$SlotsCntnr.append(aBreakEle);
				$(aBreakEle).text("...")
					.css({border:"none"});
				aPgNum = this.mVDated.mPageCount - this.mProps.mEndSlotCount - 1; //loop increments it...
			}
		}

		var aThis = this;
		a$SlotsCntnr.find(">div.id_CntnrPgNum")
			.css({cursor: "pointer"})
			.mouseover(function() {
				$(this).css({"background-color": "#EDD8AC"});
			})
			.mouseout(function() {
				$(this).css({"background-color": "white"});
			})
			.click(function() {
				//alert($(this).text());
				aThis.gotoRowNum(parseInt($(this).text())-1);
				return false;
			});

		$(this.mRootEle).find("a[href$='RowsP']").click(function() {
			aThis.showParamsEditPU(this);
			return false;
		});
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.getRowNumForPageNum = function(aPageNum) {
		return this.mProps.mRowsPerPage * aPageNum;
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.gotoRowNum = function(aPageNum) {
		var aNewRowNum = this.getRowNumForPageNum(aPageNum);

		$.ajax({data: {u: Util.uniqueURLValue(),
				Svc: this.mRootEle.id, Cmd: 'gotoRowNum',
				ReqURL: window.location.toString(),
				NewRowNum: aNewRowNum
			},
			context: this,
			success: function(aXMLDoc, aStatus, aReq) {
				this.gotoRowNumComplete(aXMLDoc, aStatus, aReq);
			}
		});
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.gotoRowNumComplete = function(aXMLDoc, aStatus, aReq) {
		var aResult = $(aXMLDoc).find("Results>Status").text();
		if (aResult != "Ok")
			return;
		var aURL = $(aXMLDoc).find("Results>URL").text();
		window.location = aURL;
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.showParamsEditPU = function(aAnc) {
		//Net.cContentLoader.toggleTableRowsLoading($(this.mEditParamsPU.mPopUpEle).find("table tr.id_ToggleLoadingStatusRow").closest("table")[0]);
		var a$Txt = $(this.mEditParamsPU.mPopUpEle).find("input[type='text']");
		a$Txt.attr("value", this.mProps.mRowsPerPage);
		
		var aThis = this;
		this.mEditParamsPU.show(false, function() {
			a$Txt[0].focus();
		});
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.resetParams = function() {
		var aNewValue = $(this.mEditParamsPU.mPopUpEle).find("input[type='text']").attr("value");
		if (aNewValue == this.mProps.mRowsPerPage || aNewValue <= 0) {
			this.mEditParamsPU.hide();
			return;
		}
		
		Net.cContentLoader.toggleElement($(this.mEditParamsPU.mPopUpEle).find("a[href$='Ok']")[0], null);

		$.ajax({data: {u: Util.uniqueURLValue(),
				Svc: this.mRootEle.id, Cmd: 'resetParams',
				ReqURL: window.location.toString(),
				NewRowsP: aNewValue
			},
			context: this,
			success: function(aXMLDoc, aStatus, aReq) {
				this.resetParamsComplete(aXMLDoc, aStatus, aReq);
			}
		});
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.resetParamsComplete = function(aXMLDoc, aStatus, aReq) {
		var aResult = $(aXMLDoc).find("Results>Status").text();
		if (aResult != "Ok")
			return;
		var aURL = $(aXMLDoc).find("Results>URL").text();
		window.location = aURL;
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.test = function() {
		var aThis = this;
		var aLoader = new Net.cContentLoader("?u="+Util.uniqueURLValue() +
				"&Svc=" + this.mRootEle.id + "&Cmd=test" +
				"&ReqURL="+encodeURIComponent(window.location) + 
				"&ReqURL2="+window.location,
				{mSrcObj:this, mOnload:Util.Web.Controls.cPaginationCtl.prototype.testComplete});
		aLoader.loadData();
	}

/*-------------------------------------------------------------*/

	Util.Web.Controls.cPaginationCtl.prototype.testComplete = function(aLoader) {
	}

/*=======================================================================*/
//End Util.Web.Controls
/*=======================================================================*/


