//charset=utf-8
function XSLTProc(sxml, sxsl, holderId){
    this.holder=$(holderId);
    if (window.ActiveXObject) {
        var xslt = new ActiveXObject("Msxml2.XSLTemplate");
        var xsl = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
        xsl.async = false;
        xsl.resolveExternals = false;
        xsl.load(sxsl);
        xslt.stylesheet = xsl;
        this.xml = new ActiveXObject("Msxml2.DOMDocument");
        this.xml.async = false;
        this.xml.resolveExternals = false;
        if(sxml != '')
            this.xml.load(sxml);
        this.proc = xslt.createProcessor();
        this.setParam=function(name, value){this.proc.addParameter(name, value);};
        this.go=function(){
            this.proc.input = this.xml;
            this.proc.transform();
            this.holder.innerHTML=this.proc.output;
        }

        this.setXML=function(newXML){
            this.xml.load(newXML);
        }
        this.loadXML=function(newXML){
            xmlstr=newXML.value.replace('&','&amp;');
            this.xml.loadXML(xmlstr);
        }


    } else {
       this.proc = new XSLTProcessor();
       var myXMLHTTPRequest = new XMLHttpRequest();
       myXMLHTTPRequest.open("GET", sxsl, false);
       myXMLHTTPRequest.send(null);
       var xslStylesheet = myXMLHTTPRequest.responseXML;
       this.proc.importStylesheet(xslStylesheet);
       if(sxml != ''){
           myXMLHTTPRequest = new XMLHttpRequest();
           myXMLHTTPRequest.open("GET", sxml, false);
           myXMLHTTPRequest.send(null);
           this.xml = myXMLHTTPRequest.responseXML;
       }

       this.setParam=function(name, value){this.proc.setParameter(null, name, value);};

       this.go=function(){
           var fragment = this.proc.transformToFragment(this.xml, document);
           this.holder.innerHTML = "";
           this.holder.appendChild(fragment);
       }

       this.setXML=function(newXML){
           myXMLHTTPRequest = new XMLHttpRequest();
           myXMLHTTPRequest.open("GET", newXML, false);
           myXMLHTTPRequest.send(null);
           this.xml = myXMLHTTPRequest.responseXML;
       }

        this.loadXML=function(newXML){
            //this.xml = document.implementation.createDocument("", "", null);
            //var clonedNode = this.xml.importNode(node, true);
            //this.xml.appendChild(clonedNode);
            var parser = new DOMParser();
            var doc = parser.parseFromString(newXML.innerHTML, "text/xml");
            this.xml=doc;
        }

       return false;
    }
}


if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; }

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}

		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; }

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}


function importNodeIE(doc,node, allChildren) {
    switch (node.nodeType) {
      case 1:
        var newNode = doc.createElement(node.nodeName);
        if (node.attributes && node.attributes.length > 0)
          for (var i = 0; i < node.attributes.length; i ++)
            newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i].nodeName));
        if (allChildren && node.childNodes && node.childNodes.length > 0)
          for (var i = 0; i< node.childNodes.length; i ++){
          	newNode.appendChild(importNodeIE(doc,node.childNodes[i], allChildren));
          }
        return newNode;
        break;
      case 3:
        return doc.createTextNode(node.nodeValue);
        break;
    }
};



var gs = 0;
var curStr=1;
var curPub, curGod, curNomer, curStr;
var izd_shown=false;
var minpic, mindivid;
var tmout;
var requester=false;
var verh=180; // 150 px werh yakor


function getRequester()
{
  var xmlHttp;
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    for (var i=0; i < XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {}
    }
  }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}


function showpub(pub,god,nomer,str){
	if (pub != ''){
		var xpub=proc.xml.selectSingleNode("pressaru/pub[@codename='"+pub+"']");
		var ppub=xpub.getElementsByTagName('pub');
		if(ppub.length==0){
			if(!requester){
				requester=getRequester();
			}
			var d=xpub.getAttribute('d');
			requester.open("GET", "xml/"+pub+".xml?d="+d,false);
			requester.send(null);
			var resp=requester.responseXML;
			var node=resp.documentElement;
			if(proc.xml.importNode){
				node=proc.xml.importNode(node,true);
			}else{
				node=importNodeIE(proc.xml,node,true);
			}
			xpub.appendChild(node);
		}
		if(curPub!=pub || curGod!=god || curNomer!=nomer){
			if(!requester){
				requester=getRequester();
			}
			requester.open("GET", "log.php?p="+pub+'&g='+god+'&n='+nomer, true);
	        requester.send(null);
		}
	}
	curPub=pub;
	curGod=god;
	curNomer=nomer;
	curStr=str;
	proc.setParam('pub',pub);
	proc.setParam('god',god);
	proc.setParam('nomer',nomer);
	proc.setParam('str',str);
	otkr_str=false;
	izd_shown=false;
	proc.go();
	window.scrollTo(50,verh); // 180 px werh yakor
}

function arhiv(a){
	proc.setParam('arhiv',a);
	showpub('','','',1);
}

function sort(srt){
	proc.setParam('sort',srt);
	proc.go();
}

function pl(srt){
	proc.setParam('pl',srt);
	proc.go();
}

function show_izd(){
	if(izd_shown){
		hide("nav-izd-txt");
		izd_shown=false;
	}else{
		show("nav-izd-txt");
		izd_shown=true;
	}
}

function next_str(v,max_str){
	curStr+=v;
	if(curStr>max_str){
		curStr=1;
	}else if(curStr==0){
		curStr=max_str;
	}/*else{
		while(!proc.xml.selectSingleNode("pressaru/pub[@codename='"+curPub+"']/god[@n='"+curGod+"']/nomer[@n='"+curNomer+"']/str[@n='"+curStr+"']")){
			curStr+=v;
		}
	}*/
	proc.setParam('str',curStr);
	proc.go();
	window.scrollTo(50,verh); // 150 px werh yakor
}



function gsdvig(g){
	gs+=g;
	proc.setParam('gsdvig',gs);
	proc.go();
}

function str(n){
	curStr+=n;
	proc.setParam('str',curStr);
	proc.go();
}

function swich_min(pic){
	minpic=pic;
	tmout=setTimeout(setminpic,300);// lubaja miniatura
}

function setminpic(){
	$('min').src=minpic;
}

function noswich_min(pic){
	clearTimeout(tmout);
	minpic=pic;
	tmout=setTimeout(setminpic,700);// aktualnaja miniatura
}


function show(id){
	$(id).style.display='block';
}
function hide(id){
	$(id).style.display='none';
}

function $(id){
	return document.getElementById(id);
}

function germ(n){
	proc.setParam('germ',n);
	if(n==1){
		//$('pressa-zemli').style.display='none';
		var pubs = proc.xml.getElementsByTagName('pub');
		for(var p=0;p<pubs.length;p++){
			pubs[p].removeAttribute('hide');
		}
		proc.go();
	}else{
		$('pressa-zemli').style.display='block';
		zemli(-1);
	}
}

function zemli(n){
	if(n!=-1) proc.setParam('zemlya'+n,$('zemlya'+n).checked?1:0);
	if($('reg').checked){
		var zems, p, z, hide;
		var pubs = proc.xml.getElementsByTagName('pub');
		for(p=0;p<pubs.length;p++){
			zems=pubs[p].getElementsByTagName('zem');
			if(zems.length==0){
				if($('zemlya0').checked){
					pubs[p].removeAttribute('hide');
				}else{
					pubs[p].setAttribute('hide','1');
				}
			}else{
				//alert(zems[0].firstChild.nodeValue);
				hide=true;
				for(z=0;z<zems.length;z++){
					if($('zemlya'+zems[z].firstChild.nodeValue).checked){
						hide=false;
						break;
					}
				}
				if(hide){
					pubs[p].setAttribute('hide','1');
				}else{
					pubs[p].removeAttribute('hide');
				}
			}
		}
		proc.go();
	}
}

//--------------------- zamena img w uglu--------------------
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//----------------------- Zoom na str------------------------


var curzoom;
curzoom=62.5;

function zoomin(){
 if(curzoom){
   curzoom+=10;
 }else{
  curzoom=72.5;
 }
 document.body.style.fontSize=curzoom+'%';
}

function zoom0(){
 curzoom=62.5;
 document.body.style.fontSize=curzoom+'%';
}

function zoomout(){
 if(curzoom){
  if(curzoom>62.5){
    curzoom-=10;
  }
 }else{
  curzoom=62.5;
 }
 document.body.style.fontSize=curzoom+'%';
}

//----------------------- print------------------------

function druck(text){
text = document;
print(text);
}


