/**
* Plugin: jQuery Text-constrain, jquery.cmtextconstrain.js
* Copyright: Copyright (c) 2011 CMGdigital
* Version: 1.0.0
* Author: David A. Enete
* Date: 24 February 2011
* Description: jQuery Text-constrain plugin - A jQuery plugin to allow text elements on a page to be constrained by size.
*/

(function($) {
    
    // public methods
    var methods = {
        init: function(options) {
            return this.each(function() {
                var $this = $(this);
                data = $this.data('cmtextconstrain');
                if(!data){
                    opts = $.extend({}, $.fn.cmtextconstrain.defaults, options);
                    $this.data('cmtextconstrain', opts);
                    if(!($this.attr('id'))){
                        var dateObj = new Date();
                        var dateString = String(dateObj.getTime());
                        $this.attr({id: dateString});
                    }
                    $this.addClass('long');
                    cloneID = $this.attr('id') + '_clone';
                    $this.clone().insertBefore($this).attr({id: cloneID});
                    $this.hide();
                    var $elemClone = $('#' + cloneID);
                    
                    // create constrained string
                    if(opts.restrict['type'] == 'words'){
                        var wordArr = $elemClone.text().split(/\s+/);
                        if(wordArr.length <= opts.restrict['limit']){
                            $this.cmtextconstrain('destroy');
                            return;
                        } else {
                            var shortString = wordArr.slice(0,opts.restrict['limit']).join(' ');
                        }
                    } else if(opts.restrict['type'] == 'chars'){
                        var text = $.trim($this.text());
                        if(text.length <= opts.restrict['limit']){
                            $this.cmtextconstrain('destroy');
							$elemClone.hide();
                            return;
                        } else {

                            var charPointer = opts.restrict['limit'];
                            var shortString = text.substr(0,opts.restrict['limit']);
                            var nextChar = '';
                            var stringLength = text.length;
                            var cloneText = $.trim($elemClone.text());
                            while(nextChar != ' ' && stringLength > charPointer) {
                                shortString += nextChar;
                                nextChar = cloneText.charAt(charPointer++);
                            }
                        }
                    }
                    shortString += opts.trailingString;


                    $elemClone.text(shortString);
                    $elemClone.removeClass('long');
                    $(".cmTextElement").hide();
                    $elemClone.show();

                    $(".cmTextElement").hover(function(){
                        $('.cmTextElement').hide();
                        $('.wrapper.hide div').hide();
                        $('.wrapper.hide ul').hide();
                        $('.long').show();
                    },
                    function(){
                        $('.cmTextElement').show();
                        $('.wrapper.hide div').show();
                        $('.wrapper.hide ul').show();
                        $('.cmTextElement.long').hide();
                    });
                }
            });
        },
        destroy: function(){
            return this.each(function(){
                var $this = $(this);
                data = $this.data('cmtextconstrain');
                if(data){
                    $('#' + $this.attr('id') + '_clone').remove();
                    $this.show().removeData('cmtextconstrain');
                }
            })
        }
    };

    // passing public method calls
    $.fn.cmtextconstrain = function(method){
        if(methods[method]){
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || ! method){
            return methods.init.apply(this, arguments);
        } else {
            console.log('Method ' + method + ' does not exist on jQuery.cmtextconstrain');
        }
    };

})(jQuery);


