(function($) {

	$.fn.linkselect = function() {

		this.each( function(a) {
	      var initialSelection = $(this).children("li:first").html();
			var menuId = $(this).attr("id") + "_mod";
			var menuItemId = menuId + "_item";

			var menuHtml = '<div id="' + menuId + '" class="dropdown cf" tabindex="0">' + "\n" +
                        '	<div class="arrow">' + "\n" +
                        '		<div class="list cf">' + "\n" +
                        '			<p>' + initialSelection + '</p>' + "\n" +
                        '		</div>' + "\n" +
                        '	</div>' + "\n" +
                        '   <div class="dropdownBottom"></div>'
                        '</div>';

			var menuItemHtml = '<div id="' + menuItemId + '" class="dropdownitem" style="display: none"><div class="scrollable"><ul>' + "\n";

			var i = 0;
			$(this).children().each(function() {
				if(i!=0) {
					menuItemHtml += "<li><p>" + $(this).html()  + "</p></li>\n";
				}
				i++;
			});
			menuItemHtml += '</ul></div></div>';


			$(this).before(menuHtml + menuItemHtml);
			$(this).hide();

			var menuSelector = '#' + menuId;
			var menuItemSelector = '#' + menuItemId;
			var menuItemScroll = '#' + menuItemId +" .scrollable";
			var eventType = "";
			var firstEvent=0;
			$('div.arrow').attr('title','Liste aller Themen aufklappen');
			
			$(menuSelector).bind("focus click", function(event) {
				// show items list
				//$(menuItemSelector).toggle();
				// only care when the menu item is visible
					$eventType = event.type;
					//Fängt den ersten doppelten Event ab
					if ($eventType == "focus") {
						firstEvent = 1;
					}
					if ($eventType == "click" && firstEvent==1) {
						firstEvent = 0;
						return false;
					}					
					if($(menuItemSelector).css("display")=="none"){

						$('.dropdown').removeClass("open");
						$('.dropdown').each(function() {
							$(this).children(".arrow").css("outline","none");
						});
						
						$(this).addClass("open");						
						$(this).children(".arrow").css("outline","1px dotted grey");
						
						var selesctBoxHeight = $('.dropdown').height() - 1;
						var offset = $(this).position();
						$(menuItemSelector).css("left", offset.left + "px");
						$(menuItemSelector).css("top", offset.top + selesctBoxHeight + "px");
						$('div.arrow').attr('title','Liste aller Themen schließen');

						// highlight item list that is currently selected
						var currentText = $(menuSelector + ' p').text();
						$(menuItemSelector + ' p').each(function() {
							var itemListText = $(this).text();
							if (itemListText == currentText) {
								$(this).addClass("selected");
							}
						});
						
						$(this).find('a:last').blur(function() {
							$(menuSelector).children(".arrow").css("outline","none");
							$('.dropdown').removeClass("open");
							$('div.arrow').attr('title','Liste aller Themen aufklappen');
							$(menuItemSelector).hide();					
						
						});

						$(".dropdownitem").hide();
						$(menuItemSelector).show();						
					
					} else if(eventType != "focus"){
						$('div.arrow').attr('title','Liste aller Themen aufklappen');
						$(this).removeClass("open");
						$(menuItemSelector).hide();
						$(this).children(".arrow").css("outline","none");	
					}
			});
			


			if(!$.browser.mozilla) { //exclude mozilla cause of following bug, http://dev.jquery.com/ticket/4469
				$(menuItemSelector).hover(
			      function () {
			        //over
			      },
			      function () {
			        $(this).hide();
			        $('.dropdown').removeClass("open");
					$('.dropdown').css("outline","none");					
			      }
			    );
			}

		});




	}
})(jQuery);
