function doUptime(upSeconds) {
	var uptimeString = " ";
	var secs = parseInt(upSeconds % 60);
	var mins = parseInt(upSeconds / 60 % 60);
	var hours = parseInt(upSeconds / 3600 % 24);
	var days = parseInt(upSeconds / 86400);
	if (days > 0) {
	  uptimeString += days;
	  uptimeString += ((days == 1) ? " day" : " days");
	}
	if (hours > 0) {
	  uptimeString += ((days > 0) ? ", " : "") + hours;
	  uptimeString += ((hours == 1) ? " hour" : " hours");
	}
	if (mins > 0) {
	  uptimeString += ((days > 0 || hours > 0) ? ", " : "") + mins;
	  uptimeString += ((mins == 1) ? " minute" : " minutes");
	}
	if (secs > 0) {
	  uptimeString += ((days > 0 || hours > 0 || mins > 0) ? ", " : "") + secs;
	  uptimeString += ((secs == 1) ? " second" : " seconds");
	}
	var span_el = $("uptimer");//.set('html',uptimeString);
	
	var replaceWith = document.createTextNode(uptimeString);
	span_el.replaceChild(replaceWith, span_el.childNodes[0]);
	upSeconds++;
	setTimeout("doUptime("+upSeconds+")",1000);
}
function Statistics(data) {
	if (data==null) exit;
	var x = data.data;
	var tr = $('content_content').empty();
	var fld = new Element('fieldset').inject(tr);
	var lgnd = new Element('legend').set('html','Server Statistics and Uptime').injectInside(fld);
	var ul = new Element('ul').injectAfter(lgnd);
	var timer;
	x.each(function(ss) {
		var li = new Element('li');
		
		if (ss.Label == 'Current Uptime') {
			var val = new Element('div',{'id':'uptimer'}).set('html',ss.values).injectInside(li);
			timer = ss.value;
		} else var val = new Element('span').set('html',ss.value).injectInside(li);
		var label = new Element('label').set('html',ss.Label).injectAfter(val);
		li.inject(ul);
	});
		doUptime(timer);
}	

	
function characters(data) {
	if (data==null) exit;
	var text = ''; var counter = 0;
	var dt = data.data;
	var x = dt[0]['pop'];
	var tr = $('content_content');
	if (!$('popByClass')) {

		var fld = new Element('fieldset').inject(tr);
		var lgnd = new Element('legend').set('html','Population for Each Character Class').injectInside(fld);
		text += '<table id="popByClass"><thead><tr><th>1st Job</th><th>2nd Job</th><th>3rd Job</th></tr></thead><tbody>';
		
		x.each(function(value, key){value.each(function(v,k) {
			if (counter == 0 ) text += '<tr>';
			text += '<td>'+v+'</td>';
			counter++;
			if (counter == 3 ) {	text += '</tr>';	counter = 0; }
		});	}); 
		text += '  </tbody><tfoot><tr><th>Wizard</th><th>Knight</th><th>Fairy</th><th>Gladiator</th><th>DarkLord</th><th>Summoner</th></tr></tfoot></table>';
		var div = new Element('div').set('html',text).injectAfter(lgnd);
		var chart = new MilkChart.Column('popByClass',{'useFooter':true,'showRowNames':true,'background':'#000','width':'530','height':'200','fontColor':'#fff'});
	}
	var x = dt[0]['level'];
	var x2 = dt[0]['resets'];
	var x3 = []; var cc=0;
	if (!$('popByLevel')) {
	
		var fld = new Element('fieldset').inject(tr);
		var lgnd = new Element('legend').set('html','Population by Levels').injectInside(fld);
		text = '<table id="popByLevel"><thead><tr><th>Totals</th><th>Non-Reseted</th><th>Reseted</th></tr></thead><tbody>';
		x2.each(function(value, key){ value.each(function(v,k) {
			cc++; x3[cc] = v;
		});	}); 
		cc = 0;
		x.each(function(value, key){ value.each(function(v,k) { 
			cc++;
			text += '<tr><td>'+v+'</td><td>'+(v.toInt()-(x3[cc].toInt()))+'</td><td>'+x3[cc]+'</td></tr>';	
		});	}); 
		text += ' </tbody><tfoot><tr><th>1-50</th><th>51-100</th><th>101-50</th><th>151-200</th><th>201-250</th><th>251-300</th><th>301-350</th><th>351-400</th></tr></tfoot></table>';
		var div = new Element('div').set('html',text).injectAfter(lgnd);
		var chart = new MilkChart.Column('popByLevel',{'useFooter':true,'showRowNames':true,'background':'#000','width':'530','height':'200','fontColor':'#fff'});
	}
					
}
	window.addEvent('domready', function(){	
		var notify = new Notimoo();
		new Asset.javascript(site.root+'includes/JS/MilkChart.yc.js', 
					{id: 'Chart_script', 
					onload: function(){
								//var chart = new MilkChart.Column('test2',{'useFooter':true,'showRowNames':true,'background':'#000','width':'300','height':'200','fontColor':'#fff'});
							
							$$('#content_head li').each(function(child) {
								var siblings = child.getParent().getChildren().erase(child);
								child.addEvents({
									mouseenter: function() { siblings.tween('opacity',0.5); this.addClass('active'); },
									mouseleave: function() { siblings.tween('opacity',1); this.removeClass('active'); },
									click: function() {
										notify.show({title: 'Statistics Loading',
											message: this.id.toUpperCase()+' Module is Loading, Please wait a few seconds...'
										});
										addScript(site.location+'statistics.php?callback='+this.id);
										
									}
								});
							
							});
							addScript('statistics.php?callback',true);
							addScript('statistics.php?callback=characters',true);							
					}
		});
	});