function afterLoad () {
	startList();
	NavFix();
	
	if(document.images){ (new Image()).src="/modules/counter.php"; }
	
	Initialize();
}

var g_PopupIFrame;

function IsIE() {
    return ( navigator.appName == "Microsoft Internet Explorer" );
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

var startList = function() {

	// code for IE
	if(!document.body.currentStyle) return;
	var subs = document.getElementsByName('submenu');
	for(var i=0; i<subs.length; i++) {
		var li = subs[i].parentNode;
		if(li && li.lastChild.style) {
			li.onclick = function() {
				this.lastChild.style.visibility = 'visible';
			}
//			li.onmouseout = function() {
//				this.lastChild.style.visibility = 'hidden';
//			}
		}
	}
}

//help js
function trim(x){return x.replace(/^\s+|\s+$/g,'');}

function hasClass(obj, clName) {
  var clN = ' ' + obj.className + ' ';
  return (clN.match(' ' + clName + ' ') != null);
}

function addClass(obj, clName) {
  if (!hasClass(obj, clName)) obj.className = trim(obj.className + ' ' + clName);
}

function removeClass(obj, clName) {
  if (!hasClass(obj, clName)) return;
  var clN = ' ' + obj.className + ' ';
  eval("obj.className = trim(clN.replace(/ " + clName + " /g,''));");
}

function cumulativeOffset(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
    } while (element);
    return [valueL, valueT];
}

var NavFix = function() {

    var nav = document.getElementById('navH');
    //navigation
    var lis = nav.getElementsByTagName('li');
    for (var i=0; i < lis.length; i++) {
        if(lis[i].parentNode.id == 'navH0') {
            if(lis[i].className != 'active') {

        		lis[i].onmouseover = function() {
	    			addClass(this, 'hover');
                    if (hasClass(this, 'dropdown')) {
                        addClass(this.getElementsByTagName('ul')[0], 'dropdownshow');
                        if (IsIE()) {
                            var iFrame = document.createElement("IFRAME");
                            iFrame.setAttribute("src", "");
                            iFrame.style.position="absolute";
                            iFrame.style.left =cumulativeOffset(this.getElementsByTagName('ul')[0])[0] + 'px';
                            iFrame.style.top =cumulativeOffset(this.getElementsByTagName('ul')[0])[1] + 'px';
                            iFrame.style.width =this.getElementsByTagName('ul')[0].offsetWidth + 'px';
                            iFrame.style.height =this.getElementsByTagName('ul')[0].offsetHeight + 'px';
                            document.body.appendChild(iFrame);
                            g_PopupIFrame=iFrame;
                        }
                    }
       			}

        		lis[i].onmouseout = function() {
    				removeClass(this, 'hover');
                    if (hasClass(this, 'dropdown')) {
                        removeClass(this.getElementsByTagName('ul')[0], 'dropdownshow');
                        if (IsIE()) {
                            document.body.removeChild(g_PopupIFrame);
                            g_PopupIFrame=null;
                        }
                    }
    			}
            }
        }
    }
}
/*
var TabFix = function() {

    var tab = document.getElementsByTagName('div');
    for (var i=0; i < tab.length; i++) {
        if ((tab[i].className == 'switchBox') || (tab[i].className == 'switchBox switchBoxBordered')) {
            var tabul = tab[i].id + '_tabs'; //get ul of this tab
            var tablis = document.getElementById(tabul).getElementsByTagName('li'); //get all lis of this tab
            for (var k = 0; k < tablis.length; k++) {
                tablis[k].getElementsByTagName('a')[0].onclick = function() {
                    //clear all selections of tabs
                    for (var l = 0; l < tablis.length; l++) {
                        switch (tablis[l].className) {
                            case 'sel'          :   tablis[l].className = '';
                                                    tablis[l].getElementsByTagName('a')[0].className = '';
                                                    break;
                            case 'first sel'    :   tablis[l].className = 'first';
                                                    tablis[l].getElementsByTagName('a')[0].className = '';
                                                    break;
                        }
                    }
                    //make clicked tab selected
                    switch (this.parentNode.className) {
                        case ''          :   this.parentNode.className = 'sel';
                                                this.className = 'sel';
                                                break;
                        case 'first'    :   this.parentNode.className = 'first sel';
                                                this.className = 'sel';
                                                break;
                    }
                    //go through all tabs and hide shown
                    var dupa = this.parentNode.parentNode.getElementsByTagName('li'); //all LIs of this tab
                    for (var m = 0; m < dupa.length; m++) {
                        var divid = dupa[m].parentNode.parentNode.id + '_' + dupa[m].firstChild.href.split('#')[1];
                        if (divid == dupa[m].parentNode.parentNode.id + '_' + this.href.split('#')[1]) {
                            document.getElementById(divid).style.display = 'block';
                            SetCookie(dupa[m].parentNode.parentNode.id, divid, 1);
                            //alert('Cookie:' + dupa[m].parentNode.parentNode.id + ' Value:' + divid);
                        } else {
                            document.getElementById(divid).style.display = 'none';
                        }
                    }
                }
            }
        }
    }
}
*/

//AJAX (xmlHttpObject erzeugen)
var xmlHttpObject = false;
if(typeof XMLHttpRequest != 'undefined'){
	xmlHttpObject = new XMLHttpRequest();
}
if (!xmlHttpObject){
	try{
		xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			xmlHttpObject = null;
		}
	}
}		


var startPosition, endPosition, ScrollField, Breite;

function Initialize() {
	//Galerie
  	if (document.getElementById('scroll_field')){
	  	var ThumbBreite = document.getElementsByName('thumb');
	  	Breite = 0;
	  	for (i=0;i<ThumbBreite.length; i++){
	  		Breite += ThumbBreite[i].offsetWidth + 2*ThumbBreite[i].style.border + 4;
	  		}
	  	document.getElementById("scroll_field").style.width = Breite+'px';

		ScrollField = document.getElementById("scroll_field");
		startPosition = ScrollField.offsetLeft;
	  	endPosition = startPosition - Breite + document.getElementById("scroll_box").offsetWidth ;
		}
	// Login Submit
	if ($(window.document.login)) {
		$(window.document.login).addEvent('keydown', function(e){
			if (e.keyCode == 13) {
				this.submit();
			}
		});
	}
}

//function for button hover effect
function buttonHover(object,inOut){
	if(inOut == '1'){
		object.style.backgroundPosition = 'center bottom';
		object.style.color = '#EEEEEE';
	}else {
		object.style.backgroundPosition = 'center top';
		object.style.color = '#FFFFFF';
	}
}


//functions for Unternehmenskultur
function show(id){
	 var i = id - 1;
	 if (document.getElementById(id).style.display  == 'none'){
	 	document.getElementById(id).style.display  = 'block';
	 	document.getElementById(i).style.backgroundColor  = 'white';
	 }
}
function hide(id){
	 var i = id - 1;
	 document.getElementById(id).style.display  = 'none';
	 document.getElementById(i).style.backgroundColor  = '#F1F3FA';
}


//functions for thumbs,gallerie-scroll,image-change
var myImg;
var move = false;
function galerie(address){
	//document.getElementById('ladeanzeige').style.display = 'block';
  if (document.getElementById('video'))
		document.getElementById('video').style.display = 'none';
	myImg = document.getElementById('foto');
	//myImg.style.visibility = 'hidden';
	myImg.src = address+'&mode=image';
	document.getElementById("mb_gal").href = address+'&mode=full';
	myImg.style.display = 'inline';
  myImg.style.visibility = 'visible';
}
function laden(){
  	document.getElementById('ladeanzeige').style.display = 'none';
  	myImg = document.getElementById('foto');
  	myImg.style.display = 'inline';
  	myImg.style.visibility = 'visible';
}
function stopGalerie(){
	window.clearTimeout(move);
}
function moveGalerie(Richtung) {
	var speed = 5;
  	var xPosition = ScrollField.offsetLeft;
  	if ((Richtung == 1) & (xPosition < startPosition)){
 		xPosition += speed;
 		ScrollField.style.left = xPosition+"px";
		move = window.setTimeout("moveGalerie('"+Richtung+"')", 20);
  		}
  	else if ((Richtung == 2) & (xPosition > endPosition)){
		xPosition -= speed;
		ScrollField.style.left = xPosition+"px";
		move = window.setTimeout("moveGalerie('"+Richtung+"')", 20);
    	}
    else {
    	move = true;
    	}
}


//toggle Searchfield
function toggle(id){
	togglebox = document.getElementById(id);
	if (togglebox.style.display != 'block'){
		togglebox.style.display = 'block';
	}else{
		togglebox.style.display = 'none';
	}
}

//function show embeded Video & hide Foto
function showVideo() {
	document.getElementById('foto').style.display = 'none';
	document.getElementById('video').style.display = 'inline';
	
	/*	
	var videoObject = "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/";
	videoObject += File;
	videoObject += "&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/";
	videoObject += File;
	videoObject += "&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6\" type=\"application\/x-shockwave-flash\" allowfullscreen=\"true\" width=\"425\" height=\"344\"><\/embed><\/object>";
	document.getElementById('video').innerHTML = videoObject;
	
	var object = document.createElement("object");
		var width = document.createAttribute("width")
		width.nodeValue = "425";
		object.setAttributeNode(width);
		var height = document.createAttribute("height")
		width.nodeValue = "344";
		object.setAttributeNode(height);
	
	var param1 = document.createElement("param");
		var name = document.createAttribute("name");
		name.nodeValue = "movie";
		param1.setAttributeNode(name);
		var value = document.createAttribute("value");
		value.nodeValue = "http://www.youtube.com/v/W2AmnbTO1IY&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6";
		param1.setAttributeNode(value);
		
	var param2 = document.createElement("param");
		name = document.createAttribute("name");
		name.nodeValue = "allowFullScreen";
		param2.setAttributeNode(name);
		value = document.createAttribute("value");
		value.nodeValue = "true";
		param2.setAttributeNode(value);
		
	var embed = document.createElement("embed");
		var src = document.createAttribute("src");
		src.nodeValue = "http://www.youtube.com/v/W2AmnbTO1IY&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6";
		embed.setAttributeNode(src);
		var type = document.createAttribute("type");
		type.nodeValue = "application/x-shockwave-flash";
		embed.setAttributeNode(type);
		var allowfullscreen = document.createAttribute("allowfullscreen");
		allowfullscreen.nodeValue = "true";
		embed.setAttributeNode(allowfullscreen);
		width = document.createAttribute("width")
		width.nodeValue = "425";
		embed.setAttributeNode(width);
		height = document.createAttribute("height")
		width.nodeValue = "344";
		embed.setAttributeNode(height);			
		
	object.appendChild(param1);
	object.appendChild(param2);
	object.appendChild(embed);		
	document.getElementById('video').appendChild(object);*/
}

//functions for machine-counting
function countMachine(){	//AJAX xmlHttpObject required
	//check all input-fields
	document.getElementById('suchAnzahl').innerHTML = '';
	document.getElementById('amLager').innerHTML = '';
	document.getElementById('loadAnzahl').style.display = 'inline';
	if (typeof(wait) != 'undefined')
		window.clearTimeout(wait);
	category = document.getElementsByName('gr')[0];
	producer = document.getElementsByName('hersteller')[0];
	searchKey = document.getElementsByName('q')[0];
	
	file = 'modules/ajax_home.php?';
	file += category.name+'='+category.value;
	file += "&"+producer.name+'='+producer.value;
	file += "&"+searchKey.name+'='+searchKey.value;
	file += "&request=count";

	if (typeof(value) != 'undefined')
		file += '&search='+value;
		//alert (file);
	xmlHttpObject.open('get', file);
	xmlHttpObject.onreadystatechange = function(){
		if (xmlHttpObject.readyState == 4) {
			count = xmlHttpObject.responseText.split("/");
			document.getElementById('suchAnzahl').innerHTML = count[0];
			document.getElementById('gesamtAnzahl').innerHTML = count[1];
			document.getElementById('amLager').innerHTML = count[2];
			document.getElementById('loadAnzahl').style.display = 'none';
		}
	}
	xmlHttpObject.send(null);
	return false;
}
function fillProducer(gruppe){
	file = 'modules/ajax_home.php?request=producerList&gruppe=';
	file += gruppe;
		//ladeanzeige starten
		document.getElementById('suchAnzahl').innerHTML = '';
		document.getElementById('loadAnzahl').style.display = 'inline';
		//alert (file);
	xmlHttpObject.open('get', file);
	xmlHttpObject.onreadystatechange = function(){
		if (xmlHttpObject.readyState == 4) {
				//document.getElementById('producerList').innerHTML = xmlHttpObject.responseText;			
			eintrag = xmlHttpObject.responseText.split("|");
			anzahl = eintrag.length;
			liste = document.getElementById('producerList');
			liste.innerHTML = '';
			//option "alle"
			var option = document.createElement("option");
			    option.appendChild(document.createTextNode("alle"));
			    option.value = "-1";
			    liste.appendChild(option);
			//hersteller auflisten
			for (var i=1; i<anzahl; i++){
				name = ((eintrag[i].split("#"))[0]);
				menge = ((eintrag[i].split("#"))[1]);
				maschine = ((eintrag[i].split("#"))[2]);			
			    option = document.createElement("option");
			    option.appendChild(document.createTextNode(name+"  "+menge));
			    option.value = maschine;
			    liste.appendChild(option);
			    }			
		}
	}
	xmlHttpObject.send(null);
	wait = window.setTimeout("countMachine()",1000);
	return false;
}

var tableHover = new Class({
	Implements: [Options,Events],
	options: {
		elements: 'table.machineTable',
		cssEven: 'bgdark',
		cssOdd: 'bglight',
		cssHighlight: 'bghighlight',
		cssMouseEnter: 'bgmouse'
	},
	initialize: function(options) {
		this.setOptions(options);
		$$(this.options.elements).each(function(table) {
			this.hoverEffect(table);
		},this);
	},
	hoverEffect: function(table) {
		table.getElements('tr').each(function(tr,i) {
			if(tr.getFirst().get('tag') != 'th') {
				var options = this.options, klass = i % 2 ? options.cssEven : options.cssOdd;
				tr.addClass(klass).addEvents({
					mouseenter: function () {
						if(!tr.hasClass(options.cssHighlight)) tr.addClass(options.cssMouseEnter).removeClass(klass);
					},
					mouseleave: function () {
						if(!tr.hasClass(options.cssHighlight)) tr.removeClass(options.cssMouseEnter).addClass(klass);
					},					
					click: function() {
						self.location.href = tr.getElement('a');
						//this.open(tr.indexOf(myTarget),tr);
						/* JK 20090730 disable highlight-Effekt
						if(!tr.hasClass(options.cssHighlight))
							tr.removeClass(options.cssMouseEnter).addClass(options.cssHighlight);
						else
							tr.addClass(options.cssMouseEnter).removeClass(options.cssHighlight);*/
					}
				});
			}
		},this);
	}
});
