/************************************************************************
*	swVisToggler v1.0
*	Show or hide one or more Objects on click or valuechange of
*	input, radio, checkbox, button or select.
*	Multiple selectors are possible. View selector-specification
*	of JQuery for syntax: http://docs.jquery.com/Selectors
*
*	Author: Christian Koban/Manfred Wischin
*	Date: 25.05.2009
*	Copyright (c) 2009 Agentur Zeitpunkt (agentur-zeitpunkt.at)
*========================================================================
************
*=> RADIO:
*	Syntax: $.swVisToggler.radio("div", "selector", "value");
*		div		...	Object to toggle
*		selector...	Input selector
*		value	...	if this value is selected, the div is shown
*
*	-> EXAMPLES:
*	$.swVisToggler.radio("#contB, #contC", "input:radio[name=myradio]", "Y");
*	$.swVisToggler.radio("#contA .mydiv", "input:radio[name=myTest]", "This is a Value");
************
*=> BUTTON:
*	Syntax: $.swVisToggler.button("div", "button", "initState");
*		div		...	Object to toggle
*		button	...	Object or button to click on
*		initState...Initial status of ->div (show||hide)
*
*	-> EXAMPLES:
*	$.swVisToggler.button("#contA", "#TestButton1", "show");
*	$.swVisToggler.button("#contA, #contB", "#TestButton1, #TestButton2", "hide");
************
*=> SELECT:
*	Syntax: $.swVisToggler.select("div", "selector", "value");
*		div		...	Object to toggle
*		selector...	Input selector
*		value	...	if this value is selected, the div is shown //[true/false/value]
*					set string "false" to show div when no value is selected
*					set string "true" to show div when no value is selected
*
*	-> EXAMPLES:
*	$.swVisToggler.select("#contD1", "select:select[name=mysel]", "A"); //show div on value "A"
*	$.swVisToggler.select("#contD2, #contD1", "#mysel:select[name=mysel]", "C"); //show div on value "C"
*	$.swVisToggler.select("#contD2, #contD1", "#mysel:select[name=mysel]", "false"); //show div if no value selected
************
*=> CHECKBOX:
*	-> EXAMPLES:
*	$.swVisToggler.checkbox({
*			checkbox: "input:checkbox[name=mycheckbox1]",	//checkbox-button to click for toggling [string]
*			invert: true,									//invert init status [true/false] (default false: checked=open)
*			invertTarget: "#contE1Toggle, #contE4",			//2nd element to toggle against "target" [string]
*			target: "#contE1"								//1st element to toggle [string]
*		});
************************************************************************/
jQuery.swVisToggler = {

	radio: function(target, elmRadios, value) {
		
		$(target).wrap("<div></div>");
		$(target).each(function() {
			$(this).css("height", $(this).height()+"px");
			if ($(elmRadios).val() != value) $(this).parent().hide();
		});
		
		var oldValue = $(elmRadios).val();
		
		$(elmRadios).click(function() {
				
				elmVal = $(this).val();
				if (elmVal == value) {
					if (oldValue != elmVal) {
						$.swVisToggler.animation($(target).parent(), "show");
					}
				} else {
					$.swVisToggler.animation($(target).parent(), "hide");
				}
				oldValue=elmVal;
			});
	},

	button: function(target, elmButtons, initState) {
		
		$(target).wrap("<div></div>");
		$(target).each(function() {
			$(this).css("height", $(this).height()+"px");
			if (initState == "hide") $(this).parent().hide();
		});
		//display flicker fix first shown, then hidden
		//set style display=none in html first for hidden elements!
		$(target).css("display","block");
		
		$(elmButtons).click(function() {
			var targetDisplay = $(target).parent().css("display");
			if (targetDisplay == "block") { $.swVisToggler.animation($(target).parent(), "hide");
			} else $.swVisToggler.animation($(target).parent(), "show");
			$(this).blur();
			return false;
		});
	},

	select: function(target, elmSelects, value) {
		
		$(target).wrap("<div></div>");
		$(target).each(function() {
			$(this).css("height", $(this).height()+"px");
			
			switch (value) {
				case "true":
					if (!$(elmSelects).val()) $(this).parent().hide();
					break;
					
				case "false":
					break;
					
				default:
					var values = value.split(",");
					
					var selVal = "";
					
					for(var i = 0; i < values.length; i++){
						//sometimes we want to trigger the same dif with different values
						if ($(elmSelects).val() == $.trim(values[i])){
							selVal = $.trim(values[i]);
							break;
						}							
					}

					if ($(elmSelects).val() != selVal) $(this).parent().hide();
					break;
			}
			
		});
		
		$(elmSelects).change(function() {
			elmVal = $(this).val();
			
			switch (value) {
				case "true":
					if (elmVal) { $.swVisToggler.animation($(target).parent(), "show");
					} else $.swVisToggler.animation($(target).parent(), "hide");
					break;
					
				case "false":
					if (!elmVal) { $.swVisToggler.animation($(target).parent(), "show");
					} else $.swVisToggler.animation($(target).parent(), "hide");
					break;
					
				default:
				
					var values = value.split(",");
					
					var selVal = "";
					
					for(var i = 0; i < values.length; i++){
						//sometimes we want to trigger the same dif with different values
						if (elmVal == $.trim(values[i])){
							selVal = $.trim(values[i]);
							break;
						}	
					}
				
					if (elmVal == selVal) { $.swVisToggler.animation($(target).parent(), "show");
					} else $.swVisToggler.animation($(target).parent(), "hide");
					break;
			}
		});	
	},

	checkbox: function(options) {
		var defaults = {
				target: "",
				checkbox: "",
				invertTarget: "",
				invert: false
			};
		var opts = $.extend(defaults, options);
		
		if (opts.target != "") {
			$(opts.target).wrap("<div></div>");
			$(opts.target).each(function() {
				$(this).css("height", $(this).height()+"px");
				var elmVal = $(opts.checkbox).is(":checked");
				if ((opts.invert && elmVal) || (!opts.invert && !elmVal)) $(this).parent().hide();
			});
		}
		
		if (opts.invertTarget != "") {
			$(opts.invertTarget).wrap("<div></div>");
			$(opts.invertTarget).each(function() {
				$(this).css("height", $(this).height()+"px");
				var elmVal = $(opts.checkbox).is(":checked");
				if ((opts.invert && !elmVal) || (!opts.invert && elmVal)) $(this).parent().hide();
			});
		}
		
		$(opts.checkbox).click(function() {
			elmVal = $(this).is(":checked");
			if (opts.invert) {
				if (opts.invertTarget != "") {
					if (elmVal) {
						$.swVisToggler.animation($(opts.target).parent(), "hide");
						$.swVisToggler.animation($(opts.invertTarget).parent(), "show");
					} else {
						$.swVisToggler.animation($(opts.target).parent(), "show");
						$.swVisToggler.animation($(opts.invertTarget).parent(), "hide");
					}
				} else {
					if (elmVal) { $.swVisToggler.animation($(opts.target).parent(), "hide");
					} else $.swVisToggler.animation($(opts.target).parent(), "show");
				}
			} else {
				if (opts.invertTarget != "") {
					if (elmVal) {
						$.swVisToggler.animation($(opts.target).parent(), "show");
						$.swVisToggler.animation($(opts.invertTarget).parent(), "hide");
					} else {
						$.swVisToggler.animation($(opts.target).parent(), "hide");
						$.swVisToggler.animation($(opts.invertTarget).parent(), "show");
					}
				} else {
					if (elmVal) { $.swVisToggler.animation($(opts.target).parent(), "show");
					} else $.swVisToggler.animation($(opts.target).parent(), "hide");
				}
			}
		});
		
	},

	animation: function (target, value) {
		
		if (value == "hide") { 
			$(target).animate({height: 'hide'}, {duration: 700, easing: 'easeOutCubic'});
		} else if (value == "show") {
			$(target).animate({height: 'show'}, {duration: 700, easing: 'easeOutCubic'});
		}
		
	}


};

