(function($){
	$.fn.selectr = function(options) {
		var opts = $.extend({toggle:false},$.fn.selectr.defaults,options);

		var _over = function(t) {
			$(t).css({backgroundImage: 'url('+opts.iconSelect+')', backgroundColor: opts.hiColor})
				.parent().find('.desc').html($(t).attr('href')) 
		};

		var _out = function(t) {
			if (!$(t).attr('selected'))
				$(t).css({backgroundImage: 'none', backgroundColor: opts.loColor})
					.parent().find('.desc').html($(t).parent().find('A[selected=true]').attr('href')) 
		}

		return this.each(function(){
			var $obj = $(this);
			var _select = (typeof options == 'string' ) ? options : '';
				
			if (!$obj.data('installed')) {
				_select = opts.startValue || '';
				if (jQuery.isFunction(opts.callback))
					$.fn.selectr.defaults.cb[$obj.attr('id')] = opts.callback;

				$obj.find('A').each(function(){
					$(this).attr('href', $(this).attr('href').replace(/^.*\//,''));
				});
				$obj.data('installed',true).find('A')
				.css({ display: 'block', float: 'left', 
					background: opts.loColor+' '+opts.iconAttach })
				.css(opts.css)
				.hover(function(){ _over(this) },function(){ _out(this) });

				if (opts.toggle)
					$obj.find('A').click(function(){ 
						if ($(this).attr('selected')) {
							$(this).removeAttr('selected'); _out(this);
						} else $(this).attr('selected',true);

						var arr = [];
						$obj.find('A[selected=true]').each(function(){
							arr.push($(this).attr('href'))
						});
						var _fn = $.fn.selectr.defaults.cb[$obj.attr('id')];
						if (jQuery.isFunction(_fn)) _fn(this,arr.join(','));
						return false;
					})
				else
					$obj.find('A').click(function(){ 
						var _t = $obj.find('A[selected=true]');
						if (!$(this).attr('selected')) {
							_t.removeAttr('selected'); _out(_t);
							$(this).attr('selected',true);
    
							var _fn = $.fn.selectr.defaults.cb[$obj.attr('id')];
							if (jQuery.isFunction(_fn))
								_fn(this);
						}
						return false;
					})
			}
        
			if (_select)
				$.each(_select.split(','), function() {
					var _c = $obj.find('A[href="'+this+'"]');
					_c.attr('selected',true);
					_over(_c);
				});
		});
	};

	$.fn.selectr.defaults = {
		cb: {},
		iconSelect: '/gfx/n.gif', hiColor: '#9c0', loColor: 'transparent',
		iconAttach: 'none no-repeat bottom center',
		css: { margin: '1px', padding: '1px', height: '25px', border: '0px' }
	};
})(jQuery);
