﻿/// <reference path="../ActivityDataAccess.js" />
/// <reference path="../AgilityData.js" />


Agility.RegisterNamespace("Subaru.Editorial");

(function(Editorial) {

    var EditorialDialog = null;
    var EditorialTitleLabel = null;
    var RecentPostsLabel = null;
    var PostedLabel = null;
    var CurrentEditorialID = null;
    var CurrentEditorial = null;
    var Editorials = null;


    Editorial.OnInit = function(PanelID, titleLabel, recentPostLabel, postedLabel) {
        EditorialDialog = $("#" + PanelID).dialog({
            autoOpen: false,
            modal: true,
            closeText: 'hide',
            dialogClass: 'CreateEditorialDialog',
            title: "",
            resizable: false
        });

        EditorialTitleLabel = titleLabel;
        RecentPostsLabel = recentPostLabel;
        PostedLabel = postedLabel;
    };

    Editorial.LoadEditorial = function(EditorialID) {

        Subaru.TemplateUtils.CloseAllDialogs();
        if (EditorialDialog != null) {
            CurrentEditorialID = EditorialID;

            if (Editorials == null) {
                Subaru.AgilityData.GetEditorials(Editorial.LoadEditorialsCallBack);

            }
            else {
                //data already loaded
                Editorial.LoadEditorialsCallBack(Editorials)
            }
            EditorialDialog.dialog("open");
        }
    };

    Editorial.LoadEditorialsCallBack = function(data) {

        Editorials = data;

        for (var i = 0; i < Editorials.length; i++) {
            if (Editorials[i].Date == null) {
                Editorials[i].Date = "";
            }
        }

        var panel = $("#EditorialDialogContentPanel");
        var templateUrl = Agility.ResolveUrl("~/ClientTemplates/EditorialPanel.htm");
        panel.setTemplateURL(templateUrl);
        panel.setParam("ModuleTitle", EditorialTitleLabel);
        panel.setParam("RecentPosts", RecentPostsLabel);
        panel.setParam("PostedLabel", PostedLabel);
        panel.processTemplate(data);

        if (CurrentEditorialID > 0) {
            Editorial.SetCurrentEditorial(CurrentEditorialID);
        }
        else {
            if (Editorials.length > 0) {
                Editorial.SetCurrentEditorial(Editorials[0].EditorialID);
            }
        }
    };

    Editorial.SetMainImage = function(imageUrl, caption) {
        if (imageUrl != null) {
            imageUrl = Subaru.TemplateUtils.S3ThumbURL(imageUrl, 371, 273);

            $("#EditorialMainImage").html("<img src='" + imageUrl + "'/>");
            $("#EditorialCaption").html(caption);
        }
        else {
            $("#EditorialMainImage").html("");
            $("#EditorialCaption").html("");
        }
    };

    Editorial.NextPage = function() {
        if (CurrentEditorial != null) {
            if (CurrentEditorial.Pages[CurrentEditorial.CurrentPage + 1] != null) {

                $("#EditorialPageBody").html(Encoder.htmlDecode(CurrentEditorial.Pages[CurrentEditorial.CurrentPage + 1]));
                CurrentEditorial.CurrentPage++;
                $("#EditorialPageBodyPagerContent").html((CurrentEditorial.CurrentPage + 1) + " of " + CurrentEditorial.Pages.length);
            }
        }
    };

    Editorial.PreviousPage = function() {
        if (CurrentEditorial != null) {
            if (CurrentEditorial.Pages[CurrentEditorial.CurrentPage - 1] != null) {

                $("#EditorialPageBody").html(Encoder.htmlDecode(CurrentEditorial.Pages[CurrentEditorial.CurrentPage - 1]));
                CurrentEditorial.CurrentPage--;

                $("#EditorialPageBodyPagerContent").html((CurrentEditorial.CurrentPage + 1) + " of " + CurrentEditorial.Pages.length);
            }
        }
    };

    Editorial.SetCurrentEditorial = function(EditorialID) {

        CurrentEditorialID = EditorialID;
        CurrentEditorial = null;
        for (var i = 0; i < Editorials.length; i++) {
            if (Editorials[i].EditorialID == CurrentEditorialID) {
                CurrentEditorial = Editorials[i];
            }
        }

        if (CurrentEditorial != null) {

            $("#LoadedEditorialTitle").html(CurrentEditorial.Title);
            $("#LoadedEditorialDate").html(CurrentEditorial.Date);
            $("#EditorialPageBody").html(CurrentEditorial.Pages[0]);

            CurrentEditorial.CurrentPage = 0;

            $("#EditorialPageBodyPagerContent").html((CurrentEditorial.CurrentPage + 1) + " of " + CurrentEditorial.Pages.length);

            if (CurrentEditorial.Images.length > 0) {

                Editorial.SetMainImage(CurrentEditorial.Images[0].Url, CurrentEditorial.Images[0].Caption);
            }
            else {
                Editorial.SetMainImage(null, null);
            }

            var thumbnailHTML = "";

            for (var i = 0; i < CurrentEditorial.Images.length; i++) {
                var url = Subaru.TemplateUtils.S3ThumbURL(CurrentEditorial.Images[i].Url, 71, 53);

                thumbnailHTML += "<img class='Thumb' src='" + url + "' onClick='Subaru.Editorial.SetMainImage(\"" + CurrentEditorial.Images[i].Url + "\", \"" + CurrentEditorial.Images[i].Caption + "\")'>";
            }

            if (thumbnailHTML != "") {
                $("#EditorialThumbnails").html(thumbnailHTML);
            }
            else {
                $("#EditorialThumbnails").html("");
            }
        }
    };

})(Subaru.Editorial);