
timer = 0;
akt_opacity = 0;


var isIE = (navigator.userAgent.lastIndexOf('MSIE')>-1) ? 1 : 0; // ternary operator statements to check if IE. set 1 if is, set 0 if not.
isopera = (navigator.userAgent.lastIndexOf('Opera')>-1) ? 1 : 0;

var myWidth = 0, myHeight = 0, myScroll = 0; myScrollWidth = 0; myScrollHeight = 0;
var zoomOpen = false, preloadFrame = 1, preloadactive = false, preloadTime = 0, imgpreload = new Image();
var zoombackto = 0;
var preloadbox = 0;
var zoomedbox = 0;
var zoomimg = 0;
var zoominterval = 5;
var fadeinterval = 5;
var startzoomx=0, startzoomy=0, startzoomw=0, startzoomh=0, endzoomw=0,endzoomh=0, zoomchx=0, zoomchy = 0,zoomchw = 0, zoomchh = 0;
var zoomactive = false;
var currentcount = 0;
var zoomedimage = 0;
var zoomtimer = 0;
var fadeactive = false;
var fadetimer = 0;
var whattopreload = 0, whatevt=0;

function setOpacity(opacity, theID) {

	var object = document.getElementById(theID);

	// If it's 100, set it to 99 for Firefox.

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) { opacity = 99.9999; } // This is majorly awkward
	}

	// Multi-browser opacity setting

	object.style.filter = "alpha(opacity=" + opacity + ")"; // IE/Win
	object.style.opacity = (opacity / 100);                 // Safari 1.2, Firefox+Mozilla

}

function firstx()
{
var firsttag = document.getElementsByTagName('body')[0];
if (firsttag==null)
{
	setTimeout("firstx()",100);

} else
	{
var preloaddiv = document.createElement("div");
preloaddiv.setAttribute('id', 'preloadanim');
preloaddiv.style.position = 'absolute';
preloaddiv.style.left = '10px';
preloaddiv.style.top = '10px';
preloaddiv.style.visibility = 'hidden';
preloaddiv.style.zIndex = 502;
firsttag.insertBefore(preloaddiv, firsttag.firstChild);

var preloadingpic = document.createElement("img");
preloadingpic.setAttribute('id', 'preloadingpic');
preloadingpic.setAttribute('src','zs/loader.gif');
preloaddiv.appendChild(preloadingpic);


var zoombox = document.createElement("div");
	zoombox.setAttribute('id', 'zoomer');
	zoombox.style.position = 'absolute';
	zoombox.style.left = '10px';
	zoombox.style.top = '10px';
	zoombox.style.visibility = 'hidden';
	zoombox.style.zIndex = 501;

	firsttag.insertBefore(zoombox, preloaddiv.nextSibling);


	htmltext = "<table border=0 cellspacing=0 cellpadding=0><tr><td><img src=\"zs/zs1.png\"></td><td style=\"background-image:url(zs/zs2.png)\"></td><td><img src=\"zs/zs3.png\"></td></tr>";
	htmltext += "<tr><td style=\"background-image:url(zs/zs4.png)\"></td><td><img src=\"zs/spacer.gif\" id=\"zoomedimage\" border=0></td><td style=\"background-image:url(zs/zs5.png)\"></td></tr>";
	htmltext += "<tr><td><img src=\"zs/zs6.png\"></td><td style=\"background-image:url(zs/zs7.png)\"></td><td><img src=\"zs/zs8.png\"></td></tr></table>";
if (isIE)
{

	htmltext = "<table border=0 cellspacing=0 cellpadding=0><tr><td><img src=\"zs/spacerblack.gif\"></td><td style=\"background-image:url(zs/spacerblack.gif)\"></td><td><img src=\"zs/spacerblack.gif\"></td></tr>";
	htmltext += "<tr><td style=\"background-image:url(zs/spacerblack.gif)\"></td><td><img src=\"zs/spacer.gif\" id=\"zoomedimage\" border=0></td><td style=\"background-image:url(zs/spacerblack.gif)\"></td></tr>";
	htmltext += "<tr><td><img src=\"zs/spacerblack.gif\"></td><td style=\"background-image:url(zs/spacerblack.gif)\"></td><td><img src=\"zs/spacerblack.gif\"></td></tr></table>";
}

	zoombox.innerHTML=htmltext;
	document.getElementById('zoomedimage').onclick=function (event) {vanish(0,20);return false;};
	zoomedbox = zoombox;
	zoomedimage = document.getElementById('zoomedimage');
	injecttags();
}
}
function injecttags()
{
	if (! document.getElementsByTagName) {
		return;
	}
	links = document.getElementsByTagName("a");
	for (i=0;i<links.length;i++)
	{
		if (links[i].getAttribute("href")) 
		{
			xhref = links[i].getAttribute("href");
			if ((xhref.lastIndexOf('javascript:popupWindow')>-1))
			{
				links[i].onclick = function (event) {return zoom_on_click(this,event); };
				links[i].onmouseover = function () { preloadthis(this); };
			}
		}
	}
}

function preloadthis(what)
{
	var imghref = "";
	var s="";
	if (what.firstChild.getAttribute('src')!=null)
	{
		s = what.firstChild.getAttribute('src');
		if (s.lastIndexOf('images/products/')>-1)
		{
			s = s.substring(s.lastIndexOf('images/')+7);

		}
		if (s.lastIndexOf('/bilderdestages2/th')>-1)
		{
			s = '/bilderdestages2/'+s.substring(s.lastIndexOf('/th')+3);
		}
		if (s.lastIndexOf('/bilderdestages/th')>-1)
		{
			s = '/bilderdestages/'+s.substring(s.lastIndexOf('/th')+3);
		}
//		alert(s);
		imghref = s;
	}
	if (s=="")
	{
		what.onclick = function (event) {return true;}
		return;
	}
	
	if (imgpreload.src != imghref)
	{
		preloadactive = true;
		imgpreload = new Image();
		imgpreload.onload = function() {preloadactive = false;};
		imgpreload.src=imghref;
	}
else
	{
		preloadactive=false;
	}
}

function getSize() {

	// Window Size

	if (self.innerHeight) { // Everyone but IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		myScroll = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 Strict
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		myScroll = document.documentElement.scrollTop;
	} else if (document.body) { // Other IE, such as IE7
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		myScroll = document.body.scrollTop;
	}

	// Page size w/offscreen areas

	if (window.innerHeight && window.scrollMaxY) {	
		myScrollWidth = document.body.scrollWidth;
		myScrollHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // All but Explorer Mac
		myScrollWidth = document.body.scrollWidth;
		myScrollHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		myScrollWidth = document.body.offsetWidth;
		myScrollHeight = document.body.offsetHeight;
	}
}



function anothertry()
{
	zoom_on_click(whattopreload,whatevt);
}

function zoom_on_click(what,evt)
{
	var shift = getShift(evt);
	



	// Check for Command / Alt key. If pressed, pass them through -- don't zoom!
	if (! evt && window.event && (window.event.metaKey || window.event.altKey)) {
		return true;
	} else if (evt && (evt.metaKey|| evt.altKey)) {
		return true;
	}

	setOpacity(0,'zoomer');
	document.getElementById('zoomer').style.visibility = "visible";
	getSize();
	preloadbox = document.getElementById('preloadanim');
	if (preloadactive==true)
	{
		zw = preloadbox.style.width;
		zh = preloadbox.style.height;
		zw = zw.substring(0,zw.length-2);
		zh = zh.substring(0,zh.length-2);
	 	preloadbox.style.left = (myWidth-zw)/2+'px';
	 	preloadbox.style.top = (myHeight-zh)/2+'px';
	 	preloadbox.style.visibility = "visible";
	 	whattopreload = what;
	 	whatevt = evt;
		setTimeout("anothertry()",50);
		return false;
	}
else
	{
		preloadbox.style.visibility = "hidden";
	}
	document.getElementById('zoomedimage').setAttribute('src',imgpreload.src);
	zoomit(what,shift);
	return false;
}

function getShift(evt) {
	var shift = false;
	if (! evt && window.event) {
		shift = window.event.shiftKey;
	} else if (evt) {
		shift = evt.shiftKey;
		if (shift) evt.stopPropagation(); // Prevents Firefox from doing shifty things
	}
	return shift;
}


function zoomit(what,shift)
{
	preloadbox.style.visibility = "hidden";
	sw = what.childNodes[0].width;
	sh = what.childNodes[0].height;
	sp = getelementpos(what.childNodes[0]);
	
	startzoomx = sp[0];
	startzoomy = sp[1];
	startzoomw = sw;
	startzoomh = sh;
	
	endzoomw = imgpreload.width;
	endzoomh = imgpreload.height;
	
	zoomchx = ((myWidth-endzoomw)/2-startzoomx);
	zoomchy = ((myHeight-endzoomh)/2-startzoomy+myScroll);
	zoomchw = endzoomw-startzoomw;
	zoomchh = endzoomh-startzoomh;
	
	if (!(zoomactive || fadeactive))
	{
		zoombackto = what;
		
		zoomtimer = setInterval("zoomtag(0,"+startzoomw+","+zoomchw+","+startzoomh+","+zoomchh+","+startzoomx+","+zoomchx+","+startzoomy+","+zoomchy+",20)",zoominterval);
		zoomactive = true;
	}
}

function vanish(count,steps)
{
	if (!(zoomactive || fadeactive))
	{
		fadetimer = setInterval("vanishit("+count+","+steps+")",zoominterval);
		fadeactive=true;
	}
}
function vanishit(count,steps)
{
	count++;
	if (count<steps)
	{
		setOpacity((steps-count)/steps*100,'zoomer');
		clearInterval(fadetimer);
		fadetimer = setInterval("vanishit("+count+","+steps+")",zoominterval);
	}
	else
	{	
		setOpacity(0,'zoomer');
		document.getElementById('zoomer').style.top = "-2000px";
		fadeactive=false;
		clearInterval(fadetimer);
	}
}
		
function zoomtag(currentcount,startw,chw,starth,chh,startx,chx,starty,chy,steps)
{
	zoomopen = true;
	currentcount++;
	clearInterval(zoomtimer);
	if (currentcount==steps)
	{
		zoomactive = false;
		setOpacity(100,'zoomer');
		zoomedimage = document.getElementById('zoomedimage');
		w = startw+(chw*currentcount)/steps;
		h = starth+(chh*currentcount)/steps;
		xx = startx+(chx*currentcount)/steps;
		yy = starty+(chy*currentcount)/steps;
		zoomedbox=document.getElementById('zoomer');
		zoomedbox.style.left = xx+'px';
		zoomedbox.style.top = yy+'px';
		zoomedimage = document.getElementById('zoomedimage');
		zoomedimage.style.width=w+'px';
		zoomedimage.style.height = h+'px';
		document.getElementById('zoomer').style.visibility = 'hidden';	
		document.getElementById('zoomer').style.visibility = 'visible';
	}
	else
	{
		w = startw+(chw*currentcount)/steps;
		h = starth+(chh*currentcount)/steps;
		xx = startx+(chx*currentcount)/steps;
		yy = starty+(chy*currentcount)/steps;
		zoomedbox=document.getElementById('zoomer');
		zoomedbox.style.left = xx+'px';
		zoomedbox.style.top = yy+'px';
		zoomedimage = document.getElementById('zoomedimage');
		zoomedimage.style.width=w+'px';
		zoomedimage.style.height = h+'px';
		setOpacity(Math.abs(currentcount/steps*100),'zoomer');
		zoomtimer = setInterval("zoomtag("+currentcount+","+startw+","+chw+","+starth+","+chh+","+startx+","+chx+","+starty+","+chy+","+steps+")",zoominterval);
	}
}
	
	
function getelementpos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}		
