(function($){
    $.fn.xOptions = function(options)
	{
        var defaults = {
            itemHoverColor: "#ffd799",
            itemHoverColorOut: "#ffffff"
        };  
        var options = $.extend(defaults, options);

        return this.each(function()
		{
			obj = $(this);
            objPositions = obj.position();
            objWidth = obj.width();
            objHeight = obj.height();
            objName = obj.attr("name");
			
            optionNumber = obj.find("option").size();
            
            listPositionTop = (parseInt(objPositions.top)+objHeight+2);
            listWidth = (parseInt(objWidth)+32);
            
            var optionHtml = "";
            var selectHtml = "";
            var isNotVisible = "";
            var firstOptionValue = "";
			var itemIsClicked = false;
			
			
			
			
			
			function numberVisibleOptions(mainId)
			{
			    var j = 0;
			    for (i = 0; i < optionNumber; i++)
			    {
			        if ($("#" + mainId + " .optionItem:eq(" + i + ")").css("display") != "none")
			        {
			            j++;
			        }
			    }
			    return j;
			}
			
			
            function replaceBB(str)
			{
                var regImg = new RegExp("\\\[img\\\](.*)\\\[\/img\\\]", "g");
				str = str.replace(regImg, "<img src=\"$1\" />");
				
                var regBr = /\[br\]/g;
                str = str.replace(regBr, "<br>");
				
                str = str.replace(/\[b\]/g, "<b>");
                str = str.replace(/\[\/b\]/g, "</b>");
                str = str.replace(/\[i\]/g, "<i>");
                str = str.replace(/\[\/i\]/g, "</i>");
                str = str.replace(/\[u\]/g, "<u>");
                str = str.replace(/\[\/u\]/g, "</u>");
                
                return str;
            }
            
            function clearBB(str, objHeight)
			{
                var regBr = /\<br\>/g;
				var regImg = /\<img/g;
				
                str = str.replace(regBr, " ");
                str = str.replace(regImg, "<img style=\"max-height: " + (parseInt(objHeight)-2) + "px;\"");
                
                return str;
            }
			
			function nextItem(selectId, selectNumber, objHeight, mainId)
			{
				selectNumber = parseInt(selectNumber)+1;
				if (selectNumber < numberVisibleOptions(mainId))
				{
					$("#" + mainId + " .optionItem").css("backgroundColor", options.itemHoverColorOut);
					
					objItem = $("#" + mainId).find(".optionItem:eq(" + selectNumber + ")");			
					
					$("#" + mainId + "_hidden").val(objItem.attr("id"));
					
					objItem.css("backgroundColor", options.itemHoverColor);					
					$("#" + mainId).find("#xSelected_" + mainId).html(clearBB(objItem.html(), objHeight));
					
					$("#" + mainId).find("#xSelected_" + mainId).attr("selectedid", objItem.attr("id"));
					$("#" + mainId).find("#xSelected_" + mainId).attr("selectednumber", selectNumber);
				}
			}
			
			function prevItem(selectId, selectNumber, objHeight, mainId)
			{
				selectNumber = parseInt(selectNumber)-1;
				if (selectNumber >= 0)
				{
					$("#" + mainId + " .optionItem").css("backgroundColor", options.itemHoverColorOut);
					
					objItem = $("#" + mainId).find(".optionItem:eq(" + selectNumber + ")");			
				
					$("#" + mainId + "_hidden").val(objItem.attr("id"));
					objItem.css("backgroundColor", options.itemHoverColor);
					$("#" + mainId).find("#xSelected_" + mainId).html(clearBB(objItem.html(), objHeight));
					
					$("#" + mainId).find("#xSelected_" + mainId).attr("selectedid", objItem.attr("id"));
					$("#" + mainId).find("#xSelected_" + mainId).attr("selectednumber", selectNumber);
				}
			}
			
			//alert(obj.find("option").attr("selected"));
			var selectString = "";
			var y = optionNumber;
            for (x = 0; x < y; x++)
            {
                optionString = obj.find("option:eq(" + x + ")").html();
				
				//alert(obj.find("option:eq(" + x + ")").attr("selected"));

                if (!optionString)
				{
                    optionString = "&nbsp;"
                }
                
                if (obj.find("option:eq(" + x + ")").attr("setvisible") == "no")
                {
                    isNotVisible = "display: none;";
                }
                else
                {
                    isNotVisible = "display: block;";
                }

				if (obj.find("option:eq(" + x + ")").attr("selected"))
				{
					selectString = optionString;
					selectHtml = "<div class=\"xSelect\" id=\"" + obj.attr("id") + "\" style=\"width: " + (parseInt(objWidth)+32) + "px;\">";
					selectHtml += "<input type=\"text\" style=\"position: absolute; width: " + (parseInt(objWidth)+32) + "px;\" id=\"xSelectInput_" + obj.attr("id") + "\" class=\"xSelectInput\" />";
					selectHtml += "<div class=\"xSelected\" id=\"xSelected_" + obj.attr("id") + "\" selectedid=\"" + obj.find("option:eq(" + x + ")").val() + "\" selectednumber=\"" + x + "\" style=\"max-height: " + objHeight + "px; overflow: hidden; max-width: " + (parseInt(objWidth)+10) + "px;\">" + clearBB(replaceBB(selectString), objHeight) + "</div>\n";
					
                    firstOptionValue = obj.find("option:eq(" + x + ")").val();
				}

				if (selectString == optionString)
				{
					setHover = " style=\"background-color: " + options.itemHoverColor + ";\"";
				}
				else
				{
					setHover = "";
				}
					
                optionHtml += "<div id=\"" + obj.find("option:eq(" + x + ")").val() + "\"" + setHover + " class=\"optionItem\" style=\""+ isNotVisible + "\">" + replaceBB(optionString) + "</div>\n";
            }            
            replaceHtml = "";
            replaceHtml += selectHtml;
            replaceHtml += "<div id=\"optionList_" + obj.attr("id") + "\" class=\"optionList\" style=\"display: none; position: absolute; top: " + listPositionTop + "px; left: " + objPositions.left + "; width: " + listWidth + "px;\">\n";
            replaceHtml += optionHtml;
            replaceHtml += "\n</div></div><input type=\"hidden\" name=\"" + objName + "\" id=\"" + obj.attr("id") + "_hidden\" value=\"" + firstOptionValue + "\" />\n";
            
            obj.html("");
            obj.replaceWith(replaceHtml);
			
			
			objTwo = this.id
			
			

            $("#" + this.id).click(function ()
			{
				objPositions = $(this).position();
    	        objWidth = $(this).width();
	            objHeight = $(this).height();
				
				listPositionTop = (parseInt(objPositions.top)+objHeight+2);
				
				
				$(this).find(".optionList").css("top", listPositionTop);
				$(this).find(".optionList").css("left", objPositions.left);
				
				//alert(this.id);
                if ($(this).find(".optionList").css("display") == "none")
				{
                    $(this).find(".optionList").fadeIn();
                }
				else
				{
                    $(this).find(".optionList").fadeOut();
                }
            });
			
			

            $("#" + this.id + " .optionItem").click(function (e)
			{
				mainId = $(this).parent().parent().attr("id");
				clickedId = $(this).attr("id");
				var selectedNumber = 0;
				$("#" + mainId + " .optionItem").each(function(i){
					if(clickedId == $(this).attr("id"))
					{
						selectedNumber = i+1;
					}
				});
				itemIsClicked = true;
                $("#" + mainId + "_hidden").val($(this).attr("id"));
				$(this).css("backgroundColor", options.itemHoverColor);
                $("#" + mainId).find("#xSelected_" + mainId).html(clearBB($(this).html(), objHeight));
                $("#" + mainId).find(".optionList").fadeOut("normal", function () {
																						itemIsClicked = false;
																						});
				$("#" + mainId).find("#xSelected_" + mainId).attr("selectedid", $(this).attr("id"));
				$("#" + mainId).find("#xSelected_" + mainId).attr("selectednumber", (parseInt(selectedNumber)-1));
            });
			
			
			
			$("#" + this.id + " #xSelectInput_" + this.id).focus(function ()
			{
				$(this).attr("readonly", "readonly");
			});
			
			
			
			$("#" + this.id + " #xSelectInput_" + this.id).blur(function ()
			{
				mainId = $(this).parent().attr("id");
				if ($("#" + mainId).find(".optionList").css("display") != "none")
				{
                    $("#" + mainId).find(".optionList").fadeOut();
                }
			});
			
			
			
			$("#" + this.id + " #xSelectInput_" + this.id).keypress(function (e) {
				mainId = $(this).parent().attr("id");
				keyNum = e.keyCode;
				if(keyNum == 40) // Pfeil nach oben
				{
					nextItem($("#" + mainId).find("#xSelected_" + mainId).attr("selectedid"), $("#" + mainId).find("#xSelected_" + mainId).attr("selectednumber"), objHeight, mainId);
				}
				else if(keyNum == 38) // Pfeil nach unten
				{
					prevItem($("#" + mainId).find("#xSelected_" + mainId).attr("selectedid"), $("#" + mainId).find("#xSelected_" + mainId).attr("selectednumber"), objHeight, mainId);
				}
				else if(keyNum == 13) // Enter
				{
					$("#" + mainId).find(".optionList").fadeOut();
					return false;
				}
			});
            
			
			
            $("#" + this.id + " .optionItem").hover(function ()
			{
				mainId = $(this).parent().parent().attr("id");
				if(!itemIsClicked)
				{
					$("#" + mainId + " .optionItem").css("backgroundColor", options.itemHoverColorOut);
	                $(this).css("backgroundColor", options.itemHoverColor);
				}
            },
			function ()
			{
            });
        });
    };
})(jQuery);

