﻿Type.registerNamespace("majax");

majax.Watermark = function() {
    majax.Watermark.initializeBase(this);
    this._pageLoadedHandler = null;
    this._appLoadHandler = null;
}

majax.Watermark.prototype = {

    initialize : function() {
        majax.Watermark.callBaseMethod(this, 'initialize');

        // create the handlers
        this._pageLoadedHandler = Function.createDelegate(this, this.onPageLoaded);
        this._appLoadHandler = Function.createDelegate(this, this.onLoad);

        // wire up event handlers 
        var pageRequestMgr = Sys.WebForms.PageRequestManager.getInstance();
        pageRequestMgr.add_pageLoaded(this._pageLoadedHandler);
        Sys.Application.add_load(this._appLoadHandler);
    },


    dispose : function() {
        // tear down event handlers 
        var pageRequestMgr = Sys.WebForms.PageRequestManager.getInstance();
        pageRequestMgr.remove_pageLoaded(this._pageLoadedHandler);
        Sys.Application.remove_load(this._appLoadHandler);

        majax.Watermark.callBaseMethod(this, 'dispose');
    },

    onLoad : function(sender, args) {     
        if(!args.get_isPartialLoad()){
            Array.forEach($majax.getElementsByClassName('watermark'), function(e){ this.apply(e); }, this);
        }                      
    },

    onPageLoaded : function(sender, args) {
        var updatedPanels = args.get_panelsUpdated();
        if(updatedPanels && updatedPanels.length > 0){
            // find all elements with the 
            // and remember the scroll position 
            for(var i = 0; i < updatedPanels.length; i++) {
                Array.forEach($majax.getElementsByClassName('watermark', null, updatedPanels[i]), function(e){ this.apply(e); }, this); 
            }
        } 
    },
    
    apply : function(e) {
        
        //  if the field is empty, show the watermark
        if(e.value == '') {
            e.value = e.title;
            Sys.UI.DomElement.addCssClass(e, 'watermark');
        }
        
        //  when the field has focus hide the watermark
        $addHandler(e, 'focus', function(){  
            if(e.value == e.title) {
                e.value = '';
                Sys.UI.DomElement.removeCssClass(e, 'watermark');
            }
        });
        
        //  when the field loses focus and the
        //  input is empty, show the watermark
        $addHandler(e, 'blur', function(){  
            if(e.value == '') {
                e.value = e.title;
                Sys.UI.DomElement.addCssClass(e, 'watermark');
            }
        });  
                      
    }
}

//  register the class
majax.Watermark.registerClass('majax.Watermark', Sys.Component);

//  create the singleton
$create(majax.Watermark, null, null, null);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
