/** Viximo JS functions **/

/**
 * Initialize Viximo.
 */
function initializeViximo(viximoData) {
    urls = new Object();
    urls['user'] = viximoData['faniqBaseURL'] + 'profile.php?id=:user_id';
    viximoData['urls'] = urls; 
    viximo.init(viximoData);
}

/**
 * Callback for when Viximo initialization is completed.
 */
function initializeViximoCallback() {
    // change some default Viximo text to Faniq specific text
    setupTranslations();

    // setup component defaults
    viximo.ui.storefront.defaults.rows = 4;
    viximo.ui.storefront.defaults.perRow = 4;
    viximo.ui.purchase.defaults.paymentMethods = {disabled: ['mobile']};

    // now display the viximo components on the page
    displayViximoComponentsIfNecessary();
}

/**
 * Change certain default Viximo text to Faniq specific text.
 */
function setupTranslations() {
    var getStuffLink = " ";
    var sendGiftTitle = " ";
    var sendGiftLink = " ";

    if (typeof getViximoInitData == 'function') {
        var data = getViximoInitData();
        if (data['user'] != undefined) {
            // login session
            getStuffLink = "<a onclick='sendGift(" + data['user'] + ",\"" + data['name'] + "\")'>Get Stuff</a>";
            sendGiftLink = "<a onclick='sendGiftHelper()'>Send them a gift</a>";
        } else {
            // anonymous            
        }

        sendGiftTitle = "The user's locker is currently empty.";
        if (typeof viximoSendToUser != 'undefined') {
            sendGiftTitle = viximoSendToUser + "'s locker is currently empty.";
        }
    }

    viximo.translations.add({
        giftings: {
            sent: {
                title: 'Sent',
                empty: {
                    guest: {
                        title: 'No Gifts Sent',
                        description: ' '
                    },
                    owner: {
                        title: 'No Gifts Sent',
                        description: ' '
                    }
                }
            },
            received: {
                title: 'Received',
                empty: {
                    guest: {
                        title: sendGiftTitle,
                        description: sendGiftLink
                    },
                    owner: {
                        title: 'Your locker is currently empty.',
                        description: getStuffLink
                    }
                }
            }
        }
    });

    viximo.translations.add({
        goods: {
            banner: {
                earn: 'Buy {{currency}}',
                noCreditCards: ' '
            }
        }
    });
}

/**
 * Display the Viximo buy popup.
 */
function buyFanIQBucks() {
    viximo.ui.purchase({
        success: function() {
            if (typeof viximoPurchaseCallback == 'function') {
                viximoPurchaseCallback();        
            }
        }
    });
    return false;
}

function fanIQBucksBalance(divID, memberID) {
    var url = '/ajax/virtualcurrency/balance.php';
    if (memberID > 0) {
        url = url + '?memberID=' + memberID;
    }
    new Ajax.Request(url,
    {
        method: 'get',        
        onSuccess: function(response) {
            var result = response.responseText.evalJSON();            
            if (result.status == 'SUCCESS') {
                document.getElementById(divID).innerHTML = result.balance;
            } else {
                document.getElementById(divID).innerHTML = 'NA';
            }
        }
    });
}

function fanIQBucksTransactions(divID) {
    new Ajax.Request('/ajax/virtualcurrency/bucks_transactions.php',
    {
        method: 'get',
        onSuccess: function(response) {
            document.getElementById(divID).innerHTML = response.responseText;
        }
    });
}

/**
 * Display the Viximo send gift popup.
 * @param memberID
 * @param name
 */
function sendGift(memberID, name) {
    viximo.ui.storefront({
        rows: 4,
        perRow: 4,
        sort: 'featured',
        displayAutocomplete: false,
        receivers: [
            {
                id: memberID,
                name: name
            }
        ],
        giftingClose: refreshViximoComponentsIfNecessary
    });
    return false;
}

function displayViximoComponentsIfNecessary() {
    if (typeof displayViximoComponents == 'function') {
        displayViximoComponents();
    }
}

function refreshViximoComponentsIfNecessary() {
    if (typeof refreshViximoComponents == 'function') {
        refreshViximoComponents();
    }
}

function displayGift(id) {
    viximo.ui.good({
        id: id,
        details: ['name'],
        detailAlign: 'center'
    });
    return false;
}