var header = {
	domready: function() {
		header.update_clock();
		header.update();
		
		var res = "<div id='visitorbox' style='display: none; z-index: 1000;'>";
			res += "<div id='visitorbox_ajax' style='padding: 4px;'><center><img src='/img/ajax/01.gif'/></center></div>";
			res += "<div id='visitorbox_list'></div>";
		res += "</div>";
		
		$("#header_visitors").append(res);
		
		$("#header_visitors").disableSelection();
		$("#header_public").disableSelection();
		$("#header_warservers").disableSelection();
		
		//$("#header_visitors").click(function() { header.toggle_visitorbox(); });
		$("#header_public").click(function() { location.href = "/servrar"; });
		$("#header_warservers").click(function() { location.href = "/boka/warserver"; });
		
		$("#visitorbox").click(function(event) { event.stopPropagation(); });
	},
	
	update: function() {
		$.get("/ajax/header", header.cb_update);
	},
	
	cb_update: function(data) {
		a = data.split(" ");
		
		if (a[0] == "OK") {
			$("#header_stats_visitors").html(a[1]);
			$("#header_stats_members").html(a[2]);
			$("#header_stats_public").html(a[3]);
			$("#header_stats_warservers").html(a[4]);
		}
		
		setTimeout(header.update, 5000);
	},
	
	update_clock: function() {
		d = new Date();
		
		r = "";
		
		a = d.getDay()
		if (a == 0) r = "Söndag";
		else if (a == 1) r = "Måndag";
		else if (a == 2) r = "Tisdag";
		else if (a == 3) r = "Onsdag";
		else if (a == 4) r = "Torsdag";
		else if (a == 5) r = "Fredag";
		else if (a == 6) r = "Lördag";
		
		a = d.getDate() + "";
		//if (a.length == 1) a = "0" + a;
		
		r += " " + a + " <span>";
		
		a = d.getMonth();
		if (a == 0) r += "Januari";
		else if (a == 1) r += "Februari";
		else if (a == 2) r += "Mars";
		else if (a == 3) r += "April";
		else if (a == 4) r += "Maj";
		else if (a == 5) r += "Juni";
		else if (a == 6) r += "Juli";
		else if (a == 7) r += "Augusti";
		else if (a == 8) r += "September";
		else if (a == 9) r += "Oktober";
		else if (a == 10) r += "November";
		else if (a == 11) r += "December";
		
		r += "</span> ";
		
		a = d.getHours() + "";
		if (a.length == 1) a = "0" + a;
		r += a + ":";
		
		a = d.getMinutes() + "";
		if (a.length == 1) a = "0" + a;
		r += a;
		
		$("#headerclock").html(r);
		
		setTimeout(header.update_clock, 5000);
	},
	
	toggle_visitorbox: function() {
		if ($("#visitorbox").css("display") == "none") {
			$("#visitorbox").show("fade", 100);
			header.update_visitorbox();
		}
		else {
			$("#visitorbox").hide("fade", 100);
		}
	},
	
	update_visitorbox: function() {
		$("#visitorbox_ajax").show();
		$("#visitorbox_list").hide();
		$.get("/ajax/visitors", header.cb_update_visitorbox);
	},
	
	cb_update_visitorbox: function(data) {
		var res = "";
		var a = data.split("\n");
		
		if (a[0] == "OK") {
			for (var i = 1; i < a.length; i++) {
				var b = a[i].split(",");
				
				var clanid = b[4];
				var clanname = decodeURIComponent(b[5]);
				var clantag = decodeURIComponent(b[6]);
				
				if (i > 1) res += "<div class='visitoruser' style='margin-top: 2px;'>";
				else res += "<div class='visitoruser'>";
					res += "<img src='" + decodeURIComponent(b[3]) + "'/>";
					res += decodeURIComponent(b[2]) + "<br/>";
					
					if (clanid > 0) {
						res += "<span style='font-size: 0.9em;'>" + clantag + "</span>";
					}
				res += "</div>";
			}
		}
		else {
			res = "Ett fel uppstod :(<br/>Försök igen senare...";
		}
		
		$("#visitorbox_list").html(res);
		
		$("#visitorbox_ajax").hide("blind", 100);
		$("#visitorbox_list").show("blind", 200);
	}
};

$(header.domready);


