/* 

	jQuery Dot Trails Pro
	Version:    1.0
	Created By: Tyler Colwell
	Website:    http://tyler.tc/devpress/
	
	Copyright © 2011 Tyler Colwell

*/

// Start plugin
(function($){

	// Start main plugin function
	$.fn.dotTrailsPro = function(options){
		
				// Setup the options for the tooltip that can be accessed from outside the plugin
				var defaults = {
					length: 100,
					more: "Read more",
					less: "Read less",
					ani_speed: 600,
					out_animation: "slide",
					expandable: true
				};

				// Extend options and apply defaults if they are not set
				var options = $.extend(defaults, options);
				
				// For each matching element:
				$(this).each(function(){
										
					// Cache any vars
					var $new_length = defaults.length-3;			
					var $og_string = $(this).html();
					
					// If false on expansion
					if(defaults.expandable == false){
						
						// Retrun text only
						var $ihtml = $og_string.substr(0, $new_length) + '...';
						
					} else { // Otherwise build the expansion in
					
						// Create the string and divs for the short string
						var $temp_string = '<div class="proUnhidden">' + $og_string.substr(0, $new_length) + '... <a class="morebtn" href="#' + defaults.more + '">' + defaults.more + '</a></div>';
						
						// Create the string and div for the full string
						var $ihtml = $temp_string + '<div class="proHidden" style="display:none;">' + $og_string + ' <a class="lessbtn" href="#' + defaults.less + '">' + defaults.less + '</a></div>';
						
					}
					
					// Replace the current element with the new HTML string
					$(this).html($ihtml);
					
				});
				
				// When more button is clicked
				$('.morebtn').click(function(){
					
					// Fade out shot string
					$(this).parent().fadeOut(defaults.ani_speed, function(){
						
						// On complete fade in the full string													
						$(this).parent().children('.proHidden').fadeIn(defaults.ani_speed);
						
					});
					
				}); // End more button click
	
				// When less button is clicked
				$('.lessbtn').click(function(){
					
					// If set to use slide out:
					if(defaults.out_animation == "slide"){
						
						// Slide out the full string
						$(this).parent().slideUp(defaults.ani_speed, function(){
							
							// Fade in the short string												
							$(this).parent().children('.proUnhidden').fadeIn(defaults.ani);
							
						}); // end animation
						
					} else { // Use the fade animation
						
						// Fade out the full string
						$(this).parent().fadeOut(defaults.ani_speed, function(){
							
							// Fade in the short string												
							$(this).parent().children('.proUnhidden').fadeIn(defaults.ani);
							
						}); // end animation
						
					} // End if else for exit animation
					
				}); // End less button click

	}; // End Main Function

})(jQuery); // End Plugin

