(function(jQuery) {
	jQuery.fn.glideMenu = function(o) {
		o = jQuery.extend({
			speed: 500,
			fx: 'swing',
			startItem: 0,
			cursorClass: 'cursor',
			click: function() { return true; }
		}, o || {});
	
		return this.each(function() {
			var $li = jQuery('li[class!=noglide]', this);			
			var $selected = jQuery($li[o.startItem]);
			var $ce = jQuery('li.active', this)[0] || jQuery($selected).addClass('active')[0];
			var $back = jQuery('<li class="' + o.cursorClass + '"></li>').appendTo(this);
			
			$li.mousedown(function(e) {
				jQuery($ce).removeClass('active');
				var moving = '';
				if (jQuery(this).children('a').attr('rel') != '') {
					moving = jQuery(jQuery(this).children('a').attr('rel')).parent('li')[0];
				} else {
					moving = this;
				}
				move(moving);
				
				jQuery(this).addClass('active');
				$ce = this;

				return o.click.apply(this, [e, this]);
			});
	
			if (o.cursorClass == 'calendar-cursor') {
				$back.css({ left: $ce.offsetLeft, top: $ce.offsetTop - $ce.innerHeight });
			} else {
				$back.css({ left: $ce.offsetLeft, top: $ce.offsetTop });
			}
			if (o.cursorClass == 'slider') {
				$back.css({ left: $ce.offsetLeft - 12, top: $ce.offsetTop - 3 });
			}
	
			function move(el) {
				if (!el) {
					el = $ce;
				}
				var bx = 0, by = 0;
				if (!jQuery.browser.msie) {
					bx = ($back.outerWidth() - $back.innerWidth()) / 2;
					by = ($back.outerHeight() - $back.innerHeight()) / 2;
				}
				if (o.cursorClass == 'calendar-cursor') {
					by = by + $back.innerHeight() / 3;
				}
				if (o.cursorClass == 'slider') {
					bx = 12;
					$back.stop().animate({
						left: el.offsetLeft - bx
					}, o.speed, o.fx);
				} else {
					$back.stop().animate({
						left: el.offsetLeft - bx,
						top: el.offsetTop - by
					}, o.speed, o.fx);
				}
			};
		});
	};
})(jQuery);