/**
 * jQuery("#moreless").moreless({height:50, moreText:'show more',lessText:'show less'});
 * moreless -- Given a selector, create a more/less structure.  Activate on parent element container of collapsing element.
 * @usage:  jQuery(".moreless").moreless();
 * @author: mcn
 */
;(function(jQuery) {
	jQuery.fn.moreless = function(options){
		if (typeof(options) == 'undefined') {
			var options = {};
		}
		var newDiv, newDiv2, content;
		var control = jQuery('<a href="" class="adjust"></a>');
		
		// initialize defaults
		var defaultOptions = {
				height: 100,
				width: 600,
				controlHtml: control,
				moreText: 'more',
				lessText: 'hide',
				collapsingClass: '.eventsPromo'
		};
		// override defaults
		for (var key in defaultOptions) {
			if (typeof options[key] == 'undefined') {
				options[key] = defaultOptions[key];
			}
		}
		
		// adjust class is required
		options.controlHtml.addClass("adjust");
		
		/**
		 * Use a temporary element to calculate the height (in PX) of given height (even proportional)
		 * Takes:   (string) Any valid CSS height
		 * Returns: (int)    Minimum Pixel value of a block display element having some text
		 */
		function getCalculatedHeight(givenHeight) {
			var tempStyle = {
				"position":"absolute",
				"left":"-9000px",
				"top":"-9000px",
				"z-index":"-9999",
				"visibility":"hidden",
				"height": givenHeight
			};
			var temporaryElement = jQuery('<div id="temporaryElement">loremipsum</div>').css(tempStyle);
			jQuery("body").append(temporaryElement);
			var calculatedHeight = temporaryElement.height();
			temporaryElement.remove();
			return calculatedHeight;
		}
		
		// create more/less structure from given selector
		jQuery(this).each(function(){
			
			// If the height of the collapsing class is less than the height, then do nothing.
			if (jQuery(this).find(options.collapsingClass).height() !== null && jQuery(this).find(options.collapsingClass).height() < getCalculatedHeight(options.height) ) {
				return;
			}
			// If the collapsing class is empty then do nothing.
			if (jQuery(this).find(options.collapsingClass).is(":empty")) {
				return;
			}
			var id  = typeof jQuery(this).attr("id") != "undefined" ? jQuery(this).attr("id") : 'jqmoreless'+Math.floor(Math.random()*50000000);
			content = jQuery("<p></p>").html(jQuery(this).html());
			newDiv  = jQuery("<div></div>").addClass("more-block").append(content);
			newDiv2 = jQuery("<div></div>").addClass("more-less").append(newDiv).addClass(jQuery(this).attr('class')).attr("id",id);
			jQuery(this).replaceWith(newDiv2);
			
			/**
			 * More / Less 
			 * ref [http://shakenandstirredweb.com/240/jquery-moreless-text]
			 */
			var controlHtml = options.controlHtml;
			
			// The "more" link text
			var moreText = options.moreText;
			// The "less" link text
			var lessText = options.lessText;	
			// Sets the .more-block div to the specified height and hides any content that overflows
			jQuery("#"+id).find(".more-block").css('height', options.height).css('overflow', 'hidden');
			// The section added to the bottom of the "more-less" div
			jQuery("#"+id).append(options.controlHtml);
			// Set the "More" text
			jQuery("#"+id).find("a.adjust").html(moreText);
			jQuery("#"+id).find(".adjust").toggle(function() {
			    jQuery(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
			    // Hide the ...more when expanded
			    jQuery(this).parents("div:first").find("p.continued").css('display', 'none');
			    jQuery(this).html(lessText);
			}, function() {
			    jQuery(this).parents("div:first").find(".more-block").css('height', options.height).css('overflow', 'hidden');
			    jQuery(this).parents("div:first").find("p.continued").css('display', 'block');
			    jQuery(this).html(moreText);
			});
		}); //each
	};
})(jQuery);
