ListiaWidget = new function() {
	var BASE_URL = 'http://www.listia.com/';
	var STYLESHEET = BASE_URL + 'stylesheets/widget.css';
	var CONTENT_URL = BASE_URL + 'widget/alpha';
	var ROOT = 'listia_widget_content';

	function requestStylesheet(stylesheet_url) {
		stylesheet = document.createElement("link");
		stylesheet.rel = "stylesheet";
		stylesheet.type = "text/css";
		stylesheet.href = stylesheet_url;
		stylesheet.media = "all";
		document.lastChild.firstChild.appendChild(stylesheet);
	}

	function requestContent( local ) {
		var script = document.createElement('script');
		// How you'd pass the current URL into the request
		// script.src = CONTENT_URL + '&url=' + escape(local || location.href);
		// default to 75px width/height if not set
		if (typeof(listia_width) == 'undefined') {
			listia_width = 200;
		}		
		if (typeof(listia_username) == 'undefined') {
			listia_username = 'listia';
		}		
		if (typeof(listia_display_id) == 'undefined') {
            listia_display_id = 'listia_widget_content';
        }
        ROOT = listia_display_id;

		script.src = CONTENT_URL + '?username=' + listia_username + '&num_rows=' + listia_num_rows + '&border_color=' + listia_border_color + '&background_color=' + listia_background_color + '&font_color=' + listia_font_color + '&width=' + listia_width;
		// IE7 doesn't like this: document.body.appendChild(script);
		// Instead use:
		document.getElementsByTagName('head')[0].appendChild(script);
	}

	this.serverResponse = function( data ) {
		if (!data) return;
		var div = document.getElementById(ROOT);
		var txt = '';
		for (var i = 0; i < data.length; i++) {
			if (data[i].length > 0) { 
				txt += data[i];
            }
		}
		div.innerHTML = txt;  // assign new HTML into #ROOT
		div.style.display = 'block'; // make element visible
	}
	
	this.init = function() {
		requestStylesheet(STYLESHEET);
		requestContent();
	}
}

ListiaWidget.init();
