﻿
MyAppointmentsMgr = Class.create();
//option to override "onItemDataBound" function if (typeof this.funcOnItemDataBound == 'function')
MyAppointmentsMgr.prototype.funcOnItemDataBound = 0;//For dynamic overriding the repeater onItemDataBound function
MyAppointmentsMgr.prototype.funcOnDataBind = 0;//For dynamic overriding the repeater onDataBound function
MyAppointmentsMgr.prototype.init = function(sTemplate,sInnerTemplate,oData,oTarget,pageNum,PagingTarget,iItemsPerPage,oMapping,oParseTags,nextObjToExecute)
{
    //debugger
    this.template = sTemplate;
    this.innerTemplate = sInnerTemplate;
    
    this.dataSource = oData;
    this.target = oTarget;
    if(oMapping)
        this.mapping = oMapping;
    else
        this.mapping = this.defaultMapping;
        
    if(iItemsPerPage)
        this.ItemsPerPage = iItemsPerPage;
    else
        this.ItemsPerPage = this.defaultItemsPerPage;
        
    this.pagingTarget = PagingTarget;
    this.currentPageID = pageNum;
    
    if(oParseTags)
        this.parseTags = oParseTags;
    else
        this.parseTags = null;
        
    if(nextObjToExecute)
        this.nextExecute = nextObjToExecute;
    
}


MyAppointmentsMgr.prototype.defaultItemsPerPage = 7;

MyAppointmentsMgr.prototype.defaultPagesNumDisplay = 10;


MyAppointmentsMgr.prototype.defaultMapping = {
                                                     NAME      : "name"
                                                    ,appointID  : "id"
                                                    ,SERVICE    : "service"
                                                    ,STATUS     : "status"
                                                    ,BUTTONS    : "" //just prepering the mapping part for the buttons to come...
                                               };


MyAppointmentsMgr.prototype.execute = function()
{
    
    //Preparing the 'start' and 'end' to the Data Object slice()
    this.toItem = parseInt(this.currentPageID,10) * this.ItemsPerPage;
    if(this.toItem >= this.dataSource.length) this.toItem = this.dataSource.length;
    this.fromItem = (parseInt(this.currentPageID,10) * this.ItemsPerPage) - this.ItemsPerPage;
    
    if(!this.repeater)
    {
        //new instant of the TC.Controls.Repeater from Repeater.js
        this.repeater = new TC.Controls.Repeater(this.template,this.innerTemplate,null,this.parseTags);
    }
    else
    {
        //If excist, clear Previews content
        this.repeater.content = null;
    }
    
    //On the Parent Data single Item (Object) Binding - 'appointMgr'
    this.repeater.onItemDataBound = function(item)
    {
        //For Phase2 on the wizard - Rendering all services under one business title
        if(item.allTemplates.timeStamp)
        {
            if(item.itemIndex>0 && item.data[item.itemIndex-1].date == item.dataItem.date)
            {
                item.template = item.allTemplates.item;
            }
            else
            {
                item.template = item.allTemplates.timeStamp + item.allTemplates.item;
            }
        }
        //End For Phase2...
        //For Phase2 on the wizard - Rendering all services under one business title
        if(item.allTemplates.businessTitle)
        {
            if(item.itemIndex>0 && item.data[item.itemIndex-1].BusinessID == item.dataItem.BusinessID)
            {
                item.template = item.allTemplates.item;
            }
            else
            {
                item.template = item.allTemplates.businessTitle + item.allTemplates.item;
            }
        }
        //End For Phase2...
        //For Phase1, Phase2 and Phase3 on the wizard - Whether the checkboxes of the Business/Service/Time are ON or OFF
        if($('preDisplay'))
        {  
            var entityTitle = '';
            if(!isNullOrUndefined(item.itemSettings))
                setCheckBoxAsPreDisplay(item);
        }
        //End For Phase1...
        this.mapping = item.mapping;
    }
    
    //For Phase3 (outlook calendar style) at the wizard - rendering the Service Names as navigate bar
    if(!isNullOrUndefined(this.mapping.SERVICES_NAMES))
    {
        this.getServicesNames = function(sTemplate,oData)
        {
            this.dataItem = oData;
            //obtaining the template
            var strTemplate1 = sTemplate;
            //defining mapping:
            try{
                var oMapping1 = {
                                    serviceID               : "ServiceId"//"ServiceId"
                                    ,SERVICE_NAME           : "ServiceName"
                                    ,ITEM_INDEX             :   ""
                                };
                }catch(e){
                                        var oMapping1 = {
                                        serviceID               : "ServiceId"//"ServiceId"
                                        ,SERVICE_NAME           : "ServiceName"
                                        ,ITEM_INDEX             :   ""
                                     };
                }finally{
                    var rm = new TC.Controls.Repeater(strTemplate1);
                    rm.onItemDataBound = function(item)
                    {
                        item.mapping.ITEM_INDEX = item.itemIndex;
                    }
                        
                    var theServices = '';
                    if(!isNullOrUndefined(this.dataItem))
                    {
                        rm.dataBind(oMapping1, this.dataItem);
                        theServices = rm.content.toString();//Returning an HTML as string (the render() returns HTML and dataBind() returns HTML string)
                    }
                    return theServices;
                }
            
        }
        
        if(!isNullOrUndefined(this.mapping.SERVICES_NAMES))
        {
            this.mapping.SERVICES_NAMES = "";
           this.mapping.SERVICES_NAMES += this.getServicesNames(this.innerTemplate,this.dataSource);
        }
    }
    //overriding this.repeater.onItemDataBound
    if(typeof this.funcOnItemDataBound == 'function')
        this.repeater.onItemDataBound = this.funcOnItemDataBound;
    //overriding this.repeater.onDataBound
    //debugger
    if(typeof this.funcOnDataBind == 'function')
        this.repeater.onDataBind = this.funcOnDataBind;
    
    if(this.target)
    {
        this.repeater.render(this.target, this.mapping, this.dataSource.slice(this.fromItem,this.toItem));
        if(this.mapping.settings)
        {
            if(this.mapping.settings.rounded)
                roundImages(this.dataSource.slice(this.fromItem,this.toItem));
        }
            
        return '';
    }
    else
    {
        this.repeater.dataBind(this.mapping, this.dataSource.slice(this.fromItem,this.toItem), this.dataSource);
        return this.repeater.content.toString();//Returning an HTML as string (the render() returns HTML and dataBind() returns HTML string)
    }

}


MyAppointmentsMgr.prototype.getAppointments = function(iCustomerID, iPageNum, iStatusID,linkID,pageNum)
{       //debugger
        showLoader();
        //this.linkSelected(linkID);//TODO: to locate the selected link and change his class
        var params = new Array();
        params[0] = iCustomerID;
        params[1] = iPageNum;
        params[2] =iStatusID;
        var appointData = jsonObj.syncReturnValue('jsonHandler','getMyAppointmentsList',params);
        if(!isNullOrUndefined(appointData) && appointData!="")
        {
            appointData = this.CollectionFix(appointData);
            //this.dataSource = eval('(' + appointData + ')');
            this.dataSource = MassiveParserJSON(appointData);
            if(this.dataSource.length>0)
            {
                this.setRepeater(this.dataSource);
                this.setPagingBar(this.dataSource,iPageNum);
            }
            else
                this.setRepeater();
        }
        
    
}

MyAppointmentsMgr.prototype.setPagingBar = function(data,pageNum)
{
    pagingBar.mapping.settings.currentPage = pageNum;
    pagingBar.init(pagingBar.template,pagingBar.target,data,pagingBar.mapping);
    pagingBar.execute(appointMgr);
}

MyAppointmentsMgr.prototype.setRepeater = function()
{
    
    this.init(this.template,this.innerTemplate,this.dataSource,this.target,1,this.pagingTarget,null,this.mapping);
    this.execute();
}

MyAppointmentsMgr.prototype.getAppointmentsLinksProperties = function(mgrName,funcName)//objID,funcCall,statusID)
{
    jsonObj.asyncWithCallback(mgrName,funcName,null,'setAppointmentsSums');
    
}
//for myBusinessAppointments.ascx page use
MyAppointmentsMgr.prototype.getAppointmentsSummery = function(cusID,serviceID,businessID,mgrName,funcName)//objID,funcCall,statusID)
{
    var params = new Array();
    params[0] = cusID;
    params[1] = serviceID;
    params[2] = businessID;
    jsonObj.asyncWithCallback(mgrName,funcName,params,'setAppointmentsSums');
    
}

MyAppointmentsMgr.prototype.CollectionFix = function(dataArray)
{
    var firstChange = ':[{';
    var secondChange = '}],\"';
    var thirdChange = '}]}';
    dataArray = dataArray.replace(/:{/g,firstChange);
    dataArray = dataArray.replace(/},\"/g,secondChange);
    dataArray = dataArray.replace(/}}/g,thirdChange);
    return dataArray;
}

MyAppointmentsMgr.prototype.linkStyles = function(id,sum,appointStatusID,doSetEvent)
        {
            var obj = $(id);
            //TODO: changing the hard coded params
//            if(doSetEvent)
//                obj.setAttribute('href', "appointMgr.getAppointments("+userID+",1,"+appointStatusID+")");                
            if(parseInt(sum,10)<=0)
            {
                obj.childNodes[1].innerHTML = '(0)';
            }
            else
            {
                obj.childNodes[1].innerHTML = '(' + sum + ')'; 
            }
        }

