// ============================================
// 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 () {};
}


// ============================================
// LINE
// ============================================


function newLineObject(el,r,g,b) {
	var o = new Animatable(el);
	
	o.s_x = 0;
	o.s_y = r;
	o.s_w = g;
	o.s_h = b;
	o.animate = animateLine;
	
	o.stop = stopLine;
	
	return o;
}

function stopLine() {
	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 animateLine() {
	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.backgroundColor = 'rgb('+distR+','+distG+','+distB+')';
	this.style.width = distX+'px';
	
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function overLine(l,w,r,g,b) {
	l.setTarget(w,r,g,b);
	animator.addToAniQueue(l);
	//window.console.log('OVER w: ' + w + ' aW: ' + aW + ' l: ' +l.el.id+' aL: ' +aL);
	if ( (w!=aW) && (l.el.id!=aL) ) { //
		window.clearTimeout(lastOver);
		loTimer = false;
		hideSubnav();
		//window.console.log('CLEAR '+lastOver);
	} else if (l.el.id!=aL) { // 
		showSubnav();
	}
}

function outLine(l,w,r,g,b) {
	l.setTarget(w,r,g,b);
	animator.addToAniQueue(l);
	
	//window.console.log('OUT w: ' + w + ' aW: ' + aW + ' l: ' +l.el.id+' aL: ' +aL);
	if ( (w==aW) && (l.el.id!=aL)) { // (w!=aW) &&
		lastOver = window.setTimeout("timedSubnav()",500);
		loTimer = true;
		//window.console.log('SET TIMER ' + lastOver);
	}
}

function timedSubnav() {
	//window.console.log('TIMER EXEC ' + lastOver+loTimer);
	if (loTimer) showSubnav();
}

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

// ============================================
// SUBNAV
// ============================================


function newSubnavObject(el) {
	var o = new Animatable(el);
	
	o.s_x = 0;
	o.s_y = 0;
	o.s_w = 0;
	o.s_h = 0;
	o.animate = animateSubnav;
	
	o.stop = stopSubnav;
	
	return o;
}

function stopSubnav() {
	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 animateSubnav() {
	var perc = sinustable[this.step];
	var opacity = this.s_x + parseInt(perc*(this.t_x - this.s_x));
	
	
	this.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	this.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	this.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	this.style.opacity = opacity/100;
	
	
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function hideSubnav() {
	//window.console.log(subnav.t_x);

	subnav.setTarget(0,0,0,0);
	animator.addToAniQueue(subnav);
}

function showSubnav() {
	subnav.setTarget(100,0,0,0);
	animator.addToAniQueue(subnav);
}

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


// ============================================
// Link object, implements animatable	
// ============================================


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.color = 'rgb('+distR+','+distG+','+distB+')';
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function stopLinkObject() {
	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 newLinkObject(el, i) {
	var o = new Animatable(el);
	
	o.s_x = 0;
	o.s_y = 127;
	o.s_w = 131;
	o.s_h = 135;
	o.animate = overSub;
	
	o.stop = stopLinkObject;
	
	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 (aL == 'line') 
				a.setTarget(0,rR,rG,rB);
			else 
				a.setTarget(0,tR,tG,tB);
			animator.addToAniQueue(a);
		}
	}
}

function popdown(el) {
	for (var i=navOrder.length; i--;) {
		var a = navOrder[i];
		if (a.el == el) {
			a.setTarget(0,127,131,135);
			animator.addToAniQueue(a);
		}
	}
}


// ============================================
// Side Link object, implements animatable	
// ============================================




function stopSideLinkObject() {
	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 newSideLinkObject(el, i) {
	var o = new Animatable(el);
	
	o.s_x = 0;
	o.s_y = 229;
	o.s_w = 232;
	o.s_h = 233;
	o.animate = overSub;
	
	o.stop = stopSideLinkObject;
	
	el.onmouseover = function() {
		popup2(this);
	}
	el.onmouseout = function() {
		popdown2(this);
	}
	
	
	return o;
}


function popup2(el) {
	for (var i=navOrder2.length; i--;) {
		var a = navOrder2[i];
		if (a.el == el) {
			if (a.step != 0) return;
			if (aL == 'line') 
				a.setTarget(0,rR,rG,rB);
			else 
				a.setTarget(0,tR,tG,tB);
			animator.addToAniQueue(a);
		}
	}
}

function popdown2(el) {
	for (var i=navOrder2.length; i--;) {
		var a = navOrder2[i];
		if (a.el == el) {
			a.setTarget(0,229,232,233);
			animator.addToAniQueue(a);
		}
	}
}


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

// ============================================
// globals
// ============================================
var navItems = [];
var navObj = [];
var navItems2 = [];
var navObj2 = [];
var navOrder = [];
var navOrder2 = [];
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 = false;
var lastOver = null;

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

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

var line = null;
var line2 = null;
var subnav = null;

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 init() {
  sinustable = initSinusTable(steps);
  sinustablelong = initSinusTable(20);
  
  animator = new Animator();
  
  el = document.getElementById('line');
  line = newLineObject(el,240,241,242);

  el2 = document.getElementById('submenu');
  subnav = newSubnavObject(el2);
  
  el3 = document.getElementById('line2');
  line2 = newLineObject(el3,127,131,135);
  
  outLine(line,tW,tR,tG,tB); 
  outLine(line2,rW,rR,rG,rB); 
  showSubnav();
  
  
    navItems = document.getElementById('content').getElementsByTagName('a');
   totalItems = navItems.length;
	for (var i=0;i<totalItems; i++) {
		navObj[i] = newLinkObject(navItems[i],i);
		navOrder[i] = navObj[i];
  	}
  	
  	navItems2 = document.getElementById('sidebar').getElementsByTagName('a');
   	totalItems2 = navItems2.length;
	for (var i=0;i<totalItems2; i++) {
		navObj2[i] = newSideLinkObject(navItems2[i],i);
		navOrder2[i] = navObj2[i];
  	}
  	
}

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");
}

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


