/*******************************************************************************************
 * jquery.wh.dropdowns.js
 * Ersetze Dropdowns durch schönere jQuiiiiiiiiiuery Konstrukte
 * @autor   Klaus Meyer
 * @date    07.12.2009
 * @version 1.0
 *******************************************************************************************/
jQuery.fn.wh_dropdown = function(set_options) {
	/***************************************************************************************
	 * Default Options
	 ***************************************************************************************/
	var options = {
		 speed:			set_options.speed 			|| "slow"
		,type:			set_options.type  			|| "hiddenfield"
		,margin_right:	set_options.margin_right  	|| 0
	};
	/***************************************************************************************
	 * Initialisierung
	 ***************************************************************************************/
	$(this).each(function () {
		$dom_dropdown          = $(this);
		$wh_dropdown_container = $("<div class='jquery-wh-dropdown'></div>");
		$wh_dropdown_select    = $("<div class='jquery-wh-select'></div>");
		$wh_dropdown_caption   = $("<span class='jquery-wh-caption'></span>");
		$wh_dropdown_selector  = $("<a class='jquery-wh-selector' href='javascript:void(0);'>&nbsp;</a>");
		$wp_dropdown_options   = $("<div class='jquery-wh-options'></div>");
		$options = $(this).find("option");
		// Über alle Options loopen
		$options.each(function () {
			// Option in WH-SELECT erstellen
			var $wh_dropdown_option = $("<a class='jquery-wh-option' href='javascript:void(0);' rel='" + $(this).val() + "' title='" + $(this).html() + "'>" + $(this).html() + "</a>");
			// Eventuell onClick Event übernehmen
			if("function" == typeof($(this).attr("onclick"))) {
				$wh_dropdown_option.bind("click", $(this).attr("onclick") );
			}
			$wp_dropdown_options.append($wh_dropdown_option);
			
		});
		$wh_dropdown_select.append($wh_dropdown_caption).append($wh_dropdown_selector);
		$wh_dropdown_container.append($wh_dropdown_select).append($wp_dropdown_options);
		if("hiddenfield" == options.type) {
			var $hiddenfield = $("<input type='hidden' name='" + $dom_dropdown.attr("name") + "' id='" + $dom_dropdown.attr("id") + "' value='" + $dom_dropdown.val() + "' />");
			$wh_dropdown_container.append($hiddenfield);
		}
		// Breite des originalen Selects übernehmen
		var width = $dom_dropdown.width() == 0 ? parseInt($dom_dropdown.css("width").replace("px","")) : $dom_dropdown.width();
		$wh_dropdown_select.css("width",  width + options.margin_right + "px");
		$wp_dropdown_options.css("width", width + options.margin_right + "px");
		// Wert des originalen Selects übernehmen
		$wh_dropdown_caption.html($dom_dropdown.find("option").filter(':selected').text());
		$dom_dropdown.after($wh_dropdown_container).remove();
	}); 
	/***************************************************************************************
	 * OnClick Event des "Selektors"
	 ***************************************************************************************/
	$("a.jquery-wh-selector").unbind("click").bind("click",function(e) {
		// Option Elemente auswählen
		var $options = $(this).parent().parent().find("div.jquery-wh-options");
		// Alle anderen schließen
		$("div.jquery-wh-options").not($options).hide();
		// Menü auf- / zuklappen
		$options.slideToggle(options.speed);
	});
	$("span.jquery-wh-caption").unbind("click").bind("click", function() {
		$(this).parent().find("a.jquery-wh-selector").click();
	});
	/***************************************************************************************
	 * OnClick Event einer Option
	 ***************************************************************************************/
	$("a.jquery-wh-option").bind("click", function() {
		var $options = $(this).parent().parent().find("div.jquery-wh-options");
		// Caption neu setzen
		$(this).parent().parent().find("span.jquery-wh-caption").html($(this).html());
		// Hidden Field setzen
		if("hiddenfield" == options.type) {
			$(this).parent().parent().find("input[type=hidden]").val($(this).attr("rel"));
		}
		// Menü zuklappen
		$options.slideUp(options.speed);
	});
	/***************************************************************************************
	 * onMouseLeave Event der Options
	 ***************************************************************************************/
	$("div.jquery-wh-options").unbind("mouseleave",function() {
		$(this).slideUp(options.speed);
	});
	// Don't break the Chain
	return this;
}

/*******************************************************************************************
 * Beim Laden der Webseite alle Select Felder durch jQuery-Versionen ersetzen
 *******************************************************************************************/
$(document).ready(function() {
	//return 0;
	$("select").not(".listen-filter").wh_dropdown({
		 speed: "fast"
		,type:	"hiddenfield"
	});
	$("select.listen-filter").wh_dropdown({
		 speed: "fast"
		,type:	"hiddenfield"
		,margin_right: 15
	});
});