/// <reference name="MicrosoftAjax.js" />
Type.registerNamespace("WebTemplate");

// Constructor
WebTemplate.WebTemplateControl = function(element) {

    WebTemplate.WebTemplateControl.initializeBase(this, [element]);
}

WebTemplate.WebTemplateControl.prototype = {


    get_service: function() {
    /// <summary> Gets a reference to the web service used for server side functions </summary>
    /// <return> A webs service javascript wrapper </returns>
        return this._servicename;
    },
    set_service: function(value) {
        this._servicename = value;
        //this.get_service().set_defaultFailedCallback(this._FailedCallback);
    },
    
    _service: function() {
        return this.get_service();
    },
    
     // finds an object in the page and returns the DOM elements
    _findcontrol: function(name) {
        return $(this.get_element()).find("*[id$=" + name + "]")[0];
    },
    
     // finds an object inside the lement and returns the jquery element
    _findjcontrol: function(name) {
        return $(this.get_element()).find("*[id$=" + name + "]");
    },
    
    // finds a radio button in this control
    _getRadioGroupValue: function(name) {
        var selectedRB = $(this.get_element()).find('input[@name=' + name + ']:checked');
        // if selectedRB length == 1, return val
        return selectedRB[0].value;
    },
    
        // finds a radio button in this control ?value?
    _setRadioGroupValue: function(groupname, value) {
        var targetRB = $(this.get_element()).find('input[@name=' + groupname + ']');
        // if selectedRB length == 1, return val
        if(targetRB.length == 1)
        {
            targetRB.attr("checked", "checked");
        }
        else
        {
            // we should raise an error here
            alert('element not found');
        }
    },
    
    _FailedCallback: function FailedCallback(error)
        {
            var stackTrace = error.get_stackTrace();
            var message = error.get_message();
            var statusCode = error.get_statusCode();
            var exceptionType = error.get_exceptionType();
            var timedout = error.get_timedOut();
           
            // Display the error.    
            var message = 
                "Stack Trace: " +  stackTrace + "\n" +
                "Service Error: " + message + "\n" +
                "Status Code: " + statusCode + "\n" +
                "Exception Type: " + exceptionType + "\n" +
                "Timedout: " + timedout;
        },
    
    
    
    // get mode value
    get_mode: function() {
        return this._findcontrol("Mode").value;
    },
    set_mode: function(value) {
        this._findcontrol("Mode").value = value;
    },
       
    // Release resources before control is disposed.
    dispose: function() {

        WebTemplate.WebTemplateControl.callBaseMethod(this, 'dispose');
    },

    initialize: function() {

        WebTemplate.WebTemplateControl.callBaseMethod(this, 'initialize');
    },
    
    // called after all controls are loaded onthe page
    init: function () {
    
    
    },
    
    hide: function() {
        eval(this.get_element().id + "_dialog.Close()");
        //$(this.get_element()).hide();
    },
    
    show: function() {
        eval(this.get_element().id + "_dialog.Show()");
        //$(this.get_element()).show();
    },
    
    clearAllEvents: function() {
    
        this.get_events = function() {            
                if (!this._events) {                
                    this._events = new Sys.EventHandlerList();            
                }            
                return this._events;        
            }  
    },
    
    lock: function() {
        var me = this;  // we need this to pass to the handling closures

        // 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.prepend("<div  id=\"" + containerid + "_modalmask"  + "\" class=\"ListControlActivityModalMask\" />");
        
        // set the modal mask to the same size and shape as the container div
        var modalmask = container.find("*[id*=" + containerid + "_modalmask" + "]");
        modalmask.height(container.height());
        modalmask.width(container.width());
        
        $(window).bind('resize.listmodalmask',
            function() {
                var myself = me;
                
                // get a reference to our container
                var container = $(me.get_element());
                
                // set the modal mask to the same size and shape as the container div
                var modalmask = container.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.find("*[id*=" + containerid + "_modalmask" + "]");
        modalmask.remove();
        
        $(window).unbind('resize.listmodalmask')

    }

}
// register the class and indicate we inherit from SYS.UI.Control
WebTemplate.WebTemplateControl.registerClass('WebTemplate.WebTemplateControl', Sys.UI.Control);

// 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();
