/**
 * (c) by aheadware.com
 */

var app = app || {};
app.util = app.util || {};

app.util.Helper = {

    /**
     * @param {string} str
     * @param {string} characters
     * @param {string} flags
     */
    trim : (function ()
    {
        "use strict";

        function escapeRegex(string)
        {
            return string.replace(/[\[\](){}?*+\^$\\.|\-]/g, "\\$&");
        }

        return function trim(str, characters, flags)
        {
            characters = characters || " ";
            flags = flags || "g";
            if (typeof str !== "string" || typeof characters !== "string" || typeof flags !== "string")
            {
                throw new TypeError("argument must be string");
            }

            if (!/^[gi]*$/.test(flags))
            {
                throw new TypeError("Invalid flags supplied '" + flags.match(new RegExp("[^gi]*")) + "'");
            }

            characters = escapeRegex(characters);

            return str.replace(new RegExp("^[" + characters + "]+|[" + characters + "]+$", flags), '');
        };
     }())

    ,

    crop : function( text, maxChars )
    {
        var mc = +maxChars;

        if ( text.length >  ( maxChars + 3 ) )
        {
            text = text.substring( 0, maxChars );
            text = '...';
        }

        return text;
    }

    ,

    getUnixTimeStampFromUTCTime : function( dateTime )
    {
        var serverUtc = dateTime,
            serverUtcMoment, serverLocalMoment;

        if ( dateTime && dateTime.hasOwnProperty( 'date' ) )
        {
            serverUtc = dateTime.date;
        }

        return moment.utc( serverUtc ).unix();
    }

    ,

    getMomentFromUTCTime : function( dateTime )
    {
        var serverUtc = dateTime,
            serverUtcMoment, serverLocalMoment;

        if ( dateTime && dateTime.hasOwnProperty( 'date' ) )
        {
            serverUtc = dateTime.date;
        }

        serverUtcMoment = moment.utc( serverUtc );
        return moment.unix( serverUtcMoment.unix() );
    }

    ,

    copyInputElementClipboard : function( inputElement )
    {
        if ( inputElement )
        {
            inputElement.select();
            document.execCommand( "copy" );
            inputElement.blur();
            return true;
        }
        else
        {
            return false;
        }
    }

    ,

    getMomentFromInputDate : function( $inputDate )
    {
        return moment( $inputDate.val(), moment.HTML5_FMT.DATE ).add( 12, 'hours' );
    }

    ,

    getMomentFromInputDateAndTime : function( $inputDate, $inputTime )
    {
        return moment( $inputDate.val() + "T" + $inputTime.val(), moment.HTML5_FMT.DATETIME_LOCAL )
    }

    ,

    nl2br : function( text )
    {
        return text.replace(/\r?\n/g, '<br />');
    }

    ,

    copyInputToClipboard : function( $input )
    {
        var copyText = $input.get( 0 );
        copyText.select();
        document.execCommand( "copy" );
    },

    /**
     * Example:
     * YourObject.sort( app.util.Helper.dynamicSort( '[-]propertyName' ) );
     *
     * @param property use "-" for descending
     * @returns {function(*, *): number}
     */
    dynamicSort : function( property )
    {
        var sortOrder = 1;
        if( property[0] === "-" )
        {
            sortOrder = -1;
            property = property.substr(1);
        }

        return function (a,b)
        {
            /* next line works with strings and numbers,
             * and you may want to customize it to your needs
             */
            var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
            return result * sortOrder;
        }
    }
};