loadImages = function () { 
	if (typeof images == 'object') {
		for (var img in images) {
			if (images[img].length > 0) {
				for (var i=0; i<images[img].length; i++) {	
					var src = images[img][i].match('(src=")(.*)(" width)');
					preload(src[2]);
				}
			}
		}
	}
}

setLinks = function () {	
	for (var i=1; i<=4; i++) {
		var element = doc("thumb"+i);
		if (element) {
			element.onclick=function () {
				changeImage(this);
				return false;
			}
		}
	}
	
}

changeImage = function (which) {
	number = parseInt(which.id.replace(/thumb/,''));
	if (images['big'][number] && images['small'][0]) {
		doc("bigimg").innerHTML = images['big'][number];
		doc(which.id).innerHTML = images['small'][0];
		img_mem_b = images['big'][0];
		img_mem_s = images['small'][0];
		images['big'][0] = images['big'][number];
		images['small'][0] = images['small'][number];
		images['big'][number] = img_mem_b;
		images['small'][number] = img_mem_s;
		return false;
	}
}


sortCountries = function() {
	var elm = doc('countrylist');
	var sort_arr = new Array();
	for (var i=0; i<elm.childNodes.length; i++) {
		sort_arr[i] = new Array(elm.childNodes[i].firstChild.firstChild.nodeValue, elm.childNodes[i].firstChild.id);
	}
	sort_arr.sort();
	for (var i=0; i<sort_arr.length; i++) {
		if (doc(sort_arr[i][1]).className != 'active') elm.appendChild(doc(sort_arr[i][1]).parentNode);
		else var act = doc(sort_arr[i][1]).parentNode;
	}
	if (act && act != elm.firstChild) elm.insertBefore(act, elm.firstChild);

}

changeOption = function(elm) {
if (elm.className != 'active') {	
	doc('inputcountry').value = elm.id; 
	if (elm.parentNode != elm.parentNode.parentNode.firstChild) elm.parentNode.parentNode.insertBefore(elm.parentNode, elm.parentNode.parentNode.firstChild); 
	doc('countrylist').className = '';
	mem_option.className = '';
	elm.className = 'active';
	mem_option = elm;
	sortCountries();
}
}

setDropDown = function() { 

	if (doc('countryselect')) {

		var elm = doc('countryselect');
		var new_id = 'countrylist';

		var newInput = document.createElement('input');
		newInput.setAttribute('type', 'hidden');
		newInput.setAttribute('name', elm.getAttribute('name'));
		newInput.id = 'inputcountry';
		elm.parentNode.appendChild(newInput);

		var newUL = document.createElement('ul');
		newUL.id = new_id;
		elm.parentNode.insertBefore(newUL, elm);
		
		var s = 0;
		var roots = elm.childNodes;
		for (var i=0; i<roots.length; i++) {
			if (roots[i].nodeName == 'OPTION') {
				s++;
				var newLI = document.createElement('li');
				var newA = document.createElement('a');
	
				newA.id = roots[i].getAttribute('value'); 
				newA.setAttribute('href', 'javascript:changeOption(doc(\''+newA.id+'\'));');

				newA.onfocus = function () { this.parentNode.parentNode.className = 'open'; }
				newA.onblur = function () { this.parentNode.parentNode.className = ''; }

				var txt = document.createTextNode(roots[i].firstChild.nodeValue);
				newA.appendChild(txt); newLI.appendChild(newA);
				
				if (roots[i].getAttribute('selected')) doc(new_id).insertBefore(newLI, doc(new_id).firstChild);
				else doc(new_id).appendChild(newLI);
			}
		}

		elm.removeAttribute('name');
		elm.style.visibility ='hidden';

		mem_option = newUL.firstChild.firstChild;
		mem_option.className = 'active';
		
		doc('inputcountry').value = mem_option.id; 
		
		newUL.onmouseover = function() {
			this.className = 'open';
		}
		newUL.onmouseout = function() {
			this.className = '';
		}
	}
}


setSearchBlendout = function() {
	if (doc('tx-contactslist-pi1_form')) {
		doc('tx-contactslist-pi1_form').onsubmit = function () { 
			doc('loader').style.visibility = 'visible'; 
			doc('loader').style.background = 'url(fileadmin/patte/templates/img/loading.gif) no-repeat center';
		}
	}
	if (doc('tx-contactslist-pi1') && !doc('blendoutswitcher')) {
		var newDiv = document.createElement('div');
		newDiv.id = 'blendoutswitcher';
		newDiv.className = 'open';
		newDiv.setAttribute('title', 'Hide results');
		newDiv.onclick = function() { 
			doc('tx-contactslist-pi1').style.display = (this.className == 'open') ? 'none' : 'block';
			this.className = (this.className == 'open') ? 'close' : 'open';
			this.setAttribute('title', (this.className == 'open') ? 'Show results' : 'Hide results');
		}
		doc('tx-contactslist-pi1_form').appendChild(newDiv);		
	}
}

setNews = function() {
	if (doc('newscontent')) {
		doc('newscontent').style.display = 'none';
		var roots = doc('newscontent').childNodes;	
		var s = 0;
	
		var newUL = document.createElement('ul');
		newUL.id = 'newslinks';

		for (var i=0; i<roots.length; i++) {
			if (roots[i].className == 'newssnippet') {
				var thisChilds = roots[i].childNodes;
				for (var a=0; a < thisChilds.length; a++) {
					if (thisChilds[a].className == 'newstitle') {
						s++;
						var newLI = document.createElement('li');
						var newA = document.createElement('a');
						newA.id = 'a_'+s; newA.setAttribute('href', 'javascript:void(0)');
						newA.onclick = function() { setNewsVisible(this); }						
						var txt = document.createTextNode(thisChilds[a].firstChild.nodeValue);
						newA.appendChild(txt); newLI.appendChild(newA);
						newUL.appendChild(newLI);
					}
				}
				roots[i].id = 'e_'+s;
				roots[i].style.display = 'none';
				mem_id = 1;
			}
		}	
		doc('newscontent').style.top = '150px';
		doc('newscontent').style.display = 'block';
		if (s>0) {
			doc('newscontent').parentNode.insertBefore(newUL, doc('newscontent'));
			setNewsVisible(doc('a_1'));
		}
		if (s==1) doc('newslinks').style.display = 'none';
		
	}
	
}

setNewsVisible = function(which) {
	var id = which.id.split('_')[1]; 
	doc('e_'+mem_id).style.display = 'none';
	doc('e_'+id).style.display = 'block';
	doc('a_'+mem_id).className = '';
	which.className = 'active';
	mem_id = id;
}

setProducts = function() {
	if (doc('shopcontent')) { 
		doc('shopcontent').style.display = 'none';
		var roots = doc('shopcontent').childNodes;

		var newUL = document.createElement('ul');
		newUL.id = 'shoplinks';

		var s = 0;
		for (var i=0; i<roots.length; i++) {
			if (roots[i].className == 'shopproduct' ) { 
				var thisChilds = roots[i].childNodes;
				for (var a=0; a < thisChilds.length; a++) {
					if (thisChilds[a].className == 'sptitle' && thisChilds[a].firstChild) {
						s++;
						var newLI = document.createElement('li');
						var newA = document.createElement('a');
						newA.id = 'a_'+s; newA.setAttribute('href', 'javascript:void(0)');
						newA.onclick = function() { setShopproductsVisible(this); }
						newA.onfocus = function() { doc('shoplinks').className = 'open'; }
						newA.onblur = function() { doc('shoplinks').className = ''; }
						var new_value = (thisChilds[a].firstChild && thisChilds[a].firstChild.nodeName == 'A') ? thisChilds[a].firstChild.firstChild.nodeValue : thisChilds[a].firstChild.nodeValue;
						var newTxt = document.createTextNode(new_value+' ');
						newA.appendChild(newTxt); newLI.appendChild(newA);
						newUL.appendChild(newLI);
					}
					if (thisChilds[a].className == 'text') {
						var shoptextcont = thisChilds[a].firstChild && thisChilds[a].firstChild.className == 'shopimage' ? thisChilds[a].childNodes[1] : thisChilds[a].firstChild;
						if (typeof shoptextcont == 'object' && shoptextcont.nodeName == 'P') {
							var new_value = shoptextcont.firstChild.nodeValue;
							var newEm = document.createElement('em');
							var newTxt = document.createTextNode(new_value.split(',')[0]);
							newEm.appendChild(newTxt);
							if (new_value.split(',').length > 1) newA.appendChild(newEm);
						}
					}
				}
				roots[i].id = 'e_'+s;
				roots[i].style.display = 'none';
				mem_id = 1;
			}
		}	
		doc('shopcontent').style.display = 'block'; 

		if (s>0) {
			doc('shopcontent').insertBefore(newUL, doc('shopcontent').firstChild);

			var newDiv = document.createElement('span');
			newDiv.style.marginLeft = '228px'; newDiv.style.lineHeight = '2.35em'; newDiv.style.paddingLeft = '15px'; newDiv.style.fontWeight = 'bold'; newDiv.style.cursor = 'default';
			
			var newTxt = document.createTextNode('Produktauswahl');
			newDiv.appendChild(newTxt);
			doc('shopcontent').insertBefore(newDiv, doc('e_1'));
			
			setShopproductsVisible(doc('a_1'));
		}		
		if (s>1) {
			doc('shoplinks').style.display = 'block';
			doc('shoplinks').className = 'open';
			var new_height = doc('shoplinks').parentNode.offsetHeight < doc('shoplinks').offsetHeight ? doc('shoplinks').offsetHeight : doc('shoplinks').parentNode.offsetHeight;
			doc('shoplinks').parentNode.style.height = new_height+'px';
			doc('shoplinks').className = 'close';
			
			doc('shoplinks').onmouseover = function() {
				doc('shoplinks').className = 'open';
			}
			doc('shoplinks').onmouseout = function() {
				doc('shoplinks').className = '';
			}
		}
	}
	
}

setShopproductsVisible = function(which) {
if (which.className != 'active') {
	var id = which.id.split('_')[1]; 
	doc('e_'+mem_id).style.display = 'none';
	doc('e_'+id).style.display = 'block';
	if (which.parentNode != document.getElementById('shoplinks').firstChild) document.getElementById('shoplinks').insertBefore(which.parentNode, document.getElementById('shoplinks').firstChild);
	doc('a_'+mem_id).className = ''; 
	doc('shoplinks').className = '';
	which.className = 'active';
	mem_id = id;
}
}

//### HELPER ###//

function addEvent(obj, evType, fn) {
	
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	}
	else if (obj.attachEvent) {
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}
	else {
		return false;
	}
}

function preload(img){
	if(document.images) { 
		if(!document.pr) document.pr=new Array();
		j= document.pr.length;
		document.pr[j]=new Image;
		document.pr[j].src=img;
	}
}

function doc(element) {
	return document.getElementById(element);
}


// set Variables
var images = new Object();
images['small'] = new Array();
images['big'] = new Array();


// start everything off on window load
addEvent(window, 'load', loadImages);
addEvent(window, 'load', setLinks);
addEvent(window, 'load', setSearchBlendout);
//addEvent(window, 'load', setDropDown);
//addEvent(window, 'load', setNews);
//addEvent(window, 'load', setProducts);

