//==================================================================================
// $
//==================================================================================

//function $(obj) { return document.getElementById(obj); }
function $N(obj) { return document.getElementsByName(obj); }

//==================================================================================
//	browserCheck
//==================================================================================

function browserCheck() {
	var agent = navigator.userAgent;
	if (agent.indexOf("MSIE") > -1) {
		var anfang = agent.indexOf("MSIE");
		var ende = agent.lastIndexOf(";");
		var version = parseInt(agent.substring(anfang+5,ende-2));
		return "IE" + version;
	}
	else { return "MO"; }
}


//==================================================================================
// naviFx
//==================================================================================

function  naviFx(obj) {
	var txt = obj.innerHTML;
	obj.onmouseout		= function() { obj.style.backgroundImage = ""; }
	obj.onmouseover	= function() {}
	function naviCharFx() {
		if (txtCounter < txt.length) { obj.innerHTML += txt.substr(txtCounter,1); }
		else {
			clearInterval(charInterval);
			obj.onmouseover = function() { naviFx(obj); }
		}
		txtCounter++;
	}
	if (obj.style.backgroundImage == "") {
		if(browser != "IE6") {
			clearInterval(this.charInterval);
			//obj.style.backgroundImage = "url(/pix/naviBtOver.gif)";
			obj.innerHTML = "";
			var txtCounter = 0;
			var charInterval = setInterval(naviCharFx,20);
		} else {
			//obj.style.backgroundImage = "url(/pix/naviBtOver.gif)";
			obj.onmouseover = function() { naviFx(obj); }
		}
	}
}


//==================================================================================
// camelCase
//==================================================================================

function camelCase(str) { return str.replace(/-\D/g, function(match) { return match.charAt(1).toUpperCase(); }) }

//==================================================================================
// getStyle
//==================================================================================

function getStyle(obj, stil)
{
	if(document.getElementById(obj)) obj = document.getElementById(obj);
	
	if(window.getComputedStyle)	return window.getComputedStyle(obj,"" ).getPropertyValue(stil);
	else if (document.defaultView) return document.defaultView.getComputedStyle(obj, null).getPropertyValue(stil);
	else if(obj.currentStyle) return obj.currentStyle[camelCase(stil)];
	else if(document.ids) return document.layers[obj.id][camelCase(stil)];
	else if(document.all) return document.all[obj.id].style[camelCase(stil)];
}

//==================================================================================
// setButtonFx
//==================================================================================

function setButtonFx(bt) {
	// Initialisierung
	bt.style.cursor	= "pointer";
	var format			= ".gif";
	var status			= new Array("normal","hover","press");
	var path = (!bt.src)?getStyle(bt,"background-image"):bt.src;
	// Status Durchlauf
	for (var i=0; i < status.length; i++) { if(path.indexOf(status[i]) > -1) { var statusId = i; break; } }
	
	// Setzt events
	if(!bt.eventIsset) {
		bt.omup							= (bt.onmouseup)?bt.onmouseup:function(){};
		bt.omout						= (bt.onmouseout)?bt.onmouseout:function(){};
		bt.omdown						= (bt.onmousedown)?bt.onmousedown:function(){};
		bt.eventIsset = true;
	}
	// Zuweisung
	if (!bt.src) {
		bt.style.backgroundImage		= path.replace(status[statusId],status[1]);
		bt.onmouseover					= function() { this.style.backgroundImage = path.replace(status[statusId],status[1]); }
		bt.onmouseout					= function() { this.style.backgroundImage = path.replace(status[statusId],status[0]); bt.omout(); }
		bt.onmousedown					= function() { this.style.backgroundImage = path.replace(status[statusId],status[2]); bt.omdown(); return false; }
		bt.onmouseup					= function() { this.style.backgroundImage = path.replace(status[statusId],status[1]); bt.omup(); }
	} else {
		bt.src							= path.replace(status[statusId],status[1]);
		bt.onmouseover					= function() { this.src = path.replace(status[statusId],status[1]); }
		bt.onmouseout					= function() { this.src = path.replace(status[statusId],status[0]); bt.omout(); }
		bt.onmousedown					= function() { this.src = path.replace(status[statusId],status[2]); bt.omdown(); return false; }
		bt.onmouseup					= function() { this.src = path.replace(status[statusId],status[1]); bt.omup(); }
	}
}


//==================================================================================
// moveRollTor
//==================================================================================

function moveRollTor(rt, startPos, endPos, speed)
{
	var auf = (startPos < endPos)?true:false;
	$(rt).style.display="block";
	var startMoveRolltor = setInterval(moveRolltor, 20);
	function moveRolltor() {
		var newPos = (auf)?startPos/speed:startPos*speed;
		$(rt).style.backgroundPosition = "0px " + Math.round(newPos) + "px";
		startPos = newPos;
		if (auf && Math.round(startPos) >= Math.round(endPos)) {
			$(rt).style.backgroundPosition = "0px " + endPos + "px";
			clearInterval(startMoveRolltor);
			//moveRollTor(rt,-1,-151,1.5);
			$(rt).childNodes[0].style.display="block";
		} else if(!auf && Math.round(startPos) <= Math.round(endPos)) {
			$(rt).style.backgroundPosition = "0px " + endPos + "px";
			clearInterval(startMoveRolltor);
			$(rt).style.display="none";
		}
	}
}

function closeRollTor(bt,id)
{
	$(bt).style.display="none";
	$("anzahl"+id).value=1;
	moveRollTor('rollTor' + id,-1,-151,1.5);
}

//==================================================================================
// setAnzahl
//==================================================================================

function setAnzahl(obj,plus)
{
	var val = Number(obj.value);
	if (plus) { obj.value = Number(obj.value) + 1; }
	else if(val-1 > 0) { obj.value = Number(obj.value) - 1; }
}

//==================================================================================
// imagePreloader
//==================================================================================

function imagePreloader() {
	if(document.images) {
		if(!document.aPics) document.aPics = new Array();
			var i,j = document.aPics.length, argumente = imagePreloader.arguments;
			for(var i = 0; i < argumente.length; i++) {
				if (argumente[i].indexOf("#")!=0) {
				document.aPics[j] = new Image;
				document.aPics[j++].src = argumente[i];
			}
		}
	}
}

//==================================================================================
// showBorder
//==================================================================================

function showBorder()
{
	var navi = $("navigation").offsetHeight+90+15;
	var content = $("contentContainer").offsetHeight-52;
	var h = (navi>content)?navi:content;
	$("borderHGB").style.height = h + "px";
	$("border").style.visibility="visible";
}

//==================================================================================
// showProductContainer
//==================================================================================

var activeProdId = "leer";

function showProductContainer(prodId)
{
	
	var bt = $("btDetails" + prodId);
	var btBottom = (browser=="IE6")?24:23;
	
	if(activeProdId != "leer") {
		enableButton($("btDetails" + activeProdId));
		$("productContainer" + activeProdId).style.display = "none";
	}
	
	if(prodId == activeProdId) {
		enableButton(bt);
		$("productContainer" + prodId).style.display = "none";
		activeProdId = "leer";
	} else {
		disableButton(bt);
		$("productContainer" + prodId).style.display = "block";
		activeProdId = prodId;
	}
	
	function enableButton(bt) {
		bt.parentNode.style.backgroundColor="#434343";
		bt.style.bottom = "-"+btBottom+"px";
		bt.src="/pix/buttons/bt_details_normal.gif";
		setButtonFx(bt);
	}
	
	function disableButton(bt) {
		bt.parentNode.style.backgroundColor="#494949";
		bt.onmouseover = function(){};
		bt.onmouseout = function(){};
		bt.onmousedown = function(){};
		bt.style.bottom = "-"+(btBottom+1)+"px";
		bt.src="/pix/buttons/bt_details_active.gif";
	}
	
	showBorder();
}

//==================================================================================
// updateBasket
//==================================================================================

function updateBasket(id,anzahl)
{
	window.location.href="index.php?updateArticle="+id+"&anzahl="+anzahl;
}



var scrollCont = null;
var cont = null;
function startScroll(bt,id, pos)
{
	bt.onmouseup = stopScroll;
	bt.onmouseout = stopScroll;
	cont = document.getElementById(id);
	scrollCont = setInterval("scroll" + pos + "()", 40);
}
function scrolldown() { cont.scrollTop += 5; }
function scrollup() { cont.scrollTop -= 5; }
function stopScroll() { clearInterval(scrollCont); }


//==================================================================================
// S T A R T
//==================================================================================

function setButtonFX()
{
	var buttons = $$(".btuttonFX");
	buttons.each(function(el){
		el.style.outLine = "none";
		el.onmouseover = function(){ this.setOpacity(0.9); }
		el.onmouseout = function(){ this.setOpacity(1); }
		el.onmousedown = function(){ this.setOpacity(0.6); }
		el.onmouseup = function(){ this.setOpacity(0.9); }
	});
}

window.addEvent("load",setButtonFX);

//==================================================================================

var browser = null;
window.addEvent("load",function(){
	browser = browserCheck();
	var btPath = "/pix/buttons/";
	imagePreloader(btPath + "bt_150_g_hover.gif",
						btPath + "bt_150_g_press.gif",
						btPath + "bt_68_g_hover.gif",
						btPath + "bt_68_g_press.gif",
						btPath + "arrow_up_hover.gif",
						btPath + "arrow_up_press.gif",
						btPath + "arrow_down_hover.gif",
						btPath + "arrow_down_press.gif",
						btPath + "bt_details_normal.gif",
						btPath + "bt_details_hover.gif",
						btPath + "bt_details_press.gif",
						btPath + "bt_details_active.gif");
	showBorder();
});

//==================================================================================

function zoomImage (a_src){
	var img = $('zoomImagePicture');
	img.src = a_src;
	overlay.show($('zoomImageContainer'));
}
