/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Samsung");

Samsung.Watermark = function(element) {

    this._controlID = null;
    this._topMargin = null;

    Samsung.Watermark.initializeBase(this, [element]);
}

Samsung.Watermark.prototype = {

    initialize: function() {
        Samsung.Watermark.callBaseMethod(this, 'initialize');
    },

    dispose: function() {
        Samsung.Watermark.callBaseMethod(this, 'dispose');
        //$clearHandlers($get(this._controlID)); //genera un conflitto con AutoCompleteExtender
        $clearHandlers($get(this._controlID).watermark);
    },

    get_controlID: function() {
        return this._controlID;
    },

    set_controlID: function(value) {
        this._controlID = value;
        var input = $get(this._controlID);
        input.empty = true;

        $addHandler(input, "focus", this.fucusIn);
        $addHandler(input, "blur", this.fucusOut);

        var div = document.createElement('div');
        input.parentNode.insertBefore(div, input);
        div.className = "Watermark";
        div.style.position = "absolute";
        setInterval("if ($get('" + this._controlID + "').value != '') $get('" + this._controlID + "').watermark.style.display = 'none';", 100);

        input.input = div.input = input;
        input.watermark = div.watermark = div;

        $addHandler(div, "click", this.fucusIn);
    },

    get_label: function() {
        return this._label;
    },

    set_label: function(value) {
        this._label = $get(this._controlID).watermark.innerHTML = value;
    },

    get_topMargin: function() {
        return this._topMargin;
    },

    set_topMargin: function(value) {
        this._topMargin = value;
        $get(this._controlID).watermark.style.paddingTop = (Number(value) + 4) + "px";
    },

    fucusIn: function(e) {
        if (this.input.empty) {
            this.watermark.style.display = 'none';
            this.input.focus();
        }
    },

    fucusOut: function(e) {
        this.input.empty = this.input.value == "";

        if (this.input.empty)
            this.watermark.style.display = 'block';
        else {
            if (this.oldClassName != null) this.className = this.oldClassName;
            this.oldClassName = null;
            this.watermark.className = 'Watermark';
        }
    }
}

Samsung.Watermark.registerClass('Samsung.Watermark', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

