// be DOM-friendly
if(window.addEventListener) window.addEventListener('load',setupbar,true);
else if(window.attachEvent) window.attachEvent('onload',setupbar);
else window.onload = setupbar;

var rb = null;
var dgn = null;
var mnu = null;
var red_pos = 100;
var tds = new Array();
var mnu_opacity = 0;
var dgn_opacity = 0;
var red_top;
var red_ht;
var rb_visible = true;
var menumask;
var goindex;
var bar_move_increment = 16;
var menu_fade_increment = 6;

var is_IE = navigator.userAgent.match(/MSIE/);
var tm = (is_IE ? 1 : 2);
var mnu_fading = false;

function set_opacity(element, opacity)
{
	if(document.all)
		element.style.filter = 'alpha(opacity=' + Math.round(opacity * 100) + ')';
	else
		element.style.opacity = opacity;
}

function setupbar() {
	rb = document.getElementById('redbar');
	if(!rb)
	{
		rb = document.createElement('div');
		rb.id = 'redbar';
		rb.style.display = 'none';
		rb_visible = false;
		document.getElementById('main').insertBefore(rb, document.getElementById('menu1'));
	}
	if(!document.getElementById('design'))
	{
		var d = document.createElement('div');
		d.id = 'design';
		if(hasSVG)
			d.appendChild(createDesignSVG());
		else
		{
			var di = document.createElement('img');
			di.src= URI_PREFIX + '/img/design.png';
			di.style.width='64px';
			di.style.height='18px';
			di.alt='';
			d.appendChild(di);
		}
		document.getElementById('main').insertBefore(d, rb);
	}	
	var winheight = (window.innerHeight ? window.innerHeight : ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight));
	red_top = winheight - 31;
	red_ht = 28;
	move_top();
}

function move_top() {
	if(red_top < 144)
		red_top = 144;
	rb.style.top = red_top + 'px';
	rb.style.height = red_ht + 'px';
	if(!rb_visible)
	{
		rb.style.display = 'block';
		rb_visible = true;
	}
	if(red_top == 144)
	{
		window.setTimeout(move_bottom, 5);
		return;
	}
	red_top-=bar_move_increment;
	red_ht+=bar_move_increment;
	window.setTimeout(move_top, tm);
}

function move_bottom() {
	if(red_ht == 28)
	{
		dgn = document.getElementById('design');
		set_opacity(dgn, 0);
		dgn.style.display='block';
		window.setTimeout(fadedesign, tm * 2);
		return;
	}
	red_ht-=bar_move_increment;
	rb.style.height = red_ht + 'px';
	window.setTimeout(move_bottom, tm);
}

function fadedesign() {
	dgn_opacity += 0.01;
	set_opacity(dgn, dgn_opacity);
	if (dgn_opacity < 1)
		window.setTimeout(fadedesign, tm * 2);
	if(dgn_opacity > 0.5 && !mnu_fading)
	{
		mnu_fading = true;
		mnu = document.getElementById('menu1');
		goindex = document.getElementById('goindex');
		if(is_IE) menumask = document.getElementById('menumask'); else set_opacity(mnu, 0);
		set_opacity(goindex, 0);
		mnu.style.display='block';
		goindex.style.display='block';
		fademenu();
	}
}
function fademenu() {
	mnu_opacity+=menu_fade_increment;
	if(is_IE)
	{
		menumask.style.filter = 'alpha(opacity=' + (100 - mnu_opacity) + ')';
		goindex.style.filter = 'alpha(opacity=' + mnu_opacity + ')';
	}
	else
	{
		mnu.style.opacity = (mnu_opacity / 100);
		goindex.style.opacity = (mnu_opacity / 100);
	}
	if (mnu_opacity < 100 && (menumask || !is_IE))
		window.setTimeout(fademenu, tm);
	else if(is_IE)
	{
		menumask.parentNode.removeChild(menumask);
		menumask = false;
	}
}

function dummy() {
	mnu = document.getElementById('menu1');
	if(!is_IE)
	{
		mnu.style.display='block';
		set_opacity(mnu, 1);
	}
	dgn = document.getElementById('design');
	dgn.style.display='block';
	set_opacity(dgn, 1);
}

function createDesignSVG() {
	var svg, path;
	svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg:svg');
	svg.setAttribute('width', '64');
	svg.setAttribute('height', '18');
	
	path = document.createElementNS('http://www.w3.org/2000/svg', 'svg:path');
	path.setAttribute('stroke','none');
	path.setAttribute('fill', 'black');
	path.setAttribute('transform', 'matrix(3.8e-2,0,0,-3.92e-2,0,13.36)');
	path.setAttribute('d', 'M 227,297 L 227,62 C 227,25 228,12 231,0 L 200,0 C 198,8 197,15 197,23 L 197,30 C 183,5 165,-5 138,-5 C 87,-5 54,37 54,103 C 54,171 87,216 137,216 C 164,216 181,205 196,181 L 196,297 L 227,297 z M 141,189 C 107,189 86,157 86,105 C 86,54 107,22 141,22 C 175,22 196,54 196,105 C 196,157 175,189 141,189 z M 522,100 L 379,100 C 380,48 402,21 440,21 C 463,21 477,30 488,53 L 517,50 C 504,13 478,-5 439,-5 C 383,-5 347,37 347,103 C 347,172 382,216 437,216 C 489,216 523,174 523,109 C 523,107 523,104 522,100 z M 490,124 C 487,167 468,190 436,190 C 406,190 386,167 380,124 L 490,124 z M 790,153 L 760,153 C 757,178 744,190 717,190 C 693,190 677,177 677,159 C 677,142 686,135 726,123 C 764,112 776,106 785,93 C 791,84 794,73 794,60 C 794,20 765,-5 718,-5 C 667,-5 638,20 638,65 L 638,65 L 668,65 C 671,35 687,20 717,20 C 745,20 762,35 762,58 C 762,79 757,82 702,99 C 661,112 645,127 645,157 C 645,192 675,216 718,216 C 762,216 788,193 790,153 z M 922,210 L 955,210 L 955,0 L 922,0 L 922,210 z M 937,300 L 939,300 C 951,300 961,290 961,277 C 961,264 951,255 938,255 C 925,255 916,264 916,277 C 916,289 925,300 937,300 z M 1268,210 L 1268,187 L 1225,188 C 1237,177 1243,159 1243,140 C 1243,116 1233,97 1217,88 C 1206,82 1194,78 1175,74 C 1139,67 1130,62 1130,51 C 1130,42 1135,40 1172,37 C 1218,32 1232,29 1246,19 C 1259,9 1267,-8 1267,-25 C 1267,-66 1230,-90 1171,-90 C 1114,-90 1076,-67 1076,-30 C 1076,-4 1090,11 1121,22 C 1105,28 1098,35 1098,47 C 1098,61 1108,70 1136,80 C 1107,92 1094,111 1094,141 C 1094,185 1125,213 1171,213 C 1183,213 1192,212 1204,208 L 1268,210 z M 1169,188 C 1143,188 1124,169 1124,142 C 1124,116 1142,98 1169,98 C 1196,98 1214,116 1214,142 C 1214,169 1196,188 1169,188 z M 1169,8 C 1129,8 1105,-5 1105,-28 C 1105,-50 1131,-65 1172,-65 C 1212,-65 1236,-51 1236,-28 C 1236,-4 1213,8 1169,8 z M 1381,210 L 1411,210 C 1415,200 1415,194 1415,180 C 1429,204 1451,216 1479,216 C 1524,216 1546,191 1546,141 L 1546,121 L 1546,0 L 1515,0 L 1515,119 L 1515,134 C 1515,171 1502,189 1473,189 C 1438,189 1417,162 1417,118 L 1417,0 L 1386,0 L 1386,139 L 1386,154 C 1386,189 1386,192 1381,210 z');
	svg.appendChild(path);
	
	return svg;
}
