﻿/*
 * @author     Andrei Eftimie
 * @email      andrei.eftimie@netlogiq.ro
 * @copyright  (c) Netlogiq 
 * @web        http://www.netlogiq.ro
 *             http://www.netlogiq.com
 * 
 * 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() {
	    
	        handle = $(this);
	        if (content) {
	            content = $(content);
	        }
	        if (additionalContent) {
	            additionalContent = $(additionalContent);
	        }
	        	        
	        //Calls the toggle function on triggering the click and change events
	        handle.bind('click change',
	            function(){ 
                    toggleElement($(this),content, additionalContent);
                });
	            
            //Initially calling the function on domready
            $(function(){
               toggleElement(handle,content, additionalContent);
            });
            
		});
	};
	
	function toggleElement(handle, content, additionalContent){
        if (additionalContent) { 
                if (handle.is(':checked')) {
                    additionalContent.fadeOut('fast',
                        function(){
                            content.fadeIn('fast');
                        }
                    );   
                } else {
                    content.fadeOut('fast',
                        function(){
                            additionalContent.fadeIn('fast');
                        }
                    ); 
                }
            
            } else {
                
                if (handle.is(':checked')) {
                    content.fadeIn('fast');
                } else {
                    content.fadeOut('fast');
                }
            };
	}
	
})(jQuery);
