addDOMLoadEvent(initLinks);
addDOMLoadEvent(CheckForHash);

var HashCheckInterval = setInterval("CheckForHash()", 500);


function initLinks()
{
	// http://www.ajaxonomy.com/2007/web-design/a-better-ajax-back-button-solution
	var ajaxLocationAnchor = document.createElement('a');
	ajaxLocationAnchor.setAttribute('id','LocationAnchor');
	document.body.insertBefore(ajaxLocationAnchor, document.body.firstChild);

	if (document.getElementById('ImageMap')) {
		document.getElementById('ImageMap').className = 'JavascriptEnabled';
		
		document.getElementById('ImageMap').onmouseover = function() { iehover(this, true); }
		document.getElementById('ImageMap').onmouseout = function() { iehover(this, false); }
		
		imageLinks = document.getElementById('ImageMap').getElementsByTagName('a');
		for (i=0; i<imageLinks.length; i++) {
			if (imageLinks[i].parentNode.tagName.toLowerCase() == 'dd') {

				if (window.imagePath == null) { window.imagePath = getFilePath(imageLinks[i].href); }
				if (window.showType == null) { window.showType = 'imagemap'; }
							
				imageLinks[i].onclick = function() { makeLightbox(this, 'imagemap'); return false; }
				imageLinks[i].parentNode.onmouseover = function() { iehover(this, true); }
				imageLinks[i].parentNode.onmouseout = function() { iehover(this, false); }
			}			
		}
	}

	if (document.getElementsByTagName('html')[0].id == 'havetegning') {
		imageLinks = document.getElementById('Main').getElementsByTagName('a');
		for (i=0; i<imageLinks.length; i++) {
			if (imageLinks[i].parentNode.tagName.toLowerCase() != 'dd') {
			if (getFileExtension(imageLinks[i].href) == 'jpg') {
				
				if (window.imagePath == null) { window.imagePath = getFilePath(imageLinks[i].href); }
				if (window.showType == null) { window.showType =  'image'; }
				
				imageLinks[i].onclick = function() { makeLightbox(this, 'image'); return false; }
			}
			}
		}
	}
}


function makeLightbox(l,t)
{
	if (t == 'imagemap') {
		initLightbox(l, '<div id="Showcase"><div class="Image"><a href="#Luk" onclick="killLightbox(); RedirectLocation(\'LocationAnchor\'); return false;"><img id="MAP_image" width="500" src="'+l.href+'" /><span id="CloseButton" title="Luk">&nbsp;</span></a><p id="MAP_caption">'+l.innerHTML+'</p></div></div>');
		RedirectLocation('LocationAnchor', getFileName(l.href));
	}
	if (t == 'image') {
		initLightbox(l, '<div id="Showcase"><div class="Image" onmouseover="iehover(this, true);" onmouseout="iehover(this, false);"><a href="#Luk" onclick="killLightbox(); RedirectLocation(\'LocationAnchor\'); return false;"><img src="' + l.href + '" /><span id="CloseButton" title="Luk">&nbsp;</span></a></div></div>');
		RedirectLocation('LocationAnchor', getFileName(l.href));
	}
	
	document.getElementById('SLB_lightbox').className = getFileName(l.href);
}


function getFileName(theUrl, includeExtension){
	includeExtension = typeof(includeExtension) != 'undefined' ? includeExtension : false;
	theUrl = '' + theUrl;
	if (!includeExtension) {
		theResult = theUrl.substring(theUrl.lastIndexOf("\/")+1,theUrl.lastIndexOf("\."));
	} else {
		theResult = theUrl.substring(theUrl.lastIndexOf("\/")+1,theUrl.length);
	}
	return theUrl.substring(theUrl.lastIndexOf("\/")+1,theUrl.lastIndexOf("\."));
}

function getFileExtension(theUrl){
	theUrl = '' + theUrl;
	return theUrl.substring(theUrl.lastIndexOf("\.") + 1, theUrl.length);
}

function getFilePath(theUrl){
	theUrl = '' + theUrl;
	return theUrl.substring(0, theUrl.lastIndexOf("\/") +1 );
}

function getHash(theUrl, includeHash){
	includeHash = typeof(includeHash) != 'undefined' ? includeHash : false;
	theUrl = '' + theUrl;
	theResult = theUrl.substring( theUrl.lastIndexOf('\#'), theUrl.length );
	if (!includeHash) { theResult = theResult.replace("#",""); }
	return theResult;
}




// http://www.ajaxonomy.com/2007/web-design/a-better-ajax-back-button-solution

function CheckForHash(){
	if (document.location.hash){
		var HashLocationName = document.location.hash;
		HashLocationName = HashLocationName.replace("#","");
	
		if (document.getElementById('SLB_lightbox')) {
			currentState = document.getElementById('SLB_lightbox').className;
		} else {
			currentState = '';
		}
		
		if (currentState != HashLocationName) {
		
				imageLinks = document.getElementById('Main').getElementsByTagName('a');
				l = '';
				for (i=0; i<imageLinks.length; i++) {
					if (getFileName(imageLinks[i].href)	== HashLocationName) { l = imageLinks[i]; }
				}				
				makeLightbox(l,window.showType);
		}
	} else {
		killLightbox();
	}
}

function RenameAnchor(anchorid, anchorname){
	document.getElementById(anchorid).name = anchorname; //this renames the anchor
}

function RedirectLocation(anchorid, anchorname, HashName){
	sstchur_SmartScroller_GetCoords()
	anchorname = typeof(anchorname) != 'undefined' ? anchorname : '';
	HashName = typeof(HashName) != 'undefined' ? HashName : '#'+anchorname;
	//if (HashName == '#') HashName = '';
	RenameAnchor(anchorid, anchorname);
	document.location = HashName;
	sstchur_SmartScroller_Scroll()
}

// var HashCheckInterval = setInterval("CheckForHash()", 500);
// window.onload = CheckForHash;



// http://aspnet.4guysfromrolla.com/articles/111704-1.aspx

  function sstchur_SmartScroller_GetCoords()
   {
      var scrollX, scrollY;
      
      if (document.all)
      {
         if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
         else
            scrollX = document.documentElement.scrollLeft;
               
         if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
         else
            scrollY = document.documentElement.scrollTop;
      }   
      else
      {
         scrollX = window.pageXOffset;
         scrollY = window.pageYOffset;
      }
   
      window.scrollX = scrollX;
      window.scrollY = scrollY;
   }
   
   function sstchur_SmartScroller_Scroll()
   {
      var x = window.scrollX;
      var y = window.scrollY;
      window.scrollTo(x, y);
   }





function initLightbox(l,h)
{
	if (!document.getElementById('SLB_overlay')) {		
		overlayDiv = document.createElement('div');
		overlayDiv.setAttribute('id','SLB_overlay');
		document.body.appendChild(overlayDiv);
		overlayDiv.onclick = function() { killLightbox(); }
	} else {
		overlayDiv = document.getElementById('SLB_lightbox');
	}
	
	// show transparent overlay
	overlayDiv.style.visibility = 'visible';
	
	if (!document.getElementById('SLB_lightbox')) {		
		contentDiv = document.createElement('div');
		contentDiv.setAttribute('id','SLB_lightbox');
		document.body.appendChild(contentDiv);
		contentDiv.onclick = function() { killLightbox(); }
	} else {
		contentDiv = document.getElementById('SLB_lightbox');
		setInnerHTML(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';
		}
	}
	setInnerHTML(contentDiv, h);
}


function killLightbox()
{
	// RedirectLocation('LocationAnchor'); 
	parentElement = document.body;
	contentDiv = document.getElementById('SLB_lightbox');
	overlayDiv = document.getElementById('SLB_overlay');
	if (contentDiv != null) parentElement.removeChild(contentDiv);
	if (overlayDiv != null) 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 iehover(e, hoverState)
{
	if (hoverState == true) { classTool('add',e,'hover'); }
	if (hoverState == false) { classTool('remove',e,'hover'); }
}