// ============================================
// 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.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;
	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,74,133,31); //<-- Dies ist der Farbwert für die Mainmenu Schriftbalken
			animator.addToAniQueue(a);
		}
	}
}

function popdown(el) {
	for (var i=navOrder.length; i--;) {
		var a = navOrder[i];
		if (a.el == el) {
			if (a.el.className.indexOf('main') == -1) a.setTarget(10,240,240,240);
			else a.setTarget(0,255,255,255);
			animator.addToAniQueue(a);
		}
	}
}

// ============================================
// 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 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 init() {
  sinustable = initSinusTable(steps);
  sinustablelong = initSinusTable(20);
  
  animator = new Animator();

 initNavigation();
	
}

window.onload = init;







