function truebody() {
	return document.getElementsByTagName('body')[0]
}

function showAlt(ele, customtext) {
	var img = document.createElement("div")
	img.id = 'hovertitle'
	img.innerHTML = ele.alt

	if(customtext != undefined)
		img.innerHTML = customtext

	truebody().appendChild(img)
	document.onmousemove = function(e) { 
		coords = new Array();
		coords = followmouse(e, 10, 10).split("-")
		img.style.left = coords[0] + "px"
		img.style.top = coords[1] + "px"
	}

	document.onmousemove();

	ele.onmouseout = function() {
		ele.onmouseout = ''
		document.onmousemove = ''
		truebody().removeChild(img)
	}
}

function followmouse(ele, start_x, start_y){
	var xcoord = start_x
	var ycoord = start_y
	if (typeof ele != "undefined") {
		xcoord += ele.pageX
		ycoord += ele.pageY
	} else if (typeof window.event !="undefined") {
		xcoord += truebody().scrollLeft + event.clientX
		ycoord += truebody().scrollTop + event.clientY
	}

	return (xcoord+'-'+ycoord)
}

function getWinSize(win)
{
    if(!win) win = window;
    var s = new Object();
    if(typeof win.innerWidth != 'undefined')
    {
        s.width = win.innerWidth;
        s.height = win.innerHeight;
    }
    else
    {
         var obj = getBody(win);
         s.width = parseInt(obj.clientWidth);
         s.height = parseInt(obj.clientHeight);
    }
    return s;
}

function popup(newurl, width, height) {
	attrib = 'height=' + height + ',width=' + width + ',scrollbars=yes,menubar=no, top=0, left=0';
	var fenster = window.open('../layout/magic.shtml?img=' + newurl, 'win', attrib);
}

function imageLoad(img) {

	var imgwidth = img.offsetWidth;
	var imgheight = img.offsetHeight;

	if(window.outerHeight) {
		imgheight += (Math.floor(window.outerHeight) - Math.floor(document.body.offsetHeight));
		imgwidth += (Math.floor(window.outerWidth) - Math.floor(document.body.offsetWidth));
	}
	else {
		imgwidth += 10;
		imgheight += 50;
	}

	if(imgheight > screen.height)
	{
		imgheight = screen.height;
	}

	if(imgwidth > screen.width)
	{
		imgwidth = screen.width;
	}

	window.resizeTo(Math.floor(imgwidth), Math.floor(imgheight));

	var screenheight = (screen.height/2);
	var screenwidth = (screen.width/2);

	if(window.outerHeight) {
		var windowheight = (window.outerHeight/2);
		var windowwidth = (window.outerWidth/2);
	}
	else {
		var windowheight = (imgheight/2);
		var windowwidth = (imgwidth/2);
	}

	var newy = Math.round(screenheight - windowheight)
	var newx = Math.round(screenwidth - windowwidth)
	if(newx < 0)
		newx = 0
	if(newy < 0)
		newy = 0

	window.moveTo(newx, newy);

	var loading = document.getElementById('loading');
	if(loading)
		loading.style.display='none'
		
	var popup = document.getElementById('popup');
	if(popup)
		popup.style.overflow='auto'
}

function addCode(id, text)
{
	document.getElementById(id).value = document.getElementById(id).value + text;
}

function initSidebar() {
	var sidebar = document.getElementById('sidebar');
	if(sidebar) {
		var links = sidebar.getElementsByTagName('a');
		for(i=0; i<links.length; i++) {
			links[i].onmouseover = function() {
				var img = this.getElementsByTagName('img')[0];
				if(img) {
					if(img.src.indexOf('_hover') == -1)
						img.src = img.src.split('.png').join('_hover.png');
				}
			}

			links[i].onmouseout = function() {
				var img = this.getElementsByTagName('img')[0];
				if(img) {
					if(img.src.indexOf('_hover') > -1)
						img.src = img.src.split('_hover.png').join('.png');
				}
			}
		}
	}
}

function initPopup() {
	var popuplink = document.getElementById('popuplink');
	if(popuplink) {
		var params = window.location.search.split('=');
		popuplink.innerHTML = '<img src="' + params[1] + '" alt="" onload="imageLoad(this)" />';
		window.moveTo( (screen.width/2)-100, (screen.height/2)-100 );
	}
}

window.onload = function() {
	initSidebar();
	initPopup();
}