var lastLi = false;
var thePopup = null;
var selTab = null;
var selBox = null;

// opens windows
function openWin(theURL,winName,features) {
  window.open(theURL,winName,features);
}
// nospam

function convertEmails(){
	spans = document.getElementsByTagName("span");
	numSpans = spans.length;
	for(a = 0; a < numSpans; a++){
		if(spans[a]){
			if(spans[a].className == "epost"){
				at = new RegExp("\\(snabel\-a\\)", "i");
				punkt = new RegExp("\\(punkt\\)", "ig");
				address = spans[a].firstChild.data.replace(at, "@").replace(punkt, ".");
				theLink = document.createElement("A");
				theLink.setAttribute("href", "mailto:" + address);
				theLink.className = "email";
				theLink.appendChild(document.createTextNode(address));
				spans[a].replaceChild(theLink, spans[a].firstChild);
			}
		}
	}
}

function nospam(user,domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}

function selectCurrentPage(ul_id, li_position){
	var currentPage = location.href;
	var ul = document.getElementById(ul_id);
	if (ul) {
		var lis = ul.getElementsByTagName("li");
		var numLis = lis.length;
		for (i = 0; i < numLis; i++) {
			if (lis[i] && lis[i].className == "sub") {
				var linkPage = lis[i].childNodes[li_position].getAttribute("href");
				if (currentPage == linkPage) {
					lis[i].className = "currentPage";
				}
			}
		}
	}
}

function closeIt() {
	while (thePopup && thePopup.open && !thePopup.closed) {
    	thePopup.close();
   	}
	return true;
}

function viewLarge(imgURL, titleTxt, imgWidth, imgHeight) {
	var leftPos = (screen.width - imgWidth) / 2;
	var topPos = (screen.height - imgHeight) / 2;
    var theURL = "http://www.jsm2008.se/viewlarge.php?titleTxt=" + escape(titleTxt) + "&imgURL=" + imgURL + "&imgWidth=" + imgWidth + "&imgHeight=" + imgHeight;
	if (closeIt()) {
    	thePopup = window.open(theURL,'viewLarge','toolbar=0,status=0,directories=0,scrollbars=0,resizable=1,menubar=0,location=0,width=' + imgWidth + ',height=' + imgHeight + ',left=' + leftPos + ',top=' + topPos);
    	thePopup.focus();
	}
	else {
		viewLarge(imgURL, titleTxt, imgWidth, imgHeight);
	}
}


function init(){

   	selectCurrentPage("mainMenu",0); // 0 taggar mellan <li> och <a>
   	selectCurrentPage("subMenu",0); // 1 tag mellan <li> och <a>. I detta fall <h2>

   	convertEmails();
   //	setCoFoldOut();

   	var links = document.getElementsByTagName("a");
   	var num = links.length;
   	for(var i = 0; i < num; i++){
       	links[i].setAttribute("onfocus","this.blur()");
   	}

}

function showBox(_id, _btn){
	hideBox();
	var self = this;
	var box = document.getElementById(_id);
	box.style.display = "block";
	if(selTab){
		selTab.className = selTab.className.replace(/selected/i, "");
	}
	selTab = _btn;
	_btn.className += "selected";
	selBox = _id;
	document.onmouseup = hideBox;
}

function hideBox() {
	document.onmouseup = null;
	if(selBox){
		var box = document.getElementById(selBox);
		box.style.display = "none";
		if(selTab){
			selTab.className = selTab.className.replace(/selected/i, "");
		}
		selTab = null;
		selBox = null;
	}
}
