﻿var ClassName = "nSelect";
var iCount = 1;
(function($) {
    $.fn.NewSelect = function(SelectSettings) {
		var Settings = $.extend({ ClassName: 'nSelect', IEtop: '0px' }, SelectSettings || {});
		ClassName = Settings.ClassName;
		var IeTop = Settings.IEtop;
		$(this).each(function(){
			var elName = $(this).attr("name");
			var standardText = "";
			var standardValue = "";
			var oCount = 1;
			var iet = "";
			$(this).children("option").each(function(){
				if(oCount == 1 || $(this).attr("selected")) { standardText = $(this).html(); standardValue = $(this).attr("value"); }
				oCount++;
			});
			if (jQuery.browser.msie == true && (jQuery.browser.version.substring(0, 1) == "7" || jQuery.browser.version.substring(0, 1) == "6" || jQuery.browser.version.substring(0, 1) == "5")) {
				iet = " top: " + IeTop + ";";
			}
			var newBuild = "<div class=\"" + ClassName + "\" style=\"position: relative;\" onclick=\"nSelectSwapOptions("+ iCount +",'" + ClassName + "');\" id=\"nSelect" + iCount + "\"><span>" + standardText + "</span><ul style=\"display: none; position: absolute; z-index: 7;left: 0px;" + iet + "\">";
			oCount = 1;
			$(this).children("option").each(function(){
				newBuild = newBuild + "<li onclick=\"nSelectSetValue(" + iCount + ",'"+ $(this).attr("value") +"','"+ $(this).html() +"');\"><span>" + $(this).html() + "</span></li>";
				oCount++;
			});			
			newBuild = newBuild + "</ul></div>";			
			newBuild = newBuild + "<input type=\"hidden\" value=\"" + standardValue + "\" id=\"nSelectHidden" + iCount + "\" name=\"" + elName + "\" />"
			$(this).after(newBuild);			
			
			$(this).remove();
			iCount ++;
		});
	}
})(jQuery);


function nSelectSwapOptions(oId, oClass) {
	var ToDo = 0;
	if($("#nSelect" + oId + " ul").css("display") == "none") { ToDo = 1 };
	$("." + oClass + " ul").hide();
	if(oId != 0) {
		if(ToDo == 1) {
			$("#nSelect" + oId + " ul").show();
		} else {
			$("#nSelect" + oId + " ul").hide();
		}
	}
}

function nSelectSetValue(sId,oValue,oHtml) {
	$("#nSelectHidden" + sId).val(oValue);
	$("#nSelect" + sId).children("span").html(oHtml);
}
