/*
 *
 * Reduces text and creates a read more link.  Readmore link will add a tool tip with full text
 * 
 * Copyright (c) 2011 Rob LaSota
 * rob.lasota@gmail.com
 */

(function($) {
 
    var options = {
    	characters						:	100,
    	defaultWidth					:	500,
    	defaultPadding					:	25,
    	defaultBackgroundColor			:	'#fff',
    	defaultMaskBackground			:	'#000',
    	defaultMaskBackgroundOpacity	:	'.5',
    	defaultCloseButtonPath			:	'assets/media/images/closeButton.png'
    };
    
    var mask,
    	readMore,
    	windowWidth,
    	documentHeight,
    	windowHeight,
    	startPos,
    	readMoreCss;
    
    
   	var methods = {
        
        init: function(params) {
        	
        	if ( params ) { 
        		$.extend( true, options, params );
      		}
      		
      		var target = $(this);
      		target.each(function(){
	      		
	      		var fullText = $(this).html();
	      		
	      		var reducedText = fullText.substring(0,options.characters);
	      		
	      		$(this).html(reducedText);
				
	      		$(this).append($('<a/>',{
					"class"	:	"readMore",
					text 	:	"...Read More",
					click	:	function(){methods.showFull(fullText,reducedText)}
				}));
			});
			
			        
        },
        
        showFull: function(fullText,reducedText){
        	
        	windowWidth = $(window).width();
        	documentHeight = $(document).height();
    		windowHeight = $(window).height();
    		startPos = $(window).scrollTop();
        	
        	mask = $('<div/>',{
        		'id'	:	'readMoreMask'
        	});
        	
        	readMore = $('<div/>',{
				'class'		:	'fullText',
				'html'		:	fullText
			});
			
			maskCss = {
				'z-index'			:	'2000',
				'position'			:	'absolute',
				'top'				:	0,
				'left'				:	0,
				'width'				:	windowWidth,
				'height'			:	documentHeight,
				'backgroundColor'	:	options.defaultMaskBackground,
				'opacity'			:	options.defaultMaskBackgroundOpacity
			
			}
			
			readMoreCss = {
				'z-index'			:	'2001',
	    		'position'			:	'absolute',
	    		'left'				:	windowWidth/2-options.defaultWidth/2,
	    		'backgroundColor'	:	options.defaultBackgroundColor,
	    		'width'				:	options.defaultWidth,
	    		'padding'			:	options.defaultPadding
	    	}
			
			closeButton = $('<div/>',{
				'id'	:	'closeReadMore'
			});
			
			closeButtonCss = {
				'backgroundImage'	:	'url(' + options.defaultCloseButtonPath + ')',
				'backgroundRepeat'	:	'no-repeat',
				'position'			:	'absolute',
				'top'				:	-20,
				'right'				:	-20,
				'height'			:	40,
				'width'				:	40
					
			}
						
			mask.appendTo('body').css(maskCss);
			
			readMore.appendTo('body').css(readMoreCss);
			
			closeButton.appendTo(".fullText").css(closeButtonCss);
			
			var divHeight = readMore.outerHeight();
			
			readMore.css('top', (windowHeight/2-divHeight/2)+startPos);
			
			mask.click(methods.removeFull);
			readMore.click(methods.removeFull);
			
			/* $('body').css('overflow','hidden'); */
        },
        
        removeFull:	function(){
        	
        	mask.remove();
        	readMore.remove();
        	$('body').css('overflow','visible');
        }
    };
	
	//Plug-in (change 'template' to whatever your plugin is called) /////////////////////
    $.fn.readMore = function(method) {
        if (methods[method]){
			//Method call
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
			//Original call
            return methods.init.apply(this, arguments);
        } else {
			//Error for unidentified call
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };
    
})(jQuery);
