function imgChange(oImg, oldState, newState) {
	if(oImg && oImg.src) {
		oldState = oldState.split(",");
		newState = newState.split(",");
		var src = oImg.src;
		for(var i = 0; i < oldState.length; i++)
			src = src.split(oldState[i]).join(newState[i]);
		if(src != oImg.src) {
			oImg.src = src;
		}
	}
}

var IDMAP = [];

function forTags(idPattern, func) {
	if(!IDMAP.length) {
		var cc = document.all;
		if(!cc)
			cc = document.getElementsByTagName("*");
		for(var i = 0; i < cc.length; i++) {
			if(cc[i].id)
				IDMAP[IDMAP.length]=(cc[i].id);
		}
		//alert(IDMAP.join("\n"));
	}
	var matches = 0;
	for(var i = 0; i < IDMAP.length; i++) {
		if(matches = IDMAP[i].match(idPattern))
			func(document.getElementById(IDMAP[i]), matches);
	}
}

//------------------------------------------------------------------------

var flashVer = -1;
var isIE     = 0;

function sniff() {
	window.flashVer = 0;
	if(!navigator.userAgent)
		return;
//	if(navigator.userAgent.indexOf("Win") < 0)
//		return;
	if(navigator.plugins && navigator.plugins["Shockwave Flash"]) {
		var s = navigator.plugins["Shockwave Flash"].description;
		window.flashVer = parseInt(s.match(/\d+/));
	} else if(!window.opera && navigator.userAgent.indexOf("MSIE") >= 0 && navigator.appVersion.indexOf("Win") >= 0) {
		isIE = 1;
		if(window.ActiveXObject) {
			try {
				var s = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
				window.flashVer = parseInt(s.GetVariable('$version').match(/\d+/));
			} catch(e) {}
		}
	}
}

var _sndTimer = 0;
var _sndURL   = '';
var _sndTitle = '';

//------------------------------------------------------------------------

function hasFlash() {
	if(window.opera)
		return false;
	if(window.flashVer < 0)
		sniff();
	if(window.flashVer < 6)
		return false;
	return true;
}


function _playSound() {
	if(!hasFlash())
		return
	var mov = isIE ? window["FP_0"] : document["FP_0"];
	if(mov) {
		// let mozilla discover the player
		var backLink = 0;
		try {
			backLink = mov.GetVariable("BACK_LINK");
		} catch(e) {
			backLink = 0;
		}
		if(!backLink) {
			_sndTimer = setTimeout("_playSound()", 5);
			return;
		}
		mov.SetVariable("soundUrl", _sndURL);
		mov.SetVariable("soundTitle", _sndTitle);
		mov.GotoFrame(3);
	}
}

function playSound(url, title) {
	clearTimeout(_sndTimer);
	_sndURL = url;
	_sndTitle = title;
	setTimeout("_playSound()", 5);
}

function playListTrack(productID, path, href) {
	var oCon = document.getElementById("idPlayerContainer");
	var oPos = document.getElementById("idPlayerPos" + productID);

	try {
		if(oCon.parentNode != oPos) {
			oCon = oCon.parentNode.removeChild(oCon);
			oPos.appendChild(oCon);
		}
		oCon.style.visibility = "visible";
	} catch(e) {}
	playSound(path, href.innerHTML);
}

function stopTracks(trackID) {
	forTags(/^idPlayerBtn_(\d+)/, function(o, matches) {
		if(matches[1] == trackID)
			imgChange(o, "ii", "aa");
		else
			imgChange(o, "aa", "ii");
	});
}


function playDetailsTrack(trackID, path, href) {
	stopTracks(trackID);
	document.getElementById("idPlayerInfo").innerHTML = href.innerHTML;
	playSound(path, href.innerHTML);
}

//------------------------------------------------------------------------

var naviTimer = 0;
var naviTopic = 0;
var naviCurr  = 0;

NAVI_MAX = 5;

function getPos(o) {
	var xy = [0, 0];
	while(o && o.tagName != "BODY") {
		xy[0] += o.offsetLeft;
		xy[1] += o.offsetTop;
		o = o.offsetParent;
	}
	return xy;
}

function naviShow(n) {

	forTags(/^idNaviMenu_(.+)/, function(o, matches) {
		o.style.visibility = (matches[1] == n) ? "visible" : "hidden";
	});
	forTags(/^idNavi_(.+)/, function(o, matches) {
		if(matches[1] == n)
			imgChange(o, "xx", "zz");
		else
			imgChange(o, "zz", "xx");
	});
}


function _naviOut() {
	if(naviTimer)
		clearTimeout(naviTimer);
	naviTimer = 0;
	naviShow(naviTopic);
}

function naviOut() {
	naviTimer = setTimeout("_naviOut()", 500);
}

function naviOver(n) {
	if(naviTimer)
		clearTimeout(naviTimer);
	naviTimer = 0;
	if(n)
		naviShow(n);
}

//------------------------------------------------------------------------

var idxTimer = 0;

function idxMenu(mnuID, oLink) {
	if(idxTimer)
		clearTimeout(idxTimer);
	idxTimer = 0;
	if(!mnuID)
		return;

	var xy = getPos(oLink);
	forTags(/^idIdxMenu_(.+)/, function(o, matches) {
		if(matches[1] == mnuID) {
			o.style.left = xy[0];
			o.style.top  = xy[1] - o.offsetHeight - 2;
			setTimeout("document.getElementById('" + matches[0] + "').style.visibility='visible'", 1);
		} else {
			o.style.visibility = "hidden";
		}
	});
}

function _idxMenuHide() {
	forTags(/^idIdxMenu_(.+)/, function(o, matches) {
		o.style.visibility = "hidden";
	});
	return;

	for(var i = 1; i <= NAVI_MAX; i++) {
		var mnu = document.getElementById("idIdxMenu_" + i);
		mnu.style.visibility = "hidden";
	}
}

function idxMenuHide() {
	idxTimer = setTimeout("_idxMenuHide()", 500);
}

//------------------------------------------------------------------------

function selectorCheck(oCheckBox) {
	if(oCheckBox.name.length < 4)
		return;
	var pfx  = oCheckBox.name.substr(0, 3);
	var numb = oCheckBox.name.charAt(3);
	
	if(numb == 0 && oCheckBox.checked) {
		for(var i = 0; i < oCheckBox.form.length; i++) {
			var o = oCheckBox.form[i];
			if(o["type"] && o["type"] == "checkbox" && o["name"] && o["name"].indexOf(pfx) == 0)
				o.checked = false;
		}
		oCheckBox.checked = true;
		return;
	}
	if(numb != 0 && oCheckBox.checked) {
		var o = oCheckBox.form[pfx + "0"];
		if(o)
			o.checked = false;
	}	
}

function selectorToggle() {
	var siA = document.getElementById("idSelectorA");
	var siB = document.getElementById("idSelectorB");
	var pic = document.getElementById("idSelectorImage");
	if(siA.style.display != "none") {
		siA.style.display = "none";
		siB.style.display = "block";
		imgChange(pic, "ii", "aa");
	} else { 
		siA.style.display = "block";
		siB.style.display = "none";
		imgChange(pic, "aa", "ii");
	}
}

//------------------------------------------------------------------------

function basketEnableButton(bEnable) {
	var s1 = bEnable? "ii,zz" : "aa,zz";
	var s2 = bEnable? "aa,zz" : "ii,xx";
	imgChange(document.getElementById("idAktualize1"), s1, s2);
	imgChange(document.getElementById("idAktualize2"), s1, s2);
}

function basketChange(oInput) {
	var c = 0;
	for(var i = 0; i < oInput.form.elements.length; i++) {
		var o = oInput.form.elements[i];
		if(o.name && o.name.indexOf("qty_") == 0) {
			var initQty = o.getAttribute("init") || o.init;
			if(initQty != o.value) {
				o.className = "basketFormQtyHot";
				c++;
			} else {
				o.className = "basketFormQty";
			}
		}
	}
	basketEnableButton(c);
}

//------------------------------------------------------------------------

var _imgCache = [];
function initRollovers() {

	function b(cc) {
		for(var i = 0; cc && i < cc.length; i++) {
			var o = cc[i];
			if(o.src && o.src.indexOf("xx") > 0) {
				var n = _imgCache.length;
				_imgCache[n] = new Image();
				_imgCache[n].src = o.src.replace(/xx/, "zz");
				o.onmouseover = function() { imgChange(this, "xx", "zz") }
				o.onmouseout  = function() { imgChange(this, "zz", "xx") }
			}
		}
	}
	b(document.getElementsByTagName("IMG"));
	b(document.getElementsByTagName("INPUT"));
}

//------------------------------------------------------------------------

function orderToggleShipAddress(oCb) {
	var o = document.getElementById("idShipAddress");
	if(o)
		o.style.display = (oCb.checked) ? "" : "none";
}

//------------------------------------------------------------------------


function detailsShowPic(n) {
	var oPopup = document.getElementById("idDetailsPicPopup");
	document.getElementById("idDetailsPic0").src = detailsPicUrl[n][0];
	if(oPopup.style.display != "none")
		document.getElementById("idDetailsPic1").src = detailsPicUrl[n][1];
	detailsPicCurr = n;
}

function detailsZoomPic() {
	detailsShowPopup(true);
	detailsShowPic(detailsPicCurr);
}

function detailsShowPopup(b) {
	var oPopup = document.getElementById("idDetailsPicPopup");
	oPopup.style.display = b ? "block" : "none";
}

//------------------------------------------------------------------------

function windowOpen(url, name, width, height, center, scrollbars) {
	var x = 0, y = 0;
	if(!width) {
		width = screen.availWidth;
	}
	if(!height) {
		height = screen.availHeight;
	}
	if(center){
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
	}
	if(!name) {
		name = 'VOICESPOPUP';
	}
	if(!scrollbars) {
		scrollbars = 0;
	}
	var w = window.open(
		url,
		name,
		'width='+width+',height='+height+',left='+x+',top='+y+',scrollbars='+scrollbars+',resizable=1'
	);
	if(w) {
		w.focus();
		return true;
	}
	return false;
}

//------------------------------------------------------------------------
// http://www.quirksmode.org/viewport/compatibility.html

function scrollGet() {
	if(typeof(self.pageYOffset) != "undefined")
		return self.pageYOffset;
	var s1 = 0;
	var s2 = 0;
	if (document.documentElement && typeof(document.documentElement.scrollTop) != "undefined")
		s1 = document.documentElement.scrollTop;
	if(document.body)
		s2 = document.body.scrollTop || 0;
	return Math.max(s1, s2);
}

var _scroll = { y : 0, t : 0 };

function _scrollSet() {
	if(_scroll.y >= _scroll.t)
		return window.scrollTo(0, _scroll.t);
	var d = Math.round((_scroll.t - _scroll.y) * 0.3);
	if(d < 3)
		return window.scrollTo(0, _scroll.t);
	window.scrollTo(0, _scroll.y += d);
	setTimeout("_scrollSet()", 3);
}

function scrollSet() {
	if(!window.scrollTo)
		return;
	if(!document.location || !document.location.search || !document.location.search.match)
		return;
	var m = document.location.search.match(/scr=(\d+)/);
	if(!m)
		return;
	_scroll.t = parseInt(m[1]) || 0;
//	_scrollSet();
	window.scrollTo(0, _scroll.t);
}

function scrollAdd(elem) {
	var s = scrollGet();
	if(!s)
		return;
	if(elem.value.length)
		elem.value += "&scr=" + s;
	else
		elem.value = "scr=" + s;
}

//------------------------------------------------------------------------

function _eGetText(id) {
	var o = document.getElementById(id);
	var text = "";
	if(o) {
		if(o.innerHTML)
			text = o.innerHTML;
		else if(o.firstChild)
			text = o.firstChild.nodeValue;
	}
	return text;
}

function eConfirm(id) {
	var text = _eGetText(id);
	if(text.length)
		return confirm(text);
	else return true;
}

function eAlert(id) {
	var text = _eGetText(id);
	if(text.length)
		return alert(text);
	else return true;
}


function catCheckInOrder(frm) {
	var oAdd = frm.elements['add'];
	if(!oAdd || !window.ORDER_ITEMS)
		return true;
	if(oAdd.options)
		var v = oAdd.options[oAdd.selectedIndex];
	else
		var v = oAdd.value;
	if(window.ORDER_ITEMS.indexOf("|" + v + "|") < 0)
		return true;
	return eConfirm("msgIsInOrder");
}


//------------------------------------------------------------------------

window.onerror = function() { return true };

var _ONLOAD_FUNCS = [];

function ONLOAD_FUNCTION(func) {
	_ONLOAD_FUNCS[_ONLOAD_FUNCS.length] = func;
}


window.onload = function() {
	scrollSet();
	initRollovers();
	if(naviTopic)
		naviOver(naviTopic);
	for(var i = 0; i < _ONLOAD_FUNCS.length; i++)
		_ONLOAD_FUNCS[i]();
}


