var sp = new Array();

var mx = 0;
var my = 0;

var maxdist = 30;

function span(el)
{
	this.el = el;
	var posi = findPos(this.el);
	this.x = this.mx = this.cx = posi[0];
	this.y = this.my = this.cy = posi[1];
	
	this.render = function()
	{
		this.mx = this.mx + (this.x - this.mx) / 5;
		this.my = this.my + (this.y - this.my) / 5;
		this.cx = this.cx + (this.mx - this.cx) / 5;
		this.cy = this.cy + (this.my - this.cy) / 5;
		this.el.style.left = this.cx + "px";
		this.el.style.top = this.cy + "px";
	}
}

function init()
{
	document.onmousemove = mm;
	var div = document.getElementById("flee");
	var str = div.innerHTML;
	var str = str.replace(/(.{1})/ig, '<span>$1</span>');
	var str = str.replace(/<span><<\/span>.+?<span>><\/span>/img, '<br>');
	div.innerHTML = str;
	
	var spans = document.getElementsByTagName("span");
	
	for(i = 0; i < spans.length; i++)
	{
		sp[i] = new span(spans[i]);
	}
	for(i = 0; i < spans.length; i++)
	{
		sp[i].el.style.position = "absolute";
	}
	
	div.style.left = "0px";
	div.style.top = "0px";
	
	render();
	
	init_beta();
}

var uh = false;
var dh = false;
var delt = 0;

function render()
{
	for(i = 0; i < sp.length; i++)
	{
		var xdiff = Math.abs(mx - sp[i].cx);
		var ydiff = Math.abs(my - sp[i].cy);
		var dist = Math.sqrt(xdiff * xdiff + ydiff * ydiff);
		if(dist < maxdist)
		{
			sp[i].mx = sp[i].mx + (sp[i].cx - mx) * (1 - dist / maxdist);
			sp[i].my = sp[i].my + (sp[i].cy - my) * (1 - dist / maxdist);
		}
	}
	
	for(i = 0; i < sp.length; i++)
	{
		sp[i].render();
	}
	
	if(scrollel)
	{
		var offset = scrollel.offset();
		var x = mx - offset.left;
		var y = my - offset.top;
		var w = scrollel.width();
		var h = scrollel.height();
		var inner = scrollel.children(".inner");
		var speed = y / h - 0.5;
		
		var ih = inner.height();
		
		if(speed < -0.2)
			speed += 0.2;
		else if(speed > 0.2)
			speed -= 0.2;
		else
			speed = 0;
		speed *= -20;
		curm = parseInt(inner.css("margin-top"));
		var newm = (curm + speed + delt);
		delt = 0;
		newm = Math.min(0, newm);
		newm = Math.max(newm, 0 - ih + h);
		
		if(newm == 0)
		{
			$(".up").hide();
			uh = true;
		}
		else if(uh)
		{
			$(".up").show();
			uh = false;
		}
			
		if(newm == 0 - ih + h)
		{
			$(".down").hide();
			dh = true;
		}
		else if(dh)
		{
			$(".down").show();
			dh = false;
		}
			
		inner.css("marginTop", newm + "px");
	}
	
	window.setTimeout("render();", 20);
}

function go_referenzen()
{
	$("#angebot").queue("fx", []);
	$("#angebot").animate({top: "-500px"}, 1000, "easeOutBounce");
	$("#referenzen").queue("fx", []);
	$("#referenzen").animate({left: "250px"}, 1000, "easeOutBounce");
}

function go_angebot()
{
	$("#referenzen").queue("fx", []);
	$("#referenzen").animate({left: "-700px"}, 1000, "easeOutBounce");
	$("#angebot").queue("fx", []);
	$("#angebot").animate({top: "50px"}, 1000, "easeOutBounce");
}

function go_away()
{
	$("#angebot").queue("fx", []);
	$("#angebot").animate({top: "-500px"}, 1000, "easeOutBounce");
	$("#referenzen").queue("fx", []);
	$("#referenzen").animate({left: "-700px"}, 1000, "easeOutBounce");
}

var suspend = false;

var scrollel = null;

var eatme = "";
var maxeat = 10;

$(document).keydown(function(event)
{
	eatme += String.fromCharCode(event.keyCode);
	eatme = eatme.substr(eatme.length - maxeat);
	if(eatme.indexOf("BETA") != -1)
	{
		eatme = "";
		$("#menu").show();
	}
});

function init_beta()
{
	$("#menu").css("left", "-1000px");
	$("#menu").append("&nbsp;");
	$(".fuffdiv").each(function()
	{
		$("#menu").append('<a href="javascript:void(0);" onclick="javascript:go_' + $(this).attr("id") + '();">' + $(this).attr("alt") + "</a>&nbsp;");
	});
	
	var l = (0 - $("#menu").width()) + "px";
	
	$("#menu").css("left", l);
	
	$("#menu").append("&gt;&gt;");
	
	//easeInOutBack
	//easeOutBounce
	//easeOutElastic
	
	$("#menu").hover(
		function() {
			$(this).queue("fx", []);
			$(this).animate(
			{ 
				left: "0px"
			}, 500, "easeOutBounce");
		},
		function()
		{
			$(this).queue("fx", []);
			$(this).animate({margin: "0px"}, 3000);
			$(this).animate(
			{ 
				left: l
			}, 500, "easeOutBounce");
		}
	);
}

$(document).ready(function()
{
	$(".fuffdiv").hover(
		function () {
		}, 
		function () {
		}
	);
	
	$("#angebot").css("top", "-500px");
	$("#referenzen").css("left", "-700px");
	$("#refscroll").css("height", "295px");
	
	$(".scrolldiv").hover(
		function () {
			scrollel = $(this);
		}, 
		function () {
			scrollel = null;
		}
	);
	
	$(".scrolldiv").mousewheel(function(event, delta)
	{
			delt += delta * 6;
	});
		
	$(".scrolldiv").prepend('<div class="up" style="position: absolute; left: 100%; margin-left: -30px; width: 30px;">/\\</div>');
	$(".scrolldiv").prepend('<div class="down" style="position: absolute; left: 100%; margin-left: -30px; top: 100%; margin-top: -30px; width: 30px;">\\/</div>');
});


function getRandom(min, max)
{
	return min + Math.floor(Math.random() * (max - min));
}

var t = null;

function mm(ev)
{
	var t = null;
	if(window.event)
	{
		mx = window.event.clientX;
		my = window.event.clientY;
		t = window.event.srcElement;
	}
	else
	{
		mx = ev.pageX;
		my = ev.pageY;
		t = ev.target;
	}
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if(obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while(obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function isImageOk(img)
{
	if(!img)
		return false;
	
	if(!img.complete)
	{
		return false;
	}
	
	if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
	{
		return false;
	}

	return true;
}


function PadDigits(n, totalDigits)
{
	n = n.toString();
	var pd = ''; 
	if (totalDigits > n.length)
	{
		for (i=0; i < (totalDigits-n.length); i++)
		{
			pd += '0';
		}
	}
	return pd + n.toString();
}


function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}