var zoom = {
	x: 0,
	y: 0,
	div: '',
	index: 1000,
	locked: false,
	opendivs: 0,
	imgarray: [],
	fadeOutTimer:0,
	fadeInTimer:0
}

function IE()
{
    if(navigator.appName == 'Microsoft Internet Explorer') return true;
    else return false;
}

function mouseX(evt){ 
	if (window.event) return window.event.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	else return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
}

function mouseY(evt){ 
	if (window.event) return window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else return (evt.clientY ? evt.clientY : window.event.clientY) + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}	

function follow(evt) {
		var obj = document.getElementById(zoom.div).style;	
		obj.left = (parseInt(mouseX(evt))-zoom.x ) + 'px'; 
		obj.top = (parseInt(mouseY(evt))-zoom.y) + 'px';				
}

//var c=0;
function fadeIn(div,max) {
	if (IE()){
		if (!document.getElementById(div).filters.alpha) c = 0;
		else c = document.getElementById(div).filters.alpha.opacity;
	}
	else{
		if (!document.getElementById(div).style.opacity) c = 0;
		else c = parseFloat(document.getElementById(div).style.opacity) * 100;
	}
	var t
	if (c == 0)
	{
	document.getElementById(div).style.opacity = '0.0';
	document.getElementById(div).style.filter = 'alpha(opacity=00)';
	}
	c = c + 20;	
	if(c > max)
	{
		clearInterval(zoom.fadeInTimer);
		zoom.fadeInTimer = 0;
		c=0;
		return;
	}
	document.getElementById(div).style.opacity = c / 100;
	document.getElementById(div).style.filter = 'alpha(opacity=' +c+ ')';	
	//if(c <= max) t = setTimeout('fadeIn("'+div+'","'+max+'")',50);	
	if(c <= max && zoom.fadeInTimer == 0) zoom.fadeInTimer = setInterval('fadeIn("'+div+'","'+max+'")',50);	
}

function fadeOut(div) {
	var step
	var thumbId;
	if (IE()) step = parseFloat(document.getElementById(div).filters.alpha.opacity);
	else step = parseFloat(document.getElementById(div).style.opacity) * 100;	
	step = step - 20;
	if(step <= -20)
	{
		thumbId = document.getElementById(div).title;
		document.getElementById(thumbId).style.position = 'static';
		document.getElementById(thumbId).style.visibility = 'visible';
		fadeIn(thumbId,100);
		clearInterval(zoom.fadeOutTimer);
		zoom.fadeOutTimer = 0;
		deletediv();
		return;
	}

	if (document.getElementById(div))
		{	
		if (!IE()) document.getElementById(div).style.opacity = step / 100;
		else document.getElementById(div).style.filter = 'alpha(opacity=' + step + ')';
		}
	//if(step > -1) t = setTimeout('fadeOut("'+div+'","'+step+'")',50);	
	if(step > -1 && zoom.fadeOutTimer == 0) zoom.fadeOutTimer = setInterval('fadeOut("'+div+'")',50);	
}

function ModalDiv(){
	var mdiv = document.createElement("div");
	mdiv.id = 'modaldiv';
	mdiv.style.width = screen.width +'px';
	mdiv.style.height = screen.height + 'px';
	mdiv.style.opacity = '0.8';
	mdiv.style.filter = 'alpha(opacity=80)';
	mdiv.style.backgroundColor = 'black';
	mdiv.style.position = 'fixed';
	mdiv.style.top = '0px';
	mdiv.style.left = '0px';
	mdiv.style.zIndex = '100';
	mdiv.onmouseup = stopmove;
	document.body.onselectstart=function(){return false}
	document.body.ondragstart = function() { return false; };
	document.body.style.MozUserSelect="none";
	document.body.onmousedown=function(){return false}
	document.body.appendChild(mdiv);	
	//fadeIn('modaldiv',80);
}
	
function hidemodaldiv(){
	if(document.getElementById("modaldiv"))
		{
		parentObj = document.getElementById("modaldiv").parentNode; 
		childObj = document.getElementById("modaldiv");
		parentObj.removeChild(childObj);
		}
}	

function NewDiv(element){
	var canadd = true;
		for(i=0;i<zoom.imgarray.length;i++)
		{		
		if (zoom.imgarray[i] == element.title) canadd = false;
		}
			if (canadd)	
			{			
		  fadeThumb(element.longDesc,100);
			ModalDiv(); 
			var zoomdiv = document.createElement("div");
			var temp, inw, inh;
			
			temp = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
			innerw = (window.innerWidth ? window.innerWidth : document.documentElement.clientWidth);
			innerh = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight);			
			zoomdiv.id = element.title;
			zoomdiv.title = element.longDesc;			
			zoomdiv.style.border = '2px outset gray';
			zoomdiv.style.background = 'url(billeder/loading.gif) #000000 no-repeat 50% 50%';
			zoomdiv.style.color = '#FFFFFF';
			zoomdiv.style.textAlign = 'center';
			zoomdiv.style.fontFamily = 'arial';
			zoomdiv.style.fontSize = '11px';
			zoomdiv.style.zIndex = zoom.index++;
			zoomdiv.style.position = 'absolute';
			zoomdiv.style.overflow = 'hidden';
			zoomdiv.style.width = (element.width*2 + 40) + 'px';			  
			zoomdiv.style.height = (element.height*2 + 44) + 'px';			
			zoomdiv.style.left = (window.innerw/2) - (parseInt(zoomdiv.style.width)/2) + 'px';	
			zoomdiv.style.top = (window.innerh/2 + temp) - (parseInt(zoomdiv.style.height)/2) + 'px';

			zoomdiv.innerHTML += '<div style="width:99%;height:20px"> <div onclick="hidemodaldiv();" id="hidemodal">Åbn ny</div> <div onclick="fadeOut(zoom.div, 100);" id="closearea">Luk</div></div> ';
			zoomdiv.innerHTML += '<img name="'+element.title+'" id="'+element.lowsrc+'" onmousedown="if(zoom.locked == false) movediv(event);" onmouseup="stopmove();" onload="document.getElementById(\''+element.lowsrc+'\').style.visibility = \'visible\'" src="'+element.longDesc+'" style="width:'+element.width*2+'px;visibility:hidden;cursor:move;border:2px inset gray;">';
			zoomdiv.innerHTML += '<div style="width:99%;height:20px;"><div id="resize" onMouseDown="if(zoom.locked == false) document.onmousemove = resize;" onMouseUp="stopresize();" ><img title="Træk for at ændre størrelse" alt="Træk for at ændre størrelse" src="billeder/resize.jpg"></div> '+element.title+' </div> ';

			document.body.appendChild(zoomdiv);			
			fadeIn(zoomdiv.id, 100);			
			zoom.div = zoomdiv.id;		
			zoomdiv.onmousedown = indexup;
			document.body.onmouseup = stopmove;
			zoom.opendivs++;
			zoom.imgarray.push(zoomdiv.id);			
			}	
}

function fadeThumb(id,step) {	
	var obj = document.getElementById(id).style;

	step = step - 20;
	if(step <= -20)
	{
		obj.visibility = 'hidden';
		clearTimeout(t);		
		return;
	}
	obj.opacity = step / 100;
	obj.filter = 'alpha(opacity=' +step+ ')';
	if(step > -1) t = setTimeout('fadeThumb("'+id+'","'+step+'")',50);
}


function resize(evt) {	
		if (document.getElementById) 
		{
			zoom.locked = true;
			var div = document.getElementById(zoom.div).style;
							
				if (IE())
				{
					var imgID = document.getElementById(zoom.div).childNodes[1].id;
				}
				else
				{
					var imgID = document.getElementById(zoom.div).childNodes[2].id;	
				}

				div.width = (parseInt(mouseX(evt))-parseInt(div.left)) + 5 + 'px';		
				div.height = (parseInt(mouseY(evt))-parseInt(div.top)) + 5 + 'px';

			var imgObj = document.getElementById(imgID).style;
			var bund = parseInt(div.top)+parseInt(div.height);

			if (parseInt(div.width) <= 150) div.width = 150 + 'px';
			if (parseInt(div.height) <= 150) div.height = 150 + 'px';
			imgObj.width = parseInt(div.width) - 40 + 'px';
			imgObj.height = parseInt(div.height) - 44 + 'px';
		}
}

function stopresize(){
	zoom.locked = false;		
	document.onmousemove=null;
}

function deletediv(){
	for(i=0;i<zoom.imgarray.length;i++)
		{		
		if (zoom.imgarray[i] == zoom.div) zoom.imgarray.splice(i,1,"");
		}
		if(document.getElementById(zoom.div))
			{			
			parentObj = document.getElementById(zoom.div).parentNode; 
			childObj = document.getElementById(zoom.div);
			parentObj.removeChild(childObj);
			zoom.opendivs--;
			}

	if(document.getElementById("modaldiv") && zoom.opendivs == 0)
		{
		parentObj = document.getElementById("modaldiv").parentNode; 
		childObj = document.getElementById("modaldiv");
		parentObj.removeChild(childObj);
		}
}

function setfocus(){
	if(zoom.locked == false) zoom.div = this.id;
}

function indexup(){
	if(zoom.locked == false) 
		{
		zoom.div = this.id;
		this.style.zIndex = zoom.index;
		this.style.zIndex++;
		zoom.index = this.style.zIndex;
		}
}

function movediv(evt){
	document.body.onselectstart=function(){return false};
	document.body.style.MozUserSelect="none";
	var element;
	if (evt.target)
  {
  element = evt.target;	
  }
	else if (evt.srcElement)
  {
  element = evt.srcElement; 
  }
	zoom.div = element.name;	
	if (!zoom.div) zoom.div = element.id;
	if (document.getElementById(zoom.div)) {
		var obj = document.getElementById(zoom.div).style;
		var x,y, offscroll;
		zoom.locked = true;	
		obj.zIndex = zoom.index;
		obj.zIndex++;
		zoom.index = obj.zIndex;
		offscroll = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);	
		x = parseInt(obj.left);
		y = parseInt(obj.top);
		zoom.x = parseInt(evt.clientX) - x;
		zoom.y = parseInt(evt.clientY) - (y -offscroll);	
		document.onmousemove=follow;
	}
}

function stopmove(){
	document.body.onselectstart=function(){return true}
	document.body.style.MozUserSelect="text";
	stopresize();
	if(document.getElementById(zoom.div))
			{
			var obj = document.getElementById(zoom.div).style;
			zoom.locked = false;
			obj.zIndex = zoom.index; 
			obj.zIndex = obj.zIndex--;
			zoom.index = obj.zIndex;	
			document.onmousemove=null;
			}
}


