function loadPage() {
  hideSpecialBox(Math.floor(Math.random()*3));
}
function getElementsByNameIE( name) {
     
     var elem = document.getElementsByTagName("p");
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function showJob(nr) {
	var jobDescs = getElementsByNameIE("jobDesc");
	for (var i=0; i<jobDescs.length; i++) {
		if (short_text[i].state == 'l' && i == nr) {
			jobDescs[i].className = "ov_hide";
			document.getElementById("jobTitle"+i).className = "jobTitle";
			var temp = {"desc" : jobDescs[i].innerHTML, "state": 's'};
			jobDescs[i].innerHTML = short_text[i].desc;
			short_text[i] = temp;
		} else if (short_text[i].state == 's') {
			document.getElementById("jobTitle"+i).className = "jobTitle noPrint";
		  setTimeout("resizeShort("+i+", "+jobDescs[i].clientHeight+")", 20);
		}
	}

	var e = document.getElementById("jobDesc"+nr);
  if (e != null) {
		var clientHeight = e.clientHeight;
		e.style.height = "19px";
		setTimeout("resizeLong("+nr+", 19, "+clientHeight+")", 20);
	}
}

function replaceAllJobs() {
	var jobDescs = getElementsByNameIE("jobDesc");
	for (var i=0; i<jobDescs.length; i++) {
		var temp = {"desc" : jobDescs[i].innerHTML, "state": 'l'};
	  jobDescs[i].innerHTML = short_text[i]+' <a onclick="showJob(\''+i+'\')">Stellenbeschreibung</a>';
		short_text[i] = temp;		
		document.getElementById("jobTitle"+i).className = "jobTitle noPrint";
	}
}

function shorten_text(text, length) {  
  return substr(text, 0, strpos(text, " ", length));
}

function resizeLong(nr, actHeight, newHeight) {
	actHeight+=3;
	if (actHeight < newHeight) 
    document.getElementById("jobDesc"+nr).style.height = actHeight + "px";
	else 
    document.getElementById("jobDesc"+nr).style.height = "auto";
	if (actHeight < newHeight) {
  	setTimeout("resizeLong("+nr+", "+actHeight+", "+newHeight+")", 10);
	}  
}

function resizeShort(nr, actHeight) {
	actHeight-=3;
	if (actHeight > 19) 
    document.getElementById("jobDesc"+nr).style.height = actHeight + "px";
	else {
		var e = document.getElementById("jobDesc"+nr);
		e.className = "noPrint";
    e.style.height = "";
		var temp = {"desc" : e.innerHTML, "state": 'l'};
		e.innerHTML = short_text[nr].desc;
		short_text[nr] = temp;
	}
	if (actHeight > 19) {
  	setTimeout("resizeShort("+nr+", "+actHeight+")", 10);
	}  
}

function togglePiktogramm(name, value) {
	document.getElementById('piktogramm'+name).src = 'images/piktogramme/'+name.toLowerCase()+value+'.png';
}

function getPosX(obj) {	
	var x = obj.offsetLeft;
	if (!obj.offsetParent) 
	  return x;
	else 
	  return x+getPosX(obj.offsetParent);
}
function getPosY(obj) {	
	var y = obj.offsetTop;
	if (!obj.offsetParent) 
	  return y;
	else 
	  return y+getPosY(obj.offsetParent);
}

// ####### AJAX Start

var http_request = false;
if (window.XMLHttpRequest) { 				// Mozilla, Safari,...
	http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { 	// IE
	try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
}
if (!http_request) {
	document.write('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
}



function setInformationBoxPrint(value) {
	if (value) {
    document.body.className = "infoPrint";
	} else {
    document.body.className = "";
	}
}

function showBoxPlaces( placeid) {
	http_request.open('POST', 'ajax/stores.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('placeid='+placeid);
	http_request.onreadystatechange = loadInformationBox;
	initInformationBox(350, 160, 300, 200);
}

function getScroll() {
  var scrOfY = 0;
  if (typeof (window.pageYOffset) == 'number') {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if (document.body && document.body.scrollTop) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function initInformationBox(left, top, width, height) {
	var obj = document.getElementById('InformationBox');
	document.getElementById('InformationContent').innerHTML = '<div id="Loading"><img src="images/general/loading.gif" alt="Daten werden geladen" /></div>';
	obj.style.display = 'block';
	obj.style.left = left + "px"; 
	obj.style.top = (getScroll() + top) + "px";
	obj.style.width = width + "px";
	obj.style.height = "auto"; 
	setInformationBoxPrint(true);
}

function loadInformationBox() {  
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
		document.getElementById('InformationContent').innerHTML = http_request.responseText;
  }
}

function closeInformationBox() {
	document.getElementById('InformationBox').style.display = 'none';
	setInformationBoxPrint(false);
}


function setSpecialBox(special_id) {
  document.getElementById('SpecialContainer').innerHTML = special_box[special_id];
}

function showSpecialBox(special_id) {
	var obj = document.getElementById('SpecialBox');
	if (obj.offsetLeft > 511) {
		obj.style.left = (obj.offsetLeft - Math.ceil(Math.abs(510 - obj.offsetLeft)/10))	+ "px";
    setTimeout("showSpecialBox("+special_id+")", 40);
		document.getElementById('piktogramme').getElementsByTagName('img')[special_id].style.backgroundImage = 'url(images/piktogramme/showback.gif)';
	} else {
    setTimeout("hideSpecialBox("+special_id+")", 4000);
	}
}


function hideSpecialBox(special_id) {
	var obj = document.getElementById('SpecialBox');
	if (obj.offsetLeft < 670) {
		obj.style.left = (obj.offsetLeft + Math.ceil(Math.abs(510 - obj.offsetLeft)/10))	+ "px";
    setTimeout("hideSpecialBox("+special_id+")", 40);
	} else {	
		document.getElementById('piktogramme').getElementsByTagName('img')[special_id].style.backgroundImage = 'none';
		special_id = (special_id+1)%3;
		setSpecialBox(special_id);
    setTimeout("showSpecialBox("+special_id+")", 5000);
	}
}


function showProductInformation(productid) {
	http_request.open('POST', 'ajax/products.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('productid='+productid);
	http_request.onreadystatechange = loadInformationBox;
	initInformationBox(250, 160, 500, 600);
}

function showSpecialInformation() {
	http_request.open('POST', 'ajax/special.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('');
	http_request.onreadystatechange = loadInformationBox;
	initInformationBox(270, 160, 480, 400);
}

function showMealsInformation(id) {
	http_request.open('POST', 'ajax/meals.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	if (id == 0) {
		http_request.send('');
	} else {
		http_request.send('storeid='+id);
	}
	http_request.onreadystatechange = loadInformationBox;
	initInformationBox(60, 60, 900, 600);
  document.body.className = "infoPrint dinnerPrint";	
}

function showPictureBox(picture) {
	http_request.open('POST', 'ajax/picture.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('picture='+picture);
	http_request.onreadystatechange = loadInformationBox;
	initInformationBox(250, 160, 500, 600);
} 


function showNewsletter(storeid) {
	http_request.open('POST', 'ajax/newsletter.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	if (storeid)
	  http_request.send('storeid='+storeid);
	else 
	  http_request.send('');
	http_request.onreadystatechange = loadInformationBox;
	initInformationBox(280, 160, 460, 400);
}

function getNewsletter() {
	http_request.open('POST', 'ajax/newsletter.php', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var stores = document.getElementsByName('nlStores');
	var store_set = '';
	for (var i=0; i<stores.length; i++) {
		if (stores[i].checked) 
		  store_set += '&stores[]='+stores[i].value;
	}
	http_request.send('name='+document.getElementById('nlName').value+'&mail='+document.getElementById('nlMail').value+store_set);
	http_request.onreadystatechange = resultNewsletter;
	
}

function resultNewsletter() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
		document.getElementById('newsletterResult').innerHTML = http_request.responseText;
	}
}


// Galerie Skripte
var gallery_id;

function setOpacity(elem, value) {
		elem.opacity = value/100;
		elem.filter = '-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity='+value+')";';	
		elem.filter = 'alpha(opacity='+value+')';	
}
var timeoutGallery = 0;
function setGalleryPicture(count) {
	var gallery = document.getElementById('galleryOverview');
	for (var i=0; i<gallery.childNodes.length; i++) setOpacity(gallery.childNodes[i].style, 40);
  var image = gallery.childNodes[count];
	setOpacity(image.style, 100);
	document.getElementById('showpicture').src = 'galerie/'+gallery_id+'/'+image.id;
	var margin = (count-2) * -110+55;
	if (margin > 0) margin = 0;
	if (margin < -110*(gallery.childNodes.length-6)) margin = -110*(gallery.childNodes.length-6);
	var gc = document.getElementById('GalleryContainer');
	if (gc.className == 'showAll') {
		gc.className = '';		
		document.getElementById('galleryOverview').style.marginLeft = margin+'px';
	} else {
		if (timeoutGallery != 0) clearTimeout(timeoutGallery); 
		moveGalleryTo(margin);  
	}
}

function moveGallery(direction) {
	var gallery = document.getElementById('galleryOverview');	
	var oldValue = gallery.style.marginLeft.substr(0, gallery.style.marginLeft.indexOf('p'));
	if (timeoutGallery != 0) clearTimeout(timeoutGallery); 
	var margin = (Math.round(oldValue/110))*110-(direction * 6*110);
	if (margin > 0) margin = 0;
	if (margin < -110*(gallery.childNodes.length-6)) margin = -110*(gallery.childNodes.length-6);
	moveGalleryTo(margin);  
}

function moveGalleryTo(newValue) {
	var gallery = document.getElementById('galleryOverview');
	var oldValue = gallery.style.marginLeft.substr(0, gallery.style.marginLeft.indexOf('p'));
	var direction = (oldValue-newValue)==0?1:(oldValue-newValue)/Math.abs(oldValue-newValue);
  var newPos = oldValue-(Math.sqrt(Math.abs(oldValue - newValue)) * direction); 
  if (Math.abs(newValue - newPos) < 2) {
    gallery.style.marginLeft = Math.round(newValue)+'px';	
		timeoutGallery = 0;
	} else {
    gallery.style.marginLeft = Math.round(newPos)+'px';	
    timeoutGallery = setTimeout('moveGalleryTo('+newValue+')', 30);
	}
}

function showGallery() {
	document.getElementById('GalleryContainer').className = 'showAll';
	document.getElementById('galleryOverview').style.marginLeft = '0px';
	
}
