﻿/*
* twitterCounter
* 
* Displays a counter with the remaining text, in Twitter's style
* 
* Example:
*   $("#description").twitterCounter();
*   $("#description").twitterCounter('bottom'); // Puts the counter at the bottom of the element
*
* $Version: 2009-07-24
* Created by Flavio Pripas (flavio@bymk.com.br) - 2009-07-24
*/
jQuery.fn.twitterCounter = function(arg) {
    var twitterCounterChars_Max = 140;
    var twitterCounterChars_Watch = 30;
    var twitterCounterChars_Warning = 15;
    var twitterCounterFontFamily = "'Georgia','Serif'";
    var twitterCounterFontSize = "22pt";
    var twitterCounterColor_OK = "#cccccc";
    var twitterCounterColor_Watch = "#d40d12";
    var twitterCounterColor_Warning = "#5c0002";
    var twitterCounterColor_Error = "#000000";

    var currentSize = $(this).val().length;
    var charsLeft = twitterCounterChars_Max - currentSize;

    if ($(this).hasClass("twitterCounter_Enabled")) {
        $("#twitterCounter_Text").text(charsLeft);

        if (charsLeft < 0) {
            $("#twitterCounter_Text").css("color", twitterCounterColor_Error);

            if (arg != null) {
                $(this).val($(this).val().substring(0, twitterCounterChars_Max));
                $("#twitterCounter_Text").text(0);
                arg.preventDefault();
                arg.stopPropagation();
                return false;
            }
        } else if (charsLeft <= twitterCounterChars_Warning) {
            $("#twitterCounter_Text").css("color", twitterCounterColor_Warning);
        } else if (charsLeft <= twitterCounterChars_Watch) {
            $("#twitterCounter_Text").css("color", twitterCounterColor_Watch);
        } else {
            $("#twitterCounter_Text").css("color", twitterCounterColor_OK);
        }

        return true;
    }
    else {
        // Create the twitterCounter element
        $(document.body).append("<div id='twitterCounter_Text' style='position: absolute; width: 60px; height: 38px; text-align: right; z-index: 1000;'>" + twitterCounterChars_Max.toString() + "</div>");
        $("#twitterCounter_Text").css("font-family", twitterCounterFontFamily);
        $("#twitterCounter_Text").css("font-size", twitterCounterFontSize);
        $("#twitterCounter_Text").css("color", twitterCounterColor_OK);

        var offset = $(this).offset();
        $("#twitterCounter_Text").css("left", offset.left + $(this).width() - 60);

        if (arg != null) {
            if (arg == 'bottom') {
                $("#twitterCounter_Text").css("top", offset.top + $(this).height());
            }
            else {
                $("#twitterCounter_Text").css("top", offset.top - 38);
            }
        }
        else {
            $("#twitterCounter_Text").css("top", offset.top - 38);
        }

        $(this).addClass("twitterCounter_Enabled");

        $(this).keyup(function(event) {
            $(this).twitterCounter(event);
        });
    }
};
