/*
 * @author     Andrei Eftimie
 * @email      andrei@eftimie.com
 * @copyright  (c) Andrei Eftimie
 * @web        http://designpunct.ro/projects/toggleElement/
 *             http://bitbucket.org/k3liutzu/jquery.toggleelement.js/
 * 
 * Required Markup
 * ---------------
 * <input class="handler" type="radio" />
 * or
 * <input class="handler" type="checkbox" />                        //This is the handler
 *
 * <div class="content">[content]</div>                             //This is the content we wish to toggle relative to the handler
 * <div class="additionalContent">[additional content]</div>        //This is the additional content we wish to toggle opposed the the first content
 *
 * Calling
 * -------
 * $('.handler').toggleElement('.content');                         //Toggles '.content' relative to whether the handler is checked or not
 * or
 * $('.handler').toggleElement('.content', '.additionalContent');   //Toggles '.content' and '.additionalContent' one opposed to eachother
 *
 */

(function($){
	$.fn.toggleElement = function(content, additionalContent, options) {
	
		var defaults = {
			//No options for now
		};
		
		var options = $.extend(defaults, options);
		
	    return this.each(function() {
	    	
	    	//Initialize variables
	        var $handle = $(this);
	        
	        //Checking the type since we are behaving differently on :checkbox vs :radio
	        type = false;	        
	        if ($handle.is(':radio')) type = 'radio'
	        else if ($handle.is(':checkbox')) type = 'checkbox';
	        
	        //Content & Additional Content
	        var $content = $additionalContent = false; //If calling the plugin without parameters	        	        
	        if (content) $content = $(content);	        
	        if (additionalContent) $additionalContent = $(additionalContent);	      	        
	       	
	       	//Radio
			if (type == 'radio') $eventCheck = $(':radio[name="'+$handle[0].name+'"]');
			//Checkbox
			else if (type == 'checkbox') $eventCheck = $handle;								
			
			//Binding the events
			$eventCheck.bind('change click', function(){
				toggleElement($handle, $content, $additionalContent);
			});       	      	      
	            
            //Initially calling the function on domready
            $(function(){
               toggleElement($handle, $content, $additionalContent);
            });
            
		});
	};
	
	function toggleElement($handle, $content, $additionalContent){
	
		//With additional content
        if ($additionalContent) { 
                if ($handle.is(':checked')) {
                    $additionalContent.fadeOut('fast',
                        function(){
                            $content.fadeIn('fast');
                        }
                    );   
                } else {
                    $content.fadeOut('fast',
                        function(){
                            $additionalContent.fadeIn('fast');
                        }
                    ); 
                }            
        }
        
        //No additional content
        else {
            if ($handle.is(':checked')) {
                $content.fadeIn('fast');
            } else {
                $content.fadeOut('fast');
            }
        };
        
	}
	
})(jQuery);
