/************************************************************************
*  Typografická pravidla                                                *
*                                                                       *
*  Aplikuje některá typografická pravidla na dokument.                  *
*                                                                       *
*  Funkce SvazPredlozky() sváže jednoslabičné předložky s následujícím  *
*  slovem nedělitelnou mezerou (\160).                                  *
************************************************************************/

function OnLoad(){
        // Typografická pravidla
        window.defaultStatus = "Probíhá aplikace typografických pravidel...";
        document.getElementById("pravidla").style.visibility = "visible";
        window.setTimeout('ZpozdenaAplikaceTypografie();', 0);
}
function ZpozdenaAplikaceTypografie(){
        var oTypografie = new cTypografie();
        AplikujTypografiiNaStrom(document, oTypografie);
        window.defaultStatus = "";

}

function PopupPic(sPicURL) {
     window.open( "popup.htm?"+sPicURL, "",
     "resizable=1,HEIGHT=200,WIDTH=200");
}

function cTypografie(){
	var sa, as;
	this.sNbsp = String.fromCharCode(160);
	
	// --- Vytvoříme regulární výraz pro předložky --- //
	sa = "a ale ať aby ani až bez do i je jsem jsou již jen k ke na nad než o od ob před po pod pro při"
	   + " s se si u už v viz ve z ze za že ISBN kdy kde kdo co jak tzv.";
	sa += " and are as at but for if in of the with"; // anglicke; vynechavam "to on" - touklo by se s cestinou 
	// Eskejpujeme specialni znaky regularnich vyrazu 
	sa = sa.replace(/\./g, "\\.");
	as = sa.split(" ");
	this.rePredlozky = new RegExp("\\b(" + as.join("|") + ") ", "gi");
	this.rePredlozky.substitute = "$1" + this.sNbsp;
	
	// --- R.E. pro zalozky --- //
	sa = "Ph.D. Inc. a.s. as. s.r.o. sro.";
	// Eskejpujeme specialni znaky regularnich vyrazu 
	sa = sa.replace(/\./g, "\\.");
	as = sa.split(" ");
	this.reZalozky = new RegExp(" (" + as.join("|") + ")", "g");
	this.reZalozky.substitute = this.sNbsp + "$1";

	// --- R.E. pro dvouslovné fráze --- //
	sa = "Miner JS|Miner VGA";
	sa.replace(/\./g, "\\.");
	as = sa.split("|");
	//for(var i in as){ as[i] = "(?=" + as[i].split(" ").join(")( )(?=") + ")"; }
	//alert("\\b(" + as.join("|") + ")\\b");
	//this.reFraze = new RegExp("\\b(" + as.join("|") + ")\\b", "g");
	this.aaFraze = new Array();
	// Pole [ [vzor, nahrada], ... ]
	for(var i in as){ this.aaFraze.push( new Array(new RegExp(as[i], "g"), as[i].split(" ").join(this.sNbsp)) ); }
	
	// --- R.E. pro verze - Neco 2.0 --- //
	this.reVerze = new RegExp("([A-ZĚŠČŘŽÝÁÍÉŤĎŇÚŮÓ]\\w) (\\d+\\.\\d+)", "g");
	this.reVerze.substitute = "$1" + this.sNbsp + "$2";
	
	// --- R.E. pro iniciály - X. Yz --- //
	this.reInicialy = new RegExp("([A-ZĚŠČŘŽÝÁÍÉŤĎŇÚÓ]\\.) ([A-ZĚŠČŘŽÝÁÍÉŤĎŇÚÓ]\\w+)", "g");
	this.reInicialy.substitute = "$1" + this.sNbsp + "$2";
	
	// --- R.E. pro roky - "(abc|r.) 1997(.|,|;| )" --- //
	//this.reRoky = new RegExp("(\\w+|r\\.) (\\d\\d\\d\\d\\W)", "g");
	this.reRoky = new RegExp("(\\w+|\\b\\w{1,3}\\.) (\\d\\d\\d\\d\\W)", "g");
	this.reRoky.substitute = "$1" + this.sNbsp + "$2";
	
	// --- R.E. pro řadové číslovky - "25. narozeniny" --- //
	this.reRadovky = new RegExp("\\b(\\d+\\.) ([a-zěščřžýáíéúóďťň])", "g");
	this.reRadovky.substitute = "$1" + this.sNbsp + "$2";
}

cTypografie.prototype.SvazVsechno = function(oTextNode){
	this.SvazPredlozky(oTextNode);
	this.SvazZalozky(oTextNode);
	this.SvazFraze(oTextNode);
	this.SvazVerze(oTextNode);
	this.SvazInicialy(oTextNode);
	this.SvazRoky(oTextNode);
	this.SvazRadovky(oTextNode);
}

// Předložky - Až_navěky, o_nás, bez_nás, ...
cTypografie.prototype.SvazPredlozky = function(oTextNode){
	oTextNode.nodeValue = oTextNode.nodeValue.replace(this.rePredlozky, this.rePredlozky.substitute);
}

// Záložky - Pan X Y,_Ph.D.
cTypografie.prototype.SvazZalozky = function(oTextNode){
	oTextNode.nodeValue = oTextNode.nodeValue.replace(this.reZalozky, this.reZalozky.substitute);
}

// Fráze - "Miner JS", "Miner VGA", atd.
cTypografie.prototype.SvazFraze = function(oTextNode){
	//oTextNode.nodeValue = oTextNode.nodeValue.replace(this.reFraze, this.reFraze.substitute);
	var s = oTextNode.nodeValue;
	for(var i in this.aaFraze){
		s = s.replace(this.aaFraze[i][0], this.aaFraze[i][1]);
	}
	oTextNode.nodeValue = s;
}

// Verze - Neco 2.0 
cTypografie.prototype.SvazVerze = function(oTextNode){
	oTextNode.nodeValue = oTextNode.nodeValue.replace(this.reVerze, this.reVerze.substitute);
}

// Iniciály - X. Yz 
cTypografie.prototype.SvazInicialy = function(oTextNode){
	oTextNode.nodeValue = oTextNode.nodeValue.replace(this.reInicialy, this.reInicialy.substitute);
}

// Roky - Neco 1997 
cTypografie.prototype.SvazRoky = function(oTextNode){
	oTextNode.nodeValue = oTextNode.nodeValue.replace(this.reRoky, this.reRoky.substitute);
}

// Řadové číslovky - "25. narozeniny"
cTypografie.prototype.SvazRadovky = function(oTextNode){
	oTextNode.nodeValue = oTextNode.nodeValue.replace(this.reRadovky, this.reRadovky.substitute);
}




// AplikujTypografiiNaStrom(doc, oTypografie) 
function AplikujTypografiiNaStrom(doc, oTypografie){
	if(1 && doc.implementation.hasFeature("Traversal","2.0")){
		// Je k dispozici rozhrani DOM Traversal //
		var callbackFilterFunc = function(oNode){
			return oNode.nodeName != "pre" && oNode.nodeName != "code" && oNode.nodeType == 3 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
		}
		var noteIter = doc.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, callbackFilterFunc, false);
		var oNodeIter; while( oNodeIter = noteIter.nextNode() ){
			oTypografie.SvazVsechno(oNodeIter);
		}
	}else{
		// Neni k dispozici rozhrani DOM Traversal //
		var fFunc = function(oNode){
			// Přeskočíme uzly kódu 
			if(oNode.nodeName == "pre" || oNode.nodeName == "code") return;
			
			// Pokud ma tento element ID, pridame do seznamu. //
			if(3 == (oNode.nodeType||oNode.type)){
				oTypografie.SvazVsechno(oNode);
			}
			// Projedem deti touto funkci.           //
			var aChildren = (oNode.childNodes||oNode.children);
			for(var i in aChildren){
				arguments.callee(aChildren[i]);
			}
		}
		// Zavolame rekurzivni fci na dokument v argumentu doc //
		fFunc(doc);
	}
}

/*** Usage:

	var oTypografie = new cTypografie();
	if(!window.bTypografieAplikovana){
		AplikujTypografiiNaStrom(document, oTypografie);
		window.bTypografieAplikovana = true;
	}
	
*/
