/*jslint strict:true, evil: true, onevar: true, nomen: false  */ /*global $, jQuery, console, hbx, window, ndm, document  */

"use strict";

/*
    Function: jQuery.fn.CG_invokeAccordion
    Simple open and close function for accordions
*/

jQuery.fn.CG_checkFunctionKey = function (event,allowPaste) {
    var curKey = event.keyCode,
        ctrlKey = event.ctrlKey,
        shiftKey = event.shiftKey,
        altKey = event.altKey,
        functionKeys = [8,9,13,16,17,27,33,34,35,36,37,38,39,40,45,46,112,113,114,115,116,117,118,119,120,121,122,123],
        ctrlFunctionKeys = [67,86],
        checkFunctKeys = false,
        checkCtrlKeys = false,
        checkPaste = false,
        checkSelectedText = false;

    if(shiftKey){}
    if(altKey){}

    if($.inArray(curKey, functionKeys) !== -1){
        checkFunctKeys = true;
    }

    if(ctrlKey && $.inArray(curKey, ctrlFunctionKeys) !== -1){
        checkCtrlKeys = true;
    }

    if(ctrlKey && curKey === 86){
        checkPaste = true;
    }

    if(document.selection || document.selectionStart){
        checkSelectedText = true;
    }

    if(checkFunctKeys || checkCtrlKeys){
        if(allowPaste){
            return true;
        }else{
            if(!checkPaste){
                return true;
            }else{
                return false;
            }
        }
    }else{
        return false;
    }
};

jQuery.fn.CG_charCounter = function (options) {
    jQuery(this).each(function() {

        var fieldID = $(this).attr("id"),
            settings = jQuery.extend({
            ccMinChars : 0,
            ccMaxChars : 100,
            ccId :  fieldID + '_charCounter',
            ccTag: 'span',
            ccTagClass: 'CG_charCounter',
            ccNumTag: 'strong',
            ccMsgTag: 'em',
            ccMsg : 'characters remaining',
            ccWarnChars : 10,
            ccWarnClass : 'CG_charCounter_warn'
        },options||{}),

        //Create the character counter html

        ccTagStart = '<' + settings.ccTag + ' id="' + settings.ccId + '_holder" class="' + settings.ccTagClass + '">',
        ccNumHTML = '<' + settings.ccNumTag + ' id="' + settings.ccId + '">' + (settings.ccMaxChars - $(this).val().length) + '</' + settings.ccNumTag + '>',
        ccMsgHTML = '<' + settings.ccMsgTag + '>' + settings.ccMsg + '</' + settings.ccMsgTag + '>',
        ccTagEnd = '</' + settings.ccTag + '>',
        charsLeft,
        curCharCount,
        curVal,
        shortVal;

        $(this).after(ccTagStart + ccNumHTML + ccMsgHTML + ccTagEnd);

        $(this).keydown(function(event){
            curVal = $(this).val();
            curCharCount = curVal.length;
            charsLeft = settings.ccMaxChars - curCharCount;

            if(charsLeft <= settings.ccMinChars){
                //Allow function keys only and not paste
                if(!$(this).CG_checkFunctionKey(event,false)){
                    event.cancelBubble = true;
                    event.returnValue = false;
                    event.preventDefault();
                    return false;
                }
            }
        });

        $(this).keyup(function(event){
            curVal = $(this).val();
            curCharCount = curVal.length;
            charsLeft = settings.ccMaxChars - curCharCount;

            //Catch if they paste something too long and truncate it
            if(charsLeft <= settings.ccMinChars) {
                //Truncate the end of the value
                shortVal = curVal.substring(0,settings.ccMaxChars-settings.ccMinChars);
                $(this).val(shortVal);
                charsLeft = settings.ccMinChars;
            }

            if(charsLeft < settings.ccWarnChars){
                $("#"+settings.ccId+"_holder").addClass(settings.ccWarnClass);
            }else{
                $("#"+settings.ccId+"_holder").removeClass(settings.ccWarnClass);
            }

            $("#"+settings.ccId).html(charsLeft);
        });
    });
};
