﻿/*! Copyright © 2010 General Mills. All Rights Reserved. No permission is granted to use, copy or extend this code. */
QRV.namespace('QRV.CommunityToolbar');
QRV.CommunityToolbar =
{
    init: function(commentsPlaceholder, contentWrapper, itemID, simplePath, type)
    {
        QRV.CommunityToolbar.url = '/handlers/AJAXHandler.ashx';
        QRV.CommunityToolbar.itemID = itemID;
        QRV.CommunityToolbar.type = type;
        QRV.CommunityToolbar.path = simplePath;

        $(document).ready(function()
        {
            if (QRV.PageManager.IsLoggedUser && $.fn.starRater != undefined)
            {
                $('.starRater').starRater({ count: 5, url: QRV.CommunityToolbar.url });
            }
            if ($.fn.shareThat != undefined)
            {
                $('img.compartir').shareThat({ GUID: itemID, path: simplePath });
            }

            //for printing
            QRV.CommunityToolbar.contentWrapper = $(contentWrapper);
            QRV.CommunityToolbar.printButton = $('a.imprimir');
            QRV.CommunityToolbar.printButton.click(QRV.CommunityToolbar.print);

            //comments
            QRV.CommunityToolbar.commentsWrapper = $('.comments-wrapper');                                  //wraps the entir comments section of the control
            if (QRV.CommunityToolbar.commentsWrapper.length > 0)
            {
                //div container on page in which to insert comments panel
                QRV.CommunityToolbar.commentsPlaceholder = $(commentsPlaceholder);
                QRV.CommunityToolbar.commentsWrapper.remove().appendTo(QRV.CommunityToolbar.commentsPlaceholder);   //move toolbar comments control from current location to inside target container
                QRV.CommunityToolbar.commentsWrapper.show();

                if (QRV.PageManager.IsLoggedUser)
                {
                    QRV.CommunityToolbar.saveCommentsButton = $('.btnSaveComments');
                    QRV.CommunityToolbar.saveCommentsButton.click(QRV.CommunityToolbar.saveComments);

                    QRV.CommunityToolbar.txtComments = $('#txtComments');
                    QRV.CommunityToolbar.commentBubble = $('#comenta');
                    QRV.CommunityToolbar.commentBubble.click(function()
                    {
                        QRV.CommunityToolbar.txtComments.focus();
                    });

                    QRV.CommunityToolbar.commentCount = $('.comment-count');
                    QRV.CommunityToolbar.statusIcon = $(document.createElement('div')).addClass('ajax-loading').attr('style', 'height:20px;width:20px');

                    //if panel is visible, then user does not have an existing screen-name, so display link to update profile page...
                    QRV.CommunityToolbar.userNameWrapper = $('div.user-name');
                    if (QRV.CommunityToolbar.userNameWrapper.length > 0)
                    {
                        QRV.CommunityToolbar.saveCommentsButton.hide();
                        QRV.CommunityToolbar.txtComments.attr('disabled', 'disabled');
                        QRV.CommunityToolbar.txtComments.css('background-color', '#eee');
                        $('.first-comment').css('color', '#ccc');
                        QRV.CommunityToolbar.saveCommentsButton.parent().css('width', '100%');
                        QRV.CommunityToolbar.userNameLink = $('#userNameLink');
                        QRV.CommunityToolbar.userNameLink.click(function()
                        {
                            location.href = 'https://' + QRV.PageManager.absoluteURI + '/cuenta/ActualizarPerfil.aspx?redir=' + encodeURIComponent(simplePath);
                        });
                    }
                }

                QRV.CommunityToolbar.commentsPanel = $('#commentsPanel');
                QRV.CommunityToolbar.commentsList = $('.comments-list');
                QRV.CommunityToolbar.toggleCommentsButton = $('.toggleComments');
                QRV.CommunityToolbar.toggleCommentsButton.click(QRV.CommunityToolbar.toggleComments);
                QRV.CommunityToolbar.firstComment = $('.first-comment');
                if (QRV.CommunityToolbar.commentsPanel.children().length > 0)
                {
                    QRV.CommunityToolbar.commentsList.show();
                    QRV.CommunityToolbar.commentsPanel.isVisible = true;
                }
            }
        });
    },

    saveComments: function(e)
    {
        QRV.CommunityToolbar.statusIcon.attr('class', '');
        QRV.CommunityToolbar.statusIcon.addClass('ajax-loading');
        QRV.CommunityToolbar.saveCommentsButton.before(QRV.CommunityToolbar.statusIcon);
        QRV.CommunityToolbar.saveCommentsButton.hide();

        var params =
        {
            id: QRV.CommunityToolbar.itemID,
            action: "saveComments",
            url: encodeURIComponent(QRV.CommunityToolbar.path),
            comment: encodeURIComponent(QRV.CommunityToolbar.txtComments.val())
        };
        $.get(QRV.CommunityToolbar.url, params, QRV.CommunityToolbar.saveComments_callback, 'json');
    },

    saveComments_callback: function(results)
    {
        QRV.CommunityToolbar.statusIcon.remove();
        if (results.success)
        {
            QRV.CommunityToolbar.commentsPanel.html(results.content);
            QRV.CommunityToolbar.txtComments.val('');
            QRV.CommunityToolbar.commentCount.text(results.commentCount);
            QRV.CommunityToolbar.commentsList.show();
            QRV.CommunityToolbar.commentsPanel.isVisible = true;
            QRV.CommunityToolbar.firstComment.hide();
        }
        QRV.CommunityToolbar.saveCommentsButton.show();
    },

    toggleComments: function(e)
    {
        if (QRV.CommunityToolbar.commentsPanel.isVisible)
        {
            QRV.CommunityToolbar.commentsPanel.hide('slow');
            QRV.CommunityToolbar.commentsPanel.isVisible = false;
        }
        else
        {
            QRV.CommunityToolbar.commentsPanel.show('slow');
            QRV.CommunityToolbar.commentsPanel.isVisible = true;
        }
    },

    print: function(number, title)
    {
        var html = '<?xml version="1.0" encoding="UTF-8"?> ';
        html += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> '
        html += '<html xmlns="http://www.w3.org/1999/xhtml"> ';
        html += '<head><meta http-equiv="content-Type" content="text/html" /> ';
        html += '<title>' + document.title + '</title> ';
        html += '<style type="text/css">table{width:600px;} .family-article-body p{margin:0;}</style> </head> ';
        html += '<body><table border="0" cellpadding="0" cellspacing="0" align="center"><tr><td> ';
        html += '<h2 class="print-title">' + document.title + '</h2> ';
        html += QRV.CommunityToolbar.contentWrapper.html();
        html += '</td></tr></table></body></html> ';

        var printWindow = window.open('', 'imprintir', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1');
        printWindow.document.write(html);
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();

        if (QRV.CommunityToolbar.type == 'recipe')
        {
            //log print activity
            var params =
            {
                id: QRV.CommunityToolbar.itemID,
                activity: "print",
                action: "log"
            };
            $.get(QRV.CommunityToolbar.url, params);
        }
        return false;
    }
};

