	/**
 * Webnews (www.webnews.de) comments syndication - stories list script.
 * @version 2.1 $Date: 2009-02-18 13:11:21 +0100 (Śr, 18 lut 2009) $
 */
WN_stories_list = {

	/**
	 * @param {String} Location host
	 */
	host : '',

	/**
	 * @param {String} Story url
	 */
	apikey : '',

	/**
	 * @param {Bool} Sortable version
	 * @since 2.0
	 */
	sortable : false,

	/**
	 * @param {Object} Sorting information
	 * @since 2.0
	 */
	sorting : { field:'s_date', order:'desc' },

	/**
	 * @param {Object} Displaing fields
	 * @since 2.0
	 */
	display : { comment_count:true, comment_date:true, author:true, s_date:true },

	/**
	 * @param {Object} JSON object returned by remote server
	 */
	json : '',
	
	/**
	 * @param {Int} Comments limit
	 */
	limit : 0,
	
	/**
	 * @param {Int} Minimum comments count
	 */
	minComments : 0,
	
	/**
	 * @param {Int} Time range for stories 
	 */
	timeRange : 0,

	/**
	 * Running script method
	 */
	run : function() {

		this.host = document.location.host;
		if (typeof WN_Host != 'undefined' && WN_Host != '') {
			this.host = WN_Host;
		}

		if (typeof WN_ApiKey != 'undefined' && WN_ApiKey != '') {
			this.apikey = WN_ApiKey;
		}

		if (typeof WN_Sort == 'string' && WN_Sort != '') {

			this.sortable = true;

			matches = WN_Sort.toLowerCase().match(/([^:]+):?(.*)/);
			switch (matches[1]) {
				case 'author':
				case 'title':
				case 's_date':
				case 'c_count':
				case 'c_date':
					this.sorting.field = matches[1];
					break;
			}

			switch (matches[2]) {
				case 'asc':
				case 'desc':
					this.sorting.order = matches[2];
					break;
			}
		}

		if (typeof WN_Display == 'string') {
			for (field in this.display) {
				if (WN_Display.toLowerCase().indexOf(field) == -1) {
					this.display[field] = false;
				}
			}
		}
		
		if (typeof WN_Limit == 'number' && WN_Limit > 0) {
			this.limit = WN_Limit;
		}
		
		if (typeof WN_TimeRange == 'number' && WN_TimeRange > 0) {
			this.timeRange = WN_TimeRange;
		}
		
		if (typeof WN_MinComments == 'number' && WN_MinComments > 0) {
			this.minComments = WN_MinComments;
		}

		this.makeCall();
	},

	/**
	 * Making call to remote server
	 */
	makeCall : function() {

		optionalParams = '';
		if (this.sortable) {
			optionalParams += '&sort=' + this.sorting.field + '&order=' + this.sorting.order;
		}
		if (this.minComments > 0) {
			optionalParams += '&mincomm=' + this.minComments;
		}
		if (this.timeRange > 0) {
			optionalParams += '&timerange=' + this.timeRange;
		}
		document.write('<scr'+'ipt src="http://tools.webnews.de/api_cs/stories?apikey=' + this.apikey + '&host=' + this.host + optionalParams + '"></scr'+'ipt>');
	},

	/**
	 * makeCall() method callback
	 *
	 * @param {Object} json JSON data
	 * @see #makeCall
	 */
	callback : function(json) {
		if (json.host == this.host) {
			this.json = json;
			this.generateOutput();
		}
	},

	/**
	 * Generating HTML output
	 */
	generateOutput : function() {

		if (this.json.data != null) {

			out = '';
			if (!this.sortable) {
				for (i=0; i<this.json.data.length; i++) {

					out += '<li><a href="' + this.json.data[i].url + '">'
						+ '<span class="WN_title">' + this.json.data[i].title + '</span></a>'
						+ '<div class="WN_comment-count"><span>Anzahl Kommentare:</span>' + this.json.data[i].comment_count + '</div></li>';
				}
			} else {

				resultsToShow = this.json.data.length;
				if (this.limit > 0 && this.limit < resultsToShow) {
					resultsToShow = this.limit;
				}
				
				for (i=0; i<resultsToShow; i++) {

					out += '<li><a href="' + this.json.data[i].url + '">'
						+ '<span class="WN_title">' + this.json.data[i].title + '</span></a>';

					if (this.display.s_date) {
						out += '<span class="WN_date">' + this.json.data[i].add_date + '</span>';
					}
					if (this.display.comment_count) {
						out += '<div class="WN_comment-count"><span>Anzahl Kommentare:</span> ' + this.json.data[i].comment_count + '</div>';
					}
					if (this.display.comment_date) {
						out += '<div class="WN_comment-date"><span>Letzte Kommentare:</span> ' + this.json.data[i].comment_date + '</div>';
					}
					if (this.display.author) {
						out += '<div class="WN_comment-author"><span>Author:</span> ' + this.json.data[i].comment_author + '</div>';
					}
					out += '</li>';
				}

			}
			document.write('<ul class="WN_stories">' + out + '</ul>');
		}
	}
}

/* Running script */
WN_stories_list.run();