/**
 * Webnews (www.webnews.de) comments syndication - comments list script.
 * @version $Id: comments_list.js 1129 2009-01-19 09:21:07Z dominik $
 */
WN_comments_list = {

	/**
	 * @param {String} Story url
	 */
	url : '',

	/**
	 * @param {String} Api key
	 */
	apikey : '',

	/**
	 * @param {Object} JSON object returned by remote server
	 */
	json : '',

	/**
	 * @param {Bool} Show all comments
	 */
	allComments : false,
	
	/**
	 * @param {Int} Comments limit
	 */
	limit : 0,

	/**
	 * Running script method
	 */
	run : function() {

		this.url = document.location;
		if (typeof WN_Story != 'undefined' && WN_Story != '') {
			this.url = WN_Story;
		}

		if (typeof WN_ApiKey != 'undefined' && WN_ApiKey != '') {
			this.apikey = WN_ApiKey;
		}

		if (typeof WN_AllComments != 'undefined' && WN_AllComments == true) {
			this.allComments = true;
		}
		
		if (typeof WN_Limit == 'number' && WN_Limit > 0) {
			this.limit = WN_Limit;
		}

		this.makeCall();
	},

	/**
	 * Making call to remote server
	 */
	makeCall : function() {
		document.write('<scr'+'ipt src="http://tools.webnews.de/api_cs/list?&apikey=' + this.apikey + '&url=' + this.urlencode(this.url) + (this.allComments ? '&all=1' : '') + '"></scr'+'ipt>');
	},

	/**
	 * Urlencode method
	 *
	 * @param {String} str String to encode
	 * @return Encoded string
	 * @type String
	 * @link http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urlencode/
	 * @author Philip Peterson
	 * @author Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	 */
	urlencode : function(str) {

	    var ret = str;

	    ret = ret.toString();
	    ret = encodeURIComponent(ret);
	    ret = ret.replace(/%20/g, '+');

	    return ret;
	},

	/**
	 * makeCall() method callback
	 *
	 * @param {Object} json JSON data
	 * @see #makeCall
	 */
	callback : function(json) {
		if (json.url == this.url) {
			this.json = json;
			this.generateOutput();
		}
	},

	/**
	 * Generating HTML output
	 */
	generateOutput : function(reload) {

		if (typeof reload == 'undefined') {
			reload = false;
		}

		output = '';
		if (this.json.data != null) {

			if (!reload) {
				document.write('<ul id="WN_comments" class="WN_comments">');
			}
			
			resultsToShow = this.json.data.length;
			if (this.limit > 0 && this.limit < resultsToShow) {
				resultsToShow = this.limit;
			}
			
			for (i=0; i<resultsToShow; i++) {

				output += '<li id="WN_comm' + this.json.data[i].CommID + '">' +
					'<div class="WN_meta">' +
					'<span class="WN_author">von <span>' + this.json.data[i].author + '</span></span> ' +
					'<span class="WN_created">' + this.json.data[i].time_ago + '</span> ';
					output += '</div>' +
						'<span class="WN_text">' + this.json.data[i].content + '</span>';

				if (this.json.data[i].sub.length > 0) {
					output += '<ul class="WN_subcomments">';
					for (j=0; j<this.json.data[i].sub.length; j++) {
						output += '<li>' +
							'<div class="WN_meta">' +
							'<span class="WN_author">von <span>' + this.json.data[i].sub[j].author + '</span></span> ' +
							'<span class="WN_created">' + this.json.data[i].sub[j].time_ago + '</span> ' +
							'</div>' +
							'<span class="WN_text">' + this.json.data[i].sub[j].content + '</span>' +
							'</li>';
					}
					output += '</ul>';
				}
				if (typeof WN_comments_form != 'undefined' && this.json.data[i].storage != 'session') {
						output += '<span class="WN_answer"><a href="#" onclick="WN_comments_form.setParentComment(' + this.json.data[i].CommID + ', \'' + this.json.data[i].author + '\', \'' + this.json.data[i].time_ago + '\'); document.location.hash = \'WN_form\'; return false;"><span>antworten</span></a></span> ';
				}
				output += '</li>';
			}

			if (!reload) {
				document.write(output+'</ul>');
			} else {
				document.getElementById('WN_comments').innerHTML = output;
			}
		}
	}
}

/* Running script */
WN_comments_list.run();