﻿(function ($) {
		
		jQuery.fn.advancedreadmore = function (options) {
			
			//check if element has already been truncated
			if ($(this).hasClass('truncatedtrue')) {
				return;
			}
			
			//set default variables for plugin
			var defaults = {
				maxWords : 60,
				moreText : 'Read more',
				lessText : 'Hide text',
				linkPosition : 'right',
				showOriginal : 'fade',
				showOriginalDirection : 'down',
				hideOriginal : 'fade',
				hideOriginalDirection : 'up',
				showTruncated : 'fade',
				showTruncatedDirection : 'down',
				hideTruncated : 'fade',
				hideTruncatedDirection : 'down',
				showSpeed : 300,
				hideSpeed : 300,
				highlightLink : true,
				highlightColor : '#ffff99',
				highlightSpeed : 300,
				linkClass : 'show-hide-link',
				linkTopMargin : 10,
				linkBottomMargin : 10,
				alternativeText : null,
				truncatedTextMarker : "...",
				flexibility: null
			}
			
			//merge user provided options with the default ones in a new object
			
			var settings = $.extend({}, defaults, options);
			
			// use each method to allow the plugin to work on multiple DOM elements
			this.each(function () {
					
					//create reference for current element
					var $currentel = $(this);
					
					// if there is alternative text provided by the user use it
					
					if (settings.alternativeText != null) {
						$currentel.wrapInner('<div class="original-text" />');
						$currentel.append('<div class="truncated-text"></div>');
						$currentel.children(".original-text").hide();
						$currentel.children(".truncated-text").append(settings.alternativeText);
						var $linkContainer = $('<div class="link-container"></div>');
						$linkContainer.css("margin-top", settings.linkTopMargin);
						$linkContainer.css("margin-bottom", settings.linkBottomMargin);
						var $moreLink = $('<a href="javascript:;"></a>');
						$moreLink.text(settings.moreText);
						$moreLink.addClass(settings.linkClass);
						$currentel.children(".truncated-text").append(settings.truncatedTextMarker);
						$currentel.children(".truncated-text").after($moreLink);
						$moreLink.wrap($linkContainer);
						$currentel.children(".link-container").css("text-align", settings.linkPosition);
					}
					
					//if there is no alternative text proceed and truncate the original text
					else if (settings.alternativeText == null) {
						var originalContent = $currentel.text();
						
						//creates an array to hold all the words
						originalNumWords = jQuery.trim(originalContent).split(" ");
						
						//if content words are less than that set by the user AND flexibility is NOT defined than stop the function
						if ((originalNumWords.length < settings.maxWords) && (!settings.flexibility)) {
							return;
						 }
						
						
						//Check the flexibility parameter AND if maxWords is within the limit then stop the function
						if (settings.flexibility)
						{
						var upperLimit = settings.maxWords + settings.flexibility;
						
								if (originalNumWords.length < upperLimit)
								{	
									return;
								}
						
						}
						
						//create containers for original text, truncated text and hide the original
						$currentel.wrapInner('<div class="original-text" />');
						$currentel.append('<div class="truncated-text"></div>');
						//$currentel.children(".original-text").hide();
						$currentel.children(".truncated-text").hide();
						
						//iterate through all words and add them to the truncated container
						jQuery.each(originalNumWords, function (index, value) {
								
								$currentel.children(".truncated-text").append(originalNumWords[index] + " ");
								
								//when the word limit is reached add the container for the link and additional options and quit
								if (index == (settings.maxWords - 1)) {
									var $linkContainer = $('<div class="link-container"></div>');
									$linkContainer.css("margin-top", settings.linkTopMargin);
									$linkContainer.css("margin-bottom", settings.linkBottomMargin);
									var $moreLink = $('<a href="javascript:;"></a>');
									$moreLink.text(settings.lessText);
									$moreLink.addClass(settings.linkClass);
									$currentel.children(".truncated-text").append(settings.truncatedTextMarker);
									$currentel.children(".truncated-text").after($moreLink);
									$moreLink.wrap($linkContainer);
									$currentel.children(".link-container").css("text-align", settings.linkPosition);
									
									return false;
								}
							});
					}
					
					//add truncatedtrue class to the parent element so it won't be truncated again
					$(this).addClass('truncatedtrue');
					
					//add the link logic with the show/hide animation options for each truncated element
					$currentel.find("." + settings.linkClass + ":first-child").click(function () {
							if ($(this).parent().prevAll(".original-text").css("display") == 'none') {
								$(this).parent().prevAll(".truncated-text").hide(settings.hideTruncated, {
										direction : settings.hideTruncatedDirection
									}, settings.hideSpeed, function () {
										$(this).nextAll(".link-container").children("a").text(settings.lessText);
										$(this).prevAll(".original-text").show(settings.showOriginal, {
												direction : settings.showOriginalDirection
											}, settings.showSpeed);
										
										if (settings.highlightLink == true) {
											$(this).next(".link-container").children("." + settings.linkClass).stop(true, true).effect("highlight", {
													color : settings.highlightColor
												}, settings.highlightSpeed);
											
										}
									});
								
							} else {
								$(this).parent().prevAll(".original-text").hide(settings.hideOriginal, {
										direction : settings.hideOriginalDirection
									}, settings.hideSpeed, function () {
										$(this).nextAll(".link-container").children("a").text(settings.moreText);
										$(this).next(".truncated-text").show(settings.showTruncated, {
												direction : settings.showTruncatedDirection
											}, settings.showSpeed);
										
										if (settings.highlightLink == true) {
											$(this).nextAll(".link-container").children("a").stop(true, true).effect("highlight", {
													color : settings.highlightColor
												}, settings.highlightSpeed);
										}
									});
								
							}
							
						});
					
				});
			
		};
	})(jQuery);
 
