
jQuery.fn.autocorrect = function(options)
{
    // If plugin attached to text/textarea field then don't need to proceed further
    if (0 > jQuery.inArray(jQuery(this).attr("type"), new Array("text", "textarea")))
    {
        return;
    }

    // Default parameters for plugin with some default corrections
    var defaults = {
            corrections: {
					rain: '\u2602',
					love: "\u2665",
					heart: "\u2665",
					club: "\u2663",
					ur : "you are",
					arent: "are not",
					its: "it's",
					aint: "is not",
					isnt: "is not",
					lol: "\u263b",
					phone: "\u260f",
						arrow: "\u279c",
					snow: "\u2603",
						snowman: "\u2603",
					star: "\u2605",
					sun: "\u2600",
					cloud: "\u2601",
					comet: "\u2604",
					lightning: "\u2607",
					tit: "\u2609",
					bomb: "\u260c",
					tick: "\u2611",
					cross: "\u2612",
					left: "\u261c",
						right: "\u261e",
						strawberry: "\u2619",
					skull: "\u2620",
					radioactive: "\u2622",
					russia: "\u262d",
					peace: "\u262e",
					yin: "\u262f",
					sad: "\u2639",
					moon: "\u263e",
					girl: "\u2640",
					female: "\u2640",
					boy: "\u2642",
					male: "\u2642",
					woman: "\u2640",
					man: "\u2642",
					aries: "\u2648",
						taurus: "\u2649",
						gemini: "\u264a",
						cancer: "\u264b",
						leo: "\u264c",
						virgo: "\u264d",
						libra: "\u264e",
						scorpio: "\u264f",
						sagitarius: "\u2650",
						capricorn: "\u2651",
						aquarius: "\u2652",
						pisces: "\u2653",
						king: "\u265a",
						queen: "\u265b",
						knight: "\u2658",
						horse: "\u265e",
						bishop: "\u2657",
						spade: "\u2660",
						diamond: "\u2666",
						spa: "\u2668",
						curry: "\u2668",
						music: "\u266b",
						religion: "\u2671",
						gonna: "\going to",
						pound: "\u00a3",
						copyright: "\u00a9",
						trademark: "\u2122",
						telephone: "\u2121",
						dollar: "\u0024",
						euro: "\u20ac",
						yen: "\u00a5",
						quarter: "\u00bc",
						half: "\u00bd",
						celcius: "\u2103",
						farenheit: "\u2109",
						division: "\u00f7",
						keyboard: "\u2328",
						happy: "\u263a",
						':)': "\u263a",
						smile: "\u263a",
						':(': "\u2639",
						airport: "\u2708",
						airplane: "\u2708",
						jet: "\u2708",
						scissors: "\u2702",
						snowflake: "\u2744",
						sale: "\u2739",
						flower: "\u273f",
						clover: "\u2740",
						pencil: "\u270e",
						write: "\u270d",
						mail: "\u2709",
						post: "\u2709",
						one:	"\u278a",
						two:	"\u278b",
						three:	"\u278c",
						four:	"\u278d",
						five:	"\u278e",
						six:	"\u278f",
						seven:	"\u2790",
						eight:	"\u2791",
						nine:	"\u2792",
						ten:	"\u2793",
						call:  "\u2706",
						arse: "\u328b",
						picnic: "\u314a",
						f: "\u2132",
						u: "\u0244",
						camel: "\u1f42b",
						b: "\u1e03",
					facebook: "\u2132\u1e03",
						tree: "\u267a",
						recycle: "\u267a",
						feet: "\u2032",
						wink: "\u30c4",
						face: "\u0028\u25CF\u032E\u032E\u0303\u2022\u0303\u0029",
						sixtynine: "\u278f\u2792",
						'69': "\u278f\u2792",
						disabled: "\u267f"
						


        }
    };

    // Merge corrections passed at run-time
    if (options && options.corrections)
    {
        options.corrections = jQuery.extend(defaults.corrections, options.corrections);
    }

    // Merge options passed at run-time
    var opts = jQuery.extend(defaults, options);

    // Function used to get caret's position
    getCaretPosition = function(oField)
    {
        // Initialize
        var iCaretPos = 0;

        // IE Support
        if (document.selection)
        {
            // To get cursor position, get empty selection range
            var oSel = document.selection.createRange();

            // Move selection start to 0th position
            oSel.moveStart("character", 0 - oField.value.length);

            // The caret position is selection length
            iCaretPos = oSel.text.length;
        }
        // Firefox support
        else if (oField.selectionStart || oField.selectionStart == "0")
        {
            iCaretPos = oField.selectionStart;
        }

        // Return results
        return (iCaretPos);
    }

    // Function used to set caret's position
    function setCaretPosition (oField, iCaretPos)
    {
        // IE Support
        if (document.selection)
        {
            // Create empty selection range
            var oSel = document.selection.createRange();

            // Move selection start and end to 0 position
            oSel.moveStart("character", 0 - oField.value.length);

            // Move selection start and end to desired position
            oSel.moveStart("character", iCaretPos);
            oSel.moveEnd("character", 0);
        }
        // Firefox support
        else if (oField.selectionStart || oField.selectionStart == "0")
        {
            oField.selectionStart = iCaretPos;
            oField.selectionEnd = iCaretPos;
        }
    }

    // Capture 'on key up' event for auto-correction
    this.keyup(function(e)
    {
        // If currently entered key is not 'space' then don't need to proceed further
        if (32 != e.keyCode)
        {
            return;
        }

        // Get caret's current position
        var caretPosition = (getCaretPosition(this) - 1);

        // If caret's current position is less than one then don't need to proceed further
        if (1 > caretPosition)
        {
            return;
        }

        // Value of current field
        var valueOfField = this.value;

        // Get value of field upto caret's current position from start
        var stringUptoCaretPosition = (valueOfField).substr(0, caretPosition);

        // If more than one 'space' continuously then don't need to proceed further
        if (" " == stringUptoCaretPosition.charAt(caretPosition - 1))
        {
            return;
        }

        // Split string into array using space
        var stringToArray = stringUptoCaretPosition.split(" ");

        // Get last index of array
        var lastIndexOfArray = (stringToArray.length - 1);

        // Get last element of array as string to search for correction
        var stringToSearch = stringToArray[lastIndexOfArray];

        // If string to search don't have any matching record in corrections then don't need to proceed further
        if (!opts.corrections[stringToSearch])
        {
            return;
        }

        // Build string to replace using correction
        var stringToReplace = opts.corrections[stringToSearch];

        // Store replaced string back to array as last element
        stringToArray[lastIndexOfArray] = stringToReplace;

        // Join the array to build new string
        stringUptoCaretPosition = stringToArray.join(" ");

        // Get value of field upto end from caret's current position
        var stringFromCaretPositionUptoEnd = (valueOfField).substr(caretPosition);

        // Set new value of field
        this.value = (stringUptoCaretPosition + stringFromCaretPositionUptoEnd);

        // Set caret's position
        setCaretPosition(this, stringUptoCaretPosition.length + 1);
    });
};
