var moving = false;
var winW = 800;
var timeout;
var timeout1;
var initialDelay = 20;
//var pixelToMove = 25;
var pixelToMove = 64;
var enabled;

function centerWindow(wind) {
	if(wind && wind.document) {
		var myWidth = 0, myHeight = 0;
		  if( typeof( wind.innerWidth ) == 'number' ) {
		    //Non-IE
		    myWidth = wind.innerWidth;
		    myHeight = wind.innerHeight;
		  } else if( wind.document.documentElement && ( wind.document.documentElement.clientWidth || wind.document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myWidth = wind.document.documentElement.clientWidth;
		    myHeight = wind.document.documentElement.clientHeight;
		  } else if( wind.document.body && ( wind.document.body.clientWidth || wind.document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myWidth = wind.document.body.clientWidth;
		    myHeight = wind.document.body.clientHeight;
  		}
		wind.moveTo((self.screen.width/2)-(myWidth/2),(self.screen.height/2)-(myHeight/2));
		wind.focus();
	}
}
function trimAll(sString)
{
	while (sString.substring(0,1) == " ")
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == " ")
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
function replaceIt(string,suchen,ersetzen) {
	ausgabe = "" + string;
	while (ausgabe.indexOf(suchen) > -1) {
		pos= ausgabe.indexOf(suchen);
		ausgabe = "" + (ausgabe.substring(0, pos) + ersetzen +
		ausgabe.substring((pos + suchen.length), ausgabe.length));
	}
	return ausgabe;
}
function toogleDisplay(objId) {
	var obj = getElementStyle(objId);
	if ( obj.display == "none" ) {
		obj.display = "block";
	} else {
		obj.display = "none";
	}
}

function getScrollX() {
	var the_x;
	if (self.pageYOffset) // all except Explorer
	{
		the_x = self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		the_x = document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		the_x = document.body.scrollLeft;
	}
	return the_x;
}
function getScrollY() {
	var the_y;
	if (self.pageYOffset) // all except Explorer
	{
		the_y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		the_y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		the_y = document.body.scrollTop;
	}
	return the_y;
}

function getElementStyle (id) {
    if      (document.layers)         return document.layers[id];
    else if (document.all)            return document.all[id].style;
    else if (document.getElementById) return document.getElementById(id).style;
    else                              return null;
}
function getElement (id) {
    if      (document.layers)         return document.layers[id];         // NC 4
    else if (document.all)            return document.all[id];            // IE 4
    else if (document.getElementById) return document.getElementById(id); // DOM
    else                              return null;
}

function hoverMenu() {
	//doMove(pixelToMove,initialDelay);
	doMove(1,initialDelay);
}
function doMove(pixel, delay) {

	navY = getElement("navigation").offsetTop;
	posY = getScrollY();

	// return if no more delta
	deltaY = Math.abs(navY-(posY+10));
	if((deltaY <= 1 && deltaY > 0) || !moving) {
		move(posY+10);
		moving = false;
		return;
	}

	if(navY > posY+10)
		navY -= pixel;
	else if(navY < posY+10)
		navY += pixel;

	// do the move
	move(navY);

	if(deltaY < 10 && deltaY > 0)
		timeout = setTimeout('doMove(1,'+delay+')',delay);
	else if(deltaY < 20 && deltaY >= 10)
		timeout = setTimeout('doMove(2,'+delay+')',delay);
	else if(deltaY < 40 && deltaY >= 20)
		timeout = setTimeout('doMove(4,'+delay+')',delay);
	else if(deltaY < 80 && deltaY >= 40)
		timeout = setTimeout('doMove(8,'+delay+')',delay);
	else if(deltaY < 160 && deltaY >= 80)
		timeout = setTimeout('doMove(16,'+delay+')',delay);
	else if(deltaY < 320 && deltaY >= 160)
		timeout = setTimeout('doMove(32,'+delay+')',delay);
	else if(deltaY > window.innerHeight || deltaY > document.body.offsetHeight)
		doMove((pixelToMove*4),delay);
	else
		timeout = setTimeout('doMove('+pixelToMove+','+delay+')',delay);


}
function move(posY) {
	// getElementStyle("navigation").left = posX;
	getElementStyle("navigation").top = posY;
}
function doHover() {
	stopAll();
	if(!enabled)
		return;
	moving = true;
	timeout1 = setTimeout('hoverMenu()',50);
}
function stopAll() {	
	moving = false;
	if(timeout)
		clearTimeout(timeout);
	if(timeout1)
		clearTimeout(timeout1);
}
function checkCookie() {
	enabled = getCookie('floating')
	if(enabled == "true")
		enabled = true;
	else if(enabled == "false")
		enabled = false;
	else
		enabled = true;
	
	if(enabled) {
		setCookie('floating',enabled, 365, '/', '.team2hoernchen.de', false);
		window.onscroll = doHover;
	} 
}
function setLinkText(myLink, text) {
	myLink.firstChild.nodeValue = text;
}
function setFloating(myPic) {
	myObjs = document.getElementsByName("sticky");
	if(!enabled) {
		enabled = true;
		window.onscroll = doHover;
		doHover();
		for (var i=0; i < myObjs.length; i++) {
			myObjs[i].src = "http://www.team2hoernchen.de/images/sticky1.gif";
		}
		//setLinkText(myLink,"beweglich");		
	} else {
		enabled = false;
		stopAll();
		move(10);
		window.onscroll = null;
		
		for (var i=0; i < myObjs.length; i++) {
			myObjs[i].src = "http://www.team2hoernchen.de/images/sticky2.gif";
		}
		//setLinkText(myLink,"statisch");
	}	
	setCookie('floating',enabled, 365, "/", '.team2hoernchen.de', false);	
}
function setNewMenu() {
	if(getCookie('useNewMenu') == 'true')
		deleteCookie('useNewMenu',"/", '.team2hoernchen.de');	
	else
		setCookie('useNewMenu','true', 365, "/", '.team2hoernchen.de', false);	
	location.reload();
}

function setCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
   var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
checkCookie();
window.onUnload = stopAll;
