var timerLandkarte = null;
var curImage;
var lan = '';

var iStepX = 0; // Px-Schritte
var iStepY = 0;
var iStepCountX = 1;
var iStepCountY = 1;
var iFac = 4;
var enlargeIntX;
var enlargeIntY;

window.onresize = setImage;
//window.onscroll = setScroll;

function hideImg()
{
	if(curImage)
		curImage.style.display = 'none';
	document.getElementById('overlay').style.display = 'none';
	document.getElementById('xbutton').style.display = 'none';

	document.getElementById('urlaub').style.display = 'none';	
}

function initBg() 
{
	var tNow = new Date();

	var winterEnd = new Date("April 01, 2010 12:00:00");
	var winterStart = new Date("November 15, 2010 12:00:00");
	
	winterEnd.setFullYear(tNow.getYear());

/*	if(tNow < winterEnd || tNow > winterStart) */
	if(jahreszeit == 'winter' || jahreszeit == 'Winter')
	{
		objImg = document.getElementById('bgimage');
		objImg.src = objImg.src.replace('bg', 'bgwinter');
		window.setTimeout('setImage()', 150);
		window.setTimeout('setImage()', 300);
		document.body.style.backgroundColor = '#afc0ff';

	}
	else
		setImage();	

	addEvent(document.body,'resize',setImage);

	setSearchbox();
}

function setScroll()
{
//	objImg = document.getElementById('bgdiv');
//	objImg.style.top = getScrollXY()+'px';
}

function setImage()
{
//	objImg = document.getElementById('bgimage');

//	alert(document.getElementById('bgdiv').style.height);
//	objImg.style.height = document.getElementById('bgdiv').style.height +'px';

//	objImg.style.marginLeft = (objImg.width - document.body.clientWidth) / 2 *-1 + 'px';		


	objImg = document.getElementById('bgimage');

	var iWidth = 0;
	if(window.innerWidth)
		iWidth = window.innerWidth;
	else
		iWidth = document.documentElement.clientWidth;

	var iHeight = 0;
	if(window.innerHeight)
		iHeight = window.innerHeight;
	else
		iHeight = document.documentElement.clientHeight;

//	alert(iHeight+' > '+iWidth+' || '+iHeight+' > '+objImg.height);

	objDiv = document.getElementById('fixed-top');
	objDiv.style.height = document.documentElement.clientHeight;

	if(objImg)
	{

		if(iHeight > objImg.height-20)
		{	objImg.style.height = '100%';
			objImg.style.width = '';
			
			if(iWidth > objImg.width)
			{
				objImg.style.height = '';
				objImg.style.width = '100%';
			}
		}
		else
		{
			objImg.style.height = '';
			objImg.style.width = '100%';

			if(iHeight > objImg.height)
			{
				objImg.style.height = '100%';
				objImg.style.width = '';
			}
		}

		objImg.style.marginLeft = (objImg.width - document.documentElement.clientWidth) / 2 *-1 + 'px';		
	}	
}


function showUrlaub()
{
	objUrlaub = document.getElementById('urlaub');

	//objUrlaub.src ='http://dibs009.joanneum.at:8080/emotion_oststeiermark/?boundaries=t';
	objUrlaub.src ='http://mobvis.joanneum.at/emotion_oststeiermark/?boundaries=t';
	
	iYScroll = getScrollXY();

	iPosX = (document.body.clientWidth - 1000 ) / 2;
	iPosY = 0;

//	if(!document.all)
//		iPosY -= 200;	

	objUrlaub.style.top = iPosY+'px';
	objUrlaub.style.left = iPosX+'px';	

	lastDot = findPos(document.getElementById('lastdot'));	
	document.getElementById('overlay').style.height = lastDot[1] + 5+'px';

	xButton = document.getElementById('xbutton');
	xButton.style.top = iPosY +4 +'px';
	xButton.style.left = iPosX + 4 +'px';

	document.getElementById('overlay').style.display = 'inline';
	xButton.style.display = 'inline';

	objUrlaub.style.display = 'inline';
}

function checkImgOver(evt)
{
	imgObj = (evt.target) ? evt.target : evt.srcElement;
	imgSrc = imgObj.src;

	if(imgObj.className == 'no')
		return;
	
	var sFind = 'pictures/';

	// vielleicht als background versteckt
	if(imgSrc.indexOf('dot.gif') != -1)
	{
		objParent = imgObj.parentNode;
	//	imgSrc = objParent.style.getAttribute("background",false);

		for(i=0; objParent.attributes[i] && objParent.attributes[i].nodeValue; i++)
		{
			if(objParent.attributes[i].nodeValue.indexOf('url') != -1)
				imgSrc = objParent.attributes[i].nodeValue;		
		}

		if(imgSrc.indexOf('dot.gif') != -1)
			imgSrc = objParent.style.getAttribute("background",false);
		
	}

	
	if(imgSrc.indexOf(sFind) != -1)
	{
		sImgName = imgSrc.slice(imgSrc.indexOf(sFind) + sFind.length);
		sNewImgName = sImgName.slice(0,8);
		sNewImgName += 'F.jpg';
		
		// Objekt zuerst suchen ob es schon da ist
		objSearchImg = document.getElementById(sNewImgName);
		if(objSearchImg)
		{
			if(objSearchImg.style.display == 'none')
				objSearchImg.style.display = 'inline';							
			else
			{	objSearchImg.style.display = 'none';
				document.body.style.overflow = 'inherit';
				document.getElementById('overlay').style.display = 'none';
				document.getElementById('xbutton').style.display = 'none';
				return;
			}
				
			newImage = objSearchImg;
		}
		else
		{		
		
			newImage = document.createElement("img");
			newImage.src = '/pictures/'+sNewImgName;
			newImage.setAttribute('id',sNewImgName);
			newImage.style.position = 'absolute';
			newImage.style.zIndex = '5000';
			newImage.style.border = '4px solid white';
			newImage.style.borderTopWidth = '20px';
			addEvent(newImage,'click',checkImgOver);
//			newImage.style.display = 'none';
		}
				
		iYScroll = getScrollXY();
/*
		aPos = findPos(imgObj);
		
		aX = new Array(aPos[0], aPos[0] + imgObj.offsetWidth);
		aY = new Array(aPos[1], aPos[1] + imgObj.offsetHeight);		
		
		iPixelLeft = aX[0];
		iPixelRight = screen.availWidth - aX[1];
	
		iPixelTop = aY[0] - iYScroll;
		iPixelBottom = screen.availHeight + iYScroll - aY[1];	

		iPosX = iPixelLeft > iPixelRight ? iPixelLeft : aX[1] - newImage.width;
		iPosY = iPixelTop > iPixelBottom ? aY[0] - newImage.height-10: aY[1]+10;
*/
		document.body.appendChild(newImage);	

		newImage = document.getElementById(sNewImgName);			

		iImageWidth = 490;

		iImageHeight = newImage.height;
		if(iImageHeight < 100)
			iImageHeight = 320;

		var iCHeight = 0;
		if(window.innerHeight)
			iCHeight = window.innerHeight;
		else
			iCHeight = document.documentElement.clientHeight;

		var iCWidth = 0;
		if(window.innerWidth)
			iCWidth = window.innerWidth;
		else
			iCWidth = document.documentElement.clientWidth;
		

		iPosX = (iCWidth - iImageWidth ) / 2;
		iPosY = ((iCHeight - iImageHeight) / 2) + iYScroll;	

/*
		// Auto-Vergrößerung
		iStepX = newImage.width / iFac;
		iStepY = newImage.height / iFac;

		newImage.height = 1;
		newImage.width = 1;	

		enlargeIntY = window.setInterval('enlargePicY()', 1);
		enlargeIntX = window.setInterval('enlargePicX()', 1);
*/

	//	if(!document.all)
	//		iPosY -= 200;	

		newImage.style.top = iPosY+'px';
		newImage.style.left = iPosX+'px';

		lastDot = findPos(document.getElementById('lastdot'));			
		document.getElementById('overlay').style.height = lastDot[1] + 5+'px';

		xButton = document.getElementById('xbutton');
		xButton.style.top = iPosY + 2 +'px';
		xButton.style.left = iPosX + 4 +'px';

		document.body.style.overflow = 'hidden';
		document.getElementById('overlay').style.display = 'inline';
	//	document.getElementById('overlay').style.top = getScrollXY();
		xButton.style.display = 'inline';
	//	newImage.style.display = 'inline';			

		curImage = newImage;

	}	
}

function enlargePicX()
{
	if(iStepCountX <= iFac)
	{	
		curImage.width = iStepCountX * iStepX;		
		iStepCountX++;
	}
	else	
	{	
		window.clearInterval(enlargeIntX);		
		iStepCountX = 1;				
	}
}

function enlargePicY()
{
	if(iStepCountY <= iFac)
	{	
		curImage.height = iStepCountY * iStepY;		
		iStepCountY++;		
	}
	else	
	{	
		window.clearInterval(enlargeIntY);				
	//	enlargeIntX = window.setInterval('enlargePicX()', 1);				
		iStepCountY = 1;				
	}
}

function enlargePic()
{
	if(iStep <= iFac)
	{	
		curImage.width = iStep * iStepX;
		curImage.height = iStep * iStepY;
		iStep++;
	}
	else	
	{	
		window.clearInterval(enlargeInt);
		iStep = 1;		
	}
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
			
		return [curleft,curtop];
	}
}

function getScrollXY() 
{  

	var scrOfX = 0, scrOfY = 0;  

	if( typeof( window.pageYOffset ) == 'number' ) 
	{  
		//Netscape compliant  

		scrOfY = window.pageYOffset;  

		scrOfX = window.pageXOffset;  

	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
	{  
		//DOM compliant  

		scrOfY = document.body.scrollTop;  

		scrOfX = document.body.scrollLeft;  

	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
	{  
		//IE6 standards compliant mode  

		scrOfY = document.documentElement.scrollTop;  

		scrOfX = document.documentElement.scrollLeft;  

	}  

	return scrOfY;  

} 


function timeOutLandkarte()
{
	if(timerLandkarte == null)
	{	
		timerLandkarte = window.setTimeout("hide('karte')", 500);	
	}
		
}

function setSearchbox()
{
	objBox = document.getElementById('searchbox');
	
	loc = document.location.href;
	if(loc.indexOf('hu.') != -1)
	{	objBox.value = 'Keres';
		lan = 'hu';
	}
	else if(loc.indexOf('en.') != -1)
	{	objBox.value = 'Search';
		lan = 'en';
	}
}

function eraseSearch(searchBox) 
{
	if(searchBox.value == "Suchbegriff" || searchBox.value == "Keres" || searchBox.value == "Search")
		searchBox.value = "";
	
}

function rebuildSearch(searchBox) 
{
	if(searchBox.value.length == 0)
	{
		if(lan == 'en')
			searchBox.value = "Search";
		else if(lan == 'hu')
			searchBox.value = "Keres";
		else
			searchBox.value = "Suchbegriff";

	}
}

function show(id)
{
	if(!document.getElementById(id))
		alert('ID '+id+' nicht gefunden');
	else
		document.getElementById(id).style.display = "inline";		
}

function hide(id)
{	
	document.getElementById(id).style.display = "none";
}

function landkarte()
{
	var karte = document.getElementById('karte');

	if(karte.style.display == "none" || karte.style.display == "")
		show('karte');
	else
		hide('karte');
}

function browserVersion ()
{
	var iIEv = (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;
	return iIEv;
}

function setPNG()
{
	return;

	var IEv = browserVersion();
	if(IEv < 0 || IEv > 6)
		return;
	
	document.getElementById('idlogo').src = '/images/oststeiermark-logo.gif';
	document.getElementById('idbogen').src = '/images/bogen.gif';
}


