function everlaterWidget(type) {
	var that = this;
	this.root = "http://www.everlater.com/wdgt";
	this.base_height = 400;
	this.widget;
	this.height;
	this.width;
	this.widget_index;
	this.place = function(){
		if (!window.everlaterWidgets) {
			window.everlaterWidgets = new Array();
		}
		this.widget_index = window.everlaterWidgets.length;
		document.write('<div id="everlaterWidget' + this.widget_index + '"></div>');
		this.widget = document.getElementById('everlaterWidget' + this.widget_index);
		this.location = window.location.href;
		window.everlaterWidgets.push(that);
	}
	this.place();
	
	// =====================================================================			standard widget functions
	this.embed = function(){
		if(this.opts.width && this.opts.height) { // user has set these in options
			this.width = this.opts.width;
			this.height = this.opts.height;
			//this.widget.style.width = this.width + "px";
		}else{
			this.width = this.widget.offsetWidth - 10;
			if (this.width <= 260 && !this.opts.force_photos) this.opts.hide_photos = true;
			if (this.width <= this.base_height) {
				this.height = this.width;
			}else{
				this.height = this.base_height;
			}
		}
		
		this.widget.style.padding = '5px';
		this.widget.fontFamily = '"Verdana", "Arial"';
		this.widget.innerHTML += '<a href="http://www.everlater.com/" id="everlaterWidget' + this.widget_index + '-title" style="text-decoration: none; font-size: 20px;">Loading...</a>';
		this.widget.innerHTML += '<iframe src="'+ this.root + this.url + '/' + this.width + 'x' + this.height + '?' + this.opts_to_url() + '" width="' + this.width + '" height="' + this.height + '" border="0" style="border: 0 !important; margin-top:5px;"><p>SEO Here</p></iframe>';
		this.widget.innerHTML += '<div style="float:right; text-align:right;"><p>This trip was created on <br /><a href="http://www.everlater.com/"><img src="http://www.everlater.com/images/widgets/footer_logo.png" border="0" style="border: 0;"></a></p></div>';
		this.embed_extend();
		this.widget.innerHTML += '<div style="clear:both;"></div>';
		return this;
	}
	this.preview = function(){
		this.widget.innerHTML = '<div id="everlater_preview" style="width: ' + this.width + 'px; height: ' + this.height + 'px; background: #DBDBDB;"></div>'
		return this;
	}
	this.get_json = function(url) {
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = this.root + url;
		document.getElementsByTagName('head')[0].appendChild(script);
		return script;
	}
	this.update_title = function(text, url) {
		title = document.getElementById('everlaterWidget' + this.widget_index + '-title');
		title.href = url;
		title.innerHTML = text;
	}
	this.opts_to_url = function() {
		str = new String();
		arr = new Array();
		this.opts.from = window.location;
		for (opt in this.opts) {
			if(this.opts[opt] && (opt != 'width' && opt != 'height')) arr.push("wdgt_" + opt + "=" + this.opts[opt]);
		}
		for(i = 0; i < arr.length; i++){
			str += arr[i];
			if(i != arr.length-1) str += "&";
		}
		//str += "&from=" + this.location;
		return str;
	}
	that.set_size = function(size) { // pass in width x height, like 800x600
		that.opts.width = size.split("x")[0];
		that.opts.height = size.split("x")[1];
		return that;
	}
	this.embed_extend = function() { /* fill this with extensions to embed from special actions */ }
	
	// =====================================================================			specific widget modifications
	var trip = function(id) {
		that.url = "/trip/" + id;
		that.get_data = function() {
			this.get_json('/trip/' + id + '.json?index=' + this.widget_index);
		}
		//that.get_data();
		that.receive_data = function(data){
			that.update_title(data.title, data.url);
			link = document.getElementById('everlaterWidget' + this.widget_index + '-footlink');
			link.href = data.url;
			link.innerHTML = data.title;
		}
		that.opts = {
			hide_photos:false,
			force_photos:false,
			width:false,
			height:false,
			show_feed: false
		}
		that.embed_extend = function() {
		  that.widget.innerHTML += '<div style="float:left;"><p>See the trip in full:<br /><a href="#" id="everlaterWidget' + this.widget_index + '-footlink">Loading...</a></p></div>';
			that.get_data();
		}
		that.hide_photos = function() {
			that.opts.hide_photos = true;
			return that;
		}
		that.force_photos = function() {
			that.opts.force_photos = true;
			return that;
		}
		that.show_feed = function() {
			that.opts.show_feed = true;
			that.opts.width = that.widget.offsetWidth - 10;
			that.opts.height = that.opts.width + 150;
			return that;
		}
		return that;
	}
	
	var book = function(id) {
		that.url = "/book/" + id;
		that.opts = {
			width:false,
			height:false
		};
		that.get_data = function() {
			this.get_json('/book/' + id + '.json?index=' + this.widget_index);
		}
		that.get_data();
		that.receive_data = function(data){
			that.update_title(data.title + " Book", data.url);
		}
		return that;
	}
	
	var segment = function(id) {
		that.url = "/segment/" + id;
		that.get_data = function() {
			this.get_json('/segment/' + id + '.json?index=' + this.widget_index);
		}
		that.get_data();
		that.posts;
		that.receive_data = function(data){
			that.update_title(data.title + " (" + data.dates.start + " - " + data.dates.end + ")", data.url);
			if (that.opts.include_posts) {
				that.posts = data.posts;
				posts_container = document.getElementById('everlaterWidget' + that.widget_index + '-posts');
				
				for (i = 0; i < that.posts.length; i++) {
					posts_container.innerHTML += '<h2>' + that.posts[i].title + '</h2>';
					posts_container.innerHTML += '<p>' + that.posts[i].body + '</p>';
				}
			}
		}
		that.opts = {
			hide_photos:false,
			width:false,
			height:false,
			include_posts:false
		}
		that.hide_photos = function() {
			that.opts.hide_photos = true;
			return that;
		}
		that.include_posts = function() {
			that.opts.include_posts = true;
			return that;
		}
		
		that.embed_extend = function() {
			that.widget.innerHTML += '<div id="everlaterWidget' + that.widget_index + '-posts"></div>';
		}
		return that; 
	}
	
	// =====================================================================			handler for different widgets
	if (type == 'trip') {
		return trip;
	}else if(type == 'segment'){
		return segment;
	}else if(type == 'book'){
		return book;
	}else{
		return function(x){
			that.embed = function(){
				alert('invalid widget type selected');
			}
			return that;
		}
	}
}