// ============================================
// Animator Object
// ============================================
function isInQueue(obj) {
	var found = false;
	for (var i=this.aniQueue.length; i--;) {
 		if (this.aniQueue[i]==obj) found = true;
 	}
 	for (var i=this.waitingQueue.length; i--;) {
 		if (this.waitingQueue[i]==obj) found = true;
 	}
 	return found;
}
 
function addToAniQueue(obj) {
	if (!this.isInQueue(obj) && obj) { 
		this.aniQueue[this.aniQueue.length] = obj;
		this.startAnimation();
	}
}

function appendToWaitingQueue(arr) {
	if (arr.length) { 
		this.waitingQueue = this.waitingQueue.concat(arr);
		this.startWaitingQueue();
	}
}
 
function animateStuff() {
 	for (var i=this.aniQueue.length; i--;) {
		var a = this.aniQueue[i];
		if (a.animate()) this.aniQueue.splice(i,1);
	}
  	if (this.aniQueue.length == 0) this.stopAnimation();
	
  	
}

function startAnimation() {
  if (!this.timer) this.timer = setInterval('animator.animateStuff()',animationInterval);
}

function startWaitingQueue() {
  if (!this.timer2) this.timer2 = setInterval('animator.next()',waitingInterval);
}

function stopAnimation() {
  if (!this.timer) return false;
  clearInterval(this.timer);
  this.timer = null;

}

function next() {
	this.addToAniQueue(this.waitingQueue.shift());
	if (this.waitingQueue.length == 0) {
		if (this.timer2) clearInterval(this.timer2);
		this.timer2 = null
	}
}
 
function Animator() {
	this.name ='animator';
	this.aniQueue = new Array();
	this.waitingQueue = new Array();
	this.timer1 = null;
	this.timer2 = null;
	
	this.isInQueue = isInQueue;
	this.addToAniQueue = addToAniQueue;
	this.startAnimation = startAnimation;
	this.stopAnimation = stopAnimation;
	this.animateStuff = animateStuff;
	this.appendToWaitingQueue = appendToWaitingQueue;
	this.startWaitingQueue = startWaitingQueue;
	this.next = next;
}
// ============================================

// ============================================
// animatable "interface"	
// ============================================
function Animatable(el){
	this.s_x = 0;
	this.s_y = 0;
	this.t_x = 0;
	this.t_y = 0;
	this.s_z = 0;
	this.t_z = 0;
	this.step = 0;
	this.el = el;
	this.style = this.el.style;
	
	this.setTarget = function (x,y,w,h) {
		if(x != null) this.t_x = x;
		if(y != null) this.t_y = y;
		if(w != null) this.t_w = w;
		if(h != null) this.t_h = h;
		this.step=0;
	}
	
	this.animate = function () {};
	this.stop = function () {};
}

// ============================================
// navigation object, implements animatable	
// ============================================
function overMain() {
	var perc = (this.step+1)/20; //sinustable[this.step];
	var distX = this.s_x + parseInt(perc*(this.t_x - this.s_x));
	var distR = this.s_y+parseInt(perc*(this.t_y - this.s_y));
	var distG = this.s_w+parseInt(perc*(this.t_w - this.s_w));
	var distB = this.s_h+parseInt(perc*(this.t_h - this.s_h));
	//this.style.paddingLeft = this.s_x+distX+'px';
	this.style.backgroundPosition = distX + 'px 0';
	this.style.color = 'rgb('+distR+','+distG+','+distB+')';
	this.step++;
	if (this.step == 20) {
		this.step=0;
		this.stop();
		this.s_x = this.t_x;
		this.s_y = distR;
		this.s_w = distG;
		this.s_h = distB;
		return true;
	}
}

function overSub() {
	var perc = sinustable[this.step];
	var distX = this.s_x + parseInt(perc*(this.t_x - this.s_x));
	var distR = this.s_y+parseInt(perc*(this.t_y - this.s_y));
	var distG = this.s_w+parseInt(perc*(this.t_w - this.s_w));
	var distB = this.s_h+parseInt(perc*(this.t_h - this.s_h));
	this.style.paddingLeft = distX+'px';
	this.style.backgroundColor = 'rgb('+distR+','+distG+','+distB+')';
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function stopNavObject() {
	this.s_x = this.t_x;
	//this.style.marginBottom = '1px';
	if (this.el.className.indexOf('main') == -1) {
		if (this.s_x == 20) this.el.className='over';
		else this.el.className='';
	}
}

function newNavObject(el, i) {
	var o = new Animatable(el);
	
	
	if (o.el.className.indexOf('main') == -1) {
		o.s_x = 10;
		o.s_y = 240;
		o.s_w = 240;
		o.s_h = 240;
		o.animate = overSub;
	
	} else {
		o.s_x = 0;
		o.s_y = 255;
		o.s_w = 255;
		o.s_h = 255;
		o.animate = overMain;
	}
	o.stop = stopNavObject;
	
	if (o.el.className.indexOf('selected') == -1 
		&& o.el.className.indexOf('subsub') == -1 
		&& o.el.className.indexOf('active') == -1
		 && o.el.className.indexOf('dis') == -1) {
		el.onmouseover = function() {
			popup(this);
		}
		el.onmouseout = function() {
			popdown(this);
		}
	}
	
	return o;
}


function popup(el) {
	for (var i=navOrder.length; i--;) {
		var a = navOrder[i];
		if (a.el == el) {
			if (a.step != 0) return;
			if (a.el.className.indexOf('main') == -1) a.setTarget(20,230,230,230);
			else a.setTarget(-75,32,61,122);
			animator.addToAniQueue(a);
		}
	}
}

function popdown(el) {
	for (var i=navOrder.length; i--;) {
		var a = navOrder[i];
		if (a.el == el) {
			//if (a.s_x != a.t_x) return;
			if (a.el.className.indexOf('main') == -1) a.setTarget(10,240,240,240);
			else a.setTarget(0,255,255,255);
			animator.addToAniQueue(a);
		}
	}
}

// ============================================
// HOME
// ============================================


function newNavObjectHome(el, i) {
	var o = new Animatable(el);
	
	o.bg = el.getElementsByTagName('div')[0];
	
	o.s_x = 100;
	o.s_y = 32;
	o.s_w = 61;
	o.s_h = 122;
	o.animate = overHome;
	
	
	o.stop = stopHome;
	
	el.onmouseover = function() {
		popupHome(this);
	}
	el.onmouseout = function() {
		popdownHome(this);
	}
	
	
	return o;
}

function stopHome() {
	this.s_x = this.t_x;
	this.s_y = this.t_y;
	this.s_w = this.t_w;
	this.s_h = this.t_h;
	
}

function overHome() {
	var perc = sinustable[this.step];
	var opacity = this.s_x + parseInt(perc*(this.t_x - this.s_x));
	var distR = this.s_y+parseInt(perc*(this.t_y - this.s_y));
	var distG = this.s_w+parseInt(perc*(this.t_w - this.s_w));
	var distB = this.s_h+parseInt(perc*(this.t_h - this.s_h));
	
	this.style.color = 'rgb('+distR+','+distG+','+distB+')';
	
	this.bg.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	this.bg.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	this.bg.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	this.bg.style.opacity = opacity/100;
	
	
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function popupHome(el) {
	for (var i=homeOrder.length; i--;) {
		var a = homeOrder[i];
		if (a.el == el) {
			if (a.step != 0) return;
			a.setTarget(50,255,255,255);
			animator.addToAniQueue(a);
		}
	}
}

function popdownHome(el) {
	for (var i=homeOrder.length; i--;) {
		var a = homeOrder[i];
		if (a.el == el) {
			//if (a.s_x != a.t_x) return;
			a.setTarget(100,32,61,122);
			animator.addToAniQueue(a);
		}
	}
}

// ============================================




// ============================================
// QL
// ============================================


function newQuicklink(el, i) {
	var o = new Animatable(el);
	
	o.bg = el.getElementsByTagName('div')[0];
	
	o.s_x = 10;
	o.s_y = 20;
	o.animate = overQL;
	
	
	o.stop = stopQLObject;
	
	el.onmouseover = function() {
		popupQL(this);
	}
	el.onmouseout = function() {
		popdownQL(this);
	}
	
	
	return o;
}

function stopQLObject() {
	this.s_x = this.t_x;
	this.s_y = this.t_y;
	
	if (this.s_x == 15) this.el.className='over';
	else this.el.className='';
	
}

function overQL() {
	var perc = sinustable[this.step];
	var distX = this.s_x + parseInt(perc*(this.t_x - this.s_x));
	var opacity = this.s_y+parseInt(perc*(this.t_y - this.s_y));
	
	this.style.paddingLeft = distX+'px';
	
	this.bg.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	this.bg.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	this.bg.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	this.bg.style.opacity = opacity/100;
	
	
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function popupQL(el) {
	for (var i=navOrder.length; i--;) {
		var a = navOrder[i];
		if (a.el == el) {
			if (a.step != 0) return;
			a.setTarget(15,50,null,null);
			animator.addToAniQueue(a);
		}
	}
}

function popdownQL(el) {
	for (var i=navOrder.length; i--;) {
		var a = navOrder[i];
		if (a.el == el) {
			//if (a.s_x != a.t_x) return;
			a.setTarget(5,20,null,null);
			animator.addToAniQueue(a);
		}
	}
}

// ============================================



// ============================================
// general functions
// ============================================

// ============================================
// globals
// ============================================
var navItems = [];
var navObj = [];
var navOrder = [];
var homeOrder = [];
var animator;
var image;
var debugView;
var selectedNavItem = null;
var clickedNavItem = null;
var selectedThumbItem = null;
var firstImage = null;
var image = null;
var old = null;

var gotoPage = null;

var loTimer = null;
var lastOver = null;

var steps = 15;
var sinustable = [];
var sinustablelong = [];
var waitingInterval = 800;
var animationInterval = 10;

var navTop = 225;
var navLeft = 30;
var navFreq = 16;
var hlLeft = 241;
var thumbFreq = 30;

function initSinusTable(numOfSteps) {
	var st = [];
	for (var i=0;i<numOfSteps; i++) {
		st[i] = Math.sin(((i+1)*0.5*Math.PI)/numOfSteps)
	}
	return st;
}

function debug(message) {
		debugView.innerHTML = message;
}


function initNavigation() {
	if (!document.getElementById('navigation')) return;
	
	selectedNavItem = null;
	navOrder = [];
	
	navItems = document.getElementById('navigation').getElementsByTagName('a');
	totalItems = navItems.length;
	for (var i=totalItems; i--;) {
		navObj[i] = newNavObject(navItems[i],i);
		navOrder[i] = navObj[i];
  	}
	
}

function initNavigationHome() {
	if (!document.getElementById('navigationhorizontal')) return;
	
	selectedNavItem = null;
	homeOrder = [];
	
	navItems = document.getElementById('navigationhorizontal').getElementsByTagName('a');
	totalItems = navItems.length;
	for (var i=totalItems; i--;) {
		navObj[i] = newNavObjectHome(navItems[i],i);
		homeOrder[i] = navObj[i];
  	}
	
}

function initQuicklinks() {
	if (!document.getElementById('quicklinks')) return;
	
	selectedNavItem = null;
	navOrder = [];
	
	navItems = document.getElementById('quicklinks').getElementsByTagName('a');
	totalItems = navItems.length;
	for (var i=totalItems; i--;) {
		navObj[i] = newQuicklink(navItems[i],i);
		navOrder[i] = navObj[i];
  	}
	
}

function init() {
  sinustable = initSinusTable(steps);
  sinustablelong = initSinusTable(20);
  
  debugView = document.getElementById('debug');
  
  animator = new Animator();

 initNavigation();
 initQuicklinks();
 initNavigationHome()
	
}

window.onload = init;


function openWindow(w,h,url) {
	screenWidth=screen.availWidth;
	screenHeight=screen.availHeight;
	x=(screenWidth-w-10)/2;
	y=(screenHeight-h-40)/2;
	
	window.open(url,"ims","width="+w+",height="+h+",left="+x+",top="+y+",menubar=0,status=0,location=0,scrollbars=0,resizable=0");
}




// rollover anwendungsfelder produkte
var anw_blank = new Image();
anw_blank.src = "img/anw_blank.jpg";

var anw_sitz = new Image();
anw_sitz.src = "img/anw_sitz.jpg";
	
var anw_motor = new Image();
anw_motor.src = "img/anw_motor.jpg";

var anw_brems = new Image();
anw_brems.src = "img/anw_brems.jpg";
	
var anw_schliess = new Image();
anw_schliess.src = "img/anw_schliess.jpg";
	
var anw_lenk = new Image();
anw_lenk.src = "img/anw_lenk.jpg";



var anw_plg_blank = new Image();
anw_plg_blank.src = "img/anw_plg_blank.jpg";

var anw_gebaeude = new Image();
anw_gebaeude.src = "img/anw_gebaeude.jpg";

var anw_maschine = new Image();
anw_maschine.src = "img/anw_maschine.jpg";

var anw_foerder = new Image();
anw_foerder.src = "img/anw_foerder.jpg";

var anw_geraete = new Image();
anw_geraete.src = "img/anw_geraete.jpg";

var anw_automotive = new Image();
anw_automotive.src = "img/anw_automotive.jpg";

var anw_diverse = new Image();
anw_diverse.src = "img/anw_diverse.jpg";

function imageChange(imgName, code) {
	if (document.images) {
		var myImg = 'img' + code;
		//alert(myImg);
		document.images[imgName].src = eval(myImg + ".src");
	}
}


