// Render content that should be altered dynamically client-side
function RenderDynamicContent() {
	Nav3Render();
	LeftBoxRender();
}

// Renders 3. navigation menu based on nav3menu definition if exists on page.
function Nav3Render() {
	var nav3def = document.getElementById("nav3menu");
	var nav3dest = document.getElementById("nav3");
	if (nav3def && nav3dest) {
		var nav3defLinks = nav3def.getElementsByTagName("a");
		var nav3html = "";
		for(var i=0; i<nav3defLinks.length; i++) {
			var tdclass = "nav3Unselected";
			var linkText = Trim(nav3defLinks[i].innerHTML);
			if (linkText && linkText.indexOf("!") == 0) {
				linkText = linkText.substr(1);
				tdclass = "nav3Selected";
			}
			nav3html += "<td class='" + tdclass + "'><a href='" + nav3defLinks[i].href + "'>" + linkText + "</a></td>\n";
		}
		nav3html = "<table border='0' cellspacing='0' cellpadding='0'><tr><td class='nav3Outer'>&nbsp;</td>\n" + nav3html + "<td class='nav3Outer'>&nbsp;</td></tr></table>";
		nav3def.parentNode.removeChild(nav3def);
		nav3dest.innerHTML = nav3html;
	}
}

// Moves left box to another position
function LeftBoxRender() {
	var boxDef = document.getElementById("leftBox");
	var boxDest = document.getElementById("leftBoxPosition");
	if (boxDef && boxDest) {
		boxDest.innerHTML = boxDef.innerHTML;
		boxDef.innerHTML = "";
	}
}

// Remove trailing spaces from string
function Trim(str) {
	if (str == null || str == "") return str;
	var i=0;
	while(str[i] == " " || str[i] == "\t" || str[i] == "\n" || str.charCodeAt(i) == 13) {
		i++;
	}
	return(str.substr(i));
}

//
// Dynamic tooltip
//
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var tooltip = null;

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function TooltipShow(msg){
	if (ns6||ie){
		tooltip = document.createElement("div");
		tooltip.id = "dynamictooltip";
		document.body.appendChild(tooltip);
		tooltip.innerHTML = msg;
		document.onmousemove = TooltipPosition;
		return false;
	}
}

function TooltipPosition(e){
	var offsetxpoint = -60;
	var offsetypoint = 20;
	var curX=(ns6)?e.pageX : event.x+ietruebody().scrollLeft;
	var curY=(ns6)?e.pageY : event.y+ietruebody().scrollTop;
	//Find out how close the mouse is to the corner of the window
	var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
	var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
	
	var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000
	
	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<tooltip.offsetWidth)
	//move the horizontal position of the menu to the left by it's width
	tooltip.style.left=ie? ietruebody().scrollLeft+event.clientX-tooltip.offsetWidth+"px" : window.pageXOffset+e.clientX-tooltip.offsetWidth+"px"
	else if (curX<leftedge)
	tooltip.style.left="5px"
	else
	//position the horizontal position of the menu where the mouse is positioned
	tooltip.style.left=curX+offsetxpoint+"px"
	
	//same concept with the vertical position
	if (bottomedge<tooltip.offsetHeight)
	tooltip.style.top=ie? ietruebody().scrollTop+event.clientY-tooltip.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tooltip.offsetHeight-offsetypoint+"px"
	else
	tooltip.style.top=curY+offsetypoint+"px"
}

function TooltipHide(){
	if (ns6||ie){
		document.body.removeChild(tooltip);
		document.onmousemove = null;
	}
}

// Login input box filter
function CleanNumberBox(e) {
	if(e.value.length>0) {
		e.value = e.value.replace(/[^\d]+/g, ''); 
	}
}

function CheckNumberBox(e)
{
	if (e.match(/0[0-9]{8}/g) != null)
	{
		return true;
	}

	alert("Telefonska stevilka je nepravilna. Stevilko morate vnesti brez presledkov (npr. 041700700).");
	return false;
}


//vrabec modul

var IMG_OVER = "_over";

function mOvr(img) {
	var dotIndex = img.src.lastIndexOf('.');
	var firstPart = img.src.substring(0, dotIndex);
	var ext = img.src.substring(dotIndex);
	img.src = firstPart + IMG_OVER + ext;
}

function mOut(img) {
	var dotIndex = img.src.lastIndexOf('.');
	var firstPart = img.src.substring(0, dotIndex);
	var ext = img.src.substring(dotIndex);
	if (firstPart.substring(dotIndex - IMG_OVER.length) == IMG_OVER) {
		var withoutOver = firstPart.substring(0, firstPart.length - IMG_OVER.length);
		img.src = withoutOver + ext;
	}
}

//============================================
// set page tilte in max view 
//============================================


function getElementsByClassName(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function setMaxViewPageTitle()
{
	var t = getElementsByClassName(document, 'div', 'title');

	var l = 0;
	while (l < t.length)
	{
		if (t[l].hasChildNodes() && t[l].firstChild.tagName.toUpperCase() == 'H1')
		{
			document.title = t[l].firstChild.innerHTML;
			break;
		}
		l++;
	}
}