/**
 * jQuery highlighted textarea plugin, based on code by Alan Williamson
 * from http://alan.blog-city.com/jquerylinedtextarea.htm
 *
 * Copyright (c) 2010 Alexander Solovyov
 */
(function($) {
    $.fn.hlarea = function() {
        /*
         * Helper function to make sure the line numbers are always
         * kept up to the current system
         */
        var fillOutLines = function(codeLines, h, lineNo){
            while ((codeLines.height() - h) <= 0){
                codeLines.append("<div class='lineno'>" + "&nbsp;" /*lineNo*/ + "</div>");
                lineNo++;
            }
            return lineNo;
        };

        /*
         * Iterate through each of the elements are to be applied to
         */
        return this.each(function() {
            var lineNo = 1;
            var textarea = $(this);

            /* Turn off the wrapping of as we don't want to screw up the line numbers */
            textarea.attr("wrap", "off");
            textarea.css({resize:'none'});
            var originalTextAreaWidth = textarea.outerWidth();

            /* Wrap the text area in the elements we need */
            textarea.wrap("<div class='linedtextarea'></div>");
            var linedTextAreaDiv = textarea.parent().wrap("<div class='linedwrap' style='width:" + originalTextAreaWidth + "px'></div>");
            var linedWrapDiv = linedTextAreaDiv.parent();

            linedWrapDiv.prepend("<div class='lines'></div>");

            var linesDiv = linedWrapDiv.find(".lines");
            linesDiv.height(textarea.height() + 6);


            /* Draw the number bar; filling it out where necessary */
            linesDiv.append("<div class='codelines'></div>");
            var codeLinesDiv = linesDiv.find(".codelines");
            lineNo = fillOutLines(codeLinesDiv, linesDiv.height(), 1);

            var size = function(prop) { return parseInt(linedWrapDiv.css(prop)); }
            /* Set the width */
            var sidebarWidth = linesDiv.outerWidth();
            var paddingHorizontal = size("border-left-width") + size("border-right-width") +
                size("padding-left") + size("padding-right");
            var linedWrapDivNewWidth = originalTextAreaWidth - paddingHorizontal;
            var textareaNewWidth = originalTextAreaWidth - sidebarWidth - paddingHorizontal - 20;

            textarea.width(textareaNewWidth);
            linedWrapDiv.width(linedWrapDivNewWidth);

            /* React to the scroll event */
            textarea.scroll(function(tn){
                var domTextArea = $(this)[0];
                var scrollTop = domTextArea.scrollTop;
                var clientHeight = domTextArea.clientHeight;
                codeLinesDiv.css({'margin-top': (-1*scrollTop) + "px"});
                lineNo = fillOutLines(codeLinesDiv, scrollTop + clientHeight, lineNo);
            });

            /* Should the textarea get resized outside of our control */
            textarea.resize(function(tn){
                var domTextArea = $(this)[0];
                linesDiv.height(domTextArea.clientHeight + 6);
            });

            // put functions on usual textarea object as it's single instance everywhere
            textarea[0].cleanup = function() {
                $('.codelines div').removeClass('changed').removeClass('error').removeClass('success');
            }
            textarea[0].changed = function(i) {
                $($('.codelines div')[i]).removeClass('success').removeClass('error').addClass('changed');
            }
            textarea[0].success = function(i) {
                $($('.codelines div')[i]).removeClass('changed').removeClass('error').addClass('success');
            }
            textarea[0].error = function(i) {
                $($('.codelines div')[i]).removeClass('changed').removeClass('success').addClass('error');
            }
        });
    };
})(YMaps.jQuery);


/*
  Placeholder support for jQuery
  Copyright (c) 2008-2009 Alexander Solovyov (piranha.org.ua)
  Licensed under 3-clause BSD License
 */

(function(jQuery) {
jQuery.fn.placeholder = function (color) {
    if ('placeholder' in document.createElement('input'))
        return false; // placeholder is already supported

    color = color || '#AAA';

    return this.each(function (){
        var default_color = this.style.color;
        var t = jQuery(this);
        var placeholder = t.attr('placeholder');
        if (!placeholder) { return; }

        t.blur(function () {
            if (t.val() == '') {
                t.val(placeholder);
                this.style.color = color;
                t.addClass('blur');
            }
        });

        t.focus(function () {
            if (t.val() == placeholder) {
                t.val('');
                this.style.color = default_color;
                t.removeClass('blur');
            }
        });

        t.change(function () {
            if (t.val() != placeholder) {
                this.style.color = default_color;
                t.removeClass('blur');
            }
        });

        t.blur();
    });
}

    jQuery(function() { jQuery('input,textarea').placeholder(); });
})(YMaps.jQuery);
