// http://www.thefutureoftheweb.com/blog/adddomloadevent
addDOMLoadEvent=(function(){var e=[],t,s,n,i,o,d=document,w=window,r='readyState',c='onreadystatechange',x=function(){n=1;clearInterval(t);while(i=e.shift())i();if(s)s[c]=''};return function(f){if(n)return f();if(!e[0]){d.addEventListener&&d.addEventListener("DOMContentLoaded",x,false);/*@cc_on@*//*@if(@_win32)d.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");s=d.getElementById("__ie_onload");s[c]=function(){s[r]=="complete"&&x()};/*@end@*/if(/WebKit/i.test(navigator.userAgent))t=setInterval(function(){/loaded|complete/.test(d[r])&&x()},10);o=w.onload;w.onload=function(){x();o&&o()}}e.push(f)}})();


addDOMLoadEvent(init);

function init()
{
	initLinks();
	autoShadow();
	imagePreload();
	contactHover();
	IEfixes();
}


var clear="/img/iepngfix.gif" //path to clear.gif

pngfix=function(){var els=document.getElementsByTagName('*');var ip=/\.png/i;var i=els.length;while(i-- >0){var el=els[i];var es=el.style;if(el.src&&el.src.match(ip)&&!es.filter){es.height=el.height;es.width=el.width;es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";el.src=clear;}else{var elb=el.currentStyle.backgroundImage;if(elb.match(ip)){var path=elb.split('"');var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage='none';var elkids=el.getElementsByTagName('*');if (elkids){var j=elkids.length;if(el.currentStyle.position!="absolute")es.position='static';while (j-- >0)if(!elkids[j].style.position)elkids[j].style.position="relative";}}}}}

//window.attachEvent('onload',pngfix);
		
function IEfixes()
{
	if (document.getElementById('IE')) {
		ccdiv = document.getElementById('IE');
		if (classTool('check',ccdiv,'v6_0')) { pngfix(); }
	}
}



function initLinks()
{
	if (!document.getElementById) return false;

	imageLinks = document.getElementById('Main').getElementsByTagName('a');
	for (i=0; i<imageLinks.length; i++) {
		if (imageLinks[i].href.substr(imageLinks[i].href.length-4,4) == '.jpg') {
			// alert(imageLinks[i].href);
			imageLinks[i].onclick = function() { initLightbox(this); return false; }
		}			
	}

	if (document.getElementById('ftp-test-link')) {
		//alert('hey');
		document.getElementById('ftp-test-link').onclick = function() { initLightbox(this); SI.Request.get(this.href.toString()+'&ajax=true', document.getElementById('SLB_lightbox')); return false; }
	}
}


function initLightbox(l,lightboxHtml)
{
	overlayDiv = document.createElement('div');
	overlayDiv.setAttribute('id','SLB_overlay');
	document.body.appendChild(overlayDiv);
	
	// show transparent overlay
	overlayDiv.style.visibility = 'visible';
	
	contentDiv = document.createElement('div');
	contentDiv.setAttribute('id','SLB_lightbox');
	document.body.appendChild(contentDiv);
	
	// IE6 needs some special treatment
	if (document.getElementById('IE') != null) {
		ccdiv = document.getElementById('IE');
		if (classTool('check',ccdiv,'lte6_0')) {
			htmlElement = document.getElementsByTagName('html')[0];
			htmlElement.style.overflow = 'hidden';
		}
	}
	
	if (lightboxHtml == undefined) {
		var lightboxHtml = '<div id="Showphoto"><a href="#Luk" onclick="killLightbox(); return false;"><img src="'+l.href+'" /></a><p><a href="#Luk" onclick="killLightbox(); return false;">Luk</a></div>';
	}
	
	setInnerHTML(contentDiv, lightboxHtml);
}


function setInnerHTML(target, content) {
	if (!SI) { var SI = new Object(); };
	if (SI.IE && (target.nodeName == 'TBODY' || target.nodeName == 'TABLE'))
	{
		SI.IE.fixInnerHTML(target, content);
	} else {
		target.innerHTML = content;
	}
};


function killLightbox()
{
	parentElement = document.body;
	contentDiv = document.getElementById('SLB_lightbox');
	overlayDiv = document.getElementById('SLB_overlay');
	parentElement.removeChild(contentDiv);
	parentElement.removeChild(overlayDiv);
	
	// IE6 needs some special treatment
	if (document.getElementById('IE') != null) {
		ccdiv = document.getElementById('IE');
		if (classTool('check',ccdiv,'lte6_0')) {
			htmlElement = document.getElementsByTagName('html')[0];
			htmlElement.style.overflow = 'auto';
		}
	}
}


function imagePreload()
{
	containerDiv = document.getElementById('Wrapper');

	preloadDiv = document.createElement('div');
	preloadDiv.setAttribute('id','imagePreload');
	
	newDiv = document.createElement('img');
	newDiv.setAttribute('src','/img/icn_goto_top_hover.gif');
	preloadDiv.appendChild(newDiv);
	
	containerDiv.appendChild(preloadDiv);
}


function contactHover()
{
	if (!document.getElementById) return false;
	if (document.getElementById('Contacts') == null) return false;
	containerDiv = document.getElementById('Contacts');
	
	LTEIE6 = false;
	if (document.getElementById('IE') != null && classTool('check',document.getElementById('IE'),'lte6_0')) { LTEIE6 = true; }
	
	personDivs = containerDiv.getElementsByTagName('div');
	
	for (i=0; i<personDivs.length; i++) {
		//if (personDivs[i].getAttribute('className') == 'Person') {
		if (classTool('check',personDivs[i],'Person')) {
			//personDivs[i].onmouseover = function() { this.className += ' over'; }
			//personDivs[i].onmouseout = function() { this.className.replace(' over', ''); }
			if (LTEIE6) {
				personDivs[i].onmouseover = function() { classTool('add',this,'over'); }
				personDivs[i].onmouseout = function() { classTool('remove',this,'over'); }
			}
			personDivs[i].onclick = function() { location.href = this.getElementsByTagName('a')[0].href; return false; }
		}
	}	
}


[].indexOf || (Array.prototype.indexOf = function(v,n){
  n = (n==null)?0:n; var m = this.length;
  for(var i = n; i < m; i++)
    if(this[i] == v)
       return i;
  return -1;
});



// http://tom-lee.blogspot.com/2006/06/javascript-concatenating-nodelists_08.html
function joinNodeLists() {
	if (!arguments.length) { return null; }
	var newList = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var list = arguments[i];
		for (var j = 0; j < list.length; j++) {
			// Don't use push() for IE 5 // compatibility
			newList[newList.length] = list[j];
		}
	}
return newList;
}



function autoShadow()
{
	if (!document.getElementById) return false;

	pageId = document.getElementsByTagName('html')[0].id;
	// alert(pageId+' # '+['kompetencer','referencer','kalender','lightbox'].indexOf(pageId));
	// if (['ftp'].indexOf(pageId) != -1) return false;
	// if (['filer_til_tryk','koerselsanvisninger'].indexOf(pageId) != -1) return false;

	LTEIE5_5 = false;
	if (document.getElementById('IE') != null && classTool('check',document.getElementById('IE'),'lte5_5')) { LTEIE5_5 = true; }	
	if (LTEIE5_5 == true) return false;
	
		if (document.getElementById('Cases')) {
			imageList = document.getElementById('Cases').getElementsByTagName('img');
			for (j=0; j<imageList.length; j++) {
				classTool('add',imageList[j],'NoShadow');
			}		
		}
		imageListA = document.getElementById('Main').getElementsByTagName('img');
		imageListB = document.getElementById('Secondary').getElementsByTagName('img');
		
		imageList = joinNodeLists(imageListA,imageListB);

		for (j=0; j<imageList.length; j++) {
			if (classTool('check',imageList[j],'NoShadow') == false && classTool('check',imageList[j].parentNode,'NoShadow') == false &&
			classTool('check',imageList[j].parentNode.parentNode,'NoShadow') == false) addShadow(imageList[j]);
		}
}


function addShadow(element)
{
	// if the image is a link, the parent a-tag should be used instead
	elementParent = element.parentNode;
	if (elementParent.nodeName.toLowerCase() == 'a') element = elementParent;

	// create the shadow div-wrapper and divs for all corners
	containerDiv = document.createElement('div');
	containerDiv.className = 'shadow';
	element.parentNode.insertBefore(containerDiv,element);
	
	newDiv = document.createElement('div');
	newDiv.className = 'topleft';
	containerDiv.appendChild(newDiv);
	
	newDiv = document.createElement('div');
	newDiv.className = 'topright';
	containerDiv.appendChild(newDiv);	
	
	newDiv = document.createElement('div');
	newDiv.className = 'bottomleft';
	containerDiv.appendChild(newDiv);	
	
	newDiv = document.createElement('div');
	newDiv.className = 'bottomright';
	containerDiv.appendChild(newDiv);
	
	// move the image inside the shadow div-wrapper
	containerDiv.appendChild(element);
}

/*
<div class="shadow">
  <img src="image.jpg">
  <div class="topleft"></div>
  <div class="topright"></div>
  <div class="bottomleft"></div>
  <div class="bottomright"></div>
</div>
*/


function classTool(a,o,c1,c2)
// This function takes four parameters:
// a = defines the action you want to perform
// o = the object in question
// c1 = the name of the first class
// c2 = the name of the second class
//
// Possible actions are:
// swap		= replaces class c1 with class c2 in object o
// add		= adds class c1 to the object o
// remove	= removes class c1 from the object o
// check	= tests if class c1 is already applied to object o and return true or false
{
	switch (a)
	{
		case 'swap':
			o.className=!classTool('check',o,c1)?o.className.replace(c2,c1):
			o.className.replace(c1,c2);
		break;
		case 'add':
			if(!classTool('check',o,c1)) {o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(o.className);
		break;
	}
}