// read links later
(function () {
	var H = '0', // change to your ISRIL_H number
		W = window,
		D = W.document,
		P = '__rll', // our property name
		R = 'readitlaterlist.com', // domain
		S = 'backgroundColor', // css property to modify for highlight
		V = '#ffff99', // highlight css value
		RLL = D[P]; // data object

	if (!RLL) {
		RLL = D[P] = {
			enabled: false,
			links: [],

			toggle: function () {
				var s, a, l, i;

				if (!RLL.enabled) {
					D.addEventListener('click', RLL.click, true);
				} else {
					D.removeEventListener('click', RLL.click, true);

					s = RLL.links;
					l = s.length;
					for (i = 0; i < l; i++) {
						a = s[i];
						RLL.img(a);
						RLL.msg(a);
						if (a[P].d != null) {
							W.clearTimeout(a[P].d);
						}
						a[P] = null;
					}
					RLL.links = [];
				}
				RLL.enabled = !RLL.enabled;
			},

			click: function (e) {
				var a = e.target;

				if (a.nodeType == 3) {
					a = a.parentNode;
				}
				if (a.nodeType != 1 || a.nodeName.toUpperCase() != 'A') {
					return;
				}

				e.preventDefault();
				e.stopPropagation();

				RLL.init(a);

				switch (a[P].s) {
				case 'new':
					// save
					a[P].s = 'saving';
					RLL.msg(a, 'saving');
					RLL.img(a, 'http://' + R + '/v2/r.gif?v=1&h=' + H + '&u=' + a[P].u + '&t=' + a[P].t + '&rand=' + Math.random());
					break;

				case 'saved':
					// edit
					W.open('https://' + R + '/edit?BL=1&url=' + a[P].u + '&title=' + a[P].t, '', '');
					break;
				}
			},

			load: function (e) {
				var a, err, v;

				switch (this.width) {
				case 2:
					// not logged in
					err = 'You need to be logged into Read It Later before you can save links';
					break;

				case 3:
					// ISRIL_H / user mismatch
					err = 'ISRIL_H does not match logged-in user';
					break;
				}

				if (!err) {
					// saved
					a = this.a;
					RLL.img(a);
					RLL.msg(a, 'saved');
					a[P].s = 'saved';

					v = a.style[S];
					a.style[S] = V;
					a[P].d = W.setTimeout(function () {
						a[P].d = null;
						if (a.style[S] == V) {
							a.style[S] = v;
						}
						RLL.msg(a, 'edit');
					}, 5000);

				} else {
					// error
					RLL.cancelSaving();
					W.alert(err);
				}
			},

			cancelSaving: function () {
				var s = RLL.links, l = s.length, a, i;
				for (i = 0; i < l; i++) {
					a = s[i];
					if (a[P].s == 'saving') {
						RLL.img(a);
						RLL.msg(a);
						a[P].s = 'new';
					}
				}
			},

			init: function (a) {
				if (!a[P]) {
					a[P] = {
						id: (new Date()).getTime() + '-' + Math.random(),
						s: 'new', // state: new -> saving -> saved
						u: W.encodeURIComponent(a.href), // link url
						t: W.encodeURIComponent(a.textContent).replace(/^\s\s*/, '').replace(/\s\s*$/, ''), // link title
						i: null, // image element
						m: null, // message text node
						d: null // timeout id
					};
					RLL.links.push(a);
				}
			},

			img: function (a, s) {
				var d = a[P], i = d.i;
				if (i) {
					d.i = i.a = i.onload = null;
				}
				if (s) {
					d.i = i = D.createElement('img');
					i.a = a;
					i.onload = RLL.load;
					i.src = s;
				}
			},

			msg: function (a, s) {
				var d = a[P];
				if (d.m) {
					a.removeChild(d.m);
					d.m = null;
				}
				if (s) {
					d.m = D.createTextNode(' [' + s + ']');
					a.appendChild(d.m);
				}
			}
		};
	}

	RLL.toggle();
})()
