(function()
{
	var home			= "http://www.feedbeater.com/"
	var staticdir	= "http://static.diffbot.com/";
	var imgdir		= staticdir + "images/apps/feedbeater/";
	var url			= encodeURIComponent(location.href);
	var icon			= imgdir + "rss.png";
	var text			= '&nbsp;';
	var addOptions	= 
	[
		{name:"diffbot", img:imgdir + "diffbot.gif", href:"http://www.diffbot.com/inbox/add?url=" + url},
		{name:"bloglines", img:imgdir + "bloglines.gif", href:"http://www.bloglines.com/sub/" + url},
		{name:"yahoo", img:imgdir + "yahoo.gif", href:"http://add.my.yahoo.com/content?.intl=us&amp;?url=" + url},
		{name:"newsgator", img:imgdir + "newsgator.gif", href:"https://www.newsgator.com/ngs/si.aspx?url=" + url},
		{name:"google", img:imgdir + "google.gif", href:"http://www.google.com/ig/add?feedurl=" + url},
		{name:"rss", img:imgdir + "rss.png", href:"http://api.diffbot.com/rss/" + location.href}
	]

	// lava libraries for common javascript operations
	var lava = {};

	lava.is =
	{
		defined: function(a)
		{
		     return (typeof(a) != 'undefined' && a != null);
		},
		
		'number': function(n)
		{
		     return (typeof(n) == 'number' && !isNaN(n));
		},
		
		'string': function(s)
		{
		     return (typeof(s) == 'string');
		},

		element: function(n)
		{
			return (n && n.nodeType == 1) == true;
		}
	}	

	lava.make = 
	{
		html: function(text, d)
		{
			var tag     = text.match(/<(div|table)\s*[^>]*>(.*)<\/\1>/im) ? '<div>' : '<span>';
			var y       = lava.make.element(tag, lava.get.document(d));
			y.innerHTML = text;
			var first   = y.childNodes[0];
			
			return y.childNodes.length == 1 ? lava.is.element(first) ? first : y : y;
		},

		element: function(e, d)
		{
		     d = lava.get.document(d);
		     e = e.match(/^<([A-Za-z]\w*)>$/);
		
		     return (d.createElement && e) ? d.createElement(e[1]) : null;
		}
	}

	lava.get =
	{
	     byid: function(id, doc)
	     {
	          doc = self.document;
	          return doc.getElementById ? doc.getElementById(id) : (doc.all) ? doc.all[id] : null;
	     },

		  'document': function(d) { return self.document; }
	}

	lava.event =
	{
	     _addWrapper: function(e, c)
	     {
	          function f(ev)
	          {
	               ev                          = ev || window.event;
	               ev.rightclick               = (ev.which) ? (ev.which == 3) : (ev.button == 2);
	               ev.leftclick               = !ev.rightclick;
	
	               try
	               {
	                    if(!ev.currentTarget)
	                         ev.currentTarget = e;
			
							 if(!ev.target)
                          ev.target = ev.srcElement;
	
	                    if(!lava.is.defined(ev.keyCode))
	                         ev.keyCode = ev.which;
	           
	                    if(!ev.stopPropagation)
	                          ev.stopPropagation = function() { window.event.cancelBubble = true; };
	
	                    if(!ev.preventDefault)
	                         ev.preventDefault     = function() { ev.returnValue = false; };
	               }
	               catch(err)
	               {
	                     //oh well at least we tried
	               }
	           
	               return c(ev);
	          };
	
	          return f;
	     },
	
	     add: function(e, t, f, c, l)
	     {
				 var id		= lava.event.list.length;
	          f          = f || false;
	          l          = l || lava.event.list;
	          f          = lava.event._addWrapper(e, f);
	
	          l[id] = [e, t, f, c];
	
	          if(e.addEventListener)
	               e.addEventListener(t, f, c);
	          else if(e.attachEvent)
	               e.attachEvent('on'+t, f);
	          else
	               eval('e.on'+t+'=f');
	
	          return id;
	     },
	
	     list: {}
	}

	lava.css =
	{
	     style:               function(e, s, a)
	     {
	          var l = {};
	          var q = lava.css;
	
	          for(var i in s)
	              l[i] = q._style(e, i, s[i], a);
	
	          return l;
	     },
	
	     _style:               function(e, s, v, a)
	     {
	          var q = lava.css;
	
	          switch(s)
	          {
	               // special cases that need extra attention
	               case 'height':          v = q._height(e, v);                              break;
	               case 'width':          v = q._width(e, v);                              break;
	               case 'float':          v = q._apply(e, 'css-float', v) || q._apply(e, 'style-float', v); break;
	               
	               // most css styles can be found generically
	               default:                     v = q._apply(e, s, v);                         break;
	          }
	
	          return v;
	     },
	
	     _camelize:           function(s)
	     {
	          return s.replace(/-[a-z]/g, function(w) { return w.substr(1).toUpperCase();});
	     },
	
	     _apply:                function(e, s, v)
	     {
	          var c = lava.css._camelize;
	          var d = document.defaultView;
	          var y = e.currentStyle;
	
	          if(e.style)
	          {
	               if(lava.is.defined(v))
	               {
	                    e.style[c(s)] = (lava.is.string(v)) ? v : (s != 'z-index' && s != 'opacity') ? (v += 'px') : v;
	               }
	               else
	               {
	                    v = e.style[c(s)];
	               
	                    if (!v)
	                    {
	                          if (d && d.getComputedStyle)
	                          {
	                              var css      = d.getComputedStyle(e, null);
	                              v               = css ? css[c(s)] : null;
	                          }
	                          else if (y)
	                               v           = y[c(s)];
	                    }
	               }
	          }
	
	          return v;
	     },
	
	     _width:                function(e, w)
	     {
	          var x = e.offsetWidth;
	
	          if(e.style && lava.is.defined(x))
	          {
	               if(lava.is.defined(w))
	                    e.style.width = lava.is.number(w) ? w + 'px' : w;
	               else
	                    w = x;
	          }
	          
	          return w;
	     },
	
	     _height:                function(e, h)
	     {
	          var x = e.offsetHeight;
	
	          if(e.style && lava.is.defined(x))
	          {
	               if(lava.is.defined(h))
	                    e.style.height = lava.is.number(h) ? h + 'px' : h;
	               else
	                    h = x;
	          }
	          
	          return h;
	     },
	     
	     _offset:           function(p)
	     {
	          var x = 0, y = 0;
	
	          while(p && p.offsetParent)
	          {
	               x += p.offsetLeft || 0;
	               y += p.offsetTop || 0;
	               p = p.offsetParent;
	          }
	
	          return {x:x,y:y};
	     },
	
	     coordinates:     function(e, c)
	     {
	          var v     = lava.css, m = v.style(e, ['margin-left', 'margin-top', 'position']), o = {};
	
	          if(m['position'] == 'fixed' || m['position'] == 'absolute')
	          {
	               o.x = v.style(e, 'left');
	               o.y = v.style(e, 'top');
	          }
	          else
	               o = v._offset(e);
	
	          return {x:o.x, y:o.y, height:v._height(e), width:v._width(e), margin:{top:m['margin-top'] || 0, left:m['margin-left'] || 0}};
	     }
	}

	lava.DOM     =
	{
	     parent: function(e)
	     {
	          return e.parentNode || e.parentElement || null;
	     },

		add: function(p, c)
		{
	  	   if (p.appendChild) p.appendChild(c);
  	  		 	return p;
		}
	}

	
	// this does the actual widget work
	var rand			= (new Date()).getTime();
	var timer		= null;
	var url			= location.href;
	var linkID		= "__feedbeater_icon__" + rand;
	var linkStyle	= "text-decoration:none;background: url(" + icon + ") no-repeat left center;padding-left:16px;";
	var linkHTML	= "<a id=\"" + linkID + "\" href=\"http://api.diffbot.com/rss/" + url + "\" style=\"" + linkStyle + "\">" + text + "</a>";

	var addID				= "__feedbeater_div__" + rand;
	var addStyle			= "background:white;border:solid #efefef 1px;";
	var addOptionsHTML	= "";

	for(var i = 0; i < addOptions.length; i++)
	{
		var even 			= (i % 2 == 0);
		var each 			= addOptions[i];
		addOptionsHTML		+= (even ? "<tr>" : "") + "<td><a id=\"" + addID + i + "\" href=\"" + each.href + "\" style=\"text-decoration:none;background: url(" + each.img + ") no-repeat 5px center;padding:4px 4px 4px 25px;display:block;font-size:10pt;color:#2f2f2f;border:none;\" target=\"_blank\">" + each.name + "</a></td>" + (!even ? "</tr>" : "");
	}

	if(addOptions.length % 2 != 0)
		addOptionsHTML += "<td></td></tr>";

	var addHTML  			= "<div id=\"" + addID + "\" style=\"position:absolute;top:0px;left:0px;background:url(" + imgdir + "25black.png) repeat 0 0;z-index:1000000;display:none;\"><div style=\"" + addStyle + "position:relative;top:-3px;left:-3px;\"><div style=\"padding:2px;\"><table style=\"border-collapse:collapse;\">" + addOptionsHTML + "</table></div><div style=\"font-size:8pt;background:#3f3f3f;padding:2px;text-align:right;\"><a href=\"" + home + "\" target=\"_blank\" style=\"text-decoration:none;color:#dffdff;background:transparent;border:none;\">FeedBeater.com</a></div></div></div>";

	document.write(linkHTML);
	//document.write(addHTML);
	//lava.DOM.add(document.body, lava.get.byid(addID));
	//
	var loadAdd = function(e)
	{
		lava.DOM.append(document.body, lava.make.html(addHTML));

		lava.event.add(lava.get.byid(linkID), 'mouseover', startAdd);
		lava.event.add(window, 'click', closeAdd);

		for(var i = 0; i < addOptions.length; i++)
		{
			var each = lava.get.byid(addID + i);
			lava.event.add(each, 'mouseover', choiceHighlight);
			lava.event.add(each, 'mouseout', choiceUnHighlight);
		}
	}
	
	var startAdd = function(e)
	{
		e.preventDefault();
		var coordinates	= lava.css.coordinates(lava.get.byid(linkID));	
		var add				= lava.get.byid(addID);
		lava.css.style(add, {left:coordinates.x, top:coordinates.y, display:'block'});
		timer = setTimeout(closeAdd, 3000);
	}

	var closeAdd = function(e)
	{
		var element 	= e ? e.target : null;
		var div			= lava.get.byid(addID);
		var feedbeater	= lava.get.byid(linkID);
		var found		= false;

		clearTimeout(timer);

		while(element && element != document.body)
		{
			if(element == div || element == feedbeater)
			{
				found = true;
				timer = setTimeout(closeAdd, 3000);
				break;
			}

			element = lava.DOM.parent(element);
		}

		if(!found)
			lava.css.style(div, {display:'none'});
	}

	var choiceHighlight = function(e)
	{
		lava.css.style(e.currentTarget, {"background-color":'#feffbf'});
		clearTimeout(timer);
	}

	var choiceUnHighlight = function(e)
	{
		lava.css.style(e.currentTarget, {"background-color":'transparent'});
		timer = setTimeout(closeAdd, 3000);
	}

	lava.event.add(window, 'load', loadAdd);

	//lava.event.add(lava.get.byid(linkID), 'mouseover', startAdd);
	//lava.event.add(window, 'click', closeAdd);

	//for(var i = 0; i < addOptions.length; i++)
	//{
	//	var each = lava.get.byid(addID + i);
	//	lava.event.add(each, 'mouseover', choiceHighlight);
	//	lava.event.add(each, 'mouseout', choiceUnHighlight);
	//}

})();

