	
	/* global variables */
	var attributes = {'id':'','referid':''};
	var modifiers = ['', '!first', '!last', '!odd', '!even', 'first', 'last', 'odd', 'even'];
	
	/**
	* Function to parse a xml result in a template and appends to html
	*
	* @author Pieter van Leeuwen
	* @copyright Copyright: Noterik B.V. 2009
	* @access private
	* @param xml XML Object
	* @param type Type of XML
	* @param template template to add results
	* @return appends the result to the document
	* @version $Id:$
	*/	
	function parse(xml, type, template) 
	{	
		/* define variables */
		var results = [];
		var iterator = 0;
		type = type.substr(0,type.length-1);
		var numresults = $(xml).find(type).length;	
		
		/* iterate the xml since it's the resource for each piece of template */
		$(xml).find(type).each(function() 
		{
			/* copy template */
			var marker = $(this);		
			var item = $('#_'+template).clone();
			
			item.filter(function() {
				return this.firstChild;
			})
			.attr('id', '_'+template+'_'+iterator);		
			var html = item[0].innerHTML;
			
			/* handle modifiers */
			var i = modifiers.length;
			if (i > 0) {
				do {
					var start = '%'+modifiers[i]+'%';
					var end = '%'+modifiers[i]+'\\/%';
					var expression = new RegExp(start+'([^\30]+?)'+end, 'gi');
					if (html.match(expression) != null) {
						var remove = false;
						switch (i) {
							case 1: if (iterator == 0) { remove = true; } break;
							case 2: if (iterator == numresults-1) { remove = true; } break;
							case 3:
							case 8: if (iterator % 2 != 0) { remove = true; } break;
							case 4:
							case 7: if (iterator % 2 != 1) { remove = true; } break;
							case 5: if (iterator != 0) { remove = true; } break;
							case 6: if (iterator != numresults-1) { remove = true; } break;
						}
						if (remove) {
							html = html.replace(expression, '');
						} else {
							html = html.replace(new RegExp(start, 'gi'),'').replace(new RegExp(end, 'gi'),'');
						}
					}
				}
				while(--i);
			}
					
			/* loop over all placeholders */
			var placeholders = html.match(/%(\/[^<>\s]+?)%/gi);
			var i = placeholders.length-1;
			if (i >= 0) {
				do {
					var part = placeholders[i].substr(1,placeholders[i].length-2).split("/");				
					var m = marker;
					var attribute;
					var j = 0;
					var k = part.length;
					do {
						if (part[j] in attributes) {
							attribute = m.attr(part[j]);
							break;
						} else {
							m = part[j] == "" ? m : m.find(part[j]);
						}
						j++;
					}
					while(j < k);
					var text = attribute == undefined ? m.text() : attribute;
					if((part == ",properties,presentationscreenshot") || (part == ",properties,chapterscreenshot")) {
						var uriparts = text.split("/");
						text = uriparts[3]+"/"+uriparts[4]+"/"+uriparts[5]+"/"+uriparts[6]+"/"+uriparts[7]+"/"+uriparts[8]+"/"+uriparts[9]+"/"+uriparts[10]+"/"+uriparts[11]+"/"+uriparts[12]+"/"+uriparts[13]+"/"+uriparts[14]+"/"+uriparts[15];
					}
					if(part == ",properties,collection") {
						if((chosencollection == "") && (text != "")) {
							chosencollection = text;
							//alert(chosencollection);
						} else {
							//alert(text);
						}
					}			
					html = html.replace(new RegExp(placeholders[i]), text);
				}
				while(i--);
			}
			results[iterator] = html;	
			iterator++;
		});
		
		/* append result to container */
		$('#'+template)[0].innerHTML = results.join("");
	}