if (typeof EasyCMS == 'undefined') EasyCMS={};
if (!EasyCMS.hasOwnProperty('defaultLabel')) EasyCMS.defaultLabel={};

function html_entity_decode( string )
{
    // http://kevin.vanzonneveld.net
    // +   original by: john (http://www.jd-tech.net)
    // +      input by: ger
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: html_entity_decode('Kevin &amp; van Zonneveld');
    // *     returns 1: 'Kevin & van Zonneveld'

    var ret, tarea = document.createElement('textarea');
    tarea.innerHTML = string;
    ret = tarea.value;
    return ret;
}

EasyCMS.defaultLabel.eraseLabel = function(obj, label, variable) {
    if (variable == false && obj.value == label) {
        obj.value = "";
    }
}

EasyCMS.defaultLabel.checkForLabel = function(obj, label, variable)
{
    if (obj.value == "") {
        variable = false;
        obj.value = label;
    } else {
        variable = true;
    }

    return variable;
}

EasyCMS.defaultLabel.attachDefaultLabel = function(inputId, label)
{
	label = html_entity_decode(label);

    if (!EasyCMS.defaultLabel.hasOwnProperty('labels')) {
        EasyCMS.defaultLabel.labels = new Array();
    }

    var obj = $(inputId);
    if (obj != null) {
        EasyCMS.defaultLabel.labels[inputId] = (($(obj).get('value').length > 0) ? true : false);

        EasyCMS.defaultLabel.eraseLabel(obj, label, EasyCMS.defaultLabel.labels[inputId]);
        EasyCMS.defaultLabel.labels[inputId] = EasyCMS.defaultLabel.checkForLabel(obj, label, EasyCMS.defaultLabel.labels[inputId]);

        $(inputId).addEvent('focus', function(event) {
            EasyCMS.defaultLabel.eraseLabel(this, label, EasyCMS.defaultLabel.labels[inputId]);
        });

        $(inputId).addEvent('blur', function(event) {
            EasyCMS.defaultLabel.labels[inputId] = EasyCMS.defaultLabel.checkForLabel(this, label, EasyCMS.defaultLabel.labels[inputId]);
        });

        $(inputId).getParent('form').getChildren('input[type=submit]').addEvent('click', function(event) {
            EasyCMS.defaultLabel.eraseLabel(obj, label, EasyCMS.defaultLabel.labels[inputId]);
        });
    }
}

/* Usage 
window.addEvent('domready', function() {
    EasyCMS.defaultLabel.attachDefaultLabel('field_id', 'text message');
});
*/
