window.onresize = fitdiv;
window.onload = function() {
	var loginbar = document.getElementById('loginbar'),
		login_base = document.getElementById('login_base'),
		searchbar = document.getElementById('searchbar');
	loginbar.style.visibility = 'visible';
	login_base.style.visibility = 'visible';

	searchbar.maxwidth = 174;
	searchbar.minwidth = 16;
	searchbar.pxperframe = 30;
	searchbar.delay = 100;
	searchbar.isopen = false;
	searchbar.animating = false;
	searchbar.controls = document.getElementById('search_controls');

	loginbar.maxwidth = 175;
	loginbar.minwidth = 20;
	loginbar.pxperframe = 30;
	loginbar.delay = 100;
	loginbar.isopen = false;
	loginbar.animating = false;
	loginbar.controls = document.getElementById('login_controls');

	fitdiv();
	document.getElementById('container').style.visibility = 'visible';
}

function fitdiv() {
	var win = windowSize(), newheight = win[1], newwidth = win[0],
		minheight = 500, minwidth = 550, maxwidth = 995,
		topnavheight = 160, scrollbar = 20,
	    t = document.getElementById('topnavi'),
	    c = document.getElementById('container'),
		content = document.getElementById('content'),
	    n = document.getElementById('leftnavi'),
		s = document.getElementById('spacer'),
	    r = document.getElementById('rightcolumn'),
		rtest = "<!--TYPO3SEARCH_begin--><!--TYPO3SEARCH_end-->",
		l = document.getElementById('loginpanel'),
		searchbar = document.getElementById('searchbar'),
		login_base = document.getElementById('login_base'),
		loginbar = document.getElementById('loginbar');
	if(r && (r.innerHTML.length == 0 || r.innerHTML == rtest))
		r.parentNode.removeChild(r);
	if(newheight < minheight || newwidth < minwidth) {
		if(newheight < minheight && newwidth < minwidth) {
			// zu kleine width, zu kleine height
			c.style.width = 300;
			c.style.height = 300;
			t.style.width = minwidth;
			n.style.height = parseInt(c.offsetHeight) - 100;
			s.style.width = parseInt(t.style.width);
		} else if(newheight < minheight) {
			if(newwidth < maxwidth) {
				c.style.width = newwidth - 250 - scrollbar;
				t.style.width = newwidth - scrollbar;
			} else {
				c.style.width = maxwidth - 250;
				t.style.width = maxwidth;
			}
			c.style.height = 300;
			s.style.width = parseInt(t.style.width);
			n.style.height = parseInt(c.offsetHeight) - 100;
		} else if(newwidth < minwidth) {
			c.style.width = 300;
			c.style.height = newheight - topnavheight - scrollbar;
			t.style.width = minwidth;
			s.style.width = parseInt(t.style.width);
			n.style.height = parseInt(c.offsetHeight) - 100;
		}
	}
	if(newheight >= minheight && newwidth >= minwidth) {
		c.style.height = newheight - topnavheight;
		n.style.height = parseInt(c.offsetHeight) - 100;
		if(newwidth < maxwidth) {
			c.style.width = newwidth - 250;
			t.style.width = newwidth;
			s.style.width = parseInt(t.style.width);
		} else {
			c.style.width = maxwidth - 250;
			t.style.width = maxwidth;
			s.style.width = parseInt(t.style.width);
		}
	}
	// don't do it forever
	var i = 0;
	if(r)
		while(parseInt(r.offsetHeight) < parseInt(content.offsetHeight) - 20 && i++ < 200)
			r.style.height = (parseInt(content.offsetHeight) - 20 > 0 ? parseInt(content.offsetHeight) - 20 : 0);
	bar_abortAnimation('searchbar');
	searchbar.style.top = getPageOffset(l)[1];
	bar_abortAnimation('loginbar');
	loginbar.style.top = parseInt(searchbar.style.top) + 40;
	login_base.style.top = parseInt(loginbar.style.top);
	loginbar.controls.style.top = parseInt(loginbar.style.top);
}

function alter_bar(barstr) {
	var searchbar = document.getElementById(barstr);
	if(searchbar.animating)
		bar_abortAnimation(barstr);
	searchbar.isopen = !searchbar.isopen;
	bar_animate(barstr, searchbar.isopen);
}

function bar_animate(barstr, fw) {
	var bar = document.getElementById(barstr);
//	FIRST CALL WHEN OPEN
	if(bar.offsetWidth == bar.maxwidth) {
		bar.controls.style.visibility = 'hidden';
		bar.animating = true;
	}
//	FIRST CALL WHEN CLOSED
	if(bar.offsetWidth == bar.minwidth) {
		bar.style.visibility = 'visible';
		bar.animating = true;
	}
	bar.style.visibility = 'hidden';
	bar.style.width = parseInt(bar.offsetWidth) + parseInt(bar.pxperframe) * (fw ? 1 : -1);
	bar.style.backgroundPosition = 'right top';
	bar.style.visibility = 'visible';
//	COMPLETE APPEARANCE OF THE PANEL.
	if(bar.offsetWidth > bar.maxwidth - bar.pxperframe) {
		bar.animating = false;
		bar.style.width = bar.maxwidth;
		bar.controls.style.visibility = 'visible';
		if(window.to)
			window.clearTimeout(window.to);
	}
//	THE COMPLETE DISAPPEARANCE
	if(bar.offsetWidth < bar.minwidth + bar.pxperframe) {
		bar.animating = false;
		bar.style.width = bar.minwidth;
		bar.style.visibility = 'hidden';
		if(window.to)
			window.clearTimeout(window.to);
	}
	if(bar.animating) window.to = window.setTimeout('bar_animate(\'' + barstr + '\',' + fw + ');', bar.delay);
}

function bar_abortAnimation(barstr) {
	var bar = document.getElementById(barstr);
	if(bar.animating) {
		if(window.timeout)
			window.clearTimeout(window.timeout);
		while(bar.animating)
			bar_animate(barstr, bar.isopen);
	}
}

