﻿/// <reference path="~/Expert/scripts/jquery-1.2.6.js"/>
/// <reference path="~/Expert/scripts/json2.min.js" />
/// <reference path="~/Expert/scripts/jquery-jtemplates.js" />

function FilteredList(serviceUrl, serviceMethodName, pageSize, defaultSortOrder
    , autoSearchCharCount, contentTemplate, showDebugInfo, currentNodePath, culture) {
    this.ServiceUrl = serviceUrl;
    this.ServiceMethodName = serviceMethodName;
    this.PageSize = pageSize;
    this.CurrentPage = 1;
    this.ItemCount = -1;
    this.PageCount = -1;
    this.SearchExpression = '';
    this.SortOrder = defaultSortOrder;
    this.FilterCollection = new Array();
    this.AutoSearchCharCount = autoSearchCharCount;
    this.ContentTemplate = contentTemplate;
    this.ShowDebugInfo = showDebugInfo;
    this.CurrentNodePath = currentNodePath;
    this.CurrentCulture = culture;
    this.ResultElementJQSelector = '#listResults:first';
    this.WaitingForDataElementJQSelector = '#listFetchingData:first';

    this.Refresh = function() {
        $(this.ResultElementJQSelector).hide();
        $(this.WaitingForDataElementJQSelector).show();
        this.CallService();
    }

    this.GetFilterString = function() {
        var filter = new Object();
        filter.SearchExpression = this.SearchExpression;
        filter.SortOrder = this.SortOrder;
        filter.CurrentNodePath = this.CurrentNodePath + '/%';
        filter.Data = this.FilterCollection;
        if (typeof filter == 'undefined')
            return '{}';
        else
            return JSON2.stringify(filter);
    }

    this.UpdateResult = function(data) {
        this.PageCount = data.PageCount;
        this.ItemCount = data.Items.length;

        $(this.ResultElementJQSelector).setTemplate(this.ContentTemplate);
        $(this.ResultElementJQSelector).processTemplate(data);

        if (this.ShowDebugInfo)
            $(this.ResultElementJQSelector).append('<div>' + JSON2.stringify(data) + '</div>');

        $(this.ResultElementJQSelector).slideDown('slow');
        $(this.WaitingForDataElementJQSelector).slideUp();
    }

    this.CallService_Finnished = function(msg) {
        var data = msg[this.serviceName + 'Result'];
        data.PageCount = Math.ceil(data.TotalItemCount / data.PageSize);

        list.UpdateResult(data);

    }

    this.CallService_Error = function(msg) {
        alert(JSON2.stringify(msg));
    }

    this.CallService = function() {
        var data = '{"filter":' + this.GetFilterString() + ',"page":'
            + this.CurrentPage + ',"pageSize":' + this.PageSize
            + ',"cultureName":"' + this.CurrentCulture + '"}';
        $.ajax({
            type: "POST",
            url: this.ServiceUrl + '/' + this.ServiceMethodName,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: this.CallService_Finnished,
            error: this.CallService_Error,
            serviceName: this.ServiceMethodName
        });
    }

    this.ChangePage = function(dIndex) {
        var newIndex = this.CurrentPage + dIndex;
        return (newIndex >= 1 && newIndex <= this.PageCount) ? newIndex : this.CurrentPage;
    }

}