RotzyWidget = new function() {
	var BASE_URL = 'http://www.rotzy.com/';
	var STYLESHEET = BASE_URL + 'stylesheets/widget/global.css';
	var CONTENT_URL = BASE_URL + '_widget/content/alpha.js';
	var ROOT = 'rotzy_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(rotzy_img_size) == 'undefined') {
			rotzy_img_size = 75;
		}		
		if (typeof(rotzy_username) == 'undefined') {
			rotzy_username = '';
		}		
		if (typeof(rotzy_latitude) == 'undefined') {
            rotzy_latitude = '';
        }
		if (typeof(rotzy_longitude) == 'undefined') {
            rotzy_longitude = '';
        }
		if (typeof(rotzy_display_id) == 'undefined') {
            rotzy_display_id = 'rotzy_widget_content';
        }
        ROOT = rotzy_display_id;

		script.src = CONTENT_URL + '?username=' + rotzy_username + '&num_rows=' + rotzy_num_rows + '&num_cols=' + rotzy_num_cols + '&border_color=' + rotzy_border_color + '&background_color=' + rotzy_background_color + '&font_color=' + rotzy_font_color + '&image_size=' + rotzy_img_size + '&latitude=' + rotzy_latitude + '&longitude=' + rotzy_longitude;
		// 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();
	}
}

RotzyWidget.init();
