Event.observe(window, 'resize', function() {
	calculateHeight();
});

function reopenOrderbox() {
	if (xmlHttp.readyState==4) {
		work=0;
		doReopenOrderbox = 0;
		openOrderbox();
	}
}

function changeAmount(product_id, amount) // 'plus' oder 'minus' als 2. Parameter
{
	if (xmlHttp && work==0)
	{
		stamp = new Date() * getRandom();
		work=1;
		xmlHttp.open('GET', domain + 'ajax_amount.php' + '?sid=' + sid + '&pid=' + product_id + '&set_amount=' + amount + '&' + stamp, true);
		xmlHttp.onreadystatechange = reloadCart;
		xmlHttp.send(null);
	} else if (work==1) setTimeout("changeAmount("+product_id+", '"+type+"')", 100);
}

function send_order2(el) {
	Effect.Fade('productbox-wrapper');

	var text = "<div style='overflow:hidden; height:520px; text-align:center;'>";
	text += "<p><img src='/main-res2/pics/submit.gif' alt='' height='15'></p>\n";
	//text += "<br/>";
	text += "<p>Die Bestellung wird &uuml;bermittelt ...</p>";
	text += "</div>";
	$('trackingbox').innerHTML = text;

	hs.htmlExpand(el, { contentId: 'trackingbox-wrapper' });
	send_order();
	return false;
}

function transferOrder() {
	resetHS();
	post_order();
	listCart();
}

function reloadOrderbox() {
	//if (!trackingboxID) var trackingboxID = 'orderbox';
	if (xmlHttp.readyState==4) {
		work=0;
		text = xmlHttp.responseText;
		$('trackingbox').innerHTML=text;
//		var ajaxload = document.getElementById("ajaxload");
//		ajaxload.innerHTML = "";
		resetHS();
		setTimeout('ordertracking()', 1000);
	}
}

function getWindowHeight(win) {
	if (win == undefined) win = window;
	if (win.innerHeight) {
			return win.innerHeight;
	}
	else {
			if (win.document.documentElement
					&& win.document.documentElement.clientHeight) {
					return win.document.documentElement.clientHeight;
			}
			return win.document.body.offsetHeight;
	}
}

function calculateHeight() {
	// Ab 675 Pixel Höhe, wird in der Produktbox der Warenkorb unten angezeigt.
	minHeight = 675;
	productboxHeight = (getWindowHeight() < minHeight) ? 80 : 122;
	productboxMargin = (getWindowHeight() < minHeight) ? 0 : 42;
	footerZindex = (getWindowHeight() < minHeight) ? 5 : 100;

	overflowY = (getWindowHeight() < (minHeight-100)) ? 'auto' : 'hidden';

	$('productbox-wrapper').style.height=(getWindowHeight() - productboxHeight) + "px";
	$('productbox-wrapper').style.marginBottom = productboxMargin + "px";

	$('productbox-wrapper').style.overflowY = overflowY;

	$('contentWrapper2').style.height=(getWindowHeight() - 122) + "px";
	$('contentWrapper2').style.overflowY="auto";
	$('footerWrapper').style.zIndex = footerZindex;
}

function openProductbox(el, productID, size) {
	if (!cartVisible) { toggleFooterbar(); }
	stamp = new Date() * getRandom();
	work=1;
	if (size=="edit") {
		xmlHttp.open('GET', domain + 'ajax_dialog.php?itemID=' + productID + '&sid=' + sid + '&' + stamp, true);
	} else {
		xmlHttp.open('GET', domain + 'ajax_dialog.php?productID=' + productID + '&size=' + size + '&sid=' + sid + '&' + stamp, true);
	}
	xmlHttp.onreadystatechange = openProductboxWrapper;
	xmlHttp.send(null);
}

function openProductboxWrapper() {
	if (xmlHttp.readyState==4) {
		work=0;
		text = xmlHttp.responseText;
		$('productbox').innerHTML = text;
		Effect.Appear('productbox-wrapper');
	}
}

function openOrderbox() {
	if (!cartVisible) { toggleFooterbar(); }
	stamp = new Date() * getRandom();
	work=1;
	xmlHttp.open('GET', domain + 'ajax_order.php' + '?sid=' + sid + '&' + stamp, true);
	xmlHttp.onreadystatechange = openOrderboxWrapper;
	xmlHttp.send(null);
}

function openOrderboxWrapper() {
	if (xmlHttp.readyState==4) {
		work=0;
		text = xmlHttp.responseText;
		$('productbox').innerHTML = text;
		checkForm();
		Effect.Appear('productbox-wrapper');
	}
}

var cartVisible = true;

function toggleFooterbar(y) {
	var hoehe = 250;
	if ($('cart')) { hoehe = $('cart').getHeight(); }

	if (!y) y=0;
	cartY = (cartVisible) ? '+'+hoehe+'px' : y + 'px';
	cartVisible = !cartVisible;

	if ($('footerWrapper')) $('footerWrapper').morph('bottom:'+cartY+";", { duration: 0.5 });
	if (cartVisible) $('footerWrapper').title = "Warenkorb einblenden"; else $('footerWrapper').title= "Warenkorb ausblenden";
}


function reloadCart() {
	if (xmlHttp.readyState==4) {

		work=0;
		text = xmlHttp.responseText;
		var fehler = text.split("|||");
		if (text != "" && fehler[0] != "FEHLER" && ! errorbox_open)
		{ // das ist drinne neu
			$('cart').innerHTML = text;
			if ($('cart_num') && $('cart_sum') && $('anzahl_preis'))
			{
				var h = $('cart').getHeight();
				$('anzahl_preis').innerHTML = "Inhalt: "+$('cart_num').innerHTML+" Artikel &nbsp;Gesamtpreis: "+$('cart_sum').innerHTML;
				if (!cartVisible) { $('footerWrapper').style.bottom = h+"px"; forceOpenCart = false; }
			}
		}
		else if (fehler[0] == "FEHLER") errorbox(fehler[1], fehler[2]);
		if ($('ajaxloadCart') != null) $('ajaxloadCart').innerHTML="&nbsp;";

		// das hier nicht mit ins globale nehmen
		if ($('footerWrapper')) {
			if (cartVisible) {
				toggleFooterbar(0);
				setTimeout('toggleFooterbar()', 2000);
			}
		}

	}
}

/*
// Legt ein fertiges Produkt in den Warenkorb.
function addCart(id,groesse) {
	stamp = new Date() * getRandom();
	if (xmlHttp && work==0) {
		dialog=0;
		if ($('ajaxloadCart') != null) $('ajaxloadCart').innerHTML = "<img src=\"" +domain+"res/pics/ajaxload.gif\" alt=\"\">";
		work=1;
		xmlHttp.open('GET', domain + 'ajax_add_produkt.php?id=' + id + '&groesse=' + groesse + '&sid=' + sid + '&' + stamp, true);
		xmlHttp.onreadystatechange = reloadCart;
		xmlHttp.send(null);
	} else if (work==1) setTimeout("addCart("+id+","+groesse+")", 100);
	if (cartVisible) { forceOpenCart = true; }
}
*/


function reloadTracking() {
	if (xmlHttp.readyState==4) {
		var styledisplay;
		var zaehler;
		work=0;
		var text = xmlHttp.responseText;
		var textteile = text.split('|');

		if (textteile[0] != tracking && textteile[0] >= 0)
		{
			//var arr = document.getElementsByClassName('status');
			var arr = $$('.status');
				for (var i = 0; i < arr.length; i++)
			{
				arr[i].className = 'status1';
			}

			$('tracking').innerHTML += textteile[1];
			resetHS();
   	}
		tracking = textteile[0];
	if (tracking == 3 || tracking == 4)
	{
			//var arr = document.getElementsByClassName('status');
			var arr = $$('.status');
			for (var i = 0; i < arr.length; i++)
			{
				arr[i].className = 'status1';
			}
	}

	if (textteile[0] != 3 && textteile[0] != 4) setTimeout('ordertracking()', 10000);
	}
}

