/**
 * 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.
 * 
 * Don't define height to use the default height, which is 3x the line-height of any given div prepended to dom.
 * @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>').addClass("adjust");
		
		// Default Height is 3x a Div's lineheight
	  if (jQuery.browser.msie) {
			threeXLineHeight = "60px";
		}
		else {
			var threeXLineHeight =  3 * parseInt($("<div></div>").css("line-height").replace(/px/, ''));
		}
		
		// initialize defaults
		var defaultOptions = {
				height: threeXLineHeight,
				width: "600px",
				controlHtml: control,
				moreText: 'more &raquo;',
				lessText: 'hide &raquo;',
				collapsingClass: '.eventsPromo'
		};
		// override defaults
		for (var key in defaultOptions) {
			if (typeof options[key] == 'undefined') {
				options[key] = defaultOptions[key];
			}
		}
		/**
		 * 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").prepend(temporaryElement);
			var calculatedHeight = temporaryElement.height();
			temporaryElement.remove();
			return calculatedHeight;
		}
		// create more/less structure from given selector
		jQuery(this).find(options.collapsingClass).each(function(){
			// If the collapsing class is empty then do nothing.
			if (jQuery(this).is(":empty")) {
				return;
			}
			// If the height of the collapsing class is less than or equal to the set height then do not do more/less
			if (jQuery(this).height() !== null && jQuery(this).height() <= getCalculatedHeight(options.height)){
				return;
			}
			// Get id (or create random one) of current more/less element to key off on for applying controls below
			var id  = ( (typeof jQuery(this).attr("id") != "undefined") && (jQuery(this).attr("id") !== '') ) ? jQuery(this).attr("id") : 'jqmoreless'+Math.floor(Math.random()*50000000);
			
			// Rewrite the given more/less element with more/less block classes
			newDiv  = jQuery("<div></div>").addClass("more-block");
			newDiv2 = jQuery("<div></div>").addClass("more-less").append(newDiv).addClass(jQuery(this).attr('class')).attr("id",id);
			jQuery(this).replaceWith(newDiv2);
			newDiv.append(jQuery(this));
			
			/**
			 * More / Less ref [http://shakenandstirredweb.com/240/jquery-moreless-text]
			 */
			var controlHtml = options.controlHtml;
			var moreText 		= options.moreText;
			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);
			
			// Toggle the more/less text and change the height/overflow
			jQuery("#"+id).find(".adjust").toggle(function() {
			    jQuery(this).parents("div:first").find(".more-block").css({'height': 'auto', 'overflow': 'visible'});
			    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, 'overflow': 'hidden'});
			    jQuery(this).parents("div:first").find("p.continued").css('display', 'block');
			    jQuery(this).html(moreText);
			});
		}); //each
	};
})(jQuery);
