(function($) {
	$.fn.sortableList = $.fn.sortableList = function(speed, duration){
		initSortable = function(el)
		{
			el.sort_ul = $(el).children('ul');
			el.pop_ul = el.sort_ul.html();
			
			if ($(el).attr('id') == ''){
				$(el).attr('id', 'sortDiv'+$.data(el));
			}
			el.alpha_ul = null;
			el.current_sort = 'pop';
			$(el).children('ul').before($('<div>').addClass("sortLink"));
			
			$(el).children('div.sortLink').append($('<a>').attr('href', 'javascript:$(\'#'+$(el).attr('id')+'\')[0].toggle_sort()').text('A-Z'));

			el.build_sort = function(){
				var elem_array = this.sort_ul.clone().children();
				elem_array.sort(function(a,b){
					aa = $(a).children('a').html()
					bb = $(b).children('a').html()
					return (aa == bb) ? 0 : ((aa < bb) ? -1 : 1)
				});
				this.alpha_ul = $('<ul>').append(elem_array).html();
			}
			
			el.toggle_sort = function(){
				if (this.alpha_ul == null){
					this.build_sort();
				}
				if (this.current_sort == 'pop') {
					this.current_sort = 'alpha';
					this.sort_ul.html(this.alpha_ul);
					$(this).children('div.sortLink').children('a').text('Popular');
				} else {
					this.current_sort = 'pop';
					this.sort_ul.html(this.pop_ul);
					$(this).children('div.sortLink').children('a').text('A-Z');
				};
				
			}
		}
		this.each(
			function()
			{
				if(this.nodeName.toLowerCase()!= "div") return;
				initSortable(this);
			}
		)
		return this;
	}
})(jQuery);