﻿//  Functions for external links and popups
addEvent(document, 'click', function(e) {
	theObject = (window.event) ? window.event.srcElement : e.target;
	if(theObject.tagName.toLowerCase() == "a") {
        var relIndex = theObject.rel;
		if (relIndex){
			if(relIndex.indexOf("external") != -1) {
				theObject.target = "_blank";
			}
			if(relIndex.indexOf("popup")!=-1) {
				theHref = theObject.href;
				theObject.href = "javascript:void(0)";
				var popupWidth, popupHeight
				var relSplit = relIndex.split("|");
				if(relSplit[1]){
					popupWidth = relSplit[1];
					popupHeight = relSplit[2];
		        } else {
					popupWidth = 435;
					popupHeight = 350;
	        	}
	        	popMeUp(theHref,popupWidth,popupHeight);
	        }
		}
	}
})
//  adding titles to popup- and external links
addEvent(document, 'mouseover', function(e) {
	theObject = (window.event) ? window.event.srcElement : e.target;
	if(theObject.tagName.toLowerCase() == "a") {
        var relIndex = theObject.rel;
		if (relIndex){
			if(relIndex.indexOf("external") != -1) {
				theObject.title = "Öppnas i nytt fönster";
			}
			if(relIndex.indexOf("popup")!=-1) {
				theObject.title = "Öppnas i popupfönster";
	        }
		}
	}
})

function popMeUp(strURL,strWidth,strHeight) {
	theWin = window.open(strURL, 'popupWin', "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
	theWin.focus()
}
addEvent(window, 'load', function() {
      var searchCleaner = false
      var searchCleanerText
      var theSearchField
      if(!document.getElementById("searchField")) return;
      theSearchField = document.getElementById("searchField");
      searchCleanerText = theSearchField.value;
      searchCleaner = true;
      addEvent(theSearchField, 'focus', function() {
            if(searchCleaner == false) return;
            if(theSearchField.value == searchCleanerText) {
                  theSearchField.value = "" ;
            }
      })
      addEvent(theSearchField, 'blur', function() {
            if(searchCleaner == false) return;
            if(theSearchField.value == "") {
                  theSearchField.value = searchCleanerText;
            }
      })
})
addEvent(window, 'load', function(){
	if (!window.print) return;
	if(document.getElementById("printBox")) {
		thePrintBox = document.getElementById("printBox");
		thePrintButton = createElement("p");
		thePrintButton.className = "link floatRight"
		thePrint = createElement("a");
		thePrint.classname="printBut"
		thePrint.href="javascript:window.print()"
		thePrintText = document.createTextNode("Skriv ut");

		theArrow = createElement("em");
		theArrowText = document.createTextNode("> "); //createTextNode(entity("&raquo;") + " ")
		theArrow.appendChild(theArrowText);
		thePrint.appendChild(theArrow);
		thePrint.appendChild(thePrintText);
		
		thePrintButton.appendChild(thePrint);
		thePrintBox.insertBefore(thePrintButton, thePrintBox.firstChild);
	}
})

function createElement(element) {
  if (typeof document.createElementNS != 'undefined') {
    return document.createElementNS('http://www.w3.org/1999/xhtml', element);
  }
  if (typeof document.createElement != 'undefined') {
    return document.createElement(element);
  }
  return false;
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
