var content_breite = 0;
var content_hoehe = 0;
var hoehe_center = 0;

var preloads = new Array();

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() {
  var i, x, a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++){ x.src=x.oSrc;}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){
  	document.MM_sr[j++]=x;
	if (!x.oSrc) { if (x.filters && x.filters.length && x.filters[0]) x.oSrc=x.filters[0].src; else x.oSrc=x.src; } 
	x.src=a[i+2];
  }
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { if (args[i+1] != "") { nm=args[i+1]; } else { nm=val.name; } if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' muss eine echte E-Mail-Adresse sein.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' muss eine Zahl sein.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' muss eine Zahl zwischen '+min+' und '+max+' sein.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' ist erforderlich.\n'; }
  }
  document.MM_returnValue = errors;
}


function zentrieren() {
	var i, obj, breite, hoehe;
	
	
 	if (window.innerWidth) {
		breite = window.innerWidth;
  	} else if (document.body && document.body.offsetWidth) {
    	breite = document.body.offsetWidth;
	} else {
    	breite = 0;
	}	

	if (window.innerHeight) {
		hoehe = window.innerHeight;
  	} else if (document.body && document.body.offsetHeight) {
    	hoehe = document.body.offsetHeight;
	} else {
    	hoehe = 0;
	}
	obj = document.getElementById("center_div");


	if (obj) {
		i = breite - content_breite;
		i = i / 2;
		if (i < 0) i = 0;

		j = hoehe - content_hoehe;
		j = j / 2;
		if (j < 0) j = 0;
	
		obj.style.left = i+"px";
		if (hoehe_center) {
			obj.style.top = j+"px";
		}
	}
}

function zentrierenBreite(breite) {
	var i, obj, tmpBreite;
	
	content_breite = breite;
	
 	if (window.innerWidth) {
		tmpBreite = window.innerWidth;
  	} else if (document.body && document.body.offsetWidth) {
    	tmpBreite = document.body.offsetWidth;
	} else {
    	tmpBreite = 0;
	}	
	
	i = tmpBreite - breite;
	i = i / 2;
	if (i < 0) i = 0;
	
	/*return i;*/
	document.write('<style type="text/css">');
	document.write('#center_div { position: absolute; left: '+i+'px; }');
	document.write('</style>');
}

function zentrierenHoehe(hoehe, vertikal) {
	var i, obj, tmpHoehe;

	content_hoehe = hoehe;
	hoehe_center = vertikal;

	if (window.innerHeight) {
		hoehe = window.innerHeight;
  	} else if (document.body && document.body.offsetHeight) {
    	hoehe = document.body.offsetHeight;
	} else {
    	hoehe = 0;
	}

	j = hoehe - content_hoehe;
	j = j / 2;
	if (j < 0) j = 0;
	
	if (!vertikal) {
		j = 0;
	} 
	
	/*return j;*/
	document.write('<style type="text/css">');
	document.write('#center_div { position: absolute; top: '+j+'px; }');
	document.write('</style>');
}

function preload() {
  var d = document; 
  if (d.images){ 
  	if (!d.MM_p) d.MM_p = new Array();
    var j = d.MM_p.length;
    for (var i = 0; i < preloads.length; i++) {
		if (preloads[i].indexOf("#") != 0) {
			d.MM_p[j] = new Image; 
			d.MM_p[j++].src = preloads[i];
		}
    }
  }
}

function getSel()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	}
	else return;
	document.forms[0].selectedtext.value = 'Found in: ' + foundIn + '\n' + txt;
}

function addResizeEvent(func) {
  var old = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function(e) {
      old(e);
      func(e);
    }
  }
}

function addScrollEvent(func) {
  var old = window.onscroll;
  if (typeof window.onscroll != 'function') {
    window.onscroll = func;
  } else {
    window.onscroll = function(e) {
      old(e);
      func(e);
    }
  }
}

var prodgroup_effect = {};
function toggle_prodgroup(id) {
	if (!$('nlb'+id)) return;
	if (!prodgroup_effect[id]) prodgroup_effect[id] = {};
	if(  (prodgroup_effect[id].a && prodgroup_effect[id].a.state != 'finished')
	   || (prodgroup_effect[id].b && prodgroup_effect[id].b.state != 'finished')) return;
	
	if ($('nlb'+id).style.display == "none") {
		if ($('slade_button'+id)) $('slade_button'+id).className = 'seg-header-open';
		if ($('slade'+id)) prodgroup_effect[id].a = Effect.SlideDown('slade'+id);
		prodgroup_effect[id].b = Effect.SlideDown('nlb'+id);
	} else {
		if ($('slade_button'+id)) $('slade_button'+id).className = 'seg-header-close';
		if ($('slade'+id)) prodgroup_effect[id].a = Effect.SlideUp('slade'+id);
		prodgroup_effect[id].b = Effect.SlideUp('nlb'+id);
	}
	
}

function init_trader_search_input() {
	var id = "plz_ort";
	if ($(id).value == "Bitte Postleitzahl oder Ort eingeben.") $(id).value = "";
}

function trader_search_input() {
	var id = "plz_ort";
	init_trader_search_input(id);
	if ($(id).value.match(/^\s*$/)) {
		alert("Bitte geben Sie eine Postleitzahl oder einen Ortsnamen ein.")
	} else trader_search(null, $(id).value)
}

function trader_search_select(obj) {
	if (obj.selectedIndex > 0) trader_search(obj.options[obj.selectedIndex].value);
}

function trader_search(zone, value) {
	if (value == null) value = "";
	if (zone == null)  zone  = "";
	$('traderSearchResult').innerHTML = '<div class="tsr-searching"><img src="/shared/pics/trader/searching.gif" width="32" height="32" border="0" /></div>';
	
	new Ajax.Updater('traderSearchResult', '/cgi/engine.pl', {
			 method:			"post",
			 parameters:	"action=tradersearch&zone="+zone+"&value="+value
	})
}

function pressedKey(event) {
	return event.keyCode;
}

function O() {}

function submitTraderLogin(id) {
	$(id).submit();
}

function loadCartCount(id) {
	new Ajax.Updater(id, '/cgi/engine.pl', {
			 method:			"post",
			 parameters:	"action=cart_count"
	})
}

function openPrintCart(url) {
	var height = windowHeight();
	if (!height) height = 600;
	window.open(url, "printCart", "scrollbars=yes,resizable=yes,width=620,height=" + height);
}


function submitKontaktFormular(id) {
	var f = $(id);
	MM_validateForm('Vorname','','R','Name','','R','Strasse','','R','Hausnummer','','R','PLZ','','R','Ort','','R','E-Mail','','RisEmail');
	if (document.MM_returnValue != "") {
		alert(document.MM_returnValue);
	} else {
		f.submit();
	}
}



function onlineShopsChangeOrder(v) {
	var f = $('online_shop_params');
	f.order.value = v;
	f.submit();
}

function onlineShopsChangeSite(s) {
	var f = $('online_shop_params');
	f.site.value = s;
	f.submit();
}

function onlineShopsChangeFilter(obj) {
	var f = $('online_shop_params');
//	alert(obj.name +" / "+ obj.checked +" / "+f[obj.name]);
	f[obj.name].value = obj.checked ? 1 : 0;
	f.submit();
}


document.sdvPool = function (obj) {
		this.object = obj;
}
document.sdvPool.prototype.shift = function () {
	while (this.object.childNodes.length) {
		var o = this.object.childNodes[0];
//		alert("shift: " + o);
		this.object.removeChild(o);
		if (o.className == "sdv-block") return o;
	}
}
document.sdvPool.prototype.unshift = function (o) {
	if (o) this.object.insertBefore(o, this.object.childNodes[0])
}
document.sdvPool.prototype.pop = function () {
	while(this.object.childNodes.length) {
		var o = this.object.childNodes[this.object.childNodes.length - 1];
//		alert("pop: " + o);
		this.object.removeChild(o);
		if (o.className == "sdv-block") return o;
	}
}
document.sdvPool.prototype.push = function (o) {
	if (o) this.object.appendChild(o)
}

function sdv_left () {
	var c = new document.sdvPool($('sdvcontainer'));
	var p = new document.sdvPool($('sdvpool'));
	p.push(c.shift());
	c.push(p.shift());
}

function sdv_right () {
	var c = new document.sdvPool($('sdvcontainer'));
	var p = new document.sdvPool($('sdvpool'));
	p.unshift(c.pop());
	c.unshift(p.pop());
}



function xScroll () {
		if (window.innerHeight && window.scrollMaxY) {	
			return window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			return document.body.scrollWidth;
		} else { 
			return document.body.offsetWidth;
		}
}

function yScroll () {
		if (window.innerHeight && window.scrollMaxY) {	
			return window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			return document.body.scrollHeight;
		} else { 
			return document.body.offsetHeight;
		}
}

function windowWidth () {
		if (window.innerWidth) {	// all except Explorer
			if(document.documentElement && document.documentElement.clientWidth){
				return document.documentElement.clientWidth; 
			} else {
				return window.innerWidth;
			}
		} else if (document.documentElement && document.documentElement.clientWidth) {
			return document.documentElement.clientWidth;
		} else { 
			return document.body.clientWidth;
		}	
}

function windowHeight () {
		if (window.innerHeight) {	// all except Explorer
			return window.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			return document.documentElement.clientHeight;
		} else { 
			return document.body.clientHeight;
		}	
}


function showShadow() {
	var s = $('shadow');
	
	if (!s) {
		s = document.createElement('div');
		s.id = 'shadow';
		new document.sdvPool(document.body).unshift(s);
	}
	
	var breite = xScroll() < windowWidth()  ? windowWidth()  : xScroll();
	var hoehe  = yScroll() < windowHeight() ? windowHeight() : yScroll();
	
	s.style.width   = breite + "px";
	s.style.height  = hoehe  + "px";
	s.style.display = 'block';
}

function hideShadow() {
	$('shadow').style.display = 'none';
}

function showLargeImage() {
	var i = $('largeImage');
	if (!i) {
		i = document.createElement('div');
		i.id = 'largeImage';
		new document.sdvPool(document.body).unshift(i);
	}

	var c = $('largeImageContent');
	if (c.parent != i) i.appendChild(c);

	showShadow();
//	c.style.display = 'block';
	i.style.display = 'block';
}

function hideLargeImage() {
	$('largeImage').style.display = 'none';
	hideShadow();
}

function check_gewinnform() {
	jQuery.ajax({
	    type:    'POST',
	    url:     '/cgi/engine.pl',
	    data:    $('gewinn_form').serialize(),
	    dataType: 'json',
	    success: function (response) {
			if (response.success) {
				$('geburtstagscontent').style.display = 'none';
				$('geburtstag_bottom').style.display = 'none';
				$('geburtstagscontent_danke').style.display = '';
				$('danke_bottom').style.display = '';
				$('ballons_extra').style.display = '';
				$('left-navigation').style.minHeight = '1px';
				$('left-navigation').style.height = '0';
			} else {
				$('captcha_pic').src = "/captcha.gif?"+Math.random();
				$('nachricht').addClassName('fehler');
		        $('nachricht').innerHTML = response.message;
		        $('nachricht').show();
			}
			
	    },		
	    error: function (request, status, error) {
	    	var msg = 'An error occured while your request was submitted; please retry later: ' + error + ' request: ' + request + ' status: ' + status;
	    }
	});
}

function do_onload_stuff() {
	//preload();
	if ($('shadowlayer')) {
		var hgt = getDocumentHeight();
		var wdt = getDocumentWidth();
		$('shadowlayer').style.height = hgt + 'px';
		$('shadowlayer').style.width = wdt + 'px';
	}
}

//Get the height/width of the entire document
function getDocumentHeight() {
	var a=0,b=0;
	if (!isNaN(document.body.parentNode.scrollHeight)) a=document.body.parentNode.scrollHeight;
	if (!isNaN(window.innerHeight)) b=window.innerHeight;
	return Math.max(a,b);
}
function getDocumentWidth() {
	var a=0,b=0;
	if (!isNaN(document.body.parentNode.scrollWidth)) a=document.body.parentNode.scrollWidth;
	if (!isNaN(window.innerWidth)) b=window.innerWidth;
	var w = Math.max(a,b);
	// keine horizontalen Scrollbalken erzeugen, nur weil schon ein vertikaler da ist
	if (window.innerHeight < document.body.parentNode.scrollHeight) {
		w -= getScrollerWidth();
	}
	return w;
}

function getScrollerWidth() {
	var scr = null;
	var inn = null;
	var wNoScroll = 0;
	var wScroll = 0;

	// Outer scrolling div
	scr = document.createElement('div');
	scr.style.position = 'absolute';
	scr.style.top = '-1000px';
	scr.style.left = '-1000px';
	scr.style.width = '100px';
	scr.style.height = '50px';
	// Start with no scrollbar
	scr.style.overflow = 'hidden';

	// Inner content div
	inn = document.createElement('div');
	inn.style.width = '100%';
	inn.style.height = '200px';

	// Put the inner div in the scrolling div
	scr.appendChild(inn);
	// Append the scrolling div to the doc
	document.body.appendChild(scr);

	// Width of the inner div without scrollbar
	wNoScroll = inn.offsetWidth;
	// Add the scrollbar
	scr.style.overflow = 'auto';
	// Width of the inner div with scrollbar
	wScroll = inn.offsetWidth;

	// Remove the scrolling div from the doc
	document.body.removeChild(document.body.lastChild);

	// Pixel width of the scroller
	return (wNoScroll - wScroll);
}

function hideShadowLayer() {
	//$('shadowlayer').hide();
	$('shadowlayer').fade({ duration: 1.0, from: 0.5, to: 0.00 });
}

function showShadowLayer() {
	$('shadowlayer').show();
	$('shadowlayer').fade({ duration: 1.0, from: 0.00, to: 0.5 });
}

function showGebLayer(gewinn_link) {
	var cookie = getCookie("homelayer");
	if (!cookie) {
		document.cookie = "homelayer=1";
		showShadowLayer();
		$('flash_container').show();
		var flash = new SWFObject("/shared/flash/intro.swf?clicktag="+gewinn_link+"&clicktarget=_self", "flash", "875", "735", "7");
		if (flash && flash.installedVer && flash.installedVer.major >= 9) {
			flash.addParam("wmode", "transparent");
			flash.write("flash");
		} else {
			$('flash_install').show();
		}
	}
}

function hideGebLayer() {
	hideShadowLayer();
	$('flash_container').hide();
}

function getCookie(name) {
	a = document.cookie;
	res = '';
	while(a != '' && res == '') {
		cookiename = a.substring(0,a.search('='));
		if (a.search(';') >= 0) {
			cookiewert = a.substring(a.search('=')+1,a.search(';'));
		} else {
			cookiewert = a.substring(a.search('=')+1,a.length);
		}
		
		while(cookiename.substring(0, 1) == ' ') {
			cookiename = cookiename.substring(1, cookiename.length);
		}
		
		if(name == cookiename) {res = unescape(cookiewert);}
		
		i = a.search(';')+1;
		if(i == 0) {i = a.length;}
		a = a.substring(i,a.length);
	}
	return res;
}

