/*
	### SOLARIS
	menu javascript
*/

window.addEvent('domready', function() {
	
	// # Subpages
	hideallsubpages();
	if($chk($('register'))) {
		$('register').slide('show');
		$('register').fade('show');
	}
	
	// # Tabslides
	//var tabline = new Accordion($$('.tab'), $$('.element'), {opacity : false});

	// # Ticking Clock
	if($defined($('clock_i'))) {
		var clock = function() {
			var time = h_timer();
			$('clock_i').setAttribute('value', time);	
		};
		clock.periodical(1000);
	}

});

/* ## Portal ## THIS NEEDS SOME REWORK! */

// # Show "Subpages"
function showsubpage(e) {
	hideallsubpages();
	//$$('.subpage').each(function(item, index){$(item).fade('out');});
	$(e).slide('show');
	$(e).fade('in');
}

// # Hide all Subpages
function hideallsubpages() {
	$$('.subpage').each(function(item, index){
		$(item).fade('hide');
		//$(item).set('slide', {duration: 'long'});
		//$(item).slide('out');
		$(item).slide('hide');
	});
}

/* InGame */

// creates a nice little iframe (see wiki)
function show_page(title, page, w, h, param) {
	
	// check if w or h are %ages
	if($type(w) == 'string') {
		value = w.split("%");
		w = (windowsize.x) * (value[0]/100);
		w = w.floor();
	}
	if($type(h) == 'string') {
		value = h.split("%");
		h = (windowsize.h) * (value[0]/100);
		h = h.floor();
	}
		
	// create page body
	x = (windowsize.x/2) - (w/2);
	y = (windowsize.y/2) - (h/2);
	h_window(page, w, h, x, y, 'window_ontop');

	// create the background shadow
	var shade = new Element('div', {'id': 'shade'});
	shade.setStyles({
		width: windowsize.x,
		height: windowsize.y
	});
	shade.set('opacity', '0.75');
	shade.inject($('ajax'));

	// load page-content into an iFrame
	var title = new Element('h1', {'html': title, 'class': page+'_w'});
	title.inject($(page));
	
	// add query if required
	var querystring = '';
	if(param != null && $type(param) == 'hash') {
		querystring += '?';
		param.each(function(value, key) {
			querystring += key+'='+value+'&';
		});
		querystring = querystring.substring(0, querystring.lastIndexOf('&'));
	}
	
	var element = new Element('iframe', {'src': baseurl+page+'.php5'+querystring, 'class': 'element', 'frameborder': 0, 'width': w, 'height': h});
	element.inject($(page));

	var closebutton = new Element('h1', {'html': '[ <a>X</a> ]', 'class': 'close'});
	closebutton.addEvent('click', function(event){
		event = new Event(event).stop();	
		h_window_close(page);
	});
	closebutton.inject($(page));

}

// # Helper functions #

function h_window(ident, w, h, x, y, class) {
	
	h_window_close(ident);

	var el = new Element('div', {'id' : ident, 'class' : 'box box_white'});
	if(class != null) {el.addClass(class)};
	el.setStyles({
		position: 'fixed',
		width: w,
		height: h,
		top: y,
		left: x
	});
	el.inject($('ajax'));
	$(ident).fade('in');

}

// closes a window
function h_window_close(ident) {
	
	if( $defined($(ident)) ) {
		$(ident).fade('out');
		$(ident).empty();
		$(ident).destroy();
	}

	// if it was a window
	if( $defined($('shade')) ) {
		$('shade').destroy();
	}
}

function h_timer() {

	var now = new Date();
	
	var h = now.getHours();
	var m = now.getMinutes();
	var s = now.getSeconds();

	if(h == 0) { hrs=12 }
	if(h < 10) { h="0"+h }
	if(m < 10) { m="0"+m }
	if(s < 10) { s="0"+s }

	var time = h+':'+m+':'+s;
	return time;
	
}


