﻿/*******************************************************************************************
 * jquery.wh.dropdowns.js
 * Ersetze Dropdowns durch schönere jQuiiiiiiiiiuery Konstrukte
 * @autor   Klaus Meyer
 * @date    07.12.2009
 * @version 1.03
 * @changes CR 1.01 berücksichtige disabled bei select (07.07.2010)
 * 			CR 1.02 support für hoverIntent Plugin (13.07.2010)
 * 			JR 1.03 Möglichkeit um options unter dropdown zu schieben, class attribut übernehmen
 *			CR 1.04 wenn set_options == null (nicht gesetzt), kommt Fehlermeldung - gefixt
 * @lastmodified 02.12.2010
 *
 *
 * Um beim Laden der Webseite gewünschte Select Felder durch die jQuery-Versionen zu ersetzen,
 * muss die Funktion in die allgemeine Javascript-Datei der Seite eingesetzt werden.
 * Beispiel wie das aussehen kann:
 *
	$(document).ready(function() {
		$("select").not(".listen-filter").wh_dropdown({
			 speed: "fast"
			,type:	"hiddenfield"
		});
		$("select.listen-filter").wh_dropdown({
			 speed: "fast"
			,type:	"hiddenfield"
			,margin_right: 15
		});
	});
 * 
 *******************************************************************************************/
jQuery.fn.wh_dropdown = function(set_options) {
	/***************************************************************************************
	 * Default Options, CR 02.12.2010, set_options default = {} empty object
	 ***************************************************************************************/
	if (set_options == null) set_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);
			
		});
		// Füge disabled Class hinzu (CR)
		if ($(this).attr("disabled")) {
			$wh_dropdown_select.addClass("jquery-wh-select-disabled");
		}
		$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
		//the width will be set in css file, because safari has problems to show the div in the correct width, when it will be set directly to the element, has to be checked why
		//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");
		$wh_dropdown_select.attr("style", $dom_dropdown.attr("style")); //z-index (in style attribute) necessary for IE 7
		$wp_dropdown_options.attr("style", $dom_dropdown.attr("style")); //z-index (in style attribute) necessary for IE 7
		//JR damit das dropdown ggf. unter das Auswahlfeld geschoben werden kann
		var z_index = $wp_dropdown_options.css("z-index") - 1;
		$wp_dropdown_options.css("z-index", z_index);
		$wh_dropdown_container.attr("style", ($dom_dropdown.attr("style"))); //z-index (in style attribute) necessary for IE 7
		// JR class attribute übernehmen
		$wh_dropdown_container.addClass(($dom_dropdown.attr("class")));
		// 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) {
		// finde disabled Class (CR)
		var $disabled = $(this).parent().parent().find("div.jquery-wh-select-disabled");
		if ($disabled.length == 0) {
			// 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, angepasst mit hoverIntent, CR 13.07.2010, 15.11.2010
	 ***************************************************************************************/
	if (typeof ($.fn.hoverIntent) == 'function') {
		$("div.jquery-wh-options").hoverIntent({
			over: function() {},
			timeout: 500,
			out: function() {
				$(this).slideUp(options.speed);
			}
		});
	} else {
		$("div.jquery-wh-options").unbind("mouseleave").bind("mouseleave", function() {
				$(this).slideUp(options.speed);
		});
	}
	/***************************************************************************************
	 * onMouseClick Event Outside of Dropdown, CR 13.07.2010
	$(document).bind("click",function(e){
		if (e.target != $wh_dropdown_container)
		alert(e.target);
	});
	 ***************************************************************************************/
	// Don't break the Chain
	return this;
}

