/*
    form_meal_plan_category_subscribe.js Popup for meal plan category subscription
    Dependencies : Prototype JavaScript framework 1.6
                   utilities.js
*/

var SOSCuisine = SOSCuisine || {};

SOSCuisine.recipeReview =
{
    recipeId: null,
    mode: "normal",
    recipeSaveEnabledCallback: null,
    isSignificativeRecipe: true,

    initializeRecipeView: function()
    {
        if (this.recipeId != null)
        {
            if (this.isSignificativeRecipe)
            {        
                this.updateRecipeTomatoesChart();
                this.recipeRatingsUpdateTab();
                this.recipeRatingsUpdateContent();
                this.recipeRatingsUpdateReviewsList();
            }
            this.recipeNotesUpdateTab();
            this.recipeNotesUpdateContent();
        }
    },

    recipeRatingsUpdateContent: function()
    {
        this.updateContent("recipe_reviews.inc.php", 'get', { recipe_id: this.recipeId },
            function(response)
            {
                $("recipe_my_rating_container").innerHTML = response;
            }
        );
    },

    recipeRatingsUpdateTab: function()
    {
        this.updateContent("recipe_reviews_tab.inc.php", 'get', { recipe_id: SOSCuisine.recipeReview.recipeId },
            function(response)
            {
                $("tabRatingsInner_" + SOSCuisine.recipeReview.recipeId).innerHTML = response;
            }
        );
    },

    saveRecipeRating: function()
    {
        if ($('recipe_rating_add_to_favorites') && $('recipe_rating_add_to_favorites').checked)
        {
            this.recipeWasAddedToFavorites();
        }

        var ratingForm = $('my_rating_form');
        var serializedData = ratingForm.serialize(true);

        this.updateContent("recipe_reviews.inc.php?recipe_id=" + this.recipeId, 'post', serializedData,
            function(response)
            {
                if (this.mode == "normal")
                {
                    $('recipe_my_rating_container').innerHTML = response;
                    $('recipe_rating_status').innerHTML = $('textRatingSavedChanges').innerHTML;
                    $('recipe_rating_status').fade({ duration: 3.5 });
                    this.recipeRatingsUpdateTab();
                    this.updateRecipeTomatoesChart();
                    this.recipeRatingsUpdateReviewsList();
                }
            } .bind(this)
        );
    },

    saveRecipeReviewLanguageVote: function(recipe_review_language_id)
    {
        var parameters = $('recipe_review_language_vote_form_' + recipe_review_language_id).serialize(true);

        this.updateContent("recipe_review_vote.inc.php?recipe_id=" + recipe_review_language_id, 'post', parameters,
            function(response)
            {
                $('votes_container_' + recipe_review_language_id).innerHTML = response;
            }
        );
    },

    updateRecipeTomatoesChart: function()
    {
        this.updateContent("recipe_tomatoes.inc.php", 'get', { recipe_id: this.recipeId },
            function(response)
            {
                $('recipe_tomatoes_chart_container').innerHTML = response;
            }
        );
    },

    enableRecipeRatingButtons: function()
    {
        if ($('recipe_rating_cancel_button'))
        {
            $('recipe_rating_cancel_button').disabled = false;
        }

        if ($('recipe_rating_delete_button'))
        {
            $('recipe_rating_delete_button').disabled = true;
        }


        if ($F('rating_for_tomatoes') == 0)
        {
            return;
        }
        else
        {
            if (this.recipeSaveEnabledCallback)
            {
                this.recipeSaveEnabledCallback();
            }
        }

        if ($('recipe_rating_save_button'))
        {
            $('recipe_rating_save_button').disabled = false;
        }
    },

    disableRecipeRatingButtons: function()
    {
        if ($('recipe_rating_save_button'))
        {
            $('recipe_rating_save_button').disabled = true;
        }
        if ($('recipe_rating_cancel_button'))
        {
            $('recipe_rating_cancel_button').disabled = true;
        }
        if ($('recipe_rating_delete_button'))
        {
            $('recipe_rating_delete_button').disabled = false;
        }
    },

    recipeRatingChanged: function(event)
    {
        this.enableRecipeRatingButtons();
    },

    recipeRatingResetForm: function()
    {
        this.recipeRatingSyncTomatoes($F('rating_for_tomatoes_initial_value'));
        this.disableRecipeRatingButtons();
    },

    recipeRatingTomatoClick: function(tomato)
    {
        $('rating_for_tomatoes').value = tomato;
        for (i = 1; i <= 5; i++)
        {
            $('rating_tomato_' + i).setStyle({ opacity: 1.0 })
            if (i <= tomato)
            {
                $('rating_tomato_' + i).pulsate({ pulses: 3, duration: 0.45 });
            }
        }
        this.recipeRatingChanged();
        this.recipeRatingSyncTomatoes($F('rating_for_tomatoes'));
    },

    recipeRatingSyncTomatoes: function(score)
    {
        for (i = 1; i <= 5; i++)
        {
            if (i <= score)
            {
                $('rating_tomato_' + i).src = SOSCuisine.config.urlRoot + "media/images/member_rating_full.gif";
            }
            else
            {
                $('rating_tomato_' + i).src = SOSCuisine.config.urlRoot + "media/images/rating_empty.gif";
            }
        }
    },

    recipeRatingTomatoOnMouseOver: function(score)
    {
        for (i = 1; i <= 5; i++)
        {
            if (i <= score)
            {
                $('rating_tomato_' + i).src = SOSCuisine.config.urlRoot + "media/images/member_rating_full.gif";
            }
            else
            {
                $('rating_tomato_' + i).src = SOSCuisine.config.urlRoot + "media/images/rating_empty.gif";
            }
        }

        switch (score)
        {
            case 1:
                $('hint_for_tomato_rating').innerHTML = $('textHintForScore1OutOf5').innerHTML;
                break;
            case 2:
                $('hint_for_tomato_rating').innerHTML = $('textHintForScore2OutOf5').innerHTML;
                break;
            case 3:
                $('hint_for_tomato_rating').innerHTML = $('textHintForScore3OutOf5').innerHTML;
                break;
            case 4:
                $('hint_for_tomato_rating').innerHTML = $('textHintForScore4OutOf5').innerHTML;
                break;
            case 5:
                $('hint_for_tomato_rating').innerHTML = $('textHintForScore5OutOf5').innerHTML;
                break;
        }
    },

    recipeRatingTomatoOnMouseOut: function(score)
    {
        for (i = 1; i <= 5; i++)
        {
            if (i <= $F('rating_for_tomatoes'))
            {
                $('rating_tomato_' + i).src = SOSCuisine.config.urlRoot + "media/images/member_rating_full.gif";
            }
            else
            {
                $('rating_tomato_' + i).src = SOSCuisine.config.urlRoot + "media/images/rating_empty.gif";
            }
        }
        $('hint_for_tomato_rating').innerHTML = '';
    },

    recipeRatingKeyPressed: function(event)
    {
        if (window.event) // IE
        {
            keynum = event.keyCode;
        }
        else if (event.which) // Netscape/Firefox/Opera
        {
            keynum = event.which;
        }

        if (typeof (keynum) != 'undefined')
        {
            this.enableRecipeRatingButtons();
        }
    },

    recipeRatingsUpdateReviewsList: function(_page, _sort)
    {
        page = _page || 1;
        sort = _sort || 'date';

        this.updateContent("recipe_reviews_list.inc.php", 'get', { recipe_id: this.recipeId, page: page, sort_by: sort },
            function(response)
            {
                $('recipe_reviews_list_container').innerHTML = response;
            }
        );
    },

    letUserRateRecipe: function(open_my_review_section)
    {
        if ($('my_rating_form_collapsed'))
        {
            // User is logged in
            SOSCuisine.tabControl.changeTab('MyRating', this.recipeId);
            if (open_my_review_section)
            {
                this.showRecipeReviewForm();
                $('hint_for_tomato_rating').pulsate({ pulses: 9, duration: 2.0 });
            }

        }
        else if ($('my_rating_form_collapsed_not_logged_in'))
        {
            // User is not logged in
            SOSCuisine.tabControl.changeTab('MyRating', this.recipeId);
            if (open_my_review_section)
            {
                this.showRecipeReviewFormNotLoggedIn();
            }
        }
    },

    showRecipeReviewForm: function()
    {
        $('my_rating_form_collapsed').hide();
        $('my_rating_form').show();
    },

    hideRecipeReviewForm: function()
    {
        $('my_rating_form_collapsed').show();
        $('my_rating_form').hide();
    },

    showRecipeReviewFormNotLoggedIn: function()
    {
        $('my_rating_form_collapsed_not_logged_in').hide();
        $('my_rating_form_not_logged_in').show();
    },

    hideRecipeReviewFormNotLoggedIn: function()
    {
        $('my_rating_form_collapsed_not_logged_in').show();
        $('my_rating_form_not_logged_in').hide();
    },

    showRecipeReviewTips: function()
    {
        $('recipe_review_tips').show();
        $('recipe_review_show_tips_text').hide();
        $('recipe_review_hide_tips_text').show();
    },

    hideRecipeReviewTips: function()
    {
        $('recipe_review_tips').hide();
        $('recipe_review_show_tips_text').show();
        $('recipe_review_hide_tips_text').hide();
    },

    // Recipe Notes
    recipeNotesUpdateContent: function()
    {
        this.updateContent("recipe_notes.inc.php", 'get', { recipe_id: this.recipeId },
            function(response)
            {
                $('recipe_my_notes_container').innerHTML = response;
            }
        );
    },

    recipeNotesUpdateTab: function()
    {
        this.updateContent("recipe_notes_tab.inc.php", 'get', { recipe_id: SOSCuisine.recipeReview.recipeId },
            function(response)
            {
                $("tabNotesInner_" + SOSCuisine.recipeReview.recipeId).innerHTML = response;
            }
        );
    },

    saveRecipeNotes: function()
    {
        if ($('recipe_note_add_to_favorites') && $('recipe_note_add_to_favorites').checked)
        {
            this.recipeWasAddedToFavorites();
        }

        var parameters = $('my_notes_form').serialize(true);

        this.updateContent("recipe_notes.inc.php?recipe_id=" + this.recipeId, 'post', parameters,
            function(response)
            {
                if (this.mode == "normal")
                {
                    $('recipe_my_notes_container').innerHTML = response;
                    $('recipe_notes_status').innerHTML = $('textNotesSavedChanges').innerHTML;
                    $('recipe_notes_status').fade({ duration: 3.5 });
                    this.recipeNotesUpdateTab();
                }
            } .bind(this)
        );
    },

    enableRecipeNoteButtons: function()
    {
        if ($('recipe_note_save_button'))
        {
            $('recipe_note_save_button').disabled = false;
        }

        if ($('recipe_note_cancel_button'))
        {
            $('recipe_note_cancel_button').disabled = false;
        }
        if ($('recipe_note_delete_button'))
        {
            $('recipe_note_delete_button').disabled = true;
        }
    },

    disableRecipeNoteButtons: function()
    {
        if ($('recipe_note_save_button'))
        {
            $('recipe_note_save_button').disabled = true;
        }
        if ($('recipe_note_cancel_button'))
        {
            $('recipe_note_cancel_button').disabled = true;
        }
        if ($('recipe_note_delete_button'))
        {
            $('recipe_note_delete_button').disabled = false;
        }
    },

    recipeNotesKeyPressed: function(event)
    {
        if (window.event) // IE
        {
            keynum = event.keyCode;
        }
        else if (event.which) // Netscape/Firefox/Opera
        {
            keynum = event.which;
        }

        if (typeof (keynum) != 'undefined')
        {
            this.enableRecipeNoteButtons();
        }
    },

    recipeNotesAddToFavoritesClicked: function()
    {
        this.enableRecipeNoteButtons();
    },

    recipeNotesResetForm: function()
    {
        this.disableRecipeNoteButtons();
    },

    showRecipeNoteTips: function()
    {
        $('recipe_notes_tips').show();
        $('recipe_notes_show_tips_text').hide();
        $('recipe_notes_hide_tips_text').show();
    },

    hideRecipeNoteTips: function()
    {
        $('recipe_notes_tips').hide();
        $('recipe_notes_show_tips_text').show();
        $('recipe_notes_hide_tips_text').hide();
    },

    recipeCommentsUpdateContent: function()
    {
        this.updateContent("recipe_comments.inc.php", 'get', { recipe_id: this.recipeId },
            function(response)
            {
                $('recipe_comments_container').innerHTML = response;
                recipeCommentsUpdateCommentsList(1);
            } .bind(this)
        );
    },


    recipeCommentsUpdateTab: function()
    {
        this.updateContent("recipe_comments_tab.inc.php", 'get', { recipe_id: this.recipeId },
            function(response)
            {
                $("tabCommentsInner_" + this.recipeId).innerHTML = response;
            } .bind(this)
        );
    },

    saveRecipeComment: function()
    {
        var parameters = $('comments_form').serialize(true);

        this.updateContent("recipe_comments.inc.php?recipe_id=" + this.recipeId, 'post', parameters,
            function(response)
            {
                $('recipe_comments_container').innerHTML = response;
                $('recipe_comments_status').innerHTML = $("textCommentsSavedChanges").innerHTML,
                $('recipe_comments_status').fade({ duration: 3.5 });
                this.recipeCommentsUpdateCommentsList(1);
                this.recipeCommentsUpdateTab();
            } .bind(this)
        );
    },

    enableRecipeCommentsButtons: function()
    {
        if ($('recipe_comments_preview_button'))
        {
            $('recipe_comments_preview_button').disabled = false;
        }
    },

    disableRecipeCommentsButtons: function()
    {
        if ($('recipe_comments_preview_button'))
        {
            $('recipe_comments_preview_button').disabled = true;
        }
    },

    recipeCommentsKeyPressed: function(event)
    {
        if ($('recipe_comments_text_area').value.replace(/^\s+|\s+$/g, '').length == 0)
        {
            this.disableRecipeCommentsButtons();
        }
        else
        {
            this.enableRecipeCommentsButtons();
        }
    },

    recipeCommentsResetForm: function()
    {
        this.disableRecipeCommentsButtons();
        this.recipeCommentsHideForm();
    },

    recipeCommentPreviewComment: function()
    {
        $('comments_editing_form_elements').hide();
        $('comments_previewing_form_elements').show();
        $('recipe_comment_preview').innerHTML = $F('recipe_comments_text_area').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\n/g, '<br />')
    },

    recipeCommentEditComment: function()
    {
        $('comments_editing_form_elements').show();
        $('comments_previewing_form_elements').hide();
        $('recipe_comments_text_area').focus();
    },

    recipeCommentsShowForm: function()
    {
        $('comments_form_prompt').hide();
        $('comments_form_container').show();
        $('recipe_comments_text_area').focus();
    },

    recipeCommentsHideForm: function()
    {
        $('comments_form_prompt').show();
        $('comments_form_container').hide();
    },

    recipeCommentsUpdateCommentsList: function(page)
    {

        this.updateContent("recipe_comments_list.inc.php", 'get', { recipe_id: this.recipeId, page: page },
            function(response)
            {
                $('recipe_comments_list_container').innerHTML = response;
            } .bind(this)
        );
    },

    recipeWasAddedToFavorites: function()
    {

        var i;
        var links = document.getElementsByClassName('add_to_favorites_link');
        
        for (i = 0; i < links.length; i++)
        {
            links[i].href = links[i].href.replace("&page_action[add_to_my_cookbook]", '&page_action[remove_from_my_cookbook]');
            if (links[i].getElementsByTagName('img').length == 0)
            {
                links[i].innerHTML = $("text_control_panel_remove_from_favorites").innerHTML;
                links[i].title = $("text_control_panel_remove_from_favorites").innerHTML;
            }
        }

        if ($('recipe_notes_add_to_favorites_checkbox_container') != null)
        {
            $('recipe_notes_add_to_favorites_checkbox_container').hide();
        }

        if ($('recipe_rating_add_to_favorites_checkbox_container') != null)
        {
            $('recipe_rating_add_to_favorites_checkbox_container').hide();
        }

        if ($('recipe_title_favorite_flag'))
        {
            $('recipe_title_favorite_flag').show();
        }
    },

    updateContent: function(url, method, parameters, successCallback)
    {
        new Ajax.Request(SOSCuisine.config.urlRoot + url,
        {
            method: method,
            encoding: 'ISO-8859-1',
            parameters: parameters,
            onSuccess: function(transport)
            {
                var response = transport.responseText || "";
                successCallback(response);
            }
        });
    }
};


SOSCuisine.common.addLoadEvent(function() { SOSCuisine.recipeReview.initializeRecipeView()});
