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


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


// ============================================
// HomeLink object, implements animatable	
// ============================================
function overHomeLink() {
	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.bg.style.backgroundColor = 'rgb('+distR+','+distG+','+distB+')';
	this.bg.style.height = distX+'px';
	
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function stopHomeLinkObject() {
	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 newHomeLinkObject(el, i) {
	var o = new Animatable(el);
	o.bg = el.getElementsByTagName('div')[1];
	
	o.s_x = 110;
	o.s_y = 77;
	o.s_w = 77;
	o.s_h = 77;
	o.animate = overHomeLink;
	
	o.stop = stopHomeLinkObject;
	
	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;
			a.setTarget(90,158,0,0);
			animator.addToAniQueue(a);
		}
	}
}

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


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



// ============================================
// SCHUBLADE object, implements animatable	
// ============================================
function animateBox() {
	var perc = sinustable[this.step];
	var distX = this.s_x + parseInt(perc*(this.t_x - this.s_x));
	
	this.bg.style.height = distX+'px';
	
	this.step++;
	if (this.step == sinustable.length) {
		this.step=0;
		this.stop();
		return true;
	}
}

function stopObject() {
	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 newBoxObject(el, i) {
	var o = new Animatable(el);
	o.bg = document.getElementById('loginform');
	
	o.s_x = 1;
	o.s_y = 77;
	o.s_w = 77;
	o.s_h = 77;
	o.animate = animateBox;
	
	o.stop = stopObject;
	
	el.onclick = function() {
		openBox(this);
	}
	/*el.onmouseout = function() {
		popdown(this);
	}*/
	
	return o;
}


function openBox(el) {
	if (lade.step != 0) return;
	myTarget = 27;
	if (lade.s_x != 1) myTarget = 1;
	lade.setTarget(myTarget,0,0,0);
	animator.addToAniQueue(lade);
	
	
}




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



// ============================================
// 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 athletes = false;

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;

var lade = 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('loginbutton');
  lade = newBoxObject(el,0,0,0);
 /*
  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();
  */
  
	if (!document.getElementById('teaser')) return;
    navItems = document.getElementById('teaser').getElementsByTagName('a');
   
    totalItems = navItems.length;
	for (var i=0;i<totalItems; i++) {
		navObj[i] = newHomeLinkObject(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");
	}
}

function manageSlideButtons() {
	var viewportHeight = 390;
	var a = $('#athleteninhalt');
	var athletesPos = parseInt(a.css('top'));
	var newPosDown = athletesPos - 84;
	var newPosUp = athletesPos + 84;
	var contentHeight = 474; //parseInt(a.css('height'));
	
	if ((contentHeight - viewportHeight + newPosDown) >= 0) $('#scrolldown').show();
	else $('#scrolldown').hide();
	
	if (newPosUp <= 0) $('#scrollup').show();
	else $('#scrollup').hide();
}

function slideDown() {
	var viewportHeight = 390;
	var a = $('#athleteninhalt');
	var athletesPos = parseInt(a.css('top'));
	var newPos = athletesPos - 84;
	var contentHeight = 474; //parseInt(a.css('height'));
	//alert(contentHeight - viewportHeight + newPos);
	if ((contentHeight - viewportHeight + newPos) >= 0) a.animate({top: newPos+'px'},500, 'swing', manageSlideButtons);
	//manageSlideButtons();
}

function slideUp() {
	var viewportHeight = 390;
	var a = $('#athleteninhalt');
	var athletesPos = parseInt(a.css('top'));
	var newPos = athletesPos + 84;
	var contentHeight = 474; //parseInt(a.css('height'));
	//alert(contentHeight - viewportHeight + newPos);
	if (newPos <= 0) a.animate({top: newPos+'px'},500, 'swing', manageSlideButtons);
	//manageSlideButtons();
}


function loadslide(page_id, info_id) {
	//new Ajax.Updater('stage', './slideshow.php', { method: 'get', parameters: {page_id: page_id, info_id: info_id} });
	 $("#stage").load("slideshow.php", {page_id: page_id, info_id: info_id});
	
}

function hideImg() {
	 $("#stage > img").fadeOut('slow');
}
function loadthumbs(page_id) {
	$("#thumbs").load("thumbs.php", {page_id: page_id}).fadeIn('slow');
	
}


$(document).ready(function(){
	$('a[href^=mailto:]').each(function(i) {
		var xr = this.href.match(/^mailto:([A-Za-z0-9._%+-@]+)$/);
		if(!xr) return;
		var x = xr[1]; // encoded address
		var s = ''; // decoded address
		s = Rot13.convert(x);
		//xr = s.split(/\|/);  // optionally split real address from url text
		this.href = "mailto:" + s;
		if ($(this).html() != "E-Mail") $(this).html(s); //xr[0];
		//if(xr.length > 1) $(this).html(xr[1]);
		
	
	});
	
	if (!athletes) $("#athleten a.on").css("opacity", 0.3);
	if (!athletes) $("#athleten a.on").hover(
		function () {
      		$(this).fadeTo("fast", 1.0);
    	},
    	function () {
      		$(this).fadeTo("fast", 0.3);
    	}
    );
    if (!athletes) {
		var atid = parseInt($("#athleten a.off").attr('id'));
    	var offset = Math.ceil((atid-15)/3.0)%5;
    	//alert(offset);
		
		if (offset>0 && atid > 15) 
    	{
    		topPos = -1*offset*84;
	    	$('#athleteninhalt').css("top", topPos);
    	}	
    }
   manageSlideButtons();
});


Rot13 = {
    map: null,

    convert: function(a) {
        Rot13.init();

        var s = "";
        for (i=0; i < a.length; i++) {
            var b = a.charAt(i);
            s += ((b>='A' && b<='Z') || (b>='a' && b<='z') ? Rot13.map[b] : b);
        }
        return s;
    },

    init: function() {
        if (Rot13.map != null)
            return;
              
        var map = new Array();
        var s   = "abcdefghijklmnopqrstuvwxyz";

        for (i=0; i<s.length; i++)
            map[s.charAt(i)] = s.charAt((i+13)%26);
        for (i=0; i<s.length; i++)
            map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();

        Rot13.map = map;
    },

    write: function(a) {
        document.write(Rot13.convert(a));
    }
}