function addEvent(elm, evType, fn, useCapture) {
  // cross-browser event handling for IE5+, NS6 and Mozilla 
  // By Scott Andrew 
  if (elm.addEventListener) { 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
}



// Open external links in a new window
// By Bj&#195;&#182;rn Carlsson / kraftvaerk.net
function handleExternalLinks(){
	if (!document.getElementsByTagName) return;
	var i, href;
	var anchors = document.getElementById("content");
	if (anchors) anchors = anchors.getElementsByTagName("a");
	for(i=0; i < anchors.length; i++)
	{
		if(!anchors[i].href) continue;
		href = anchors[i].href;		
		if ( (href.indexOf(location.hostname) == -1) && (href.indexOf("javascript:") == -1) &&  (href.indexOf("mailto:") == -1) && (href.indexOf("http://") != -1)) 
		{
			if ((anchors[i].className.indexOf("extlink") == -1) && (anchors[i].className.indexOf("internal") == -1)) anchors[i].className += " extlink";
			anchors[i].setAttribute("target","_blank");
			//anchors[i].target="_blank";
		}
	}
}

addEvent(window, 'load', handleExternalLinks, false);

// handlePhotoCaptions:
// set all <p> within a <div.frame> to the same width as the first <img> within that div...
// By Bj&#195;&#182;rn Carlsson / kraftvaerk.net
/* without try...catch:
function handlePhotoCaptions() {
		var frame = document.getElementsByTagName("div");
		for (i=0; i < frame.length; i++) {
			if (frame[i].className.indexOf("frame") != -1) {
				w = frame[i].getElementsByTagName("img");
				p = frame[i].getElementsByTagName("p");
				if ((w) && (p)) for (j=0; j < frame.length; j++) {
					if(p[j]) p[j].style.width = w[0].offsetWidth +'px';
				}

			}
		}	
}
*/
function handlePhotoCaptions() {
	try {
		var frame = document.getElementsByTagName("div");
		for (i=0; i < frame.length; i++) {
			if (frame[i].className.indexOf("frame") != -1) {
				w = frame[i].getElementsByTagName("img");
				p = frame[i].getElementsByTagName("p");
				for (j=0; j < frame.length; j++) {
					p[j].style.width = w[0].offsetWidth +'px';
				}
			}
		}	
	} catch(er) {
		return false;
	}
}
addEvent(window, 'load', handlePhotoCaptions, false);


/* Tips section...*/
var tips = {
	container: null,
	init:	function(obj) {
		this.container = document.getElementById(obj);
		if (!this.container) return;
		
		curr = 0;
		for (i=0; i<this.container.childNodes.length;i++) {
			if ((this.container.childNodes[i].nodeName == "DIV") && (this.container.childNodes[i].className.indexOf("tip")!=-1)) {
				this.tip[curr] = {
					box: this.container.childNodes[i],
					title: this.container.childNodes[i].getElementsByTagName("h2")[0],
					text: this.container.childNodes[i].getElementsByTagName("DIV")[0],
					init: function() {
						this.title = this.createA(this.title);
						this.title.ref = curr;
						
						if (this.box.className.indexOf("open") != -1) {this.show();} else {this.hide();}
					},
					
					createA: function(menu) {
						var a, text;
						text = menu.firstChild;
						a = document.createElement("a");
						a.href = "#";
						menu.replaceChild(a, text);
						a.appendChild(text);
						return a;
					},
					hide: function() {
						this.text.style.display="none";
						this.box.className = "tip";
						this.title.className ="tiptitle"
					},
					show: function() {
						this.text.style.display="block";
						this.box.className = "tip open";
						this.title.className ="tiptitle open"
					},
					toggle: function() {
						if (this.text.style.display=="none") {
							this.show()
						} else {
							this.hide();
						}
						
					}
				};
				
				this.tip[curr].init();
				this.tip[curr].title.onclick = function(){
					tips.tip[this.ref].toggle();
					return false;
				}
				curr++;
			}
		}
		if (v = this.container.getElementsByTagName("img")) {
			for (i=0; i<v.length; i++){
				v[i].className +="frame";
			}	
		}
	},
	
	tip: []
}

/* Profil login */

//Henter og returnere vardien af en cookie
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		{
			return c.substring(nameEQ.length,c.length);
		}
	}
	
	return false;
}

//Fjerne forste i sidste char i strengen
function parseString(str)
{
	var dst = "";
	for (var i = 1; i < str.length-1; i++)
	{
	 	dst += str.charAt(i);
	}
	return dst;
}

function fillLoginBox()
{
	
	var cookieInfo = readCookie('falckmagasinet');
	
	//var inputName = document.getElementById('name');
	//var inputPassword = document.getElementById('password');
	
	if (cookieInfo) {
		cookieInfo = cookieInfo.split(',');
		$('name').value = parseString(cookieInfo[0]);
		$('password').value = parseString(cookieInfo[1]);
	}
}