var Profiel = {
	start: function(){
		$.get("/lib/handler/profielxml.php", function(xml) {
			$(xml).find("data").each(function(){
				Profiel.createList($(this).attr("id"), $(this).find("item"));
			});
		});
	},
	parse: function(){
		var container = $('#profielform');
		container.find("a").click(function(e){
			e.preventDefault();
			Profiel.unsetLists();
			$.get("/lib/handler/profielxml.php", $(this).attr("href"), function(xml) {
				$(xml).find("data").each(function(){
					if($(this).attr("id") == 'profielresult'){
						Profiel.createDownloadList($(this).attr("id"), $(this).find("item"));
					} else {
						Profiel.createList($(this).attr("id"), $(this).find("item"));
					}
				});
			});
		});
	},
	unsetLists: function(){
		$('#profielform').empty();
		$('#downloadlist').hide();
		$('#tooltip').hide();
	},
	createList: function(listId, resultSet){
		var ul = null;
		if($('#'+listId).length) {
			ul = $('#'+listId);
			$(ul).empty();
		} else {
			ul = $('<ul></ul>');
			$(ul).attr('id',listId);
		}
		resultSet.each(function(){
			var a = $('<a></a>').html($(this).text());
			if($(this).attr("active") == 1){ $(a).attr('class', 'active'); }
			$(a).attr('href', $(this).attr("href"));
			$(ul).append($('<li></li>').html($(a)));
		});
		$("#profielform").append($(ul));
		Profiel.parse();
	},
	createDownloadList: function(listId, resultSet){
		var dl = null;
		if($('#'+listId).length) $('#'+listId).empty();
		if($('#downloadlist').length){
			$('#downloadlist').empty();
			dl = $('#downloadlist');
		} else {
			dl = $('<dl></dl>');
			$(dl).attr('id','downloadlist');
		}
		resultSet.each(function(result){
			var txt = $(this).text().replace(/_/gi, ' ');
			var anchor = $('<a></a>').html(txt);
			$(anchor).attr('href','#');
			$(anchor).attr('class','name');
			$(anchor).attr('rel', '<strong>'+ txt +'</strong> '+ $(this).attr('title'));
			var downld = $('<a></a>').html('download het profielbestand');
			$(downld).attr('href', '/downloads.php?file=' + $(this).attr('href'));
			$(downld).attr('title', $(this).text());
			$(dl).append($('<dt></dt>').html($(anchor)));
			$(dl).append($('<dd></dd>').html($(downld)));
		});
		$('#'+listId).append($(dl));
		if($('#downloadlist').length) Profiel.createTips();
		$('#downloadlist').show('normal');
	},
	createTips: function(){
		var posy = 0;
		$('#downloadlist a.name').each(function(){
			$(this).mouseover(function(){
				//if(event) var tiptop = event.pageY;
				var toffset = $(this).offset();
				var boffset = $('#profielform').offset();
				var loffset = $('#downloadlist').offset();
				var margin = (toffset.top - loffset.top) + (loffset.top - boffset.top) + 60;
				$('#tooltip').html($(this).attr('rel'));
				$('#tooltip').css('top', margin + 'px');
				$('#tooltip').mouseover(function(){
					$(this).show();

				});
				$('#tooltip').show('fast');
    		}).mouseout(function(){

      			$('#tooltip').hide();
    		});
		});
		$('#downloadlist').mouseout(function(){ $('#tooltip').hide(); });
	}
};

$(document).ready(function(){
	Profiel.start();
});
