Type.registerNamespace("WebTemplate");

// Constructor
WebTemplate.EditorControl = function(element) {

    WebTemplate.EditorControl.initializeBase(this, [element]);
}

WebTemplate.EditorControl.prototype = {

    show: function() {
        eval(this.get_element().id + "_dialog.Show()");
        this.updateTabOrder();
    },
    
    hide: function() {
        eval(this.get_element().id + "_dialog.Close()");
    },
    
    // Release resources before control is disposed.
    dispose: function() {

        var element = this.get_element();

        WebTemplate.EditorControl.callBaseMethod(this, 'dispose');
    },

    initialize: function() {

        WebTemplate.EditorControl.callBaseMethod(this, 'initialize');
    },
    
    init: function () {
    
        WebTemplate.EditorControl.callBaseMethod(this, 'init');
    },
    
    update: function() {
              
        // set the visibility and enabled status of our controls based on our mode
        switch(this.get_mode())
        {
            case "View":
                $(this.get_element()).find(":text").attr("readOnly", "true");
                $(this.get_element()).find(":radio").attr("disabled", "true");
                $(this.get_element()).find(":checkbox").attr("disabled", "true");
                $(this.get_element()).find("select").attr("disabled", "true");
                break;
            case "Edit":
                $(this.get_element()).find(":text").removeAttr("readOnly");
                $(this.get_element()).find(":radio").removeAttr("disabled");
                $(this.get_element()).find(":checkbox").removeAttr("disabled");
                $(this.get_element()).find("select").removeAttr("disabled");;
                break;
            case "Create":
                $(this.get_element()).find(":text").removeAttr("readOnly");
                $(this.get_element()).find(":radio").removeAttr("disabled");
                $(this.get_element()).find(":checkbox").removeAttr("disabled");
                $(this.get_element()).find("select").removeAttr("disabled");
                
                break;
        }
        
    },
    
    lock: function() {
    

        // get a reference to our container
        var container = $(this.get_element());
        var containerid = this.get_element().id;
        
        // create a new, absolutely positioned element to be the modal background
        container.parent().prepend("<div  id=\"" + containerid + "_modalmask"  + "\" class=\"ControlActivityModalMask\" />");
        
        // set the modal mask to the same size and shape as the container div
        var modalmask = container.parent().find("*[id*=" + containerid + "_modalmask" + "]");
        modalmask.height(container.height());
        modalmask.width(container.width());
        
    },
    
    unlock: function() {
    
        // get a reference to our container
        var container = $(this.get_element());
        var containerid = this.get_element().id;
        
        // find the modal mask and remove
        var modalmask = container.parent().find("*[id*=" + containerid + "_modalmask" + "]");
        modalmask.remove();

    },
    
    updateTabOrder: function() {
    
        var tabElements = $(this.get_element()).find(".DataEntry");
        var lowestTab = 0;
        var focusElement = null;
        
        for(var i = 0; i < tabElements.length; i++)
        {
            var tabEl = $(tabElements[i]);
            
            // respect the existing tab index
            if(tabEl.attr('taborder') <= 0)
            {
                tabEl.attr('taborder', i +1);
            }
            // enable the controls placement in the tab order
            tabEl.attr('tabEnabled', true);
            tabEl.attr('tabindex', tabEl.attr('taborder'));
            
            tabEl[0].tabIndex = tabEl.attr('taborder');
            tabEl[0].tabEnabled = true;
            
            if(lowestTab > tabEl.attr('taborder') || focusElement == null)
            {
                lowestTab = tabEl.attr('taborder');
                focusElement = tabEl;
            }
            

        }
        
        if(focusElement != null)
        {
            // set focuse to the item with the lowest tab order
            focusElement.focus();
        }
        
    }
    


}
// register the class and indicate we inherit from SYS.UI.Control
WebTemplate.EditorControl.registerClass('WebTemplate.EditorControl', WebTemplate.WebTemplateControl);

// 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();
