var timerLen = 5;
var slideAniLen = 500;
var pause = 400;
var fallbackState = 0;


var isAnimating = new Array();
var aniDirection = new Array();
var animatingObject = new Array();
var endHeight = new Array();
var startTime = new Array();
var timer = new Array();
var AJAXUrl = new Array();
var pauseTimer = new Array();
var contentStatus = new Array();


function hideUpdateShow(divname, ajaxurl, state)
{
	
		if(!isAnimating[divname])
		{
			contentStatus[divname] = state;
			animatingObject[divname] = document.getElementById(divname);
			AJAXUrl[divname] = ajaxurl;
			isAnimating[divname] = true;
			aniDirection[divname] = animatingObject[divname].style.display == 'none' ? 'show' : 'hide';
			if(aniDirection[divname] == 'hide')
			{
				// Sonst macht AJAXRequest das
				endHeight[divname] = parseInt(animatingObject[divname].offsetHeight);
			}
		
			startTime[divname] = (new Date()).getTime();
 
			if(aniDirection[divname] == 'show')
			{
				animatingObject[divname].style.height = '1px';
			}
 
			animatingObject[divname].style.display = 'block';
 
			timer[divname] = setInterval('slidetick("' + divname + '");', timerLen);
		}

	
}

function slidetick(divname)
{
	var elapsed = (new Date()).getTime() - startTime[divname];
 
	if (elapsed > slideAniLen)
	{
		clearInterval(timer[divname]);
		url = AJAXUrl[divname];
		direction = aniDirection[divname];
 
		if(aniDirection[divname] == 'hide')
		{
			animatingObject[divname].style.display = 'none';
		}
		animatingObject[divname].style.height = endHeight[divname] + 'px';
		
		
 
		delete(isAnimating[divname]);
		delete(timer[divname]);
		delete(startTime[divname]);
		delete(endHeight[divname]);
		delete(animatingObject[divname]);
		delete(aniDirection[divname]);
		delete(AJAXUrl[divname]);
		
		if(direction == 'hide')
		{
			pauseTimer[divname] = setInterval('sleepAndContinue("' + divname + '", "' + url + '");', pause);
		}
		
		return;
	}
	else
	{
		var d = Math.round(elapsed / slideAniLen * endHeight[divname]);
		if(aniDirection[divname] == 'hide')
			d = endHeight[divname] - d;
 
		animatingObject[divname].style.height = d + 'px';
	}
 
	return;
}


function AJAXRequest(url, divid, poststring)
{
	
	// Request erzeugen
	var request;
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest(); // Mozilla, Safari, Opera
	}
	else if (window.ActiveXObject)
	{
		try
		{
			request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		}
		catch (e)
		{
			try
			{
				request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			}
			catch (e)
			{
			}
		}
	}
	
	// überprüfen, ob Request erzeugt wurde
	if (!request)
	{
		alert("Kann keine XMLHTTP-Instanz erzeugen");
		return false;
	}
	else
	{
	
		// Request öffnen
		request.open('post', url, false);
		// Requestheader senden
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		// Request senden
		
		if(poststring == undefined)
		{
			poststring = 'get=' + contentStatus[divid];
			delete contentStatus[divid];
		}
		request.send(poststring);
		
		// Request auswerten
		//request.onreadystatechange = interpretRequest;

		switch (request.readyState)
		{
		// wenn der readyState 4 und der request.status 200 ist, dann ist alles korrekt gelaufen
			case 4:
				if (request.status != 200) {
					alert("Der Request wurde abgeschlossen, ist aber nicht OK\nFehler:"+request.status);
				} else {
					var content = request.responseText;
					// den Inhalt des Requests in das <div> schreiben
					if(divid != 'none')
					{
						
						
						endHeight[divid] = getTextHeight(content);
						//alert(endHeight[divid]);
						
						document.getElementById(divid).innerHTML = content;
					}
				}
				break;
			default:
				break;
		}
	}
}

function getTextHeight(content)
{
	var pfusch = document.getElementById('pfusch');
	pfusch.style.display = 'block';
	pfusch.innerHTML = content;
	var height = pfusch.offsetHeight;
	//var height = window.getComputedStyle(pfusch, null).getPropertyValue('height');
	pfusch.style.display = 'none';
	return height;
}



function sleepAndContinue(divname, url)
{
	clearInterval(pauseTimer[divname]);
	delete pauseTimer[divname];
	//alert(url);
	AJAXRequest(url, divname);
	
	hideUpdateShow(divname);
}

function sendRequest()
{
	var name = document.getElementById('Contact_Name').value;
	var first_name = document.getElementById('Contact_First_Name').value;
	var company = document.getElementById('Contact_Company').value;
	var email = document.getElementById('Contact_Email').value;
	var phone = document.getElementById('Contact_Phone').value;
	var request = document.getElementById('Contact_Request').value;
	var message = document.getElementById('Contact_Message').value;
	
	var success = false;
	
	// Check missing values
	if(email.search(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i))
	{
		requestError('Invalid Email');
		document.getElementById('Contact_Email').focus();
	}
	else if(request == "")
	{
		requestError('Please select your request');
		document.getElementById('Contact_Request').focus();
	}
	else if(name == "")
	{
		requestError('Please enter your name');
		document.getElementById('Contact_Name').focus()
	}
	else if(first_name == "")
	{
		requestError('Please enter your first name');
		document.getElementById('Contact_First_Name').focus();
	}
	else if(company == "")
	{
		requestError('Please enter the name of your company');
		document.getElementById('Contact_Company').focus();
	}
	else
	{
		success = true
	}
	
	
	
	
	if(success)
	{
		var poststring = 'name=' + name + '&first_name=' + first_name + '&company=' + company + '&email=' + email + '&phone=' + phone + '&request=' + request + '&message=' + message;
		//alert(poststring);
		AJAXRequest('contact.php', 'Contact_Response', poststring);
		document.getElementById('Contact_Submit').style.display = 'none';
		
	}
}


function requestError(errormessage)
{
	alert(errormessage);
}

function nextContent()
{
	fallbackState = (fallbackState + 1) % 3;
	hideUpdateShow('content', 'content.php', fallbackState);
	document.getElementById('Fallback_Title').style.background = 'url(gfx/fallback/title' + (fallbackState + 1) + '.jpg) center no-repeat';
}

function prevContent()
{
	if(fallbackState == 0)
	{
		fallbackState = 2;
	}
	else
	{
		fallbackState = fallbackState - 1;
	}
	hideUpdateShow('content', 'content.php', fallbackState);
	document.getElementById('Fallback_Title').style.background = 'url(gfx/fallback/title' + (fallbackState + 1) + '.jpg) center no-repeat';
}

