﻿App.module("article", function (exports, require) {

    var App = require("app");
    var Utils = require("utils");

    App.addActions({
        RateArticle: RateArticle,
        postComment: CommentAction,
        play: PlayAction
    });

    function PlayAction($el) {
        var preaction = $el.attr('data-pre-action') || "",
            overlaywidth = $el.attr("data-overlay-width") || "",
            overlayheight = $el.attr("data-overlay-height") || "",
            overlaytype = $el.attr("data-overlay-type") || "iframe";

        if (preaction && !App.isLoggedIn()) {
            App.showOverlay('renderview/preplay',
                    'ajax',
                    'preplay',
                    '',
                    '',
                    function () {
                        App.showOverlay($el.attr("data-href"), overlaytype, 'playaction', overlaywidth, overlayheight);
                    }
                );

            App.trackGooglePageView("/preplay");
        } else {
            App.showOverlay($el.attr("data-href"), overlaytype, 'playaction', overlaywidth, overlayheight);
        }

        return true;
    }

    function RateArticle($el) {
        if (!App.isLoggedIn()) {
            App.showOverlay("renderView/preplay", "ajax", "hide-play preplay");
            App.trackGooglePageView("/preplay");
            return;
        }

        $(".rating .loader").toggle();

        var rating = 1, rating_el = $(".rating");
        if ($el.attr('data-rating') == 'negative')
            rating = -1;

        App.post("AddRating", { itemId: $(".articledetail").attr("data-game-id"), rating: rating }, function (error, item) {
            $(".rating .loader").toggle();
            if (error) {
                rating_el.append("<div class='status'>" + error + "</div>");
                App.displayInfo(error);
            } else {
                App.displayInfo("Your rating has been submitted");
                rating_el.fadeOut(function () {
                    var pos = $('<div />').addClass('positive green');
                    neg = $('<div />').addClass('negative rosy');

                    rating_el
                        .html('')
                        .append('<div>rating:&nbsp;</div>')
                        .append(pos)
                        .append(neg)
                        .fadeIn('fase', function () {
                            pos.animate({ width: item.PositivePerc }, 'slow','swing', function () {
                                pos.text(item.PositivePerc+"%").attr('title',item.PositiveRatings + " thumbs up");
                            });
                            neg.animate({ width: item.NegativePerc }, 'slow','swing', function () {
                                neg.text(item.NegativePerc + "%").attr('title', item.NegativeRatings + " thumbs down");
                            });
                        });
                });
            }

        });

        var event = $el.attr("data-tracking-event") || "";
        App.trackGoogleEvent(event);

        rating_el.find("a").remove();
        rating_el.find("li").prepend("<div class='disabled' title='Thank you, new rating % will reflect shortly.'></div>");
        return false;
    }

    function CommentAction($el) {

        Utils.formPost("AddComment", $el, function (response, form) {
            App.showOverlay(response.url, "ajax", "comment");
        });
    }
});
