ShowProfiles = Class.create();
ShowProfiles.prototype = {
	XmlUrl: '/at/business/data/profiles.xml',
	categories: { 
		
		'homeoffice': 'Heimb&uuml;ro',
		'startup': 'Start-up',
		'mobile': 'Mobilit&auml;t',
		'accounting': 'Buchhaltung',	
		'architecture': 'Architektur',
		'creative': 'Kreativbranche',
		'health': 'Gesundheitswesen',
		'it': 'IT',
		'legal': 'Rechtswesen',
		'realestate': 'Immobilien',
		'retail': 'Einzelhandel'
	},
	
	initialize:function(wrapper, limit) {
		this.wrapper = wrapper;
		
		// classes on the wrapper are passed as parameters
		params = wrapper.classNames() ? wrapper.classNames().collect() : [];
		
		this.category = params[0] ? params[0] : 'all';		
		this.limit = params[1] ? params[1] : null;				
		
		this.results = [];

		try {
			var request = new Ajax.Request(this.XmlUrl, { method: 'get', onSuccess: function(response) { this.handleRequest(response); }.bind(this) });
		} catch(e) { }
	},
	
	handleRequest:function(response) {
		// get the response
		this.xml = response.responseXML.getElementsByTagName('profiles')[0];
		
		// store the results as an array of objects in this.results
		var xmlResults = this.xml.getElementsByTagName('item');
		for(i=0; i<xmlResults.length; i++) {
			var result = xmlResults[i];
			var category = (result.getElementsByTagName('category')[0]) ? result.getElementsByTagName('category')[0].firstChild.nodeValue : false;
			category = (category) ? category.split(',') : false;
			var date = (result.getElementsByTagName('date')[0]) ? result.getElementsByTagName('date')[0].firstChild.nodeValue : false;
			var result = {
				title: (result.getElementsByTagName('title')[0]) ? result.getElementsByTagName('title')[0].firstChild.nodeValue : false,
				category: (category) ? (category.length>1) ? category : category[0] : false,
				date: (date) ? date.split('/') : false,
				description: (result.getElementsByTagName('description')[0]) ? result.getElementsByTagName('description')[0].firstChild.nodeValue.truncate(84, '...') : false,
				url: (result.getElementsByTagName('url')[0]) ? result.getElementsByTagName('url')[0].firstChild.nodeValue : false,
				thumb: (result.getElementsByTagName('thumb')[0]) ? result.getElementsByTagName('thumb')[0].firstChild.nodeValue : false
			};
			// if a category has been specified, only add matching profiles to the results
			if(this.category != 'all') {
				if(typeof(result.category) == 'object') {
					for(j=0; j<result.category.length; j++) {
						if(result.category[j] == this.category) this.results.push(result);
					} 
				} else if(result.category == this.category) this.results.push(result);
			} else this.results.push(result);			
		}
		this.parseResults();
	},
	
	parseResults:function() {
		this.results.sort(this.sortByDate);
		
		// create a template to be applied to all profiles in the results
		var tProfile = new Template(
			'<div class="profile">'+
				'<a href="#{url}"><img src="#{thumb}" height="52" width="52" alt="icon"></a>'+
				'<h4><a href="#{url}">#{title}</a><small>#{date}</small></h4>'+
				'<p>#{description} <a class="more" href="#{url}">Weitere Infos</a></p>'+
			'</div>'
		);

		// only show the requested number of profiles, or all
		Element.removeClassName(this.wrapper, 'loading');
		for(i=0; i<(this.limit ? this.limit : this.results.length); i++) {
			if(this.results[i].date) this.results[i].date = this.results[i].date[1]+'/'+this.results[i].date[2]+'/'+this.results[i].date[0].substr(2);
			this.wrapper.innerHTML += (tProfile.evaluate(this.results[i]));
		} 
					
	},
	
sortByDate: function(a,b) {
		if (a.date[0] > b.date[0]) { return -1; }
		if (a.date[0] < b.date[0]) { return 1; }
		if (a.date[0] == b.date[0]) {
			if (a.date[1] > b.date[1]) { return -1; }
			if (a.date[1] < b.date[1]) { return 1; }
			if (a.date[1] == b.date[1]) {
				if (a.date[2] > b.date[2]) { return -1; }
				if (a.date[2] < b.date[2]) { return 1; }
				if (a.date[2] == b.date[2]) { 
					if (a.date > b.date) { return 1; }
					if (a.date < b.date) { return -1; }
				}
			}
			return 0;
		}
		return 0;
	}
  };
