// This was rewritten from del.icio.us, I made it more easily maintainable

var onclicks = {
	rm: rmConfirm,
	rmNo: rmConfirmNo,
	global: new Array ()
}

document.onclick = function(e) {
	e = e||window.event;
	var t = e.target||e.srcElement;

/*
var oc = t.getAttribute ('onclick');
var id = t.getAttribute ('id');
var c  = t.getAttribute ('class');
var s  = t.getAttribute ('style');
debugMsg ("t='"+t+"' oc='"+oc+"' class='"+c+"' id='"+id+"' style='"+s+"'\n");
*/

	if (t.className && onclicks[t.className]) {
		onclicks[t.className].call (t, e);

		return false;
	} else if (onclicks["global"]) {

		// Loop through all the click handlers
		for (x in onclicks["global"]) {
			onclicks["global"][x].call (t, e);
		}
	}
	
	return true;
}

function setcookie (name, value, duration) {
	var today = new Date ();
	var expire = new Date ();
	
	expire.setTime (today.getTime()+3600000*24*duration);
	document.cookie = name + "=" + escape (value) + ";expires="+expire.toGMTString ();
}

function findElements () {
  var ea;

  for (var i = 0; i < arguments.length; i++ ) {
    var e = arguments[i];

    if( typeof e == 'string' ) {
      e = document.all ? document.all[e] : document.getElementById(e);
	}

    if( arguments.length == 1 )
      return e;

    if (!ea)
      ea = new Array();

    ea[ea.length] = e;
  }

  return ea;
}

function extend (dest, src) {
	if (!src)
		return dest;
	
	for (var k in src)
		dest[k] = src[k]
	
	return dest;
}

extend ( String.prototype, {
	splitrim: function(t) { return this.split(new RegExp('\\s*'+t+'\\s*')) }
})

function create (o, t) {
	if (o == 'text') 
		return document.createTextNode (t||'');

	var e = document.createElement (o);
	if (t) {
		if (typeof t == 'string') {
			e.innerHTML = t;
		} else {
			niceExtend (e, t);
		}
		
	}

	return e;
}

function niceExtend (dest, src) {
	if (!src)
		return dest;

	if (src.html) {
		dest.innerHTML = src.html;  
		delete src.html;
	}
	
	if (src.css) {
		dest.className = src.css; 
		delete src.css;
	}
	
	if (src.attr) {
		var s = src.attr;
		for (var k in s) 
			dest.setAttribute (k, s);

		delete src.attr;
	}

	if (src.style) {
		var d = dest.style, s = src.style;
		
		for (var k in s) 
			d[k] = s[k];
		
		delete src.style;
	}

	for (var k in src) 
		dest[k] = src[k];

	return dest;
}

function rmConfirm () {
	var commands = this.parentNode,
	    post     = commands.parentNode;

	commands.style.display = 'none';
	var s = create ('span', {css: 'commands'});

	// If we're using an image delete, we need our parents refernce instead of ours
	var href = this.href ? this.href : commands.href; 

	s.appendChild (create ('span', {html: ' you sure? ', css: 'important'}));
	s.appendChild (create ('a', {html: 'no', css: 'rmNo', href: '' }));
	s.appendChild (create ('text', ' / '));
	s.appendChild (create ('a', {html: 'yes', css: 'hand rmYes', href: href}));

	post.insertBefore (s, commands);
}

function nextElement (o) {
	do o = o.nextSibling; while (o && o.nodeType != 1)
	return o;
}

function remove () {
	for (var i=0, o; o=arguments[i];i++)
		if (o && o.parentNode)
			o.parentNode.removeChild (o)
}

function rmConfirmNo () {
	var confirm = this.parentNode, 
	    commands = nextElement (confirm);

	commands.style.display = 'inline';
	remove (confirm);
}


function debugMsg (msg) {
	var debug = findElements ("debugMsg");

	if (!debug) {
		return;
	}

	debug.value += msg;
}

function placeFocus () {
	if (document.forms.length == 0)
		return;

	for (var x = 0; x < document.forms.length; x++) {
		var form = document.forms[x];

		for (var y = 0; y < form.elements.length; y++) {
			var element = form.elements[y];

			if (element.getAttribute ("noautofocus"))
				continue;

			if (element.type == 'textarea') {

				element.focus ();
				return;
			}
			if (element.type == 'text') {

				element.select ();
				element.focus ();
				return;
			}
		}
	}
}

function changeClass (obj, target) {
	var t = findElements (target);

id = t.getAttribute ("id");
debugMsg ("obj="+obj+" checked="+obj.checked+" t="+t+" t.id="+id+" target="+target+"\n");

	if (obj.checked) {
		t.className = 'private';
	} else {
		t.className = 'public';
	}
}
