﻿
/* Properties */
var callIndex = 0; // holds the current number of calls
var loadCount = 5; // how many items to load on demand
var firstLoad = true; // is first load
var itemsLoadedR = 0; // holds the total number of trades loaded for the right side
var itemsLoadedL = 0; // holds the total number of trades loaded for the left side
var _opts; // holds the cycle properties for the trade webservice callback
var _fwd; // holds the direction of the rotator for the trade webservice callback
var _initiatorUserID; // holds the initiatorUserID for the rewards webservice callback
var _targetUserID; // holds the targetUserID for the rewards webservice callback

$(document).ready(function() {
    _fwd = true;
    TellBob.Services.WebCall.GetTrades(4, 0, true, AddTradeSlides);
    callIndex++;
});

function onAfterTradeSlide(curr, next, opts, fwd) {
    if ($('#__TradeRewards').css('display') != 'none')
        $('#__TradeRewards').hide("blind", 500);
    if (fwd == true && opts.currSlide > 1 && opts.currSlide + 6 > opts.slideCount) {
        _opts = opts;
        _fwd = fwd;
        TellBob.Services.WebCall.GetTrades(loadCount, itemsLoadedR, fwd, AddTradeSlides);
        callIndex++;
    }
    else if (fwd == false && opts.currSlide - 5 < 0) {
        _opts = opts;
        _fwd = fwd;
        TellBob.Services.WebCall.GetTrades(loadCount, itemsLoadedL, fwd, AddTradeSlides);
        callIndex++;
    }
    firstLoad = false;
}

function AddTradeSlides(result) {
    if (result.length > 0) {
        if (_fwd == true)
            itemsLoadedR += result.length;
        else
            itemsLoadedL += result.length;
        if (firstLoad) {
            $('#__Trade').html("");
        }
        for (var i = 0; i < result.length; i++) {
            var template = $.createTemplate($("#__jTemplate_trade").html().replace(/%7B/g, '{').replace(/%7D/g, "}").replace(/&amp;/g, "&").replace(/&gt;/g, ">").replace(/&lt;/g, "<"));
            var html = $.processTemplateToText(template, result[i]);

            if (firstLoad)
                $('#__Trade').append(html);
            else
                _opts.addSlide(html, !_fwd);
        }
        if (firstLoad) {
            $('#__Trade').cycle({
                fx: 'scrollHorz',
                startingSlide: 1,
                speed: 500,
                timeout: 0,
                next: '#__nextTrade',
                prev: '#__prevTrade',
                before: onAfterTradeSlide
            });
        }
    }
}

function PopulateTradedRewards(result) {
    var template = $.createTemplate($("#__jTemplate_rewardsTraded").html().replace(/%7B/g, '{').replace(/%7D/g, "}"));
    var html = $.processTemplateToText(template, result);
    $('#__TradeRewards').html(html);
    $('#__TradeRewards').toggle("blind", 500);
}

function GetTradedRewards(swapID, initiatorUserID, targetUserID) {
    _initiatorUserID = initiatorUserID;
    _targetUserID = targetUserID;
    TellBob.Services.WebCall.GetTradedRewards(swapID, initiatorUserID, targetUserID, PopulateTradedRewards);
}