var console;
if (console == undefined) {
	console = new function() {
		this.error	= function(src) { alert("Error\n\n"+src); };
		this.log	= function(src)	{ alert("Log\n\n"+src); };
		this.info	= function(src)	{ alert("Info\n\n"+src); };
		this.warn	= function(src)	{ alert("Warn\n\n"+src); };
		this.debug	= function(src)	{ alert("Debug\n\n"+src); };
	}
}

/**
* Suchbox rein- und rausfahren
*/
function showSearch() {
	new Effect.Parallel (
			 [
			 new Effect.Appear("suche", {sync:true, duration: 2.1}),
			 new Effect.SlideDown("suche", {sync:true,duration: 2.1})
			 ],
			{
			}

	);
	return false;
}

function hideSearch() {
	new Effect.Parallel (
			 [
			 new Effect.SlideUp("suche", {sync:true,duration: 2.1}),
			 new Effect.Fade("suche", {sync:true, duration: 2.1})
			 ],
			{
			}

	);
	return false;
}

function showBox(id) {
	new Effect.Parallel (
		[
			new Effect.Appear(id, {sync:true, duration: 2.1}),
			new Effect.SlideDown(id, {sync:true,duration: 2.1})
		],
		{}
	);
	return false;
}

function hideBox(id) {
	new Effect.Parallel (
		[
			new Effect.SlideUp(id, {sync:true,duration: 2.1}),
			new Effect.Fade(id, {sync:true, duration: 2.1})
		],
		{}
	);
	return false;
}

function switchScale(scale) {
	var url = '/_css/font_scal_'+scale+'.css'
	$('fontcss').href = url;
	for (i = 1; i <= 3; i++) {
		switchImg('font_scale_'+i, (i == scale ? "on" : "off"));
	}
	ajax_setfont(scale);
}

function switchImg(name, state) {
	document.getElementById(name).removeAttribute("src");
	document.getElementById(name).setAttribute("src","/img/" + name + "_" + state + ".gif");

}

function value_clean (name, value) {
	var e = $(name);
	if (value && value == e.value) {
		e.setAttribute("value","");
	}
}

function changeProductNav(idx) {
	var background = $('product_background');
	var aPos = new Array(
		'0x0'  , '149x0',   '298x0',
		'0x128', '149x128', '298x128'
	);
	for (var i = 1; i < aPos.length; i++) {
		var element = $('sortiment_navi_'+i);
		if (element) {
			if (i == idx) {
				element.style.display = 'block';
				var pos = aPos[i];
				var aDim = pos.split('x');
				background.style.display = 'block';
				background.style.left = String(aDim[0])+"px";
				background.style.top = String(aDim[1])+"px";
			} else {
				element.style.display = 'none';
			}
		}
	}
}

function switchBackground(elementId, imageUrl) {
	var e = document.getElementById(elementId);
	if (e) {
		e.style.backgroundImage = 'url("'+imageUrl+'")';
	}
}

function changeWorldwideNavi (imgid, name) {
	var id = parseInt(imgid);
	switchBackground(name, '/img/karte_' + id + '.gif');
	var i = 1;
	var navInactive;
	while (navInactive = document.getElementById(name+'_point_'+i)) {
		var liInactive;
		if (liInactive = document.getElementById(name+'_li_'+i)) {
			liInactive.removeAttribute("class");
		}
		navInactive.removeAttribute("class");
		navInactive.style['color'] = 'E2001A';
		i++;
	}
	var navLi = document.getElementById(name+'_li_'+id);
	var navPt = document.getElementById(name+'_point_'+id);
	if (navLi && navPt) {
		if (imgid > 0) {
			navLi.setAttribute("class", "current");
			navPt.setAttribute("class", "tesa_blue");
			navPt.style['color'] = '6C77AB';
		}
	}
}

function redirectSelect(select) {
	document.location.href = select[select.selectedIndex].value;
}

function change_tab (name) {
	var klasse = "tab_container_" + name + " clearfix"
	document.getElementById('tab_container_list').removeAttribute("class");
	document.getElementById('tab_container_list').setAttribute("class", klasse );
	//alert (klasse);

	for (i=1; i<=4; i++) {
		if (i == name) {
			document.getElementById('content_inner_' + i).style.display ='block';

			document.getElementById('tab_nav_' + i).style.backgroundPosition ='0 -150px';
			document.getElementById('tab_nav_' + i).style.borderBottom ='0';

			document.getElementById('tab_link_' + i).style.borderBottom ='0';
			document.getElementById('tab_link_' + i).style.backgroundPosition ='100% -150px';
			document.getElementById('tab_link_' + i).style.color ='#6D75AA';
			document.getElementById('tab_link_' + i).style.paddingBottom ='3px';

		} else {
			document.getElementById('content_inner_' + i).style.display ='none';

			document.getElementById('tab_nav_' + i).style.backgroundPosition ='left top';
			document.getElementById('tab_nav_' + i).style.borderWith ='1px solid #6D75AA';

			document.getElementById('tab_link_' + i).style.color ='#E2001A';
			document.getElementById('tab_link_' + i).style.backgroundPosition ='right top';
			document.getElementById('tab_link_' + i).style.borderWith ='1px solid #6D75AA';
			document.getElementById('tab_link_' + i).style.paddingBottom ='2px';
		}
	}
}


function writeDropdown(id, hOption) {
	var dropdown = $(id);
	var oldValue = $F(id);
	var aOldNode = dropdown.getElementsByTagName('option');
	var nodes = $A(aOldNode);
	var i = 0;
	nodes.each(function(node) {
		if (i) dropdown.removeChild(node);
		i++;
		}
	);
	hOption.each(function(value) {
		var tag = document.createElement('option');
		tag.value = value[0];
		tag.selected = (tag.value == oldValue);
		var inner = document.createTextNode(value[1]);
		tag.appendChild(inner);
		dropdown.appendChild(tag);
		}
	);
}


function setDropdown(id, value) {
	var dropdown = $(id);
	var aOption = dropdown.getElementsByTagName('option');
	var aNode = $A(aOption);
	aNode.each(function(node) {
		node.selected = (node.value == value);
		}
	);
}


function openLayerVideo(status) {
	if (status == 'open') {
		document.getElementById('video_layer').style.display ='block';
	} else {
		document.getElementById('video_layer').style.display ='none';
	}
}

function openMediaPopup(url, width, height, top, left) {
	if (!top) top  = (screen.height  / 2) - 250;
	if (!left) left = (screen.width / 2) - 230;
	if (!width) width = 500; //459;
	if (!height) height = 540; //494;

	var F2=window.open(url, 'mediapopup', 'resizable=no,menubar=no,scrollbars=no,status=no,directories=no,width=' + width + ',height=' + height + ',top=' + top + ',left='+ left);
	F2.focus();
}

function abrePopup(url, width, height, top, left) {
	if (!top) top  = 100;
	if (!left) left = 100;
	if (!width) width = 539;
	if (!height) height = 334;

	window.open(url, 'Popup','resizable=no,menubar=no,scrollbars=yes,status=no,directories=no,width=' + width + ',height=' + height + ',top=' + top + ',left='+ left);

}

function openMicrositePopup(url, width, height, top, left) {
	if (!top) top  = 100;
	if (!left) left = 0;
	if (!width) width = 738;
	if (!height) height = 600;

	var F2=window.open(url, 'microsite','resizable=no,menubar=no,scrollbars=yes,status=no,directories=no,width=' + width + ',height=' + height + ',top=' + top + ',left='+ left);
	F2.focus();
}

sitemapInProgress = false;
function toggleSitemapbox(elementId, myDuration) {
	if ($(elementId) && !sitemapInProgress) {
		sitemapInProgress = true;
		if (myDuration == undefined)
			myDuration = 0.5;
		if ($(elementId).style.display == 'none')  {
			Effect.BlindDown(elementId, {duration: myDuration, afterFinish:function(){sitemapInProgress = false;}});
			document.getElementById(elementId+'_img').src="/img/icon_minus.gif";
			document.getElementById(elementId+'_link').removeAttribute("class");
			document.getElementById(elementId+'_link').setAttribute("class", "tesa_blue" );
		} else {
			Effect.BlindUp(elementId, {duration: myDuration, afterFinish:function(){sitemapInProgress = false;}});
			document.getElementById(elementId + '_img').src="/img/icon_plus.gif";
			document.getElementById(elementId+'_link').removeAttribute("class");
			document.getElementById(elementId+'_link').setAttribute("class", "" );
		}
	}
}

function preload() {
	var preSource = document.getElementsByClassName("hiddenpreload");
	var preImages = new Array;
	for (i = 0; i < preSource.length; i++) {
		preImages[i] = new Image();
		preImages[i].src = preSource[i].firstChild.data;
	}
}

function setCookie(name, value, expires) {
	var cook = name+"="+unescape(value);
	cook += (expires) ? "; expires="+expires : "";
	document.cookie = cook;
}

function getCookie(name) {
	var i=0;
	var suche = name+"=";
	while (i<document.cookie.length) {
		if (document.cookie.substring(i, i+suche.length)==suche) {
			var end = document.cookie.indexOf(";", i+suche.length);
			end = (end > -1) ? end : document.cookie.length;
			var cook = document.cookie.substring(i+suche.length, end);
			return unescape(cook);
		}
	i++;
	}
	return null;
}

function deleteCookie(name) {
	setCookie(name, '', 'Thu, 01-Jan-70 00:00:01 GMT');
}

function switchCookie(name, value, expires) {
	if (getCookie(name)) {
		deleteCookie(name);
	} else {
		setCookie(name, value, expires);
	}
}


function show(id) {
	var e = $(id);
	if (e) {
		e.style.display = 'block';
		e.style.visibility = 'visible';
		return true;
	}
	return false;
}

function hide(id) {
	var e = $(id);
	if (e) {
		e.style.display = 'none';
		e.style.visibility = 'hidden';
		return true;
	}
	return false;
}


function activate(id) {
	var e = $(id);
	if (e) {
		e.disabled = false;
		return true;
	}
	return false;
}

function deactivate(id) {
	var e = $(id);
	if (e) {
		e.disabled = true;
		return true;
	}
	return false;
}

function zoomPackshot() {
	var aPackshot = $$('.packshot');
	aPackshot = $A(aPackshot);
	for (var i = 0; i < aPackshot.length; i++) {
		if (aPackshot[i].style.display != 'none') document.location.href = aPackshot[i].parentNode.href;
	}
}

function togglePackshotShow() {
	if (!window.packshotShow) {
		window.packshotShow = new PeriodicalExecuter(nextPackshot, 10);
	} else {
		window.packshotShow.currentlyExecuting = !window.packshotShow.currentlyExecuting;
		if (window.packshotShow.currentlyExecuting) {
			$('packshotShowStart').show();
			$('packshotShowPause').hide();
		} else {
			$('packshotShowStart').hide();
			$('packshotShowPause').show();
		}
	}
}

function nextPackshot(step) {
	step = step || 1;
	var aPackshot = $$('.packshot');
	aPackshot = $A(aPackshot);
	var fadeIdx   = 0;
	var appearIdx = 0;
	if (aPackshot.length < 2) return;
	for (var i = 0; i < aPackshot.length; i++) {
		if (aPackshot[i].style.display != 'none') fadeIdx = i;
	}

	if (step == 1) {
		appearIdx = (fadeIdx + 1 >= aPackshot.length ? 0 : fadeIdx + 1);
	} else {
		appearIdx = (fadeIdx - 1 < 0 ? aPackshot.length - 1 : fadeIdx - 1);
	}
	if (fadeIdx != appearIdx) {
		aPackshot[fadeIdx].hide();
		aPackshot[appearIdx].show();
/*
		new Effect.Parallel(
			[new Effect.Fade(aPackshot[fadeIdx], {from:1.0, to:0.0, duration:0.5})],
			[new Effect.Appear(aPackshot[appearIdx], {from:0.0, to:1.0, duration:0.5})]
		);
*/
	}
}



/**
* Funktionen der Ajax Elemente
*/
function unserialize(data_serialized) {
	var data_func = eval(data_serialized);
	return data_func();
}

/**
* Callback-Funktionen
*/
var rjsCallback = {
	savepolling: function(result) {
		var a = unserialize(result);
		if (a['form']) {
			var f = $('form_'+a['form']);
			f.innerHTML = a['message'];
		}
	},

	savefeedback: function(result) {
		var a = unserialize(result);
		if (a['state'] && a['form']) {
			setCookie('feedback:'+a['id'], 1);
			var f = $(a['form']);
			f.innerHTML = a['message'];
		}
	},

	setfont: function(result) {
	}
}
var rjs = new ajax(rjsCallback);

function ajax_polling(form, element, id) {
	var e = document.forms[form].elements[element];
	if (!e) return false;

	var value = 0;
	for (i = 0; i < e.length; i++) {
		if (e[i].checked == true) {
			value = e[i].value;
			break;
		}
	}
	if (!value) return false;
	deactivate('submit_'+form);

	rjs.savepolling(id, value, form);
}

function ajax_feedback(form, id, value) {
	rjs.savefeedback(id, value, form);
}

function ajax_setfont(scale) {
	rjs.setfont(scale);
}

function updateDropdownElement(target, url, parameters, element, selectProdukt) {
	if (element) parameters += "ajax=1&produkt="+element.value+"&update="+target+"&selectProdukt="+selectProdukt;

	new Ajax.Updater (target, url, {
		method: "get",
		parameters: parameters,
		evalScripts: true
	});
}
