/**
 * @name	jQuery SelectList
 * @description	plugin is designed for styling lists in forms
 * @author	Erwin Bujak 'ErniX'
 * @version	0.2
 * @date	August 22, 2011
 * @wwww	erwinbujak.eu | erwinbujak.com
 * @copyright	(c) 2011 Erwin Bujak
 */
(function($){

	$.fn.SelectList = function (options) {

		var settings = {
			ViewInput: 'false',
			MaxHeight: '140',
			Prefix: ''
		};

		if (options)
		{
			$.extend(settings, options);
		}

		$(this).each(function () {

			InpText = '';
			InpValue = '';

			$(this).find("option").each(function (i) {

				idSelected = ($('option[selected="selected"]').length ? $('option[selected="selected"]').index() : 0);

				if (i == idSelected)
				{
					InpText = $(this).text();
					InpValue = $(this).attr("value");
				}

				$(this).replaceWith('<div class="jSL'+settings.Prefix+'FormOption' + (i == idSelected ? ' jSL'+settings.Prefix+'FormOptionSelected' : '') + '" title="'+$(this).text()+'"><input name="'+$(this).parent("select").attr("name")+'"' + (i == idSelected ? ' checked="checked"' : '') + ' class="jSL'+settings.Prefix+'FormOptionInput' + (settings.ViewInput == "true" ? "" : " jSL"+settings.Prefix+"Disabled") + '" type="radio" value="'+$(this).attr("value")+'" /><span>'+$(this).text()+'</span></div>');
			});

			$(this).each(function () {

				name = $(this).attr("name");
				value = $(this).html();
				$(this).replaceWith('<div class="jSL'+settings.Prefix+'FormSelect" name="'+name+'"><div class="jSL'+settings.Prefix+'FormSelectName" name="'+name+'">'+InpText+'</div><div class="jSL'+settings.Prefix+'FormSelectContent jSL'+settings.Prefix+'Disabled">'+value+'</div></div>');

			});

			$(".jSL"+settings.Prefix+"FormSelectContent").each(function() {

				Height = $(this).height();

				if (Height >= settings.MaxHeight)
				{
					$(this).css({
						"overflow-y": "scroll",
						"height": settings.MaxHeight
					});
				}
			});
		});

		$(".jSL"+settings.Prefix+"FormSelect").click(function () {

			IDFormSelect = $(this).index(".jSL"+settings.Prefix+"FormSelect");

			/*$(this).find(".jSL"+settings.Prefix+"FormSelectContent").removeClass("jSL"+settings.Prefix+"Disabled");*/
			$(this).find(".jSL"+settings.Prefix+"FormSelectContent").slideDown("slow");

		}).mouseleave(function () {

			/*$(this).find(".jSL"+settings.Prefix+"FormSelectContent").addClass("jSL"+settings.Prefix+"Disabled");*/
			$(this).find(".jSL"+settings.Prefix+"FormSelectContent").slideUp("slow");

		});

		$(".jSL"+settings.Prefix+"FormOption").click(function () {

			$(".jSL"+settings.Prefix+"FormSelect:eq("+IDFormSelect+") .jSL"+settings.Prefix+"FormOption").find("input").removeAttr("checked");
			$(".jSL"+settings.Prefix+"FormSelect:eq("+IDFormSelect+") .jSL"+settings.Prefix+"FormOption").removeClass("jSL"+settings.Prefix+"FormOptionSelected");

			$(this).find("input").attr("checked", "checked").change();
			$(this).addClass("jSL"+settings.Prefix+"FormOptionSelected");

			$(".jSL"+settings.Prefix+"FormSelect:eq("+IDFormSelect+") .jSL"+settings.Prefix+"FormSelectName").html($(this).text());
		});
	};

})(jQuery);
