function windowHeight() {
	if (self.innerHeight) return self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	else if (document.body) return document.body.clientHeight;
}

function windowWidth() {
	if (self.innerWidth) return self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
	else if (document.body) return document.body.clientWidth;
}

function windowScrollTop() {
	if (self.pageYOffset) return self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
	else if (document.body) return document.body.scrollTop;
}

function windowScrollLeft() {
	if (self.pageXOffset) return self.pageXOffset;
	else if (document.documentElement && document.documentElement.scrollLeft) return document.documentElement.scrollLeft;
	else if (document.body) return document.body.scrollLeft;
}
function popUp(url, width, height) {
	var left = (screen.availWidth-width) / 2 - 10;
	var top = (screen.availHeight-height) / 2 - 30;
	if (left < 0) left = 0;
	if (top < 0) top = 0;
	var popUpWindow = window.open(url,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top);
	popUpWindow.focus();
} 

function popupImageWindow(url) {
	var width = 710;
	var height = 612;
	var left = (screen.availWidth-width) / 2 - 10;
	var top = (screen.availHeight-height) / 2 - 30;
	if (left < 0) left = 0;
	if (top < 0) top = 0;
	var win_name = url.substring(url.lastIndexOf('imgID/')+6, url.lastIndexOf('.')).replace(/\W/, '');
	var img_win = window.open(url, win_name, 'toolbar=no,location=no,personalbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',left='+left+',top='+top);
	img_win.focus();
}

function changeMoreImg(which) {

	var container = which.parentNode.parentNode.parentNode.previousSibling.childNodes[which.parentNode.parentNode.parentNode.previousSibling.childNodes.length-1];
	var rem_height = container.firstChild.offsetHeight;
	var rem_width = container.firstChild.offsetWidth;
	var bez = rem_height/rem_width;

	container.firstChild.height = 40;
	container.firstChild.width = parseInt(40/bez);

	var newimg = which.firstChild.cloneNode(true);
	newimg.removeAttribute("height");
	newimg.removeAttribute("width");

	if (container == container.parentNode.firstChild) {
		var first = container.firstChild;
		first.removeAttribute('style');
		container.parentNode.insertBefore(first, container);
		first.className = 'first';
		first.onclick = function() {
			this.nextSibling.removeChild(this.nextSibling.firstChild)
			this.removeAttribute("height");
			this.removeAttribute("width");
			this.removeAttribute("onclick");
			this.className = '';
			this.nextSibling.appendChild(this);
			return false;
		}		
	}
	else container.removeChild(container.firstChild);

	container.appendChild(newimg);
	
	which.blur();
}

function stopslide() {
	window.clearInterval(slider);
	if (document.getElementById('slider')) document.getElementById('slider').id = '';
}

function slide_left() {
	if (document.getElementById('slider') && document.getElementById('slider').offsetLeft < 0) {
		document.getElementById('slider').style.left = (document.getElementById('slider').offsetLeft + 2)+'px';
	}
	else {
		if (document.getElementById('slider')) document.getElementById('slider').parentNode.previousSibling.className += ' leftinactive';
		stopslide();
	}
}

function slide_right() {
	if (document.getElementById('slider') && document.getElementById('slider').offsetLeft > -(document.getElementById('slider').offsetWidth)+document.getElementById('slider').parentNode.offsetWidth) {
		document.getElementById('slider').style.left = (document.getElementById('slider').offsetLeft - 2)+'px';
	}
	else {
		if (document.getElementById('slider')) document.getElementById('slider').parentNode.nextSibling.className += ' rightinactive';
		stopslide();
	}
}

function slide(which) {
	if (which.nextSibling && which.nextSibling.className == 'moreimg') {
		which.nextSibling.firstChild.id = 'slider'; 
		document.getElementById('slider').parentNode.nextSibling.className = 'arrow_right';
		slider = window.setInterval("slide_left()", 10);
	}
	else {
		which.previousSibling.firstChild.id = 'slider';
		document.getElementById('slider').parentNode.previousSibling.className = 'arrow_left';
		slider = window.setInterval("slide_right()", 10);
	}
}


// based on suckerfish dropdowns: http://www.alistapart.com/articles/dropdowns
setUpDropdowns = function() {
	//var ie6 = document.getElementById("navi").firstChild.appendData('');

	if (document.getElementById) {	
		if (typeof categories == "string" && document.getElementById("navi")) document.getElementById("navi").innerHTML = categories;
		var node = document.getElementById("navroot");
		if (node) setUpDropdownsrecursiv(node, 0);
 	}
}

setUpManuDropdowns = function() {
	//var ie6 = document.getElementById("manunavi").firstChild.appendData('');

	if (document.getElementById) {
		if (typeof manucats == "string" && document.getElementById("manunavi")) document.getElementById("manunavi").innerHTML = manucats;
		var manuroot = document.getElementById("manuroot");
		if (manuroot) setUpDropdownsrecursiv(manuroot);
	}
}

setUpDropdownsrecursiv = function (node) {
	for(var j=0; j<node.childNodes.length; j++ ) {

		if( node.childNodes[j].nodeName == "LI") {
			if (node.childNodes[j].id) {
				var id_array = node.childNodes[j].getAttribute('id').split('_');
				var thisid = id_array[0].replace(/c/,"");
				var is_manu = false;
				if (id_array[1]) {
					is_manu = true;
					var thismanu = id_array[1].replace(/m/,"");
				}
			}

			if ( (page != "" && page == thisid && ((!manu && !is_manu) || (manu && is_manu && thismanu == manu))) || (thisid == '' && page == '' && is_manu && thismanu == manu) || (manu == 'true' && node.getAttribute('id') == 'manuroot')) node.childNodes[j].childNodes[1].className = "active";

			if (node.childNodes[j].childNodes[2] && node.childNodes[j].childNodes[2].nodeName == "UL") {
				var check=0;
				for (var i=0; i<path_arr.length; i++) {
					if ( (thisid == path_arr[i] && ((!manu && !is_manu) || (manu && is_manu && thismanu == manu))) || (thisid == '' && is_manu && thismanu == manu)) check = 1;
				}
				if ( manu && node.getAttribute('id') == 'manuroot') check = 1;

				node.childNodes[j].childNodes[0].style.cursor = "pointer";
				if (check == 1 && node.childNodes[j].className.indexOf("open") == -1) node.childNodes[j].className += "open";
				
				node.childNodes[j].childNodes[0].onclick=function() {
					if(this.parentNode.className.indexOf("open") == -1) this.parentNode.className += "open";
					else this.parentNode.className = this.parentNode.className.replace("open", "");
				}
			}
		}
		setUpDropdownsrecursiv(node.childNodes[j]);
	}
}



selectRow = function (wich, id) {
	if (document.getElementById) {
		doc = document.getElementById(id);
		for(var i=0; i<doc.childNodes.length; i++) {
			if (doc && doc.childNodes[i].nodeName == 'TABLE') {
				doc.childNodes[i].className = "moduleRow";
				for(var j=0; j<doc.childNodes[i].childNodes[0].childNodes.length; j++) {
					if (doc.childNodes[i].childNodes[0].childNodes[j].nodeName == 'TR') doc.childNodes[i].childNodes[0].childNodes[j].className = "";
				}
			}			
		}
		wich.className = 'moduleRowSelected';
		if (wich.childNodes[0].childNodes[0].childNodes[0].childNodes[0]) wich.childNodes[0].childNodes[0].childNodes[0].childNodes[0].checked = true;
}}

getImagePopupLink = function (img, type) {
	return 'href="'+prod['popuplink']+'?pID='+prod['id']+'&imgID='+img+'&type='+type+'" target="_blank" onclick="popupImageWindow(\''+prod['popuplink']+'?pID='+prod['id']+'&imgID='+img+'&type='+type+'\'); return false;"';
}

getAllPrices = function () {
	lastprice = '1';
	var priceadd = '0';
	for (var check1=0; check1<options.length; check1++) {
		for (var check2=0; check2<options[check1].length; check2++) {
			if (option[options[check1][check2]][3] != '0') priceadd = '1';
		}
	}
	var allprice = parseFloat(prod['price']); 
	for (var i=0; i<document.forms.cart_quantity.elements.length; i++) {
		if (document.forms.cart_quantity.elements[i].name.indexOf("id[") != -1) {
			var thisoption = document.forms.cart_quantity.elements[i].value;
			allprice = option[thisoption] ? parseFloat(allprice+eval(option[thisoption][2]+parseFloat(option[thisoption][3]))) : allprice;
			if (!option[thisoption] && priceadd == '1') lastprice = '0';
		}
	}
	return allprice;
}

formatPrice = function (price, from) {
	price = parseFloat(price);
	price = price.toFixed(prod['currency'][5]);
	pricesplit = price.toString().split("."); 
	pricethousends = new Array();
	var pr = pricesplit[0].length; var count = 0;
	while (pr>0) {
		pricethousends[count] = (pr > 3) ? pricesplit[0].substr(pr-3,3) : pricesplit[0].substr(pr-pr,pr);
		pr = pr-3; count++;
	}
	price = pricethousends.reverse().join(prod['currency'][4])+prod['currency'][3]+pricesplit[1];
	price = '<span class="cursymbol">'+from+prod['currency'][1]+'</span> <span class="price">'+price+'</span> <span class="cursymbol">'+prod['currency'][2]+'</span>';
	return price;
}

changeOptions = function (parent, selfid, selfpid, element) {
	var string = ''; 
	var optionslist = new Array();
	var optimages = '', itemnumber = '';
	for (var i=0; i<pd.length; i++) {
		if (selfid == pd[i][0] && selfpid == pd[i][1]) {
			var id = pd[i][0], pid = pd[i][1], name = pd[i][2]; 
			string += '<div class="trow"><select name="id['+id+'_'+pid+']" onchange="changeOptions(this.options[this.selectedIndex].value, '+id+', '+pid+', \'attr'+id+'_'+pid+'\');">';
			//<div class="left">'+name+':</div>
			string += '<option value="'+name+'">'+name+'</option>';
			for (var o=0; o<pd[i][3].length; o++) { 
				var selected = '';
				var optionprice = (option[pd[i][3][o]][3] != '0') ? ' '+option[pd[i][3][o]][2]+' '+formatPrice(option[pd[i][3][o]][3], '') : '';
				var optionitemnumber =  ''; //(option[pd[i][3][o]][4] != '') ? ' ('+option[pd[i][3][o]][4]+')' : '';
				if (option[pd[i][3][o]][0] == parent) { 
					
					selected = "selected"; 
					var parents = option[pd[i][3][o]][5].split("|");
					if (parents == '') parents.length = 0;
					parents[-1] = option[pd[i][3][o]][0]; 
					for (var optionparents=-1; optionparents<parents.length; optionparents++) { 
						if (itemnumber == '') itemnumber = option[parents[optionparents]][4];
						if (optimages == '') {
							for (var img=0; img<images[parents[optionparents]].length; img++) { 
								if (img == 1) break;
								if (img != 0) optimages += '<div class="spacer"></div>';
								optimages += '<a '+getImagePopupLink(images[parents[optionparents]][img], 'optionimg')+'><img src="'+prod['imgfolder']+images[parents[optionparents]][img]+'" /></a>';
							} 
						}
					}
					//optionprice = '';
				}
				
				string += '<option value="'+option[pd[i][3][o]][0]+'" '+selected+'>'+option[pd[i][3][o]][1]+optionitemnumber+optionprice+'</option>';
			}
			string += '</select></div>';
		
			optionslist = getOptionsChilds (parent, optionslist);
			optionslist = optionslist.join("");
			if (document.getElementById(element)) document.getElementById(element).innerHTML = string+optionslist;
			
			var price = getAllPrices();
			var from = (lastprice == '1') ? '' : prod['from']+' ';
			if (document.getElementById('price') && price != '') document.getElementById('price').innerHTML = formatPrice(price, from);
			var vpe = price * (1 / prod['vpe'][1]);
			if (document.getElementById('vpe') && price != '') document.getElementById('vpe').innerHTML = formatPrice(vpe, '')+' '+prod['vpe'][0];
			
			break;
		}
	}
	if (optimages == '') {
		optimages = '';
		for (var img=0;img<prod['images'].length; img++) {
			if (img == 1) break;
			if (img != 0) optimages += '<div class="spacer"></div>';
			optimages += '<a '+getImagePopupLink(prod['images'][img],img==0?'itemimg':'moimg')+'><img src="'+prod['imgfolder']+prod['images'][img]+'" /></a>';
		}
	}
	if (itemnumber == '') itemnumber = prod['itemnumber'];
	if (document.getElementById('prodimages') && optimages != '') document.getElementById('prodimages').innerHTML = optimages;
	if (document.getElementById('itemnumber') && itemnumber != '') document.getElementById('itemnumber').innerHTML = itemnumber;
}

getOptionsChilds = function (parent, optionslist) {
	var sort = 0;
	for (var i=0; i<pd.length; i++) {
		if (pd[i][1] == parent ) {
			var id = pd[i][0], pid = pd[i][1], name = pd[i][2];
			optionslist[sort] = new Array();
			optionslist[sort]['string'] = '<div id="attr'+id+'_'+pid+'">';
			optionslist[sort]['string'] += '<div class="trow"><select name="id['+id+'_'+pid+']" onchange="changeOptions(this.options[this.selectedIndex].value, '+id+', '+pid+', \'attr'+id+'_'+pid+'\');">';
			//<div class="left">'+name+':</div>
			optionslist[sort]['string'] += '<option value="'+name+'">'+name+'</option>';
			for (var o=0; o<pd[i][3].length; o++) { 
				if (o==0) var first = option[pd[i][3][o]][0]; 
				var optionprice = (option[pd[i][3][o]][3] != '0') ? ' '+option[pd[i][3][o]][2]+' '+formatPrice(option[pd[i][3][o]][3], '') : '';
				var itemnumber =  ''; //(option[pd[i][3][o]][4] != '') ? ' ('+option[pd[i][3][o]][4]+')' : '';
				optionslist[sort]['string'] += '<option value="'+option[pd[i][3][o]][0]+'">'+option[pd[i][3][o]][1]+itemnumber+optionprice+'</option>';
				options[sort]['images'] = '';
			}
			optionslist[sort]['string'] += '</select>';
			var childlist = '';
			
			optionslist[sort] = optionslist[sort]['string']+childlist+'</div>'+options[sort]['images']+'</div>';
			sort++;
		}
	}
	return optionslist;	
}

checkform = function () {
	var returnvalue = true;
	var string = '';
	for(var i=0; i<document.cart_quantity.elements.length; i++) {
		var val = '';
		if (document.cart_quantity.elements[i].name.indexOf("id[") != -1) {
			val = document.cart_quantity.elements[i].value;
    			if (val.indexOf("?") != -1) {
				string += 'Bitte '+val.replace(/\?/,'')+' wählen!\n';
				returnvalue = false;
			}
		}
	}
	if (document.cart_quantity.thistext) {
		if (document.cart_quantity.thistext.value.replace(/ /g,'') == '' && document.cart_quantity.text_quantity) {
			string += 'Bitte machen Sie eine Texteingabe!';
			returnvalue = false;
		}
	}
	if (string != '') alert (string);
	return returnvalue;
}

