//var localDebug = true;
var localDebug = false;
var templUrl = "auth_pages.xtmpl?Key=#k#&page=#p#";
var templPk = "Key_#k#__page_#p#";

//----------------------------------------------------
function checkContent()
{
	var a = getElements_A_ById("pgn");
	if(a.length != 0) return;
	var pkKey = getKey(window.location.href,"pk");

//????	
	if(localDebug)
	{
		if(pkKey == null) pkKey = templPk.replace("#k#",24508).replace("#p#",201);
	}
//????	
	if(pkKey != null)
	{
		var url = "auth_pages.xtmpl?" + pkKey.replace("__","&").replace("_","=").replace("_","=");
//alert(url);
		window.location = url;
		return;
	}

/*
if(document.getElementById("pgn") == null)
{
	var pkKey = getKey(window.location.href,"pk");
}
*/
}

//----------------------------------------------------
function prev(pnKey,pnPage)
{
var url = "";
var path = "";
var curKey = getKey(window.location.href,"Key");
var pgn0 = getPgn0();
var pgn9 = getPgn9();
if(curKey == pnKey)
{
path+="0";
url = templUrl.replace("#k#",pnKey).replace("#p#",pnPage);
window.location = url;
return;
}
path+="1";
if(curKey != null)
{
	path+="2";
	if(pgn0 != null && pgn9 != null)
	{
		path+="3";
//alert("" + Math.abs(pnPage-pgn0));
//		if(Math.abs(pnPage-pgn0) == 20)
//		{
			path+="4";
			url = templUrl.replace("#k#",curKey).replace("#p#",pgn0-1);
			//alert(path+" ["+url);
			window.location = url + "&pk=" + templPk.replace("#k#",pnKey).replace("#p#",pnPage);
			return;
//		}
	}
}
path+="5";
url = templUrl.replace("#k#",pnKey).replace("#p#",pnPage);
//alert(path+" ["+url);
window.location = url;
return;
}
//----------------------------------------------------
function next(pnKey,pnPage)
{
var url = "";
var path = "";
var curKey = getKey(window.location.href, "Key");
var pgn0 = getPgn0();
var pgn9 = getPgn9();
if(curKey == pnKey || Math.abs(pgn9-pgn0) < (20-1))
{
path+="0";
url = templUrl.replace("#k#",pnKey).replace("#p#",pnPage);
window.location = url;
return;
}
path+="1";
if(curKey != null)
{
	path+="2";
	if(pgn0 != null && pgn9 != null)
	{
		path+="3";
//alert("" + Math.abs(pnPage-pgn9));
//		if(Math.abs(pnPage-pgn9) == 1)
//		{
			path+="4";
			url = templUrl.replace("#k#",curKey).replace("#p#",pgn9+1);
//alert(path+" ["+url);
			window.location = url + "&pk=" + templPk.replace("#k#",pnKey).replace("#p#",pnPage);
			return;
//		}
	}
}
path+="5";
url = templUrl.replace("#k#",pnKey).replace("#p#",pnPage);
//alert(path+" ["+url);
window.location = url;
}

//----------------------------------------------------
function getKey(href,key)
{
try
{ 
	var curKey = href;
//????	
	if(localDebug)
	{
		//var s = "http://www.sakharov-center.ru/asfcd/auth/auth_pages.xtmpl?Key=24504&page=198"
		var s = "http://www.sakharov-center.ru/asfcd/auth/auth_pages.xtmpl?Key=24508&amp;page=201"
		if( curKey.split("?").length < 2) curKey = s;
	}
//????	
	if( curKey.split("?").length < 2) return null;
	curKey = curKey.split("?")[1];
	var found = false;
	var a = curKey.split("&");
	for(i=0; i<a.length; i++)
	{
//alert("[" + a[i].split("=")[0] + "] [" + key + "] =" + (a[i].split("=")[0]==key));

		if(a[i].split("=")[0]==key)
		{
//alert("found " + a[i].split("=")[1]);
			curKey=a[i].split("=")[1];
			found = true;
			break;
		}
	}
	if(found) 
	{
//alert(curKey);
		var icurKey;
		icurKey=parseInt(curKey, 10);
		if(isNaN(icurKey))
			return curKey;
		else
			return icurKey;
	}
	return null;
}catch(e){ 
	return null;
}	
}
function getPgn0()
{
try
{ 
	var a = getElements_A_ById("pgn");
	var pgnLen = a.length;
	var pgn0 = a[0].innerHTML;
	return parseInt(pgn0, 10)
	
//	var pgnLen = $("pgn").length;
//	var pgn0 = $("pgn")[0].innerText;
//	return parseInt(pgn0, 10)
}catch(e){ 
	return null;
}	
}
function getPgn9()
{
try
{ 
	var a = getElements_A_ById("pgn");
	var pgnLen = a.length;
	var pgn9 = a[pgnLen-1].innerHTML;
	return parseInt(pgn9, 10)

//	var pgnLen = $("pgn").length;
//	var pgn9 = $("pgn")[pgnLen-1].innerText;
//	return parseInt(pgn9, 10)
}catch(e){ 
	return null;
}	
}
function getElements_A_ById($id) {

	var elements = document.getElementsByTagName('A');
	var len = elements.length;
	var nodes = new Array();
	for(var i=0; i<len; i++)
	{
		if(elements[i].id == $id)
			nodes[nodes.length] = elements[i];
	}
    return nodes;
}

//function $(eID)
//{	try{
//		//return document.all(eID); //
//		//return document.getElementById(eID+"*"); 
//		return ElementsById(eID);
//	}catch(e){ return null; }
//}

