var entriesToShow = 10;

var entries = new Array();
//var active = 0;

$(document).ready(function() {
	$("input").click(function() {
		check($(this).attr("id"));
	});
	$("#wait").ajaxStart(function() {
		$(this).show();
	}).ajaxStop(function() {
		$(this).hide();
	});

	checkAll();
});

// which = "schmuhfblog" etc
function check(which) {
	if($("#"+which).attr("checked")) {
		modCookie(which,true);
		//active++;
		//loading();
		$.get("ajax.php", { action: which }, handleXML);
	} else {
		modCookie(which,false);
		remove(which);
	}
}

/*function loading() {
	if(active>0)
		$("#wait").show();
	else
		$("#wait").hide();
}*/

function checkAll() {
	$("input").each(function() {
		readCookie($(this).attr("id"));
		if($(this).attr("checked"))
			check($(this).attr("id"));
	});
	//loading();
}

function remove(name) {
	for(var i = 0; i < entries.length; i++) {
		if(entries[i].getAttribute("name") == name)
			entries.splice(i--,1);
	}
	display();
}

function handleXML(xml) {
	var name = $("page", xml).attr("name");

	$("entry",xml).each(function() {
	
		var link = $("link",$(this)).text();
		var title = $("title",$(this)).text();
		var email = $("email",$(this)).text();
		var author = $("author",$(this)).text();
		var date = $("date",$(this)).text();
		var content = $("content",$(this)).text();
		var shortcontent = content.substr(0,300);
		var restcontent = content.substr(300);
		var bloglink = $("bloglink",$(this)).text();
		var blog = $("blog",$(this)).text();
		if(blog == "") blog = bloglink;
		var timestamp = parseDate(date);
		var comments = $("comments",$(this)).text();
	
		entries[entries.length]= buildEntry(name, link, title, email, author, date, shortcontent, restcontent, bloglink, blog, timestamp, comments);
	});
	entries.sort(dateSort);
	display();
	
	//active--;
	//loading();
}


function expand(id) {
	$("#content"+id).slideToggle('slow', function() {
		if($(this).css("display")=="block") {
			$(this).css("display", "inline");
		}
	});
	$("#dots"+id).toggle();	
}

function display() {
	$("#entries").empty();
	for(var i = 0; i < Math.min(entriesToShow,entries.length); i++) {
    	$("#entries").append(entries[i]);
	}
	$(".entry").hover(function() {
		$(this).addClass('entryhover');
	}, function() {
		$(this).removeClass('entryhover');
	});
}

function buildEntry(nam, link, title, email, author, date, shortcontent, restcontent, bloglink, blog, timestamp, comments) {
	
	var entry =
		$.create(
			"div", {"class": "entry", "id": timestamp, "name": nam},
			["div", {},
				["a", {"href": bloglink, "class": "blog"},
					[blog]
				],
			"p", {},
				["a", {"href": link, "class": "title"},
					[title]
				],
			"p", {"class": "meta"},
				[	"by ", 
					"a", {"href": email, "class": "author"}, [author],
					", published ",
					"span", {"class": date}, [date],
					", (", comments, ") comments"
				],
			"p", {"id": "shortcontent"+timestamp},
				[
					shortcontent,
					"span", {"id": "dots"+timestamp}, [restcontent==""?"":"..."],
					"span", {"class": "invisible", "id": "content"+timestamp}, [restcontent]
				],
			"p", {},
				["a", {"href": "javascript:expand("+timestamp+")", "class": "backlink"},
					["Read complete article..."]
				]
			]
		);

	return entry;
}

// helpers
function dateSort(a, b) {
	return parseInt(b.getAttribute("id"))-parseInt(a.getAttribute("id"));
}

function parseDate(date) {
	var regexp = /(\d{2})\.(\d{2})\.(\d{4}), (\d{2}):(\d{2}):(\d{2})/;
	regexp.exec(date);
	var d = new Date(RegExp.$3, RegExp.$2, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6);
	return d.getTime();
}

// cookie functions
function modCookie(which, value) {
	var jetzt = new Date();
	var exp = new Date(jetzt.getTime() + 1000 * 60 * 60 * 24 * 365);
	//document.cookie = which+"="+value+"; expires=" + exp.toGMTString() + ";"
	$.cookie(which,value,{expires:exp});
}

function readCookie(which) {
	var val = null;
	if($.cookie(which) == "true")
		val = true;
	if($.cookie(which) == "false")
		val = false;
	$("#"+which).attr("checked",val);
}