﻿// JScript File

Type.registerNamespace('Intrafinity.Web.Controls');

Intrafinity.Web.Controls.CalendarPicker = function(element){
    Intrafinity.Web.Controls.CalendarPicker.initializeBase(this, [element]);
    this._serverId = null;
    this._parentServerId = null;
    this._subServerIds = null;
    this._cbShowParentServer = null;
    this._cbShowSubServer = null;
    this._divParentServer = null;
    this._divSubServer = null;    
    this._rgSubServerList = null;
    this._scpIdPostfix = null; 
    this._selectedCalendarList = null; 
    this._hfSelectedCalendarList = null;
    this._currentScp = null; 
    this._parentScp = null; 
    this._showWorkspace = null;
    this._showLms = null;
    this._showEreg = null;   
   this._loadingPanel = null; 
}

Intrafinity.Web.Controls.CalendarPicker.prototype = {

    get_serverId : function(){
        return this._serverId;
    },
   set_serverId : function(value){
        this._serverId = value;
        this.raisePropertyChanged("serverId");
   }, 
   
   get_parentServerId : function(){
        return this._parentServerId;
   },
   set_parentServerId : function(value){
        if(value!=this._parentServerId){
            this._parentServerId = value;
            this.raisePropertyChanged("parentServerId");
        }
   },
   
   get_subServerIds : function(){
        return this._subServerIds;
   },
   set_subServerIds : function(value){
        if(value!=this._subServerIds){
            this._subServerIds = value;
            this.raisePropertyChanged("subServerIds");
        }
   },
   
   get_cbShowParentServer : function(){
        return this._cbShowParentServer;
   },
   set_cbShowParentServer : function(value){
        this._cbShowParentServer = value;
   },
   
   get_cbShowSubServer : function(){
        return this._cbShowSubServer;
   },
   set_cbShowSubServer : function(value){
    this._cbShowSubServer = value;
   },
 
    get_divParentServer : function(){
        return this._divParentServer;
    },
   set_divParentServer : function(value){
        this._divParentServer = value;
   }, 
   
   get_divSubServer : function(){
        return this._divSubServer;
   },
   set_divSubServer : function(value){
        this._divSubServer = value;
   },
   
   get_rgSubServerList : function(){
        return this._rgSubServerList;
   },
   set_rgSubServerList : function(value){
        this._rgSubServerList = value;
   },
   
   get_scpIdPostfix : function(){
        return this._scpIdPostfix;
   },
   set_scpIdPostfix : function(value){
        if(value!=this._scpIdPostfix){
            this._scpIdPostfix = value;
        }
   },
   
   get_selectedCalendarList : function(){
        return this._selectedCalendarList;
   },
   set_selectedCalendarList : function(value){
        if(value!=this._selectedCalendarList){
            this._selectedCalendarList = value;
            this.raisePropertyChanged("selectedCalendarList");
        }
   },
   
   get_hfSelectedCalendarList : function(){
        return this._hfSelectedCalendarList;
   },
   set_hfSelectedCalendarList : function(value){
        if(value!=this._hfSelectedCalendarList){
            this._hfSelectedCalendarList = value;
            this.raisePropertyChanged("hfSelectedCalendarList");
        }
   },
   
   get_currentScp : function(){
        return this._currentScp;
   },
   set_currentScp : function(value){
       if (this._currentScp !=value){
            this._currentScp = value;
            this.raisePropertyChanged("currentScp");
       }
   },
   
   get_parentScp : function(){
        return this._parentScp;
   },
   set_parentScp : function(value){
        if(value!=this._parentScp){
            this._parentScp = value;
            this.raisePropertyChanged("parentScp");
        }
   },
   
   get_showWorkspace : function(){
        return this._showWorkspace;
   },
   set_showWorkspace : function(value){
        this._showWorkspace = value;
   },
   
   get_showLms : function(){
        return this._showLms;
   },
   set_showLms : function(value){
        this._showLms = value;
   },
   
   get_showEreg : function(){
        return this._showEreg;   
   },
   set_showEreg : function(value){
        this._showEreg = value;
   },
   
   
   get_loadingPanel : function(){
        return this._loadPanel;
   },
   set_loadingPanel : function(value){
            this._loadingPanel = value;
   },
//   add_selectedCalendarDataBound : function(handler){
//        this.get_events().addHandler("selectedCalendarDataBound", handler);
//   },
//   remove_selectedCalendarDataBound : function(handler){
//        this.get_events().removeHandler("selectedCalendarDataBound", handler);
//   },
   
   /// event hander
   
   showParentServer : function(e){       
        this._divParentServer.style.display = e.target.checked ? "" : "none";
   },
   
   showSubServer : function(e){
        this._divSubServer.style.display = e.target.checked ? "" : "none";
   },
   
   rg_RowDataBound : function(sender, args){
        var si = args.get_dataItem();
        var ri = args.get_item();
        
        /// find control
        var scp = ri.findControl("divContainer");
        if(!scp.get_events().getHandler("refreshSelectedCalendar"))
            scp.add_refreshSelectedCalendar(Function.createDelegate(this, this.scp_refreshSelectedCalendar));
        
        si.WorkspaceEnabled = this._showWorkspace && si.WorkspaceEnabled;
        si.LmsEnabled = this._showLms && si.LmsEnabled;
        si.EregEnabled = this._showEreg && si.EregEnabled;
            
        scp.Reset(si);       
        scp.set_selectedCalendarList(this.getSelectedCalendarListByServerId(si.ServerId)); 
        scp.refreshSelectedCalendar();
        
   },
   
   rg_Command : function(sender, args){   
        args.set_cancel(true);                
        if(args.get_commandName()=="Page" ){
               this.loadData();
               this._loadingPanel.show(this._divSubServer.id);
        }    
   },
   
  scp_refreshSelectedCalendar : function(sender, args)
  {
       var serverId = sender.get_serverId();
        this.clearSelectedCalendarListByServerId(serverId);
        if(args && args.length>0){
            for(var i=0,t=args.length;i<t;i++){
                this._selectedCalendarList.push(args[i]);
            }
        }
       this._hfSelectedCalendarList.value = Sys.Serialization.JavaScriptSerializer.serialize(this._selectedCalendarList);
//       var h = this.get_events().getHandler("selectedCalendarDataBound");
//        if(h)
//             h(this, serverId); 
  },
   
   loadData : function(){
       if(this.get_rgSubServerList()){ 
           var tbl = this.get_rgSubServerList().get_masterTableView();
           var pageSize = tbl.get_pageSize();
           var pageIndex = tbl.get_currentPageIndex();
            var tmpServerIds= new Array();
            var startIndex = pageSize * pageIndex;
            var endIndex = startIndex + pageSize > this._subServerIds.length ? this._subServerIds.length : startIndex + pageSize;
            for(var i=startIndex; i<endIndex; i++){
                tmpServerIds.push(this._subServerIds[i]);
            }
            
            Intrafinity.Web.Services.CalendarPickerWS.GetServerInfoList(tmpServerIds, Function.createDelegate(this, this._callBackSucceed), Function.createDelegate(this, this._callBackFail));   
        }
   },   
   
   bindGridView : function(data){
        var tbl = this.get_rgSubServerList().get_masterTableView();
        tbl.set_dataSource(data);
        tbl.set_virtualItemCount(this._subServerIds.length);
        tbl.dataBind();
        tbl.clearSelectedItems();        
        this._loadingPanel.hide(this._divSubServer.id); 
   },
   
   getSelectedCalendarListByServerId : function(serverId){
        if(serverId ==0)
            return null;
        var retVal = new Array();
        if(this._selectedCalendarList && this._selectedCalendarList.length>0){
            for(var i=0,t=this._selectedCalendarList.length;i<t;i++){
                var cal = this._selectedCalendarList[i];
                if(cal.ServerId == serverId)
                    retVal.push(cal);  
            }
        }
        return retVal;
   }, 
   
   clearSelectedCalendarListByServerId : function(serverId){
        if(serverId ==0)
            return;
        else{
            var list = this.getSelectedCalendarListByServerId(serverId);
            if(list && list.length>0){
                 for(var i=0,t=list.length;i<t;i++){
                    var index = this.getSelectedIndex(list[i].CalendarId);
                    if(index>-1)
                        this._selectedCalendarList.splice(index, 1);
                 }
            }                            
        }
   },

       getSelectedIndex : function(calendarId){
        if(!this._selectedCalendarList)
            return -1;
        else{
            for(var i=0,t=this._selectedCalendarList.length;i<t;i++){
                if(this._selectedCalendarList[i].CalendarId==calendarId){
                    return i;
                }               
            }
            return -1; 
        }
   },
   
   removeSelectedCalendar : function(calendarId, serverId){
        if(serverId == 0)
            return;
        var index = this.getSelectedIndex(calendarId);
        if(index > -1){
            this._selectedCalendarList.splice(index, 1);
            var list = this.getSelectedCalendarListByServerId(serverId);
            if(this._serverId == serverId){
                this._currentScp.set_selectedCalendarList(list);
                this._currentScp.refreshSelectedCalendar();
                return;
            }
            else if(this._parentScp && this._parentServerId == serverId){
                this._parentScp.set_selectedCalendarList(list);
                this._parentScp.refreshSelectedCalendar();
                return; 
            }
            else if(this._rgSubServerList){            
                var dItems = this._rgSubServerList.get_masterTableView().get_dataItems();
                var isFound = false; 
                if(dItems && dItems.length > 0){
                    for(var i=0,t=dItems.length;i<t; i++){
                        var scp = dItems[i].findControl("divContainer");
                        if(scp.get_serverId() == serverId){
                            isFound = true;
                            scp.set_selectedCalendarList(list);
                            scp.refreshSelectedCalendar();                             
                            return;
                        }   
                    }
                    /// update sub server's selected calendar hidden field value
                    /// since refreshSelectedCalendar handler cannot be triggered here
                    if(!isFound)
                        this._hfSelectedCalendarList.value = Sys.Serialization.JavaScriptSerializer.serialize(this._selectedCalendarList);
                }            
            }u
        }
   },   

   
   _callBackSucceed : function(result, context, methodName){
        if(methodName == "GetServerInfoList"){
            if(result.IsSuccessful){
                this.bindGridView(result.Result);
            }
            else{
                alert(result.Message);
            }
        }
   },
   
   _callBackFail : function(error){
        
   },
   
   initialize : function(){
       Intrafinity.Web.Controls.CalendarPicker.callBaseMethod(this, "initialize");    
        if(this._parentServerId > 0)
            $addHandler(this._cbShowParentServer, "click", Function.createDelegate(this, this.showParentServer));
        if(this._subServerIds && this._subServerIds.length>0)
            $addHandler(this._cbShowSubServer, "click", Function.createDelegate(this, this.showSubServer));
        
        if(this.get_rgSubServerList()){                
            this.get_rgSubServerList().add_rowDataBound(Function.createDelegate(this, this.rg_RowDataBound));
            this.get_rgSubServerList().add_command(Function.createDelegate(this, this.rg_Command));    
        }
        
        if(this._parentScp){
            this._parentScp.set_selectedCalendarList(this.getSelectedCalendarListByServerId(this._parentServerId));
            this._parentScp.add_refreshSelectedCalendar(Function.createDelegate(this, this.scp_refreshSelectedCalendar));
        }
        //alert(this._scpIdPostfix);
        this._currentScp.set_selectedCalendarList(this.getSelectedCalendarListByServerId(this._serverId));                
        this._currentScp.add_refreshSelectedCalendar(Function.createDelegate(this, this.scp_refreshSelectedCalendar));        
        this.loadData();
   },
   
   dispose : function(){
        Intrafinity.Web.Controls.CalendarPicker.callBaseMethod(this, "dispose");
   } 
}

Intrafinity.Web.Controls.CalendarPicker.registerClass("Intrafinity.Web.Controls.CalendarPicker", Sys.UI.Control);
    