﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../../jquery/jquery-1.3.2-vsdoc.js" />
/// <reference path="jExtend.js" />
/// <reference path="../../../Services/Comments.asmx" />

/// TODO: laterr add the ability to start preloading the next page
/// Also later add the ability to have the comments boxes in their own java class so we can add on other abilities

Type.registerNamespace("AMS");
Type.registerNamespace("AMS.Site");
AMS.Site.Guestbook = function () {
}

AMS.Site.Guestbook.prototype = {
    commentEl: {
        el: ""
        , siteID: ""
        , auto: 1
        , memberID: ""
        , dir: 0 /* 0=oldest to newest, 1=newest to oldest */
        , total: 0
        , numb: 0
        , navLoc: 0 /* 0=top, 1=bottom, 2=both */
        , pc: "" /* number of page to build based on the number of comments per page and total number of comments */
        , cssClass: ""
        , dID: "AMS.Site.Comment"
        , tmpDiv: "#member-comment-template"
        , imgDID: "AMS.Site.Comment.Img"
        , showImage: 0
    }
    , img: {
        lID: ""
    }
    /*auto -- autoapproved - true is default */
    , Init: function (gbEl, siteID, auto, dir /* this is the direcection to show the comments */,
        numb /* number of comments to get at a time */, total /* total number of comments*/, pc, navLoc, memberID, showImage /*true=show member image in comment post*/) {
        /*  call the web service
        build comment based on returned data and the comment template
        need a service to get total number of comments
        */
        /// storing information into GB
        this.commentEl.el = gbEl;
        this.commentEl.siteID = siteID;
        this.commentEl.auto = auto;
        this.commentEl.memberID = memberID;
        this.commentEl.dir = dir;
        this.commentEl.numb = numb;
        this.commentEl.total = total;
        this.commentEl.pc = pc;
        this.commentEl.navLoc = navLoc;
        this.commentEl.showImage = showImage;

        // hide nav bars till we know we need them
        $("#enterComment").hide();
        $(this.commentEl.el).children("#gb-bottom-nav").hide();
        $(this.commentEl.el).children("#gb-header").children("#gb-top-nav").hide();
        this.BuildNav();
        this.LoadCMTs(1);

    }
    , LoadCMTs: function (pagNum) {
        for (var i = 1; i <= this.commentEl.pc; i++) {
            $(".btn" + i).removeClass("highlight");
        }
        $(".btn" + pagNum).addClass("highlight");
        //        var _t = $("#gb-nav-id-" + pagNum);
        //        $("#gb-nav-id-" + pagNum).addClass("highlight");
        var _postLink = $(".gb-post-comment");
        var _closeLink = $(".span-comment-close");
        _closeLink.click(function (e) {
            $("#enterComment").hide();
        });
        _postLink.click(function (e) {
            //var d = $(this).data("AMS.Site.Comment.Mousebuttons");
            //var _this = $(this);
            $("#enterComment").show();


        });

        _postLink.mouseover(function (e) {
            var _this = $(this);
            _this.removeClass("gb-post-comment");
            _this.addClass("gb-post-comment-hover");


        });

        _postLink.mouseout(function (e) {

            var _this = $(this);
            _this.removeClass("gb-post-comment-hover");
            _this.addClass("gb-post-comment");

        });
        AMS.Web.Services.comments.GetSiteComments(this.commentEl.siteID, this.commentEl.numb, pagNum, this.commentEl.dir,
        function (rtn, uc, mn) {

            try {
                var cList = rtn; /* stores list of comments captured from web service */

                var tmp = $(guestBookManager.commentEl.tmpDiv);
                var gb = $(guestBookManager.commentEl.el);
                gb.children(".lister").children().remove();
                gb.children("#loader").hide();

                for (var ind in cList) {
                    var cm = cList[ind];
                    var elm = tmp.clone();
                    var _imgL = elm.children(".member-image").children("#img-loader"); // image loader info
                    _imgL.attr("id", _imgL.attr("id") + "-" + cm.CommentID);


                    if (guestBookManager.commentEl.showImage) {
                        var _img = $("<img id=\"c-img-" + cm.CommentID + "\" src=\"" + cm.AvatarPath + "\" alt=\"" + cm.DisplayName + "\" />").appendTo(elm.children(".member-image")); // elm.children(".member-image").append("<img id=\"c-img-" + cm.CommentID + "\" src=\"" + cm.AvatarPath + "\" alt=\"" + cm.DisplayName + "\" />").hide();
                    } else {
                        var _img = $("<div></div>");
                    }
                    _img.hide();
                    // adds needed image data to the image
                    var _imgD = $.extend({}, guestBookManager.img, { lID: "#" + _imgL.attr("id") });
                    _img.data(guestBookManager.commentEl.imgDID, _imgD);




                    elm.attr("id", "commment-" + cm.CommentID);
                    // var t = elm.children(".comment-container").children(".header").children("span:first");
                    var hd = elm.children(".comment-container").children(".header")
                    hd.children(".displayname").html(cm.DisplayName);
                    hd.children(".date").html(cm.Date);
                    elm.children(".comment-container").children(".comment").html(cm.Comment);

                    gb.children(".lister").append(elm);

                    _img.onImagesLoaded(
                        function (_img) {
                            var d = $(_img).data("AMS.Site.Comment.Img");
                            $(d.lID).remove();
                            $(_img).fadeIn(500);
                        }
                    );

                }
                // add clear div
                gb.children(".lister").append("<div class=\"clear\"></div>");

            }
            catch (ex) {
                // alert(ex);
                PageManager.RecordError("AMS.Site.GuestBook.js - line 137 sender", "", null);
                return;
            }

        }, function (err, uc, mn) {
            var _t = err;
        });

    }
    , BuildNav: function () {
        /// build nav bar
        var nav = $("<div></div>");
        var c = 0;
        while (c < this.commentEl.pc) {
            c += 1;

            var _nitem = $("<div class=\"gb-nav-item btn" + c + "\">" + c + "</div>").appendTo(nav);

            _nitem.attr("d", c);

            _nitem.click(function (e) {
                //var d = $(this).data("AMS.Site.Comment.Mousebuttons");
                var _this = $(this);

                guestBookManager.LoadCMTs(_this.attr("d"));
                var _d = parseInt(_this.attr("d"));

            });
            _nitem.mouseenter(function (e) {

                // just toggle mouseover
                var _this = $(this);
                var _d = parseInt(_this.attr("d"));
                var g = _d;
                _this.removeClass("gb-nav-item");
                _this.addClass("gb-nav-item-hover");


            });
            _nitem.mouseleave(function (e) {
                // just toggle mouseover
                var _this = $(this);
                _this.removeClass("gb-nav-item-hover");
                _this.addClass("gb-nav-item");
            });
        }

        nav.append("<div class=\"clear\"></div>");
        $(this.commentEl.el).children("#gb-header").children("#gb-top-nav").append(nav);
        $(this.commentEl.el).children("#gb-bottom-nav").append(nav.clone(true));

        $(this.commentEl.el).children("#gb-header").children("#gb-top-nav").append("<div class=\"clear\"></div>");
        $(this.commentEl.el).children("#gb-bottom-nav").append("<div class=\"clear\"></div>");


        if (this.commentEl.navLoc == 2) {
            // show to both
            $(this.commentEl.el).children("#gb-bottom-nav").show();
            $(this.commentEl.el).children("#gb-header").children("#gb-top-nav").show();
        }
        else {
            if (this.commentEl.navLoc == 0) { $(this.commentEl.el).children("#gb-header").children("#gb-top-nav").show(); }
            else if (this.commentEl.navLoc == 1) { $(this.commentEl.el).children("#gb-bottom-nav").show(); }
        }
    }
}

AMS.Site.Guestbook.registerClass("AMS.Site.Guestbook");
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();