﻿function BaseControl(name, path, params, paged, pageSize) {
    var _Name = name;
    var _Id = "#" + name;
    var _Path = path;
    var _Paged = paged;
    var _Params = params;
    var _PageSize = pageSize;
    var _CurrentPage = 0;
    var _ResultCount = 0;
    var _Self = this;
    var _ShowList = false;
     
    //Default params
    _Params["p"] = 0;
    _Params["ps"] = pageSize;

    //Init
    $(document).ready(function () {
        //get You position
        window.CA.Settings.Mylat = -1;
        window.CA.Settings.Mylong = -1;
        //Construct
        $(_Id + "Template").template(_Name);
        $(_Id).find("#More").bind("click", null, Page_Click);
        //Construct
        $('#FilterDDL').hide();

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                window.CA.Settings.Mylat = position.coords.latitude;
                window.CA.Settings.Mylong = position.coords.longitude;
                $('#FilterDDL').show();
                _Self.Execute();
            }, function () { _Self.Execute(); });
        }
        else { _Self.Execute(); }
    });

    this.Execute = function () {
        window.CA.ShowLoader();
        if (arguments.length > 0)
            _Params = $.extend(true, _Params, arguments[0]);

        if (_Params["prodIds"] != "") {
            _ShowList = true;
        }
        _Params["Mylat"] = window.CA.Settings.Mylat;
        _Params["Mylong"] = window.CA.Settings.Mylong;
        _CurrentPage = _Params['p'];
        CA.Ajax.GetJSON(_Path, _Params, Execute_Success, null, false);
    }

    function Execute_Success(json) {
        //set selected product ids to empty 
        window.CA.Settings.productIds = "";
        window.CA.HideLoader();
        if (_Params["clear"])
            $(_Id + "Rec").empty();

        _ResultCount = json.Count;
        $.tmpl(_Name, json).appendTo(_Id + "Rec");

        var pagesNum = Math.ceil(_ResultCount / _PageSize);
        if (_CurrentPage < (pagesNum-1))
            $("#More").show();
        else
            $("#More").hide();

        //for scroll 
        var newHeight
        if (!_ShowList) {
            if (_CurrentPage == 0 || _CurrentPage == 1 || _CurrentPage == 2)
                newHeight = ((_CurrentPage + 1) * (_PageSize + 3) * 100);
            else
                newHeight = ((_CurrentPage + 1) * (_PageSize + 1) * 100);
        }
        else {
            newHeight = _ResultCount * 100;
        }


        $('#scroller').css('height', newHeight + 'px');
        myScroll.refresh();

        // Hides mobile browser's address bar when page is done loading.
        window.scrollTo(0, 1);
    }

    function Page_Click(e) {
            _CurrentPage += 1;
            _Params["p"] = _CurrentPage;
            _Params["clear"] = null;
            _Self.Execute();
    }
    this.GetAndSaveID  = function (id) {
        window.CA.Settings.productIds += id + ",";
        return id;
    }
}
