(function($){
	$.fn.menu = function(options) {
		var opts = $.extend($.fn.menu.defaults,options);
		var $this = $(this);
		var $parent = $this.parent();
		var $items = $('li', $(this));
		var $item = $('li', $(this)).eq(0);
		var $subItem = $(opts.subItem_Name);
		var $content = $subItem.children().eq(0);
		
		var expand_H = opts.expand_Height + 'px';
		var default_H = opts.default_Height + 'px';
		
		$this.shown = false;
		
		function dropdownFix(fg){
			if($.browser.msie && $.browser.version == '6.0'){
				if(fg && $('#colorbox:visible').size() < 1)$('.dropdownlist').css({visibility: "visible"});
				if(!fg)$('.dropdownlist').css({visibility: "hidden"});
			}
		}
		
		function expand(){
			dropdownFix(false);
			$content.stop();
			$subItem.stop().animate({height:expand_H},500,
				function(){
					$('#title',$subItem).html($('#list li[class$=selected] a', $subItem).attr('title'));
					$(this).css('line-height',expand_H);
					$content.stop().fadeIn(300).css('opacity','1');
					$this.shown = true;
				});
		}
		
		function close(){
			$content.stop().css('opacity','1').fadeOut(100);
			$subItem.stop().animate({height:default_H},500,
				function(){
					dropdownFix(true);
					$(this).css('line-height',default_H);
					$this.shown = false;
				});
		}
		
		function validArea(o){
			return position = {left:o.offset().left,top:o.offset().top,
							 right:o.offset().left + o.width(),
							 bottom:o.offset().top + o.height()};
		}
		
		function checkValidArea(){
			var menu = validArea($item);
			var submenu = validArea($subItem);
			
			$(document).mousemove(function(e){
				var menuInX = e.pageX >= menu.left && e.pageX <= menu.right + 30;
				var menuInY = e.pageY >= menu.top && e.pageY <= menu.bottom + 20;
				
				var submenuInX = e.pageX >= submenu.left && e.pageX <= submenu.right;
				var submenuInY = e.pageY >= submenu.top && e.pageY <= submenu.bottom + opts.expand_Height - opts.default_Height;
				
				if((menuInX && menuInY) || (submenuInX && submenuInY)){
					if(!(!$this.shown && (submenuInX && submenuInY))){
						$('.overlay',$parent).remove();
						if($('.bubbly-tab',$item).size() < 1)
							$('<div></div>').addClass('bubbly-tab').appendTo($item);
						$item.removeAttr('class').addClass('current-'+$item.attr('id'));
						expand();
					}
				}
				else{
					$item.removeAttr('class');
					$('.bubbly-tab',$item).hide().remove();
					close();
					if($('.overlay',$parent).size() < 1)
						$('<div></div>').addClass('overlay').appendTo($parent);
				}
			});
		}

		return this.each(function() {
			//Slide menu style
			checkValidArea();
			
			//Menu hover style
			$items.hover(
				function(){
					if($('.bubbly-tab',$(this)).size() < 1)
						$('<div></div>').addClass('bubbly-tab').appendTo($(this));
					$(this).removeAttr('class').addClass('current-'+$(this).attr('id'));
				},
				function(){
					$(this).removeAttr('class');
					$('.bubbly-tab',$(this)).hide().remove();
				}
			);
			
			//Sub menu style
			$('li',$subItem).hover(
				function(){
					$(this).addClass($(this).attr('id')+'current');
					$('#title',$subItem).html($('a',$(this)).attr('title'));
				},
				function(){
					$(this).removeClass($(this).attr('id')+'current');
				}
			);
			$('li',$subItem).each(function(){
				$(this).click(function(){
					$('li',$subItem).removeAttr('class');
					$(this).addClass($(this).attr('id')+'selected');
				});
			});
			
			//Title
			$('#title',$subItem).html($('#list a', $subItem).eq($('#list a', $subItem).size() - 1).attr('title'));
			
			//Inner menu
			$inner_menu = $('#inner-nav');
			$inner_menu_items = $('a',$inner_menu);
			$('<div></div>').addClass('overlay').addClass('tl').appendTo($inner_menu_items.eq(0));
			$('<div></div>').addClass('overlay').addClass('tr').appendTo($inner_menu_items.eq($inner_menu_items.size() - 1));
			$('<div></div>').addClass('overlay').addClass('bl').appendTo($inner_menu_items.eq(0));
			$('<div></div>').addClass('overlay').addClass('br').appendTo($inner_menu_items.eq($inner_menu_items.size() - 1));
			$inner_menu_items.eq($inner_menu_items.size() - 1).css('border-right','0');
			
		});
	};
		
	$.fn.menu.defaults = {
		subItem_Name: '#sub-nav',
		expand_Height: 42,
		default_Height: 4
	};
})(jQuery);
