﻿
// a generic repeater
//
var repeater = function(func, times, interval) {
    var ID = window.setInterval(function(times) {
        return function() {
            if (--times <= 0)
                window.clearInterval(ID);

            func();
        }
    } (times), interval);
};

function splitText(text) {
    return text.split('-')[1];
}


var pollTypeEnum = { "Wall": 1, "WallSearch": 2 };


var prayerwall = function() {

    this._homeUrl = "";
    this._dateStarted = "";
    this._newWallEntryCounter = 0;

    this._bProcessingNewWallEntry = false;
    this.__bProcessingNewWallSearch = false;

    this._pollInterval1 = 10000;
    this._pollInterval2 = 45000;
    this._poll1Max = 30;
    this._poll2Max = 240;
    this._newWallPollerCount = 0;
    this._newSearchPollerCount = 0;

    this._pollType = -1;

    this._searchText = "";


    var _initPage = function($homeUrl, $dateStarted) {

        _pollType = -1;


        if (btnSubmit) {

            if ($("#" + btnSubmit).attr("class").length > 0)
                _pollType = pollTypeEnum.WallSearch;
            else
                _pollType = pollTypeEnum.Wall;
        }
        else
            _pollType = pollTypeEnum.Wall;



        if (_pollType == pollTypeEnum.Wall)
            _initWallPoll($homeUrl, $dateStarted);

        /*
        else {

            _searchText = escape($("#" + tbSearch).val());

            _initSearchPoll($homeUrl, $dateStarted, _searchText);
        }
        */


        // events
        //
        $(".post-delete").bind('click', this._onDeleteWallItemClick);

    };

    var _initWallEntryPage = function($homeUrl) {

        _homeUrl = $homeUrl;



        $(".post-delete").bind("click", this._deletePostWithRedirect);

    };


    var _initWallPoll = function($homeUrl, $dateStarted) {


        _homeUrl = $homeUrl;
        _dateStarted = $dateStarted;

        repeater(_getNewWallCount, _poll1Max, _pollInterval1);



    };

    var _initSearchPoll = function($homeUrl, $dateStarted, $searchText) {

        _homeUrl = $homeUrl;
        _dateStarted = $dateStarted;
        _searchText = $searchText;

        repeater(_getNewSearchCount, _poll1Max, _pollInterval1);



    };




    this._getNewWallCount = function() {


        if (_bProcessingNewWallEntry)
            return;

        _bProcessingNewWallEntry = true;


        $.get(_homeUrl + "service/ajax/ajax.ashx", { art: "310", dateSince: _dateStarted }, function(data) {
            _showWallPoller(data);
        });

        _newWallPollerCount += 1;

        if (_newWallPollerCount == _poll1Max) {

            repeater(_getNewWallCount, _poll2Max, _pollInterval2);
        }


        _bProcessingNewWallEntry = false;
    };


    this._showWallPoller = function($newCount) {

        if (0 == $newCount)
            return;

        var link = $("<a>Refresh</a>");

        link.attr("href", _homeUrl + "prayerwall/");

        var addText = $newCount + " more results since you started viewing.  "


        $("#wallPoller").empty().append(addText).append(link).append(" to see them.").show();


    };



    this._getNewSearchCount = function() {


        if (__bProcessingNewWallSearch)
            return;

        __bProcessingNewWallSearch = true;


        $.get(_homeUrl + "service/ajax/ajax.ashx", { art: "400", dateSince: _dateStarted, q: _searchText }, function(data) {
            _showSearchPoller(data);
        });

        _newSearchPollerCount += 1;

        if (_newSearchPollerCount == _poll1Max) {

            repeater(_getNewSearchCount, _poll2Max, _pollInterval2);
        }


        __bProcessingNewWallSearch = false;
    };


    this._showSearchPoller = function($newCount) {

        if (0 == $newCount)
            return;

        var link = $("<a>Refresh</a>");

        link.attr("href", _homeUrl + "prayerwall/default.aspx?q=" + _searchText);



        var addText = $newCount + " more search results since you started viewing.  "


        $("#searchPoller").empty().append(addText).append(link).append(" to see them.").show();


    };





    this._onDeleteWallItemClick = function(e) {

        var _this = this;
        var id = $(_this).parents(".table-item").attr("id");

        e.preventDefault();

        id = splitText(id);


        if (confirm("Are you sure you want to delete?")) {
            _deletePost(id);
        }

    };

    this._deletePost = function($id) {

        // ajax call
        //
        $.get(_homeUrl + "service/ajax/ajax.ashx", { art: "320", postID: $id }, function(data) {


            $("#wallItem-" + $id).css("background-color", "#ffffcc").fadeOut("slow", function() {

                $("#wallItem-" + $id).remove();
                $("#myprayer-" + $id).remove();
                //window.location = window.location.href;

            });



        });

    };

    this._deletePostWithRedirect = function() {

        var id = -1;

        var domID = $(".post-attributes").attr("id");

        id = splitText(domID);


        if (confirm("Are you sure you want to delete?")) {

            $.get(_homeUrl + "service/ajax/ajax.ashx", { art: "320", postID: id }, function(data) {

                window.location = _homeUrl + "prayerwall/default.aspx?show=user";

            });

        }
    };


    // public
    //
    return {


        initPage: function($homeUrl, $dateStarted) {

            _initPage($homeUrl, $dateStarted);

        },

        initWallEntryPage: function($homeUrl) {
            _initWallEntryPage($homeUrl);
        }





    }; // return

} ();                                          // prayerwall
