// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function findPos(obj) 
{
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		
		return [curleft, curtop];
}

function feedback(event)
{
	$$("label.default-value").each(function(label) {
    /* Get the default value */
    var defaultValue = label.innerHTML;

    /* Get the associated input */
    var input = $(label.htmlFor);

    /* Get the form */
    var form = input.form;

    /* Store information about the input being a password so we can use this later */
    var isPassword = (input.type && input.type == "password");

    /* When input gets focus, set value to "" if value is default value */
    
    Element.observe(input, 'focus', function(event) {
	    if (input.value == defaultValue) {
		    input.value = '';
		    if (isPassword) input.type = "password";
	    }
    });

    /* When input loses focus, set value to default value if value = "" */
    Element.observe(input, "blur", function(event) {
	    if (input.value == "") {
		    input.value = defaultValue;
		    if (isPassword) input.type = "text";
	    }
    });

    /* Set default values to "" when form is submitted */
    if (form) {
	    Element.observe(form, "submit", function(event) {
		    if (input.value == defaultValue) {
			    input.value = "";
		    }
	    });
    }

    /* Set default value when page loads */
    if (input.value == "" || input.value == defaultValue) {
	    input.value = defaultValue;
	    if (isPassword) input.type = "text";
    }

    /* Hide the label using javascript so css-but-no-javascript browsers will see the label */
    label.hide();
  });
}

Element.observe(document, "dom:loaded", feedback);
