
/*
 * returns the URL of the current displayed site without URL parameters
 * Hope this one works ;-)
 */
function getCurrentURL()
{
	var url = window.location.href;
	var pos = url.indexOf('?');
	if(pos == -1)
		return url;
	return url.substr(0, pos);
}

/*
 * Animation function for the dekoBox divs moving around the header image
 */
function moveDekoBoxAroundImage(dekoBoxId, curLeft, curTop, moveDir, speed)
{
	switch(moveDir)
	{
		case "D":
			curTop++;
			break;
		case "U":
			curTop--;
			break;
		case "L":
			curLeft--;
			break;
		case "R":
			curLeft++;
			break;
	}

	var dekoBox = document.getElementById(dekoBoxId);
	dekoBox.style.left = curLeft + "px";
	dekoBox.style.top = curTop + "px";
	
	if(curLeft == -7 && curTop == 323)
		moveDir="R";
	if(curLeft ==673 && curTop == 323)
		moveDir="D";
	if(curLeft ==673 && curTop == 643)
		moveDir="R";
	if(curLeft ==792 && curTop == 643)
		moveDir="U";
	if(curLeft ==792 && curTop == 121)
		moveDir="L";
	if(curLeft ==-7 && curTop == 121)
		moveDir="D";
	
	window.setTimeout("moveDekoBoxAroundImage('" + dekoBoxId + "', " + curLeft + ", " + curTop + ", '" + moveDir + "', " + speed + ")", speed);
}


function resizeDekoBox(dekoBox, makeBigger)
{
	var l = dekoBox.offsetLeft;
	var t = dekoBox.offsetTop;
	var w = dekoBox.clientWidth;
	var h = dekoBox.clientHeight;
	if(makeBigger)
	{
		dekoBox.style.width = w * 2 + "px";
		dekoBox.style.height = h * 2 + "px";
		dekoBox.style.left = (l - w/2) + "px";
		dekoBox.style.top = (t - h/2) + "px"; 
	}
	else
	{
		dekoBox.style.width = w / 2 + "px";
		dekoBox.style.height = h / 2 + "px";
		dekoBox.style.left = (l + w/4) + "px";
		dekoBox.style.top = (t + h/4) + "px";	}
}

var moveDekoBoxLeftRightAddFlag = false;
var dekoBoxLeftRight = null;
function moveDekoBoxLeftRight(dekoBox, distance)
{
	if(dekoBoxLeftRight != null)
		return;
	dekoBoxLeftRight = dekoBox;
	var l = dekoBox.offsetLeft;
	if(moveDekoBoxLeftRightAddFlag)
		moveDekoBoxLeftRightTimer(l, 5, l+distance);
	else
		moveDekoBoxLeftRightTimer(l, -5, l-distance);
	moveDekoBoxLeftRightAddFlag = !moveDekoBoxLeftRightAddFlag;
}

function moveDekoBoxLeftRightTimer(start, step, end)
{
	if(start == end)
	{
		dekoBoxLeftRight = null;
		return;
	}
	
	start = start + step;
	dekoBoxLeftRight.style.left = start + "px";
	window.setTimeout("moveDekoBoxLeftRightTimer(" + start + ", " + step + ", " + end + ")", 10);
}

/*
 * Return the browser width of the client (whatever that may be ;-))
 */
function getClientWindowWidth() 
{
	if(window.innerWidth != 0)
		return window.innerWidth;
	if(document.documentElement && document.documentElement.clientWidth != 0)
		return document.documentElement.clientWidth;
	if(document.body && document.body.clientWidth != 0)
		return document.body.clientWidth;
	return 800;
}

/*
 * Return the browser height of the client (whatever that may be ;-))
 */
function getClientWindowHeight() 
{
	if(window.innerHeight != 0)
		return window.innerHeight;
	if(document.documentElement && document.documentElement.clientHeight != 0)
		return document.documentElement.clientHeight;
	if(document.body && document.body.clientHeight != 0)
		return document.body.clientHeight;
	return 600;
}

/*
 * Returns a new instance of XMLHttpRequest - browser dependent behaviour encapsulation.
 */
function getNewXMLHttpRequest()
{
	// Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
	if (typeof XMLHttpRequest != 'undefined')
	{
		return new XMLHttpRequest();
	}
	
    // Internet Explorer 6 und �lter
    try 
    {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch(e) {}
    
    try 
    {
    	return new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e) {}
    
	alert("This browser cannot work with asynchronous javascript calls using the XMLHttpRequest object!\nDamn man, how old is your computer??? Go out and buy a new one!");
    return null;
}

function slidePanorama(plid, start, current, step, stop)
{
	if(plid != panoramaLoopId)
		return;
		
	if(stop == 0)
		stop = document.getElementById('slideImage').width - 600;
		

	var navImageItem = document.getElementById("navimagediv");
	navImageItem.style.left = "-" + current + "px";
	
	current = current + step;
	if(current >= stop)
	{
		current = stop;
		step = -step;
	}
	if(current <= start)
	{
		current = start;
		step = -step;
	}
	
	window.setTimeout("slidePanorama(" + plid + ", " + start + ", " + current + ", " + step + ", " + stop + ")", 50);
}

var panoramaLoopId = 0;
/*
 * Animates a change in the header picture
 */
function changeHeader()
{
	var navImageItem = document.getElementById("navimagediv");
	panoramaLoopId = Math.random();
	navImageItem.innerHTML = "<img id='slideImage' src='img/panorama/" + Math.floor(Math.random()* 10) + ".jpg' alt='' onload='slidePanorama(" + panoramaLoopId + ", 0, 0, 1, 0);'/>";
}

function changeTitleImage(id)
{
	var titleImageItem = document.getElementById("navimagetitle");
	titleImageItem.style.backgroundImage = "url(img/font/" + id + ".png)";
}

/**
 * @param page page id to load
 * @param getParams used when posting params with GET
 * @param postParams used when posting params with POST (also GET params in URL are allowed)
 */
function loadPage(page, getParams, postParams)
{
	var parameters = "page=" + page  + "&partially=true";
	if(getParams != null)
		parameters += "&" + getParams;
	
	changeTitleImage(page);
	
	if(postParams == null)
		makeGetRequest(getCurrentURL(), parameters);
	else
		makePostRequest(getCurrentURL(), parameters, postParams);
}

/*
 * Requests new content for the content div from the server and replaces the content with a loading gif in the meantime
 * Also triggers a change of the header div image
 */
function makeGetRequest(requestURL, params)
{
	var contentElement = document.getElementById("content");
	contentElement.innerHTML = "<br/><br/><br/><br/><center><img src='img/loading.gif' alt='' title='page is loading - please wait!'/></center>";

	var request = getNewXMLHttpRequest();
	if(params != null)
		requestURL += "?" + params;

	request.open('GET', requestURL, true);
    request.onreadystatechange = function () 
    {
        if (request.readyState == 4)
        {
        	contentElement.innerHTML =  request.responseText;
        	if(params.substring(0, 10) == "page=track")
        		initGPXViewer();
		}
    };
    request.send(null);
}

function makePostRequest(requestURL, getParams, postParams) 
{
	var contentElement = document.getElementById("content");
	contentElement.innerHTML = "<br/><br/><br/><br/><center><img src='img/loading.gif' alt='' title='page is loading - please wait!'/></center>";
	
	var request = getNewXMLHttpRequest();
	if(getParams != null)
		requestURL += "?" + getParams;
    
	request.onreadystatechange = function ()
    {
        if (request.readyState == 4)
        {
        	contentElement.innerHTML =  request.responseText;
		}
    };
    request.open('POST', requestURL, true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", postParams);
    request.setRequestHeader("Connection", "close");
    request.send(postParams);
}

/*
 * Opens a single popup window and resizes the picture accordingly.
 * On click, it closes again. 
 */
function openSinglePictureResizedWindow(picPath, width, height)
{
	var w = 0;
	var h = 0;
	var cWidth = getClientWindowWidth();
	var cHeight = getClientWindowHeight();
	if(width * cHeight > height * cWidth)
	{
		if(width > cWidth)
		{
			w = cWidth;
			h = height * cWidth / width;
		}
		else
		{
			w = width;
			h = height;
		}
	}
	else
	{
		if(height > cHeight)
		{
			h = cHeight;
			w = width * cHeight / height;
		}
		else
		{
			h = height;
			w = width;
		}
	}

	var win = window.open("", "", "menubar=no,toolbar=no,location=no,titlebar=no,status=no,resizable=yes,fullscreen=yes,scrollbars=yes");
	win.document.write("<html><head><title>Full-size picture (" + width + "x" + height + ")</title></head><body style='margin:  0px 0px 0px 0px; background-color: #000;'><center>");	
	win.document.write("<img src='" + picPath + "' width='" + w + "' height='" + h + "' alt='' title='Click to close and return' onclick='window.close()'/>");
	win.document.write("</center></body></html>");
	win.document.close();
}

/*
 * Opens a single popup window and uses the client's window height as reference
 * On click, it closes again. 
 */
function openSinglePictureWindow(picPath)
{
	var cHeight = getClientWindowHeight();

	var win = window.open("", "", "menubar=no,toolbar=no,location=no,titlebar=no,status=no,resizable=yes,fullscreen=yes,scrollbars=yes");
	win.document.write("<html><head><title>Full-size picture</title></head><body style='margin:  0px 0px 0px 0px; background-color: #000;'><center>");	
	win.document.write("<img src='" + picPath + "' height='" + cHeight + "' alt='' title='Click to close and return' onclick='window.close()'/>");
	win.document.write("</center></body></html>");
	win.document.close();
}

/*
 * Changes the state of the loginIcon
 * 0 = not logged in, 1 = logged in, 2 = waiting
 */
function changeLoginIcon(type)
{
	var icon = document.getElementById('loginicon');
	switch(type)
	{
		case 0:
			icon.src='img/locked.png';
			icon.alt='login';
			icon.title='login';
			icon.onclick=function(){ loginClicked(false); };
			break;
		case 1:
			icon.src='img/unlocked.png';
			icon.alt='logout';
			icon.title='logout';
			icon.onclick=function(){ loginClicked(true); };
			break;
		case 2:
			icon.src='img/miniloading.gif';
			icon.alt='waiting';
			icon.title='checking...';
			icon.onclick=function(){};
			break;
	}
}

function loginClicked(isLoggedIn)
{
	changeLoginIcon(2);
	var request = getNewXMLHttpRequest();
	if(isLoggedIn)
	{
		request.open('GET', getCurrentURL() + '?action=logout', true);
		request.onreadystatechange = function () 
    	{
        	if (request.readyState == 4)
        	{
        		changeLoginIcon(0);
			}
    	};
	}
	else
	{
		var pass = prompt('Authorization requested!', '');
		request.open('GET', getCurrentURL() + '?action=login&pass=' + pass, true);
		request.onreadystatechange = function () 
    	{
        	if (request.readyState == 4)
        	{
        		if(request.responseText == 'true')
        			changeLoginIcon(1);
        		else
        			changeLoginIcon(0);
        		
			}
    	};
	}
	request.send(null);
}
