/* legacy */
function navtolocation(url) {
	window.location.href= url;	
}

/* Unordered lists to select boxes */
function ulToSelect(divId,popUp) {
	var ul = $(divId);
	var listItems = ul.childElements();
	var options = '';
	var selected = false;
	listItems.each(function(el,ind) {
		if(el.hasClassName('ulToSelectSelected')) selected = true; 
		link = el.firstDescendant();
		if (link) {
			href = link.readAttribute('href');
			label = link.innerHTML;
		} else {
			href = '';
			label = el.innerHTML;
		}
		if (selected == true) {
			option = '<option selected="selected" value="' + href +'">' + label + '</option>';						
			selected = false;
		} else {
			option = '<option value="' + href +'">' + label + '</option>';			
		}
		options = options + option;
	});
	var out = new Element('select',{ 'id': divId, 'class': 'selectNav'}).update(options);
	ul.replace(out);
	
	Event.observe($(divId),'change',function(e) {
		ulUpdate(e,popUp);
	});
}

function ulUpdate(event,pop) {
	var element = event.element();
	var url = element.getValue();
	if(url) {
		if(pop) {
			window.open('/'+url,'Distributor','location=yes,scrollbars=yes,menubar=yes,height=500,width=400');
		} else {
			window.location.href= url;			
		}
	}
}


function getDifHeight (DIV) {
	var subheight = 228;
	
	/* subheight = document.getElementById("balken_head").offsetHeight;
	subheight = subheight + document.getElementById("nav_box").offsetHeight;
	subheight = subheight + document.getElementById("footer").offsetHeight; */
	
	subheight = subheight + document.getElementById("headerimg").offsetHeight;
	
	middleheight = DIV.offsetHeight;
	windowheight = window.innerHeight;
	
	if( navigator.appName == "Microsoft Internet Explorer") {
		
		if ( parent.content ) {
			windowheight = parent.content.document.body.offsetHeight;
		} else {
			windowheight = document.body.offsetHeight;
		}
		
	}
	

	dif_height = windowheight - (middleheight + subheight );
	return dif_height;
}

function resizemiddle(DIV) {
	DIV.style.height = '';
	if ( getDifHeight(DIV) > 0 ) {
		DIV.style.height = DIV.offsetHeight + getDifHeight(DIV) +"px";
	}

	if (document.getElementById("shop_img")){
		document.getElementById("shop_img").style.left = "20px";
		document.getElementById("shop_img").style.bottom ="80px";
	}
}

function pdfLinkTargets() {
	var pdfLinks = $$('a[href$=".pdf"]');
	pdfLinks.each(function(el,ind) {
		if($('productsBranch') || $('supportBranch')) {
			el.addClassName('pdfLink');
		}
		el.writeAttribute({target: '_blank'});
	});
}


/* on load events go here */
Event.observe(window,'load',function() {
	if($('regionSelect')) ulToSelect('regionSelect');
	pdfLinkTargets();

});



/* MAIN NAV js for IE -- taken from German template */
if(window.navigator.systemLanguage && !window.navigator.language) {
  
  function hoverIE() {

	if($('middle')) {
		resizemiddle(document.getElementById('middle')); 		
	} 
	if($('main_nav')) {
	    var LI = $('main_nav').firstChild;
	    do {
	      if (sucheUL(LI.firstChild)) {
	        LI.onmouseover=einblenden; LI.onmouseout=ausblenden;
	      }
	      LI = LI.nextSibling;
	    }
	    while(LI);	
	}	  
  }

  function sucheUL(UL) {
    do {
      if(UL) UL = UL.nextSibling;
      if(UL && UL.nodeName == "UL") return UL;
    }
    while(UL);
    return false;
  }

  function einblenden() {
    var UL = sucheUL(this.firstChild);
		UL.style.display = "block"; 
  }

  function ausblenden() {
    sucheUL(this.firstChild).style.display = "none";
  }

  Event.observe(window,'load',function(e) {
		hoverIE();
	});

}