var contentWidth = 894;
var rightboxBasicMargin = 171;
var rightboxMinWidth = 745;
var minWinWidth = 970;
var staticLinksCounr = 0;
var cat1 = "";
var cat2 = "";
var topFilename = "";

var myImages = new Array();


function calculateLeftmargin() {
    if(window.innerWidth) {
        width = window.innerWidth;        
    }
    else if(document.body) {
        width = document.body.offsetWidth;
    }
    
    if(width < minWinWidth) {
        width = minWinWidth;
    }
    leftmargin = (width-contentWidth)/2;       
    //leftmargin -= 10; //scrollbar   
    return(leftmargin);
}

function setLeftmargin() {
    leftMargin = calculateLeftmargin();
    rightboxMargin = rightboxBasicMargin + leftMargin;
      
    theNavibox = document.getElementById("navibox");
    theRightbox = document.getElementById("rightbox");
    
    theNavibox.style.left = leftMargin + "px";   
	theRightbox.style.marginLeft = rightboxMargin + "px";
	
	if(theRightbox.offsetWidth <= rightboxMinWidth) {
		theRightbox.style.width = rightboxMinWidth;
	}
	
	theNavibox.style.display = "";
	theRightbox.style.visibility = "visible";    
}

function navOver(theElement) {
	theSource = theElement.src;
	newSource = theSource.replace(/_pas./,"_act.");
	theElement.src = newSource;
}

function navOut(theElement) {
	theSource = theElement.src;
	sourceArray = theSource.split("/");
    arrayLength = sourceArray.length - 1;
    var thisFilename = sourceArray[arrayLength];
    filenameArray = thisFilename.split("_");
    thisFilename = filenameArray[0];
	
	if((thisFilename != extractFilename()) && (thisFilename != topFilename)) {
		newSource = theSource.replace(/_act./,"_pas.");
		theElement.src = newSource;
	}
}

function nav1active(topFilename) {
	if(topFilename) {
		theFilename = topFilename;				
	}
	else {
		theFilename = extractFilename();
	}
	
	if(theFilename == "index") {
		theFilename = "willkommen";	
	}
	
	if(document.getElementById(theFilename)) {
		document.getElementById(theFilename).src = "img/navi/" + theFilename + "_act.gif";
	}
	
	if(document.getElementById("nav_"+theFilename)) {					//zugehoerige subnavi ausklappen
		document.getElementById("nav_"+theFilename).style.display = "";
	}
}

function prenav2() {
	for(i=1; i<document.links.length; i++) {
		if(document.links[i].className) {	
			linkClass = document.links[i].className;
			if((linkClass == "sublink") && (document.links[i].href == window.location.href)) {
				linkText = document.links[i].firstChild.data;
				cat1 = linkText;
			}
		}
	}	
}

function nav2active(c,p) {
    for(i=1; i<document.links.length; i++) {
    	if(document.links[i].className) {
    		linkClass = document.links[i].className;
    		if(linkClass == "sublink") {
    			linkText = document.links[i].firstChild.data;
    			   			    			   			
    			if(p.indexOf("&nbsp;") > -1) {    				
    				linkText = linkText.replace(/\s/,'&nbsp;');     				
    			}   			
    			    			    			
    			checkId = "nav_" + extractFilename();
				nextId 	= document.links[i].parentNode.parentNode.id;
				superId	= document.links[i].parentNode.parentNode.parentNode.id;
    			
    			if((linkText == c) || (linkText == p)) {
    				document.links[i].style.color = "#003e8e";    				
    				
    				//alert(superId+"--"+nextId+"--"+checkId);    				
    				if((nextId == checkId) || (superId == checkId)) {
    					document.links[i].parentNode.parentNode.style.display = "";    					
    				}    				
    			}
    			
				if((linkText == c) && (p == "")) {
					if(document.getElementById("subcontainer_"+c)) {
						document.getElementById("subcontainer_"+c).style.display = "";
					}
				}    			 
    		}
    	}
    }
}


function resetForm() {
	killCookie();	
	document.forms[0].reset();	
	document.getElementById("theTextarea").value = "Anfrage Produkt(e), Ihre Nachricht";
}

function submitForm() {
	killCookie();
	document.forms[0].submit();
}


///////////////////////////////////////////////////////

function noteProduct(theProduct) {
	if(document.cookie && (document.cookie.indexOf("prodNotes=")>-1)) {		
		products = readCookieContent();
		products += ", " + theProduct;    	
	}
	else {
		products = theProduct;
	}
	
	var jetzt = new Date();
	var Auszeit = new Date(jetzt.getTime() + 86400000);
	document.cookie = "prodNotes=" + products + "_END; expires=" + Auszeit.toGMTString() + ";";
	alert("Das Produkt wurde zur Anfrage vorgemerkt. Rufen Sie die Seite \"Kontakt\" auf, um dort die Anfrage abzuschicken.");
}

function setTextareaContent() {
	if(document.cookie && (document.cookie.indexOf("prodNotes=")>-1)) {
		products = readCookieContent();
		if(products) {
			document.getElementById("theTextarea").value = "Ich interessiere mich für folgende Produkte:\n"+products+"\n\nZusätzliche Nachricht:";
		}
	}
}

function readCookieContent() {
	var Wertstart = document.cookie.indexOf("prodNotes=") + 10;
	var Wertende = document.cookie.indexOf('_END');	
	if (Wertende == -1) {
		Wertende = document.cookie.length;
	}
	content = document.cookie.substring(Wertstart, Wertende);
		
	return(content);
}

function killCookie() {
	document.cookie = "prodNotes=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
}


///////////////////////////////////////////////////////

function textareaPop(newSrc) {
	var links = document.getElementsByTagName('a');
	for(var i = 0; tag = links[i]; ++i) {
		if(tag.className == 'poplink') {
			tag.target = 'popwindow';
			tag.onclick = openGlossar;
	  	}
	  	else if(tag.className == 'querverweis') {
			tagData = tag.firstChild.data;
	  	
	  		if(tag.innerHTML) {
				tag.innerHTML = '<img src="' + newSrc + '">' + tagData;
	  		}
	  		else {	  		
				theLink = tag.firstChild;
				theArrow = document.createElement('img');
				arrowSrc = document.createAttribute('src');
				arrowSrc.nodeValue = newSrc;
				theArrow.setAttributeNode(arrowSrc);
				tag.insertBefore(theArrow,theLink);
	  		}
	  	}	  		  	     
	}		
}

function openGlossar() {
	var pwin = window.open(this.href, 'popwindow', 'width=640,height=480,scrollbars=yes');
	try { pwin.focus(); } catch(e) {}
	return false;
}

//preload!!
var myImages = new Array();
function preloadImages() {
    for (i=0;i<preloadImages.arguments.length;i++) {
        myImages[i] = new Image();
        myImages[i].src = preloadImages.arguments[i];
    }
}

function extractFilename() {
    var thisUrl = window.location.href;

    urlArray = thisUrl.split("/");
    arrayLength = urlArray.length - 1;
    var thisFilename = urlArray[arrayLength];
    filenameArray = thisFilename.split(".");
    thisFilename = filenameArray[0];
    
    if(!thisFilename) {
        thisFilename = "index";
    }
    
    if(thisFilename == "index") {
    	thisFilename = "willkommen";
    }
    
    return(thisFilename);
}

function preloadEverything() {
	preloadImages("img/navi/produkte_act.gif","img/navi/ueberuns_act.gif","img/navi/referenzen_act.gif","img/navi/service_act.gif","img/navi/kontakt_act.gif","img/navi/willkommen_pas.gif");
}


function popNewsletter(inout) {
    address = document.forms["optinout"].elements[0].value;
    if(address && (address!="Ihre E-Mailadresse")) {
        var url = "http://letters.popletter.com/schraml/optinout.php?l=54&inOut="+inout+"&address="+address;
        window.open(url,"optWin","width=300,height=180,top=50,left=100,scrollbars=no");
    }
    else {
        alert("Bitte geben Sie eine Adresse ein.");
    }
}


function prodDetails(id,cat2) {
	prodDetWin = window.open("produktdetails.php?id="+id+"&cat2="+cat2,"detWin","width=760,height=380,top=50,left=60,scrollbars=yes");
	prodDetWin.focus();
}

function newsDetails(id) {
	newsDetWin = window.open("newsdetails.php?id="+id,"detWin","width=610,height=380,top=50,left=60,scrollbars=yes");
	newsDetWin.focus();
}


function showBargains() {
	schnCount = 0;	
	var divs = document.getElementsByTagName('div');
	
	for(var i = 0; tag = divs[i]; ++i) {
		if(tag.className == 'teamelement schn') {
			schnCount++;
			if(schnCount > 1) {
				if(tag.style.display == "none") {
					tag.style.display = "block";
				}
				else {
					tag.style.display = "none";
				}
				
			}			
	  	}
	}
	
	toggleLink = document.getElementById("showall").firstChild.firstChild;
	theValue = toggleLink.nodeValue;
	if(theValue.indexOf("Mehr") > -1) {
		toggleLink.nodeValue = "Schnäppchen ausblenden";
		document.getElementById("pfeil").src = "/img/buttons/up.gif";
	}
	else {
		toggleLink.nodeValue = "Mehr Schnäppchen anzeigen";
		document.getElementById("pfeil").src = "/img/buttons/down.gif";		
	}	
}
