(function($){

$.fn.ncs_menu = function(options){

	var defaults = {
		scrollSpeed: 10,
		scrollPixels: 5
	};
	
	var opts = $.extend(defaults, options);
	
	$(this).wrapInner('<div id="ncs_container"></div>');
	
	var container = $("div#ncs_container");
	container.css("position", "absolute")
		.css("top", "0px")
		.css("left","0px")
		.css("height", $(this).height()+"px")
		.css("width", $(this).width()+"px")
		.css("overflow","hidden");
	
	container.append('<div id="ncs_menu_overlay"></div>');
	
	var overlay = $(this).find("div#ncs_menu_overlay");
	overlay.css("position", "absolute")
		.css("top", "0px")
		.css("left","0px")
		.css("height", $(this).height()+"px")
		.css("width", $(this).width()+"px")
		.css("background-color", $(this).find("ul:first").css("background-color"))
		.hide();
	
	$(this).append('<a href="" id="ncs_scroll_up"></a><a href="" id="ncs_scroll_down"></a>');
	
	$("a#ncs_scroll_up").click(function(){return(false);});
	$("a#ncs_scroll_down").click(function(){return(false);});
	
	var scroll_id = null;
	
	$("a#ncs_scroll_up").hover(function(){
		
		scroll_id = setInterval( function(){
			menu_top = curr_menu.position().top + opts.scrollPixels;
			if( menu_top > 0 ){
				menu_top = 0;
			}
			if( curr_menu.position().top > 0 ){
				;
			}else{
				curr_menu.css("top", menu_top+"px");
			}
		}, opts.scrollSpeed);
	},function(){
		clearInterval( scroll_id );
	});
	
	$("a#ncs_scroll_down").hover(function(){
		scroll_id = setInterval( function(){
			menu_top = curr_menu.position().top - opts.scrollPixels;
			if( menu_top < container.height() - curr_menu.height() ){
				menu_top = container.height() - curr_menu.height();
			}
			if( container.height() - curr_menu.height() > curr_menu.position().top ){
				;
			}else{
				curr_menu.css("top", menu_top+"px");
			}
		}, opts.scrollSpeed);
	},function(){
		clearInterval( scroll_id );
	});
	
	var curr_menu = null;
	
	$(this).find("ul").each(function(){
		
		var menu = $(this);
		
		menu.before('<a href="" class="ncs_button_button"></a>');
		var menu_button = menu.prev("a.ncs_button_button");
		
		setup_button();
		
		function setup_button(){
			if( menu.find("a.ncs_selected").length == 1 ){
				menu_button.html( menu.find("a.ncs_selected").html() );
			}else{
				menu_button.html( menu.attr("title") );
			}
			
			if( menu_button.prevAll("a:first").length == 0 ){
				;
			}else if( menu_button.prevAll("ul:first").find("a.ncs_selected").length == 1 || menu_button.next("ul").find("a.ncs_selected").length == 1 ){
				;
			}else{
				menu_button.addClass("ncs_disabled");
			}
		}
		
		menu.prev("a").click(function(){
			if( $(this).hasClass("ncs_disabled") ){
				return(false);
			}else{
				curr_menu = menu;
				menu_top = 0;
				
				var button_top = $(this).position().top;
				var i = 0;
				
				menu.find("li").each(function(){
					if( $(this).children("a").hasClass("ncs_selected") ){
						menu_top = button_top - ( 28 * i );
					}
					i = i + 1;
				});
			}
			
			menu.css("top", menu_top+"px");
			overlay.show();
			menu.show();
			curr_menu_height = menu.height();
			$("a#ncs_scroll_up").show();
			$("a#ncs_scroll_down").show();
			
			return(false);
		});
		
		menu.find("li a").click(function(){
			menu.find("a.ncs_selected").removeClass("ncs_selected");
			$(this).addClass("ncs_selected");
			
			menu.prev("a").html( $(this).html() );
			menu.prev("a").attr( "href", $(this).attr("href") );
			menu.hide();
			$("a#ncs_scroll_up").hide();
			$("a#ncs_scroll_down").hide();
			overlay.hide();
			
			if( menu.attr("id") == "ncs_menu_service" ){
				$("ul#ncs_menu_country").prev("a.ncs_button_button").removeClass("ncs_disabled");
			}else if( menu.attr("id") == "ncs_menu_country" ){
				reset_button("ul#ncs_menu_county");
				reset_button("ul#ncs_menu_town");
				$("ul#ncs_menu_county").prev("a.ncs_button_button").removeClass("ncs_disabled");
			}else if( menu.attr("id") == "ncs_menu_county" ){
				reset_button("ul#ncs_menu_town");
				$("ul#ncs_menu_town").prev("a.ncs_button_button").removeClass("ncs_disabled");
			}else if( menu.attr("id") == "ncs_menu_town" ){
				;
			}
			
			return(true);
		});
		
	});
	
	function reset_button( menu_id ){
		$(menu_id).prev("a.ncs_button_button").html( $(menu_id).attr("title") ).addClass("ncs_disabled");
		$(menu_id).find("a.ncs_selected").removeClass("ncs_selected")
	}
	
}

})(jQuery);
