// make bookmarklets bookmarkable
(function () {
	var d = document,
		id = d.__bookmarkId, // id (class actually) to identify our links
		as = d.getElementsByTagName('a'), // list of link elements
		a, // link element
		e, // iphone link template
		p, // link parent element
		s, // link next sibling
		b, // iphone link
		i; // counter

	if (!id) { // generate id
		d.__bookmarkId = id = 'bookmark' + (new Date()).getTime();
	}

	e = d.createElement('a');
	e.innerHTML = '[iPhone]';

	for (i = as.length - 1; i >= 0; i--) {
		a = as[i];

		// skip non-javascript links, our links and already-processed links
		if (!/^javascript:/.test(a.href) || a.__bookmarkId == id) {
			continue;
		}

		p = a.parentNode;
		s = a.nextSibling;
		b = e.cloneNode(true);
		b.className = a.className;
		b.href = '#removeme_' + a.href;
		b.__bookmarkId = a.__bookmarkId = id;

		p.insertBefore(d.createTextNode(' '), s);
		p.insertBefore(b, s);
	}
})()
