
Type.registerNamespace("WebTemplate");

// Constructor
WebTemplate.ListControl = function(element) {

    WebTemplate.ListControl.initializeBase(this, [element]);
}

WebTemplate.ListControl.prototype = {

    
    // grid name
    get_gridname: function() {
        return this._gridname;
    },
    set_gridname: function(value) {
        this._gridname = value;
    },

    // menu name
    get_menuname: function() {
        return this._menuname;
    },
    set_menuname: function(value) {
        this._menuname = value;
    },
        
    _grid: function() {
        return eval('window.' + this.get_gridname());
    },
    
    _menu: function() {
        return eval('window.' + this.get_menuname());
    },
    
     // Release resources before control is disposed.
    dispose: function() {

        var element = this.get_element();

        WebTemplate.ListControl.callBaseMethod(this, 'dispose');
    },

    initialize: function() {
        var element = this.get_element();
        
        WebTemplate.ListControl.callBaseMethod(this, 'initialize');
    },
    
    init: function() {
        var me = this;  // we need this to pass to the handling closures
        if(this._menu() != null)
        {
            this._menu().add_itemSelect(
            
                function(sender, eventargs) {
                    var myself = me;
                    myself._handleMenuClick(sender,eventargs);
                }
            
            );
        }
    
        this._grid().add_itemDoubleClick(
        
            function(sender, eventargs) {
                var myself = me;
                myself._handleDoubleClick(sender,eventargs);
            }
        
        );
    },
    
    updatewithfilter: function(filter) {
        var me = this;  // we need this to pass to the handling closures
        var mode = this.get_mode();

        
        var griditems = this._service().Load(
            function(griditems) {
                var myself = me;
                var filterstring = filter;
                
                myself._grid().load(griditems);
                
                if(filterstring != undefined)
                {
                    if(filterstring == "") filterstring = "1==1";
                    myself.filter(filterstring);
                }

                myself._grid().render();
                myself.updateRecordCount();                
            }
        );
    },
    
    update: function() {
        var me = this;  // we need this to pass to the handling closures
        var mode = this.get_mode();

        
        var griditems = this._service().Load(
            function(griditems) {
                var myself = me;
                
                myself._grid().load(griditems);
                myself._grid().render();
                myself.updateRecordCount();
            }
        );
    },
    
    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')

    },
    
    
    filter: function(filterstring) {
    
        if(filterstring == "") filterstring = "1==1";
        
        this._grid().unSelectAll(this._grid().getSelectedItems());
        this._grid().Page(0);
        this._grid().set_recordOffset(0);
        this._grid().scrollTo(0);
        
        this._grid().filter(filterstring);
        this._grid().render();
        this.updateRecordCount();
    },
    
    updateRecordCount: function()
    {
        $(this.get_element()).find("span#lblRecordCount")[0].innerHTML = this._grid().RecordCount;
    },
    
    
    _handleDoubleClick : function(sender, e) {
    
        var selectedGridItems  = this._grid().getSelectedItems();
        
        var ev = new Sys.EventArgs();

        // we are using multiple selections
        if (selectedGridItems.length == 1)
        {
            ev.action = 'doubleclick';
            ev.gridItem = selectedGridItems[0];

            this._handleAction(sender, ev);
        }
    },
    
    _handleMenuClick: function(sender, e) {
        var me = this;  // we need this to pass to the handling closures
        
        var ev = new Sys.EventArgs();
        ev.action = e.get_item().get_id();
        
        var selectedGridItems  = this._grid().getSelectedItems();

        // we are not using multiple selections
        if (selectedGridItems.length == 1)
        {
            ev.gridItem = selectedGridItems[0];
            ev.dataItem = this._grid().Data[selectedGridItems[0].Index]    
        }
        
        this._handleAction(sender, ev);

        // always hide the menu afterwards
        this._menu().hide();
    },
    
    _handleAction: function(sender, e) {
        // this should be overriden to perform any action
    }
    
    
}
// register the class and indicate we inherit from our WebTemplate control base
WebTemplate.ListControl.registerClass('WebTemplate.ListControl', 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();

    