function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		return false;
	}
	return true;
};

function attachEventListener(target, eventType, functionRef, capture)
{
    if (typeof target.addEventListener != "undefined")
    {
        target.addEventListener(eventType, functionRef, capture);
    }
    else if (typeof target.attachEvent != "undefined")
    {
        target.attachEvent("on" + eventType, functionRef);
    }
    else
    {
        return false;
    }
    return true;
};



function WindowOnload(f) { //playnice with other onload functionality
	var prev=window.onload;
	window.onload=function(){ if(prev)prev(); f(); }
}
function WindowOnUnload(f) {
	var prev=window.onunload;
	window.onunload=function(){ if(prev)prev(); f(); }
}


function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\b\\s)"+searchClass+"(\\s\\b|$)");
	for (i = elsLen - 1, j = 0; i >= 0; i--) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function dyngallery()
{
	var picId='bigDynPic';
	var loadingId='loadingmessage';
	var allD=getElementsByClass('dynpic', null, 'div');
	
	if(!allD[0]){return;}
	
	
	if(!document.getElementById(loadingId))
	{
		var lo=document.createElement('div');
		lo.appendChild(document.createTextNode('Loading image'));
		document.body.appendChild(lo);
		lo.id=loadingId;
		lo.style.display='none';
	}
	
	for (var c = allD.length - 1; c >= 0; c--) {
		var d = allD[c];
	
		var piclinks=d.getElementsByTagName('a');	
		for(var i=0;i<piclinks.length;i++)
		{
			piclinks[i].onclick=function() {
				var li = document.getElementById(loadingId);
				li.style.display='block';
				var wH = getBrowserHeight();
				var wW = getBrowserWidth();
				var spos = getScrollingPosition();
				li.style.top = spos[1] + ((wH * 0.5) - (li.offsetHeight * 0.5)) + 'px';
				li.style.left = spos[0] + ((wW * 0.5) - (li.offsetWidth * 0.5)) + 'px';				
				
				var oldp=document.getElementById(picId);
				if(oldp) {
				 oldp.parentNode.removeChild(oldp);
				}
				var nc=document.createElement('div');
				
				document.body.appendChild(nc);
				nc.style.display='none';
				nc.id=picId;
				var newpic=document.createElement('img');
				newpic.src=this.href;
				newpic.alt=this.getElementsByTagName('img')[0].alt;
				newpic.title='Click to return to images';
				/*
				//scale the image down if needed 
				if (newpic.width+40 > wW || newpic.height+40 >  wH) {
					
					if (newpic.width > newpic.height) {
						newpic.height = ((wW - 40)/newpic.width) * newpic.height;
						newpic.width = wW - 40;
					}
					else {
						newpic.width = ((wH - 40)/newpic.height) * newpic.width;
						newpic.height = wH - 40;						
					}
				}
				*/
				newpic.onload = function() {
					document.getElementById(loadingId).style.display='none';
					setContent(document.getElementById(picId));
				};
				newpic.onclick=function() {
				 this.parentNode.parentNode.removeChild(this.parentNode);
				 document.getElementById(loadingId).style.display='none';
				}
				var indiv=document.createElement('div');
				indiv.appendChild(newpic);
				np=document.createElement('p');
				np.appendChild(document.createTextNode(this.getElementsByTagName('img')[0].alt))
				indiv.appendChild(np);
				
				nc.style.display='block';
				nc.appendChild(indiv);
				setContent(nc);
				return false;
			}
		}
	}		
}



	function getScrollingPosition()	{
		var position = [0, 0];
		
		if (typeof window.pageYOffset != 'undefined') {
		position = [window.pageXOffset,window.pageYOffset];
		}
		
		else if (typeof document.documentElement.scrollTop
			!= 'undefined' && document.documentElement.scrollTop > 0) {
		position = [document.documentElement.scrollLeft,document.documentElement.scrollTop];
		}
		
		else if (typeof document.body.scrollTop != 'undefined') {
		position = [document.body.scrollLeft,document.body.scrollTop];
		}
		
		return position;
	}
	function setContent(nc) {
		if (document.getElementById) {
			var windowHeight = getBrowserHeight();
			var windowWidth = getBrowserWidth();
			var scrollpos = getScrollingPosition();
			var cH = nc.offsetHeight;
			var cW = nc.offsetWidth;
			/*
			if ((windowHeight > 0 || windowWidth > 0) && (cW < 50 && cH < 50)){ 
				var count = 0;
				do {
					
					cH = nc.offsetHeight;
					cW = nc.offsetWidth;
					count++;
				} while ((cH < 50 && cW < 50) || count < 10)
			}
			*/
			//alert ("ncH=" + cH  + " widH=" + windowHeight + " scrollPH=" + scrollpos[1] + "  ncW=" + cW + " widW=" + windowWidth + " scrollPW=" + scrollpos[0]);
			if (windowHeight > 0) {
				if (cH > 70 && windowHeight - cH > 0) {
					nc.style.top = scrollpos[1] + ((windowHeight * 0.5) - (cH * 0.5)) + 'px';
				}
				else {
					nc.style.top = scrollpos[1] + 'px';
				}
			}
			if (windowWidth > 0) {
				if (cW > 70 && windowWidth - cW > 0) {
					nc.style.left = scrollpos[0] + ((windowWidth * 0.5) - (cW * 0.5)) + 'px';
				}
				else {
					nc.style.left = scrollpos[0] + 'px';
				}
			}
		}
	}
	/*
	WindowOnload(function() {
		setContent();
	});
	window.onresize = function() {
		setContent();
	}
	*/



// now we add the menu to the bottom of the screen if needed	

var loadListenerSet = false;
var curStyleSheet = '';
var lastTheHeight = 0;

checkBrowserSize(false);

attachEventListener(window, "resize", checkBrowserSizeHelper, false);

function checkBrowserSizeHelper() {
	checkBrowserSize(false);
}

function checkBrowserSize(override)
{
	var theWidth = getBrowserWidth();
	var theHeight = getBrowserHeight();

	if (theWidth == 0 || theHeight == 0)
	{
		var resolutionCookie = document.cookie.match(/(^|;)ck_res_layout[^;]*(;|$)/);

		if (resolutionCookie != null)
		{
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		if (loadListenerSet == false) {
			addLoadListener(checkBrowserSize);
			loadListenerSet = true;
		}
		
		return false;
	}
	
	var oHeight = 0;
	if (document.getElementById && document.getElementById('mainbody')) {
		oHeight = document.getElementById('mainbody').offsetHeight;
	}
	
	if (oHeight == 0) {
		if (loadListenerSet == false) {
			addLoadListener(checkBrowserSize);
			loadListenerSet = true;
		}
		
		return false;
	}
	//alert("changing height " + curStyleSheet + "lastTheHeight =" + lastTheHeight + " theHeight=" + theHeight + "oHeight=" + oHeight); 

	if (oHeight+4 > theHeight)
	{
		if ((curStyleSheet != '1024 x short' && theHeight != lastTheHeight) || override == true) {
			//alert("oHeight=" + oHeight + " theHeight=" + theHeight);
			setStylesheet("1024 x short");
			document.cookie = "ck_res_layout=" + escape("1024 x short");
			curStyleSheet = '1024 x short';
		}
	}	
	else if (theHeight != lastTheHeight) 
	{
		if (curStyleSheet != '1024 x 768' || override == true) {
			//alert("oHeight=" + oHeight + " theHeight=" + theHeight);			
			setStylesheet("1024 x 768");
			document.cookie = "ck_res_layout=" + escape("1024 x 768");
			curStyleSheet = '1024 x 768';
		}			
	}	
	lastTheHeight = theHeight;

/*
	if ((theWidth > 1024 && theHeight < 586) || (theWidth > 1024 && theHeight < 586 && oHeight > theHeight))
	{
		setStylesheet("1280 x short");
		document.cookie = "ck_res_layout=" + escape("1280 x short");
	}	
	else if (theWidth > 1024)
	{
		setStylesheet("1280 x 960");
		document.cookie = "ck_res_layout=" + escape("1280 x 960");
	}	
	else if ((theWidth > 840 && theHeight < 520)  || (theWidth > 840 && theHeight < 520 && oHeight > theHeight))
	{
		setStylesheet("1024 x short");
		document.cookie = "ck_res_layout=" + escape("1024 x short");
	}	
	else if (theWidth > 840)
	{
		setStylesheet("1024 x 768");
		document.cookie = "ck_res_layout=" + escape("1024 x 768");
	}	
	else if ((theWidth > 720 && theHeight > 374) || (theWidth > 720 && theHeight < 374 && oHeight > theHeight))
	{
		setStylesheet("800 x 600");
		document.cookie = "ck_res_layout="  + escape("800 x 600");
	}
	else if (theWidth > 720) 
	{
		setStylesheet("800 x short");
		document.cookie = "ck_res_layout="  + escape("800 x short");;
	}
	else
	{
		setStylesheet("smallscreen");
		document.cookie = "ck_res_layout="  + escape("smallscreen");;
	}
*/

	return true;
};


function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
};

function getBrowserHeight()
{
	if (window.innerHeight)
	{
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight != 0)
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		return document.body.clientHeight;
	}
	
	return 0;
};





function setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)
				{
					currTag.disabled = false;
				}
			}
		}
	}
	return true;
};
	
	
	
	
	
/************************************************************************************************************
(C) www.dhtmlgoodies.com, November 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/
/*
var dhtmlgoodies_slideSpeed = 40;	// Higher value = faster
var dhtmlgoodies_timer = 6;	// Lower value = faster

var objectIdToSlideDown = false;
var dhtmlgoodies_activeId = false;
var dhtmlgoodies_slideInProgress = false;

function showHideContentInstant(e, inputId) {
	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-10));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-10));
		dhtmlgoodies_activeId = false;
	}	
	
}

function showHideContent(e,inputId)
{
	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		dhtmlgoodies_activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =document.getElementById('dhtmlgoodies_a' + inputId);
	var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}else{
				dhtmlgoodies_slideInProgress = false;
			}
		}else{
			dhtmlgoodies_activeId = inputId;
			dhtmlgoodies_slideInProgress = false;
		}
	}
}



function initShowHideDivs()
{
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='dhtmlgoodies_question'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'dhtmlgoodies_q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'dhtmlgoodies_a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='dhtmlgoodies_answer_content';
			contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
	showHideContentInstant(false,1);	// Automatically expand first item
	checkBrowserSize(true);	
}
*/

WindowOnload(function()
{

	if(document.getElementById && document.createTextNode)
	{
		
		document.body.onmouseover=function()
		{
			dyngallery();
		}																
		
	}

	//initShowHideDivs();
	//checkBrowserSize(true);	
});
