
///////////////////////////////////

    
    //CONSTRUCTOR
DataFloat.Agora.AjaxManager = function(formParams, divResult, divPagesNavi) {
    $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager] - Initializing');
    //Public Properties
    this.TotalResult = 0;
    this.TotalPages = 0;
    this.DivResult = divResult;
    this.DivPagesNavi = divPagesNavi;
    this.FormParams = formParams;
    this.ItemPerPage = 10;
    this.CurrentPage = 1;
    this.ItemsOnNavBar = 5;
    this.HtmlInterface = null;
    this.GotoPage = "";
    this.MapManager = "";
    this.MapMlsException = null;
    this.UpdateExceptionList = false;
    //this.NoResultMessage = 'We did not find any results matching your criteria.'
    this.NoResultMessage = '<div id="divnoresult"></div><div id="divnoresultcentering"><div id="divnoresultcontent"><div id="divclosenoresult"><a style="color:#FFFFFF;" onclick="closeNoresult()" href="javascript:void(0)">Close</a></div><div id="divnoresultmodule"><div id="divnoresulttext" style="text-align: center; background-color:#F2F2F2; height:80px; padding:25px;"><div style="font-size: 13px; font-weight: bold; color: red; background: #ffffff; border:1px solid #ccc; padding:10px;">We are sorry, we did not find any listings that match your search criteria. Please close this box and try your search again.</div></div></div></div></div>';
    this.ActiveRequest = new Array();
    this.AgoraDebuger = new DataFloat.Agora.Debuger();
    this.NaviIconNext = "&gt;";
    this.NaviIconPrevious = "&lt;";
    this.MapDataResult = null; //Used only for map search
    $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager] - Completed');
}
    
    //STATIC (OVERWRITABLE)
    DataFloat.Agora.AjaxManager.Css = new function(){};
    DataFloat.Agora.AjaxManager.Css.IsLoading = 'AgoraLoading';
    
    
    
    //METHODS
    DataFloat.Agora.AjaxManager.prototype.NewRequest = function(ServiceUrl, Item, onSucessFnc, forceDivContainer, clearDiv){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewRequest] - Initializing');
            var elementName = DataFloat.Agora.Util.NewGuid();
	        divContainer = DataFloat.Agora.Util.StringFormat('<div class="' + DataFloat.Agora.AjaxManager.Css.IsLoading + '" id="{0}"><br/><br/>&nbsp;<br/><br/></div>', elementName);
	        destinationDiv = (forceDivContainer == null || forceDivContainer == undefined)? this.DivResult : forceDivContainer;

	        if (clearDiv)
	            $('#' + destinationDiv).html('');
	            
	        $('#' + destinationDiv).append(divContainer);
	        
	        paramPost = this.NewTransaction((this.FormParams + '&Agora__Item=' + Item))
	        paramPost = DataFloat.Agora.Util.ReplaceQueryString(paramPost, 'Agora__ItemPerPage', this.ItemPerPage);
	        if (!DataFloat.Agora.Util.IsEmptyOrNull(this.HtmlInterface))
	            paramPost = DataFloat.Agora.Util.ReplaceQueryString(paramPost, 'HtmlInterface', this.HtmlInterface);
	         
	        this.ActiveRequest.push(
	            $.ajax({
	              type: "POST",
	              url: ServiceUrl,
	              data: paramPost,
	              error: function(errorRequest){
	                errorRequest.AgoraParam = paramPost;
	                errorRequest.AgoraService = ServiceUrl;
	                $AgoraLog.DebugerAjaxErrors.push(errorRequest);
	              },
	              success: function(loadedContent){

	                $('#' + elementName).html('');
		            $('#' + elementName).append(loadedContent);
		            $('#' + elementName).removeClass();
		            if (onSucessFnc != null && onSucessFnc != undefined)
		                onSucessFnc({Status: 'ContentLoaded'});
	              }
	            })
	        );
	       
	        $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewRequest] - Completed');
	        return elementName;
        }
    
    DataFloat.Agora.AjaxManager.prototype.NewSystemRequest = function(ServiceUrl, Item, onSucessFnc){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewSystemRequest] - Initializing');
            paramPost = this.NewTransaction((this.FormParams + '&Agora__Item=' + Item));
            paramPost = DataFloat.Agora.Util.ReplaceQueryString(paramPost, 'Agora__ItemPerPage', this.ItemPerPage);
            if (!DataFloat.Agora.Util.IsEmptyOrNull(this.HtmlInterface))
	            paramPost = DataFloat.Agora.Util.ReplaceQueryString(paramPost, 'HtmlInterface', this.HtmlInterface);	            
	        this.ActiveRequest.push(
	            $.ajax({
	              type: "POST",
	              url: ServiceUrl,
	              data: paramPost,
	              error: function(errorRequest){
	                errorRequest.AgoraParam = paramPost;
	                errorRequest.AgoraService = ServiceUrl;
	                $AgoraLog.DebugerAjaxErrors.push(errorRequest);
	              },
	              success: function(loadedContent){
		            if (onSucessFnc != null && onSucessFnc != undefined){
		                onSucessFnc(loadedContent);
                    }else{
		                alert("Error: The loadedContent into the NewSystemRequest method is not defiend");
		            }
	              }
	            })
	        );
	        $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewSystemRequest] - Completed');
	       
        }
        
        //DataFloat.Agora.AjaxManager.prototype.PropertySearch
        DataFloat.Agora.AjaxManager.prototype.Call = function(fncOnSucess, returnOnMap, fncOnEachPinInsert, actionServiceCount, actionServiceDetail, typeOfRequest, limitCheck, onBeforeFirstLoad){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.Call] - Initializing');
            actionServiceCount = (DataFloat.Agora.Util.IsEmptyOrNull(actionServiceCount))? DataFloat.Agora.Services.ServiceContentManagerCount : actionServiceCount;
            
            paramPost = this.NewTransaction(this.FormParams)
            paramPost = DataFloat.Agora.Util.ReplaceQueryString(paramPost, 'Agora__ItemPerPage', this.ItemPerPage);
            if (!DataFloat.Agora.Util.IsEmptyOrNull(this.HtmlInterface))
	            paramPost = DataFloat.Agora.Util.ReplaceQueryString(paramPost, 'HtmlInterface', this.HtmlInterface);
	            
            var $this = this; //Trick for two levels reference on this
            this.ActiveRequest.push(
                $.ajax({
	              type: "POST",
	              url: actionServiceCount,
	              data: paramPost,
	              error: function(errorRequest){
	                errorRequest.AgoraParam = paramPost;
	                errorRequest.AgoraService = actionServiceCount;
	                $AgoraLog.DebugerAjaxErrors.push(errorRequest);
	              },
	              success: function(loadedContent){
		            $this.TotalResult = loadedContent; //Tick for two levels reference
		            limitCheck = (DataFloat.Agora.Util.IsEmptyOrNull(limitCheck)) ? loadedContent : limitCheck;
		            
		            if (fncOnSucess != null){
		                fncOnSucess({Status: 'Loaded'});
                    }

                    if ((loadedContent >0 && loadedContent <= limitCheck) || (limitCheck == "-1")){
                        if (returnOnMap){
                            $this.ShowResultMap(fncOnSucess, fncOnEachPinInsert, actionServiceDetail, typeOfRequest, onBeforeFirstLoad);
                        }else{
		                    $this.ShowResult(actionServiceDetail, typeOfRequest, fncOnSucess);
		                }
		            }else{
		                $('#' + $this.DivResult).html($this.NoResultMessage);
		            }
	              }
	            })
	       );
	       $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.Call] - Completed');
        }
        // -------------------------------------------------------------------------------------------- //
        
        DataFloat.Agora.AjaxManager.prototype.StopAllRequest = function(){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.StopAllRequest] - Initializing');
            for(iRequest = 0; iRequest < this.ActiveRequest.length; iRequest++){
                this.ActiveRequest[iRequest].abort();
            }
            this.ActiveRequest = null;
            this.ActiveRequest = new Array();
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.StopAllRequest] - Completed');
        }
        
        DataFloat.Agora.AjaxManager.prototype.NewTransaction = function(Params){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewTransaction] - Initializing');
            return DataFloat.Agora.Util.ReplaceQueryString(Params, "GUID", DataFloat.Agora.Util.NewGuid());
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewTransaction] - Completed');
        }
        
        DataFloat.Agora.AjaxManager.prototype.PropertySearchMapView = function (fncOnSucess, fncOnEachPinInsert, actionServiceCount, actionServiceDetail, typeOfRequest){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.PropertySearchMapView] - Initializing');
            this.Call(fncOnSucess, true, fncOnEachPinInsert, actionServiceCount, actionServiceDetail, typeOfRequest);
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.PropertySearchMapView] - Completed');
        }
        
        
        
        DataFloat.Agora.AjaxManager.prototype.ShowResult = function(actionServiceDetail, typeOfRequest, onSuccessFnc){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.ShowResult] - Initializing');
            actionServiceDetail = (actionServiceDetail == null || actionServiceDetail == undefined) ? DataFloat.Agora.Services.ServiceAdvancedSearchResultContent : actionServiceDetail;
            
            $('#' + this.DivResult).html('');

            iItemFinal = (this.ItemPerPage * this.CurrentPage);
            iItemFinal = (iItemFinal > this.TotalResult) ? this.TotalResult : iItemFinal;
            
            if (typeOfRequest == "allcontent"){
                    this.NewRequest(actionServiceDetail, this.CurrentPage, onSuccessFnc);
            }else{
                for (iShowingItem = (this.ItemPerPage * this.CurrentPage) - this.ItemPerPage + 1; iShowingItem <= iItemFinal; iShowingItem++){
                    this.NewRequest(actionServiceDetail, iShowingItem, onSuccessFnc);
                }
            }
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.ShowResult] - Completed');
        }
        // -------------------------------------------------------------------------------------------- //
        
        
        DataFloat.Agora.AjaxManager.prototype.ShowResultMap = function (fncControlReturn, fncOnEachPinInsert, actionServiceDetail, typeOfRequest, onBeforeFirstLoad){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.ShowResultMap] - Initializing');
            var iCount = 1;
            
            
            typeOfRequest = (typeOfRequest == null || typeOfRequest == undefined) ? "loop" : typeOfRequest;
                
            
            if (typeOfRequest == "allcontent"){
                this.NewItemMap(1, fncControlReturn, fncOnEachPinInsert, actionServiceDetail, typeOfRequest, onBeforeFirstLoad);
            }else{    
                iItemStart = (this.ItemPerPage * this.CurrentPage) - this.ItemPerPage + 1;
                iItemToShow= (this.ItemPerPage * this.CurrentPage);
                iItemToShow= (iItemToShow > this.TotalResult) ? this.TotalResult : iItemToShow;
                
                for (iShowingItem = iItemStart; iShowingItem <= iItemToShow; iShowingItem++){
                    var arrExecption = (this.MapMlsException == null) ? new Array() : this.MapMlsException;
                    if (arrExecption.indexOf(iShowingItem.MlsNumber) == -1)
                        this.NewItemMap(iShowingItem, fncControlReturn, fncOnEachPinInsert, actionServiceDetail, typeOfRequest, onBeforeFirstLoad);
                }
            }
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.ShowResultMap] - Completed');
        }
        
        
        DataFloat.Agora.AjaxManager.prototype.NewItemMap = function(Item, fncReturn, fncOnEachPinInsert, actionServiceDetail, typeOfRequest, onBeforeFirstLoad){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewItemMap] - Initializing');
            actionServiceDetail = (actionServiceDetail == null || actionServiceDetail == undefined) ? DataFloat.Agora.Services.ServiceAdvancedSearchResultJavascript : actionServiceDetail;
            paramPost = this.NewTransaction((this.FormParams + '&Agora__Item=' + Item));
            if (!DataFloat.Agora.Util.IsEmptyOrNull(this.HtmlInterface))
	            paramPost = DataFloat.Agora.Util.ReplaceQueryString(paramPost, 'HtmlInterface', this.HtmlInterface);
	            
            $this = this;
            this.ActiveRequest.push(
                $.ajax({
	              type: "POST",
	              url: actionServiceDetail,
	              data: paramPost,
	              error: function(errorRequest){
	                errorRequest.AgoraParam = paramPost;
	                errorRequest.AgoraService = actionServiceDetail;
	                $AgoraLog.DebugerAjaxErrors.push(errorRequest);
	              },
	              success: function(loadedContent){
	                if (onBeforeFirstLoad != null && onBeforeFirstLoad != undefined)
                        onBeforeFirstLoad($this.MapManager.PinManager);
	              
	                
	                
                    returnedData = JSON.parse(loadedContent);
                    $this.MapDataResult = returnedData;
                    
                    var arrProperty = new Array();
	                if (typeOfRequest == "allcontent"){
	                    for (var iPin=0; iPin<returnedData.length; iPin++)
	                    {
	                        var returnedSingleData = returnedData[iPin];
	                        arrProperty.push(new $this.MapManager.PinObject(returnedSingleData.MlsNumber, null, returnedSingleData.Latitude, returnedSingleData.Longitude, returnedSingleData.MlsNumber));
	                    }
	                }else{

	                    arrProperty.push(new $this.MapManager.PinObject(returnedData.MlsNumber, null, returnedData.Latitude, returnedData.Longitude, returnedData.MlsNumber));
	                }
	                
	                $this.MapManager.NewItem(arrProperty, fncOnEachPinInsert)
	                if (fncReturn != null && fncReturn != undefined){
                        fncReturn({Status: 'ItemLoaded'}, returnedSingleData, returnedData);
	                }
	              }
	            })
            );
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.NewItemMap] - Completed');
        }




        DataFloat.Agora.AjaxManager.prototype.CreateNavigationBar = function(forceDiv, toPageName) {
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.CreateNavigationBar] - Initializing');
            var sPages = '';
            if (this.TotalResult > 0) {

                totalPages = Math.ceil(this.TotalResult / this.ItemPerPage);
                this.TotalPages = totalPages;
                iInitPage = this.CurrentPage - this.ItemsOnNavBar;
                iInitPage = (iInitPage <= 0) ? 1 : iInitPage;
                iFinalPage = this.CurrentPage + this.ItemsOnNavBar;
                //pageName = (toPageName != undefined) ? toPageName : "";
                //pageName = "";
                if (this.CurrentPage > 1)
                //sPages = '<a href="#' + pageName + '" onclick="' + this.GotoPage + '(\'previous\')">' + this.NaviIconPrevious + '</a>';
                    sPages = '<a href="#" onclick="' + this.GotoPage + '(\'previous\')">' + this.NaviIconPrevious + '</a>';

                iFinalPage = (iFinalPage > totalPages) ? totalPages : iFinalPage;

                if (totalPages > 1) {
                    for (iPage = iInitPage; iPage <= iFinalPage; iPage++) {
                        if (iPage == this.CurrentPage) {
                            //sPages += '<a class="active">' + iPage + '</a>';
                            sPages += '<a href="javascript: ' + this.GotoPage + '(' + iPage + ')" class="active">' + iPage + '</a>';
                        } else {
                            //sPages += '<a href="#' + pageName + '" onclick="' + this.GotoPage + '(' + iPage + ')">' + iPage + '</a>';
                            sPages += '<a href="javascript: ' + this.GotoPage + '(' + iPage + ')">' + iPage + '</a>';
                        }
                    }
                }
                
                //if (iFinalPage > 1 && iFinalPage != this.CurrentPage)
                //sPages += '<a href="#' + pageName + '" onclick="' + this.GotoPage + '(\'next\')">' + this.NaviIconNext + '</a>';
                if (iFinalPage > 1 && iFinalPage != this.CurrentPage)
                    sPages += '<a href="#" onclick="' + this.GotoPage + '(\'next\')">' + this.NaviIconNext + '</a>';

                var divToWrite = (forceDiv == null || forceDiv == undefined) ? this.DivPagesNavi : forceDiv;
                $("#" + divToWrite).html(sPages);
            }
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.CreateNavigationBar] - Completed');
        }
        // -------------------------------------------------------------------------------------------- //
        
        
        DataFloat.Agora.AjaxManager.prototype.SeeDetails = function(Position, DestinationPage){
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.SeeDetails] - Initializing');
            var formSearchCriteria = document.createElement("FORM");
            formSearchCriteria.setAttribute('method','get');

            var arrFields = this.FormParams.split("&");
            
            for(iF=0;iF<arrFields.length;iF++){
                var arrCurrentField = arrFields[iF].split("=");
                if (arrCurrentField[0] != "Agora__Item__Detail"){
                    var newElement = document.createElement("INPUT");
                    newElement.type = "hidden";
                    newElement.name = arrCurrentField[0];
                    formSearchCriteria.appendChild(newElement);
                    newElement.value = arrCurrentField[1];
                }
            }
            
            
                var newElement = document.createElement("INPUT");
                newElement.type = "hidden";
                newElement.name = "Agora__Item__Detail";
                formSearchCriteria.appendChild(newElement);
                newElement.value = Position;
            
            formSearchCriteria.action = DestinationPage;
            
            document.body.appendChild(formSearchCriteria);
            formSearchCriteria.submit();
            $AgoraLog.WriteLog('[DataFloat.Agora.AjaxManager.SeeDetails] - Completed');
        }
        