﻿
var ScrollToGrid = false;
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnRefresh);
function OnRefresh(sender, args) {
	if (ScrollToGrid) {
		if (window != null) { window.scrollTo(0, 350); }
	}
	else {
		if (window != null) { window.scrollTo(0, 0); }
		ScrollToGrid = true;
	}
}

function OnInitializeLayoutHandler() {
	var grid = igtbl_getGridById(Page.wgMatches.id);
	var rows = grid.Rows;
}

function OnRowActivate(gridId, rowId) {
	return true;
}

function ClearGrid() {
	var grid = igtbl_getGridById(Page.wgMatches.id);
	var rows = grid.Rows;

	while (rows.length > 0) {
		rows.getRow(0).remove();
	}

	var htmlElement = grid.MainGrid;
	htmlElement.style.height = "80px";

	if (htmlElement.firstChild.childNodes.length == 2)
		htmlElement.firstChild.lastChild.style.display = "none";
}

function RefreshGrid() {
	//		<!%=Me.Page.GetPostBackClientEvent(Me.wgMatches, "")%>;
}

function ShowLoadingPanel() {
	ClearGrid();

	$get("LoadingPanel").style.display = "block";
}

function HideLoadingPanel() {
	$get("LoadingPanel").style.display = "none";
}

function GetMatchesId() {
	var inputs = document.getElementsByTagName("input");
	var StrMatchesId = "";

	for (var i = 0, length = inputs.length; i < length; i++) {
		var input = inputs[i];

		if (input.type == "checkbox" && input.attributes.gridcheckbox != null && input.checked == true) {
			StrMatchesId += input.value + ",";
		}
	}

	Page.StrMatchesID.value = StrMatchesId;
}

function togglePlanding(positionId) {
	var cont = $("*[id$=planding_" + positionId + "]");

	var url = "http://" + positionInfoUrl + "/Landing/NPPLanding/";
	url += positionId

	if (cont.data('isLoaded') != true) {
		cont.data('isLoaded', true);
		cont.closest("td").css("backgroundColor", 'White');

		cont.slideDown(function () {
			iconSpan = cont.closest("tr").find("span.openJobDescIcon");
			//return;
			cont.load(url, function () {
				iconSpan.removeClass("openJobDescIcon").addClass("closeJobDescIcon");
				iconSpan[0].attributes["title"].value = "Hide summary";
				$.cookie('NPHomePageLastClickedPId', positionId);
				$.cookie('NPHomePageLastOpenPId', positionId, { expires: 10 });
			})
		});
	}
	else {
		cont.slideToggle(function () {
			if (cont[0].style.display != "none") {
				cont.closest("td").css("backgroundColor", 'White');
				iconSpan = cont.closest("tr").find("span.openJobDescIcon");
				iconSpan.removeClass("openJobDescIcon").addClass("closeJobDescIcon");
				iconSpan[0].attributes["title"].value = "Hide summary";
				$.cookie('NPHomePageLastOpenPId', positionId, { expires: 10 });
			}
			else {
				cont.closest("td").css("backgroundColor", '');
				iconSpan = cont.closest("tr").find("span.closeJobDescIcon");
				iconSpan.removeClass("closeJobDescIcon").addClass("openJobDescIcon");
				iconSpan[0].attributes["title"].value = "Show job summary";
				$.cookie('NPHomePageLastOpenPId', null);
			}
		})
	}
}

function OpenPositionPreview(positionId) {
	location.href = "http://jobs.thejobnetwork.com/planding/" + positionId + "#PreviewAd";
	return false;
}
function OpenVideo(positionId, VideoId) {
	location.href = "http://jobs.thejobnetwork.com/planding/" + positionId + "#Video_" + VideoId;
	return false;
}

function OnGridRowClick(positionId) {
	var key = "ck_NPHomePage_Time_" + positionId
	$.cookie(key, new Date(), { path: '/' });
	$.cookie('NPHomePageLastClickedPId', positionId);
}

//Scroll to last clicked position
function ScrollToLastPositionClicked(NPHomePageLastClickedPId) {

	var lastTrViewd = $('a[href$=' + NPHomePageLastClickedPId + ']').closest("tr");

	if (lastTrViewd != null) {
		var lastOpenOffset = lastTrViewd.offset();

		if (typeof (lastOpenOffset) != "undefined" && lastOpenOffset != null) {
			window.scrollTo(0, lastOpenOffset.top);
		}
	}
}
