﻿// provide input hints
var runOnce = true;
$(document).ready(function() {
    if (runOnce) {
        runOnce = false;

        var PLACEHOLDER_SUFFIX = '_placeholder'; // used for password inputs

        jQuery('input[placeholder]').each(function(index) {
            var input = $(this);
            var placeholder, placeholder_text = input.attr('placeholder');

            if (input.attr('type') == 'password') {
                placeholder = $(cloneAsType(input, 'text'));
                placeholder.val(placeholder_text);
                placeholder.addClass('placeholder');

                if (input.attr('id')) {
                    // update input id and label
                    var newID = input.attr('id') + PLACEHOLDER_SUFFIX
                    placeholder.attr('id', newID);
                    //                jQuery('label[for="' + input.attr('id') + '"]').invoke('writeAttribute', 'for', input.attr('id') + PLACEHOLDER_SUFFIX);
                    jQuery('label[for="' + input.attr('id') + '"]').attr('for', newID);
                }

                input.attr({ 'accesskey': '', 'tabindex': '' });
                input.hide().before(placeholder);

                // when placeholder input gains focus,
                // hide it and show "real" password input
                placeholder.bind('focus', function() {
                    $(this).hide();
                    input.show();
                    input.focus();
                });

                // when "real" password input loses focus,
                // if it's empty, hide it and show placeholder input
                input.bind('blur', function() {
                    if ($(this).val() === '') {
                        $(this).hide();
                        placeholder.show();
                    }
                });
            } else {
                // insert placeholder text
                input.addClass('placeholder').val(placeholder_text);

                input.bind('focus', function() {
                    if ($(this).hasClass('placeholder')) {
                        $(this).val('').removeClass('placeholder');
                    }
                });
                input.bind('blur', function() {
                    if ($(this).val() === '') {
                        $(this).addClass('placeholder').val(placeholder_text);
                    }
                });
            }
        });

        jQuery('form').each(function(input) {
            var input = $(this);
            if (input.find('input[placeholder]').length > 0) {
                input.bind('submit', clearPlaceholders);
            }
        });
    }
});

function clearPlaceholders(input)
{
    this.find('input[placeholder]').each(function(element, index){
        if (element.val() == element.attr('placeholder'))
        {
            element.val('');
        }
    });
}

function cloneAsType(oldObject, oType) {
  var oldObjectBase = oldObject.context;
  var newObject = document.createElement('input');
  newObject.type = oType;
  if (oldObjectBase.size) newObject.size = oldObjectBase.size;
  if (oldObjectBase.value) newObject.value = oldObjectBase.value;
  if (oldObjectBase.className) newObject.className = oldObjectBase.className;
  return newObject;
}
