﻿/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />

(function()
{
    BSC.UI.Ws.Picture = function()
    {
        var pictureTimer = null;
        var localcache = {};


        /// Private Start
        var ChangePicture = function(p1, p2)
        {

            var guid = p1;
            var page = p2;

            // Check if profile has accees
            if (!BSC.D.P.IsLoggedIn() || (!BSC.D.P.IsFullMember() && BSC.D.P.IsPaying()))
            {
                BSC.UI.AlertLogin();
                return;
            }
            else if (BSC.D.P.IsFullMember() && !BSC.D.P.IsPaying())
            {
                BSC.UI.AlertCurrentlyFREE();
                return;
            }

            //LoadComments
            $("#" + guid + "_commentlist").html("");
            $("#" + guid + "_content").scrollTo({ top: 0 }, 300);


            if (!localcache[guid].Pictures) return;
            localcache[guid].Current = localcache[guid].Pictures[page - 1];
            localcache[guid].Page = page;

            // Change Pic
            $("#" + guid + "_pageindex").html("" + localcache[guid].Page);
            $("#" + guid + "_pictureid").attr("src", BSC.PicturePath(localcache[guid].Current.ID, "full"));
            //            $("#" + guid + "_picturename").html("" + localcache[guid].Current.ProfileName);
            //            $("#" + guid + "_picturetext").html("" + localcache[guid].Current.Description);
            //            
            $("#" + guid + "_votes").html("" + localcache[guid].Current.VoteCount);
            $("#" + guid + "_value").val("" + localcache[guid].Current.Rank);

            // Change Uri
            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query);
            jQ.id = localcache[guid].Current.ID;
            var query = BSC.U.QueryToString(jQ);
            BSC.UI.Ws.Get(guid).query = query;
            BSC.P.ChangeURI(w.cleanpath + "/?" + query);

            LoadComments(guid, localcache[guid].Current.ID);

            if (localcache[guid].Current.ProfileID == BSC.D.P.GetData("ID") || localcache[guid].Current.HasRated)
            {
                // Own pic, set new value
                BSC.UI.Ranking.SetValue(guid, localcache[guid].Current.Rank);
                BSC.UI.Ranking.Remove(guid);
            }
            else
            {
                BSC.UI.Ranking.Add(guid, BSC.UI.Ws.Picture.SetRanking, false, localcache[guid].Current);
            }

            //BSC.UI.Ranking.Add(guid, BSC.UI.Ws.Picture.SetRanking, false, localcache[guid].Current.Rank);
        }

        var LoadComments = function(p1, p2)
        {


            var guid = p1;
            var pictureID = p2;
            var data = {
                id: pictureID
            };
            //$.get("/Ajax/Picture/CommentsList", { __guid: guid, id: pictureID },
            BSC.D.AjaxGet("/Picture/CommentsList", data, function(res)
            {
                $("#" + guid + "_commentlist").html(res);
                BSC.E.MapHrefs();

                var json = $("#" + guid + "_commentJson").getValue();
                var data = BSC.D.JSON.Parse(json);
                if (data)
                {
                    options_top = {};
                    if (!options_top.pagekey) options_top.pagekey = "commentpage";
                    if (!options_top.next) options_top.next = "_next_top";
                    if (!options_top.prev) options_top.prev = "_prev_top";
                    if (!options_top.down) options_top.down = "_down_top";
                    if (!options_top.indexview) options_top.indexview = "_indexview_top";

                    options_buttom = {};
                    if (!options_buttom.pagekey) options_buttom.pagekey = "page";
                    if (!options_buttom.next) options_buttom.next = "_next_buttom";
                    if (!options_buttom.prev) options_buttom.prev = "_prev_buttom";
                    if (!options_buttom.down) options_buttom.down = "_down_buttom";
                    if (!options_buttom.indexview) options_buttom.indexview = "_indexview_buttom";

                    // Paging: IsNextPage, IsPreviousPage, PageIndex, PageSize, TotalCount, TotalPage
                    BSC.UI.Paging.Add(guid, data, BSC.UI.Ws.Picture.ReRenderCurrent, options_top);
                    BSC.UI.Paging.Add(guid, data, BSC.UI.Ws.Picture.ReRenderCurrent, options_buttom, true);
                }
                BSC.UI.HideStatus(guid, 200);
            }, "html", guid);
        };

        var rerenderCurrent = function(p1)
        {
            var guid = p1;
            BSC.UI.ShowStatus(guid, "Loading...");
            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query);
            if (!jQ) jQ = {};
            // jQ.view = (jQ.view) ? jQ.view : (BSC.D.P.Setting("memberfinder.result.view")) ? BSC.D.P.Setting("memberfinder.result.view") : "big";
            var query = jQ ? "&" + BSC.U.QueryToString(jQ) : "";

            $("h2#" + guid + "_topbar a").removeClass("selected");
            $("#" + guid + "_" + jQ.view).addClass("selected");

            var d1 = new Date();

            BSC.D.AjaxGet("/Picture/CommentsList", { __guid: guid + query }, function(res)
            {
                // Html
                $("#" + guid + "_commentlist").html(res);
                BSC.E.MapHrefs();
                $("#" + guid + " a.delete_comment").unbind().click(BSC.UI.Comment.Delete);
                BSC.UI.HideStatus(guid, 200);
            }, "html", guid);
        };

        var AddComment = function(p1, p2, p3)
        {
            var guid = p1;
            var pictureID = p2;
            var comment = p3;
            var data = {
                PictureID: pictureID,
                Comment: comment
            };

            // uir med paging med
            BSC.D.AjaxPost("/Picture/CommentAdd", data, function(res)
            {
                //  if (res.success)
                //			       BSC.UI.Ws.Picture.LoadComments(guid, pictureID);
                $("#" + guid + "_commentlist").html(res);
                var json = $("#" + guid + "_commentJson").getValue();
                var data = BSC.D.JSON.Parse(json);
                if (data)
                {
                    options_top = {};
                    if (!options_top.pagekey) options_top.pagekey = "commentpage";
                    if (!options_top.next) options_top.next = "_next_top";
                    if (!options_top.prev) options_top.prev = "_prev_top";
                    if (!options_top.down) options_top.down = "_down_top";

                    options_buttom = {};
                    if (!options_buttom.pagekey) options_buttom.pagekey = "commentpage";
                    if (!options_buttom.next) options_buttom.next = "_next_buttom";
                    if (!options_buttom.prev) options_buttom.prev = "_prev_buttom";
                    if (!options_buttom.down) options_buttom.down = "_down_buttom";
                    //$("#" + guid + "_resultheader").html(data.Header);
                    // Paging: IsNextPage, IsPreviousPage, PageIndex, PageSize, TotalCount, TotalPage
                    BSC.UI.Paging.Add(guid, data, BSC.UI.Ws.Picture.ReRenderCurrent, options_top);
                    BSC.UI.Paging.Add(guid, data, BSC.UI.Ws.Picture.ReRenderCurrent, options_buttom);
                }
                BSC.UI.HideStatus(guid, 200);
            }, "html", guid);
        };

        /// Private End

        var load = function(p1)
        {
            var guid = p1;
            localcache[guid] = { Current: null, Pictures: [], Page: 1, Count: 1 };
            var json = $("#" + guid + "_json").getValue();
            var data = BSC.D.JSON.Parse(json);
            if (data)
            {
                localcache[guid].Photoalbums = data.Photoalbums;
                localcache[guid].AlbumID = data.AlbumID;
                localcache[guid].Pictures = data.Pictures;
                localcache[guid].Page = data.Page;
                localcache[guid].Count = data.Count;
                localcache[guid].Current = data.Pictures[Math.min(Math.max(localcache[guid].Page - 1, 0), data.Count - 1)];
                BSC.UI.Ranking.Add(guid, BSC.UI.Ws.Picture.SetRanking, false, localcache[guid].Current);
            }

            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query);
            var id = (jQ) ? jQ.id : null;
            if (id)
                LoadComments(guid, id);
        };

        var bind = function(p1)
        {
            var guid = p1;
            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query); //
            var id = (jQ.id) ? jQ.id : null;

            if (jQ.login && !BSC.D.P.IsLoggedIn())
                BSC.UI.AlertLogin();

            $("#" + guid + "_addcommentlink").bind("click", guid, function(e)
            {
                var guid = e.data;
                $('#' + guid + '_content').scrollTo("#" + guid + "_bottom", 200);
            });

            BSC.UI.Comment.Bind(guid, id, "Picture");

            // On Topbar click
            $("#" + guid + "_topbar").bind("click", guid, function(e)
            {
                $("#" + guid + "_pagingactions").css("display", "");
                $("#" + guid + "_next").css("display", "");
                $("#" + guid + "_prev").css("display", "");
                $("#" + guid + "_status").css("display", "none");
                var el = BSC.E.GetElement(e, "div", "a");
                if (!el.id) return;
                var t = (el.id).split("_");
                var guid = e.data;
                var action = t[1];

                if (action == "next")
                {
                    var p = localcache[guid].Page + 1;
                    if (p > localcache[guid].Count)
                    {
                        p = 1;
                        for (i = 0; i < localcache[guid].Photoalbums.length; i++)
                        {
                            if (localcache[guid].Photoalbums[i].ID == localcache[guid].AlbumID)
                            {

                                if (localcache[guid].Photoalbums[i + 1] != null)
                                    var temp = localcache[guid].Photoalbums[i + 1];
                                else
                                    var temp = localcache[guid].Photoalbums[0];

                                $("#" + guid + "_pagingactions").css("display", "none");
                                $("#" + guid + "_next").css("display", "none");
                                $("#" + guid + "_prev").css("display", "none");
                                $("#" + guid + "_status").css("display", "");

                                GO("/picture/?id=" + temp.PictureID);
                                return;
                            }
                        }

                    }
                    else
                    {
                        ChangePicture(guid, p);
                    }
                }
                else if (action == "prev")
                {
                    var p = localcache[guid].Page - 1;
                    if (p < 1) p = localcache[guid].Count;
                    ChangePicture(guid, p);
                }
                else if (action == "down")
                {
                }
                else if (action == "detail")
                    $("#" + guid + "_photoalbums").toggle();

                return false;
            });

            // On Picture Click
            $("#" + guid + "_pictureid").bind("click", guid, function(e)
            {
                var p = localcache[guid].Page + 1;
                if (p > localcache[guid].Count)
                {
                    p = 1;
                    for (i = 0; i < localcache[guid].Photoalbums.length; i++)
                    {
                        if (localcache[guid].Photoalbums[i].ID == localcache[guid].AlbumID)
                        {

                            if (localcache[guid].Photoalbums[i + 1] != null)
                                var temp = localcache[guid].Photoalbums[i + 1];
                            else
                                var temp = localcache[guid].Photoalbums[0];

                            $("#" + guid + "_pagingactions").css("display", "none");
                            $("#" + guid + "_next").css("display", "none");
                            $("#" + guid + "_prev").css("display", "none");
                            $("#" + guid + "_status").css("display", "");

                            GO("/picture/?id=" + temp.PictureID);
                            return;
                        }
                    }
                }
                else
                {
                    ChangePicture(guid, p);
                }
            });
            if (!BSC.D.P.IsLoggedIn())
            {
                $("#" + guid + "_commenttext").bind("focus", guid, function(e)
                {
                    $("#" + guid + "_commenttext").trigger("blur");
                    BSC.UI.AlertLogin();
                });
            }

            // Add Comment
            $("#" + guid + "_commentadd").bind("click", guid, function(e)
            {
                var guid = e.data;
                var w = BSC.UI.Ws.Get(guid);
                var jQ = BSC.U.ParseQuery(w.query, { id: '' });
                var id = (jQ) ? jQ.id : null;
                if (!id) return;

                var comment = $("#" + guid + "_commenttext").getValue();
                AddComment(guid, id, comment);
                $("#" + guid + "_commenttext").val("");
            });

            // Bookmark
            $("#" + guid + "_bookmark").bind("mousedown", guid,
	        function(e)
	        {
	            var guid = e.data;
	            var w = BSC.UI.Ws.Get(guid);
	            var jQ = BSC.U.ParseQuery(w.query, { id: '' });
	            var id = (jQ) ? jQ.id : null;

	            BSC.UI.Bookmark.Add({ id: id, type: "picture", title: null, uri: null, callback: BSC.UI.Bookmark.Confirm, data: { guid: guid, h: BSC.T.Get("JS.BSC.UI.Ws.Picture.Bookmark")} });
	            e.preventDefault(); e.stopPropagation(); return;
	        });
        };


        var unbind = function(p1)
        {
            var guid = p1;
            delete localcache[guid];
            BSC.UI.Ranking.Remove(guid);
            $("#" + guid + "_topbar").unbind();
            $("#" + guid + "_picturepic").unbind();
            $("#" + guid + "_commentadd").unbind();
            $("#" + guid + "_bookmark").unbind();
            $("#" + guid + "_commenttext").unbind();
            $("#" + guid + " a.delete_comment").unbind();
            $("#" + guid + "_photoalbums ul").unbind();
        };

        var setRanking = function(p1, p2)
        {
            var guid = p1;
            var value = p2;
            if (!BSC.D.P.IsLoggedIn())
            {
                var w = BSC.UI.Ws.Get(guid);
                BSC.UI.AlertLogin(w.path);
                return false;
            }
            var w = BSC.UI.Ws.Get(guid);
            var jQ = BSC.U.ParseQuery(w.query); //
            var id = (jQ) ? jQ.id : null;

            if (!id) return;

            BSC.D.AjaxPost("/picture/rankingadd", { id: id, value: value }, function(r)
            {
                localcache[guid].Current.HasRated = true;
                localcache[guid].Current.Rank = r.Score;
                localcache[guid].Pictures[Math.min(Math.max(localcache[guid].Page - 1, 0), localcache[guid].Count - 1)].HasRated = true;
                localcache[guid].Pictures[Math.min(Math.max(localcache[guid].Page - 1, 0), localcache[guid].Count - 1)].Rank = r.Score;
                
                BSC.UI.Ranking.SetValue(guid, r.Score);
                BSC.UI.Ranking.Remove(guid);
            }, "json", guid);
            return;
        };


        return {
            Load: load,
            Bind: bind,
            Unbind: unbind,
            SetRanking: setRanking,
            ReRenderCurrent: rerenderCurrent
        };
    } ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Picture.Load, null, "picture");
BSC.E.Subscribe("bind", BSC.UI.Ws.Picture.Bind, null, "picture");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Picture.Unbind, null, "picture");
