﻿var giftPages = 1, currentGiftPage = 1;
var giftsPerGiftPage = 12;
var totalGifts = 0;
var giftsPanel = null;
var giftObj = new Object();

var pipenoTexatreaItems;
var pipenoTextInputs;

var giftWasSent = false;

$(document).ready(function () {

    var gifts = $('ul.gifts-list li');
    var selectedGiftImage = $('.content2 img');
    
    giftsPanel = $('.gifts-panel');
    totalGifts = gifts.length;
    
    handlePaginationButtons(currentGiftPage);

    //add classes for every page
    var index = 1;

    $(gifts).each(function () {
        index++;

        $(this).addClass('pag' + giftPages);

        if (!$(this).hasClass('pag1')) {
            $(this).hide();
        }

        if (index > giftsPerGiftPage) {
            giftPages++;
            index = 1;
        }
    });

    //bind events for pagination prev and next buttons
    $(giftsPanel).find('a.prev').click(function () {
        if (currentGiftPage > 1) {
            currentGiftPage--;
            handleGifts(giftsPanel, currentGiftPage);
            handlePaginationButtons(currentGiftPage);
        }
    });
    $(giftsPanel).find('a.next').click(function () {
        currentGiftPage++;
        handleGifts(giftsPanel, currentGiftPage);
        handlePaginationButtons(currentGiftPage);
    });

    //converting textarea to textarea pipeno control
    var texatreaItems = new Array();
    pipenoTexatreaItems = new Array();
    texatreaItems = $('.textarea');

    $(texatreaItems).each(function () {
        pipenoTexatreaItems[this.id] = new PipenoUI.RoundedSimpleInputText(this, $.trim($(this).html()), "", 250, { isTextarea: "true", inputName: this.id, inputId: this.id });
    });

    //converting input to input pipeno control
    var textInputs = new Array();
    pipenoTextInputs = new Array();
    inputs = $('.input');
    $(inputs).each(function () {
        pipenoTextInputs[this.id] = new PipenoUI.RoundedSimpleInputText(this, "1", "", 4, { type: "text", inputName: this.id, inputId: this.id, keys: [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 13, 8, 46, 37, 38, 39, 40, 109, 188, 190] });
    });

    //bind events for selecting the gifts
    $(gifts).find('a').click(function () {
        if (!$(this).hasClass('inactive')) {
            var self = $(this);
            var gift = $(self).find('img');

            $(giftsPanel).find('.gp-bottom').removeClass('white');
            $(self).parent().parent().find('a.selected').removeClass('selected');
            $(self).addClass('selected');

            if (typeof $(gift).attr('src') != 'undefined')
                $(selectedGiftImage).attr('src', $(gift).attr('src'));
            if (typeof $(gift).attr('alt') != 'undefined')
                $(selectedGiftImage).attr('alt', $(gift).attr('alt'));
            if (typeof $(gift).attr('width') != 'undefined')
                $(selectedGiftImage).attr('width', $(gift).attr('width'));
            if (typeof $(gift).attr('height') != 'undefined')
                $(selectedGiftImage).attr('height', $(gift).attr('height'));

            giftObj.id = $(self).attr('id').substring(5);
            giftObj.price = giftsArray[giftObj.id]['price']; 
            giftObj.name = giftsArray[giftObj.id]['name'];
            giftObj.username = forUserName;

            $(giftsPanel).find('strong.price').text(giftObj.price.toString());
            $(giftsPanel).find('.content2 .gift-name').text(giftObj.name);

            if (typeof giftObj.username != 'undefined') {
                $(giftsPanel).find('.content2 .username').text(giftObj.username);
            }
            else {
                return false;
            }

            $(giftsPanel).find('.content2').show();
        }
    });

    //bind event for the <<send>> button
    $(giftsPanel).find('.send-btn').click(function () {
        if (!isNaN(pipenoTextInputs['quantity'].Value()) && pipenoTextInputs['quantity'].Value() != "0") {
            $('#quantity').removeClass('error');
            giftObj.quantity = pipenoTextInputs['quantity'].Value();
            giftObj.message = pipenoTexatreaItems['message'].Value();

            $(giftsPanel).find('.confirm span.quantity').text(giftObj.quantity);
            $(giftsPanel).find('.confirm span.gift-name').text(giftObj.name);
            $(giftsPanel).find('.confirm span.price').text(giftObj.quantity * giftObj.price);
            if (giftObj.quantity > 1) {
                $(giftsPanel).find('.confirm span.plural').text('gifts');
            } else {
                $(giftsPanel).find('.confirm span.plural').text('gift');
            }

            $(gifts).find('a').addClass('inactive');
            $(giftsPanel).find('.choose').hide();
            $(giftsPanel).find('.content2 .col1 p').hide();
            $(giftsPanel).find('.confirm').show();
        } else {
            $('#quantity').addClass('error');
        }
    });

    //bind event for the <<confirm>> button
    $(giftsPanel).find('.confirm-btn').click(function () {
        if (!giftWasSent) {
            var self = this;
            $(self).parent().parent().parent().find('p.error').remove();
            $(self).parent().parent().parent().find('p.success').remove();
            $.ajax({
                url: sendGiftUrl,
                type: 'POST',
                dataType: 'json',
                data: { 'forUserId': forUserId, 'giftId': giftObj.id, 'sourceType': giftSourceType, 'quantity': giftObj.quantity, 'message': giftObj.message, 'sourceId': sourceId },
                success: function (data) {
                    window.scroll(0, 0);
                    $(giftsPanel).find('ul.gifts-list').hide();
                    $(giftsPanel).find('div.gifts-pagination').hide();
                    $(giftsPanel).find('.content2').hide();
                    $(giftsPanel).find('.gp-bottom').addClass('white');
                    if (data.Code == 0) {
                        $(giftsPanel).find('.content1').append($('<p/>', { 'class': 'success', 'text': 'Your gift was successfully sent!' }));

                        if (typeof $(".gifts-number") != 'undefined' && giftSourceType != "Comment") {
                            var number = parseInt($(".gifts-number").text()) + parseInt(giftObj.quantity);
                            $("span.gifts-number").html(number);

                            if (typeof $("div.community span.reactions") != 'undefined') {
                                $("div.community span.reactions").html(parseInt($("div.community span.reactions").html()) + parseInt(giftObj.quantity));
                            }
                        }

                        giftWasSent = false;
                    }
                    else if (data.Code == 1) {
                        if (typeof data.message != 'undefined') {
                            $(giftsPanel).find('.content1').append($('<p/>', { 'class': 'error', 'text': decodeURI(data.message) }));
                        }
                        else {
                            $(giftsPanel).find('.content1').append($('<p/>', { 'class': 'error', 'text': 'There has been an error!' }));
                        }
                        $(self).parent().find('p.error').show();
                        giftWasSent = false;
                    }
                },
                error: function () {
                    window.scroll(0, 0);
                    $(giftsPanel).find('ul.gifts-list').hide();
                    $(giftsPanel).find('div.gifts-pagination').hide();
                    $(giftsPanel).find('.content2').hide();
                    $(giftsPanel).find('.gp-bottom').addClass('white');
                    $(giftsPanel).find('.content1').append($('<p/>', { 'class': 'error', 'text': 'There has been an error!' }));
                }
            });
            giftWasSent = true;

        }
    });

    //bind event for close gifts popup
    $(giftsPanel).find('.close-btn').click(function () {
        $('.overlay-gifts').hide();
        ResetGiftPanel();
    });
});

function handlePaginationButtons(page) {
    if (page == 1) {
        $(giftsPanel).find('a.prev').hide();
    }
    else {
        $(giftsPanel).find('a.prev').show();
    }

    if (page == Math.ceil(totalGifts / giftsPerGiftPage)) {
        $(giftsPanel).find('a.next').hide();
    }
    else {
        $(giftsPanel).find('a.next').show();
    }
}

function handleGifts(container, page) {
    $(container).find('.gifts-list li').hide();
    $(container).find('.gifts-list li[class="pag' + page + '"]').show();
}

function ResetGiftPanel() {
    var giftsPanel = $('.gifts-panel');
    $(giftsPanel).find('ul.gifts-list li a').removeClass('selected');
    $(giftsPanel).find('.content2').hide();
    $(giftsPanel).find('.content2 .choose').show();
    $(giftsPanel).find('.content2 .image-holder p').show();
    $(giftsPanel).find('.content2 .confirm').hide();
    $(giftsPanel).find('ul.gifts-list li a').removeClass('inactive');
    $(giftsPanel).find('p.error').remove();
    $(giftsPanel).find('p.success').remove();
    $(giftsPanel).find('ul.gifts-list li').hide();
    $(giftsPanel).find('ul.gifts-list li.pag1').show();
    $(giftsPanel).find('ul.gift-list').show();
    $(giftsPanel).find('ul.gifts-list').show();
    $(giftsPanel).find('div.gifts-pagination').show();

    currentGiftPage = 1;
    pipenoTexatreaItems['message'].Value('');
    pipenoTextInputs['quantity'].Value('1');

    handlePaginationButtons(1);
}

