;(function($){ // $ will refer to jQuery within this closure
	$.fn.stackable = function()
	{
		var $this = $(this);
		jQuery.fn.stackable.nodes = $this;
		// return original object to support chaining
		return $this;
	};
	
	$.fn.stackable.nodes = {};
	$.fn.stackable.getcollection = function ()
	{
		var o = {};
		jQuery.fn.stackable.nodes.each(function(i){
			var t = $(this);
			if (t.css('zIndex') == 'auto') t.css('zIndex', i*2+2);
			o[t.css('zIndex')] = this;
		})
		return o;
	}
	
	$.fn.stackable.reorder = function ()
	{
		var o = jQuery.fn.stackable.getcollection();
		
		var keys = [];
		for (var s in o) keys.push(s);
		keys.sort(function(a,b){return a-b;});
		for (var i=0; i<keys.length; i++)
		{
			var e = jQuery(o[keys[i]]);
			e.css('zIndex', i*2+2);
		}
	};
	$.fn.stackable.up = function (e)
	{
		e = (undefined == e)?this:e;
		$.fn.stackable.reorder();
		e.css('zIndex', parseInt($(e).css('zIndex'))+3);
	};
	
	$.fn.stackable.down = function (e)
	{
		$.fn.stackable.reorder();
		$(e).css('zIndex', parseInt($(e).css('zIndex'))-3);
	};
	$.fn.stackable.top = function (e)
	{
		$.fn.stackable.reorder();
		$(e).css('zIndex', jQuery.fn.stackable.nodes.length*2+2);
	};
	$.fn.stackable.bottom = function (e)
	{
		$.fn.stackable.reorder();
		$(e).css('zIndex', 0);
	};
})(jQuery); // plugin code ends