﻿/// <reference path="~/Scripts/IntelliSense/jquery-1.2.3-intellisense.js" />


(function() {
    BSC.UI.ProfilePaging = function() {
        var localdata = {};
        var OnNextClick = function(p1) {
            var guid = p1;
            var query = localdata[guid].data;

            if (!query.page) query.page = 2;
            else if (query.page && query.page < localdata[guid].pagingData.TotalPages) query.page++;

            localdata[guid].pagingData.PageIndex = query.page - 1;
            localdata[guid].data = query;
            LoadPage(guid);
            UIBinding(guid);
        };
        var OnPrevClick = function(p1) {
            var guid = p1;
            var query = localdata[guid].data;

            if (!query.page) query.page = 1;
            else if (query.page && query.page > 1) query.page--;

            localdata[guid].pagingData.PageIndex = query.page - 1;
            localdata[guid].data = query;
            LoadPage(guid);
            UIBinding(guid);
        };
        var OnDownClick = function(p1, p2) {
            var guid = p1;
            var el = p2;
            var page = (!localdata[guid].data.page) ? 1 : localdata[guid].data.page;

            $("#" + guid + " div.paginationwrap ul li").removeClass("selected");
            $("#" + guid + " div.paginationwrap ul li.idx" + page).addClass("selected");

            if ($(el).hasClass("index1")) {
                $("#" + guid + "_index1view").toggle();
                $("#" + guid + "_index2view").hide();
            }
            else if ($(el).hasClass("index2")) {
                $("#" + guid + "_index2view").toggle();
                $("#" + guid + "_index1view").hide();
            }
        };

        var LoadPage = function(p1) {
            var guid = p1;
            var data = localdata[guid];

            // Scroll to top
            $("#" + guid + "_content").scrollTo({ top: 0 }, 400);

            var query = data.data;

            BSC.UI.ShowStatus(guid);
            BSC.D.AjaxPost(data.uri, query, function(res) {
                // Html
                $("#" + guid + "_result").html(res);
                // Event bind
                BSC.E.MapHrefs();
                BSC.UI.HideStatus(guid, 200);
            }, "html", guid);

            query = BSC.U.CleanQuery(query);
            BSC.UI.Ws.Get(guid).query = BSC.U.QueryToString(query);
            BSC.P.ChangeURI(BSC.UI.Ws.Get(guid).cleanpath + "/?" + BSC.UI.Ws.Get(guid).query);
        };

        var EventBinding = function(p1) {
            var guid = p1;

            $("#" + guid + " div.pagingactions").bind("click", guid, function(e) {
                var guid = e.data;
                var el = BSC.E.GetElement(e, "div", "a");

                if ($(el).hasClass("next") && !$(el).hasClass("inactive"))
                    OnNextClick(guid);
                else if ($(el).hasClass("prev") && !$(el).hasClass("inactive"))
                    OnPrevClick(guid);
                else if ($(el).hasClass("downmiddle") && !$(el).hasClass("inactive"))
                    OnDownClick(guid, el);

                e.preventDefault(); e.stopPropagation();
                return false;
            });

            $("#" + guid + " div.paginationwrap ul").bind("click", guid, function(e) {
                var guid = e.data;
                var el = BSC.E.GetElement(e, "ul", "a");
                var page = parseInt($(el).text());

                // UI
                $("#" + guid + " div.paginationwrap ul li").removeClass("selected");
                $("#" + guid + " div.paginationwrap ul li.idx" + page).addClass("selected");

                localdata[guid].pagingData.PageIndex = page - 1;
                localdata[guid].data.page = page;
                LoadPage(guid);
                UIBinding(guid);

                e.preventDefault(); e.stopPropagation();
                return false;
            });

            $("#" + guid + "_topbar").bind("click", guid, function(e) {
                var guid = e.data;
                var el = BSC.E.GetElement(e, "h2", "a");
                var t = (el.id).split("_");
                var guid = t[0];
                var view = t[1].toLowerCase();
                //debugger;
                var currentView = BSC.D.P.Setting(localdata[guid].key);
                // No change
                if (currentView == view) return;
                // Save new view
                BSC.D.P.Setting(localdata[guid].key, view);

                localdata[guid].data.view = view;

                LoadPage(guid);
                UIBinding(guid);

                e.preventDefault(); e.stopPropagation();
                return false;
            });
        };

        var UIBinding = function(p1) {
            var guid = p1;
            var data = localdata[guid];

            // Next
            if (data.pagingData.PageIndex == (data.pagingData.TotalPages - 1) || data.pagingData.TotalPages == 0)
                $("#" + guid + " div.pagingactions a.next").addClass("inactive");
            else
                $("#" + guid + " div.pagingactions a.next").removeClass("inactive");

            // Prev
            if (data.pagingData.PageIndex == 0 || data.pagingData.TotalPages == 0)
                $("#" + guid + " div.pagingactions a.prev").addClass("inactive");
            else
                $("#" + guid + " div.pagingactions a.prev").removeClass("inactive");

            // Down
            if (data.pagingData.TotalPages == 1 || data.pagingData.TotalPages == 0)
                $("#" + guid + " div.pagingactions a.downmiddle").addClass("inactive");
            else
                $("#" + guid + " div.pagingactions a.downmiddle").removeClass("inactive");

            // UI
            $("#" + guid + " div.paginationwrap ul li").removeClass("selected");
            $("#" + guid + " div.paginationwrap ul li.idx" + data.data.page).addClass("selected");

            // Page Number
            var page = (data.pagingData.TotalPages == 0) ? "0" : (data.pagingData.PageIndex + 1);
            $("span." + guid + "_pageindex").html(page);


            // View tabs
            $("#" + guid + "_topbar a").removeClass("selected");
            $("#" + guid + "_topbar a." + data.data.view).addClass("selected");

        };

        var GenerateIndex = function(p1) {
            var guid = p1;
            var count = localdata[guid].pagingData.TotalPages;
            var currentPage = (localdata[guid].data.page) ? localdata[guid].data.page : 1;
            for (i = 1; i <= count; i++) {
                var selected = (i == currentPage) ? " selected" : "";
                $("#" + guid + "_index1view ul").append("<li class=\"idx" + i + selected + "\"><a>" + i + "</a></li>");
                $("#" + guid + "_index2view ul").append("<li class=\"idx" + i + selected + "\"><a>" + i + "</a></li>");
            }

        };

        return {
            Bind: function(p1, p2, p3, p4, p5, p6) {
                var uri = p1;
                var guid = p2;
                var data = p3;
                var pagingData = p4;
                var key = (p5) ? p5 : uri;
                var options = (p6) ? p6 : {};

                // Set Default view size
                data.view = (data.view) ? data.view : (BSC.D.P.Setting(key)) ? BSC.D.P.Setting(key) : null;
                if (!data.view && options.view) data.view = options.view;



                localdata[guid] = { guid: guid, uri: uri, data: data, pagingData: pagingData, key: key, options: options };

                // Page Count
                $("span." + guid + "_pagecount").html("" + pagingData.TotalPages);

                // Render
                GenerateIndex(guid);

                // UI
                UIBinding(guid);

                // Event binding
                EventBinding(guid);
            },
            Unbind: function(p1) {
                var guid = p1;

                $("#" + guid + " div.pagingactions").unbind();
                $("#" + guid + " div.paginationwrap ul").unbind();
                $("#" + guid + "_topbar").unbind();

                delete localdata[guid];
            }
        };
    } ();
})();




/*
var onChangeViewClick = function(e)
		{
			var el = BSC.E.GetElement(e, "h2", "a");
			var t = (el.id).split("_");
			var guid = t[0];
			var view = t[1].toLowerCase();
			//debugger;
			var currentView = BSC.D.P.Setting("profile.photos.view");
			// No change
			if (currentView == view) return;
			// Save new view
			BSC.D.P.Setting("profile.photos.view", view);

			BSC.UI.Ws.Profile.PhotosList.ReRenderCurrent(guid);
			return;

			// Detail view 
			if (currentView == null || currentView == "detail" || view == "detail")
			{
				BSC.UI.Ws.Profile.PhotosList.ReRenderCurrent(guid);
				return;
			}

			$("h2#" + guid + "_topbar a").removeClass("selected");
			$("#" + guid + "_" + view).addClass("selected");
			$("#" + guid + "_result dl.profilelist").removeClass().addClass("profilelist " + view);
			$("#" + guid + "_result dl.profilelist dd a img").each(function()
			{
				var imagesSize = (view == "detail") ? "mini" : view;
				var src = $(this).attr("src").toLowerCase();
				if (src.indexOf("big") > -1)
					$(this).attr("src", src.replace(/big/ig, imagesSize));
				else if (src.indexOf("small") > -1)
					$(this).attr("src", src.replace(/small/ig, imagesSize));
				else if (src.indexOf("mini") > -1)
					$(this).attr("src", src.replace(/mini/ig, imagesSize));
			});
		};
*/







/// Paging made for custom memberfinder/result top bar.
/// If graphic is not memberfinder top bar new functionality should be added.
/// For Top and buttom paging data needs to be the same.
(function()
{
    BSC.UI.Paging = function()
    {
        var cache = {};

        function GoIndex(guid)
        {
            //BSC.UI.ShowStatus(guid, "Loading...");
            $('#' + guid + '_content').stop().scrollTo({ top: '0' }, 300);
            var data = cache[guid].data;
            var w = BSC.UI.Ws.Get(guid);
            var page = (data.PageIndex + 1);
            var query = w.query + "&" ? w.query : "";
            if (query && query.indexOf(cache[guid].options.pagekey + "=") > -1)
            {
                var rg = new RegExp(cache[guid].options.pagekey + "=(\\d)*", "gi");
                query = query.replace(rg, cache[guid].options.pagekey + "=" + page);
            }
            else
                query = query + "&" + cache[guid].options.pagekey + "=" + page;

            $("." + guid + cache[guid].options.pageindex).html("" + page);
            $("#" + guid + cache[guid].options.indexview + " li.selected").removeClass();
            $("#" + guid + "_" + page).addClass("selected");

            // Buttom paging    
            if (cache[guid].buttom)
            {
                if (query && query.indexOf(cache[guid].buttom.options.pagekey + "=") > -1)
                {
                    var rg = new RegExp(cache[guid].buttom.options.pagekey + "=(\\d)*", "gi");
                    query = query.replace(rg, cache[guid].buttom.options.pagekey + "=" + page);
                }
                else
                    query = query + "&" + cache[guid].buttom.options.pagekey + "=" + page;

                $("." + guid + cache[guid].buttom.options.pageindex).html("" + page);
                $("#" + guid + cache[guid].buttom.options.indexview + " li.selected").removeClass();
                $("#" + guid + "_" + page).addClass("selected");
            }

            BSC.UI.Ws.Get(guid).query = query;
            BSC.P.ChangeURI(w.cleanpath + "/?" + query);
            var cb = cache[guid].loadCallback;
            cb.apply(null, [guid]);
            BindEvents(guid, cache[guid].options);

            // Buttom paging
            if (cache[guid].buttom)
                BindEvents(guid, cache[guid].buttom.options);
            //BSC.UI.HideStatus(guid, 200);
        }

        function BindEvents(guid, options)
        {
            var data = cache[guid].data;
            var page = parseInt(data.PageIndex) + 1;
            if (page == 1)
                $("#" + guid + options.prev).addClass("inactive").unbind("click");
            else
                $("#" + guid + options.prev).removeClass("inactive").bind("click", { guid: guid, data: data }, BSC.UI.Paging.OnPreviousPage);

            if (page >= parseInt(data.TotalPages))
                $("#" + guid + options.next).addClass("inactive").unbind("click");
            else
                $("#" + guid + options.next).removeClass("inactive").bind("click", { guid: guid, data: data }, BSC.UI.Paging.OnNextPage);

        }

        return {
            /// <summary></summary>
            /// <param name="guid">Window ID to attach paging on.</param>
            /// <param name="data">Page numbers. (Index, totalcount aso.)</param>
            /// <param name="loadCallback">Callback function to call.</param>
            /// <param name="options">Pass which option buttons you want to bind on. Pass the end of the id. FX: id="guid + option"</param>
            /// <return></return>
            Add: function(guid, data, loadCallback, options, buttompaging)
            {
                // Paging: IsNextPage, IsPreviousPage, PageIndex, PageSize, TotalCount, TotalPage
                if (!options) options = {};
                if (!options.pagekey) options.pagekey = "page";
                if (!options.next) options.next = "_next";
                if (!options.prev) options.prev = "_prev";
                if (!options.down) options.down = "_down";
                if (!options.pageindex) options.pageindex = "_pageindex";
                if (!options.pagecount) options.pagecount = "_pagecount";
                if (!options.indexview) options.indexview = "_indexview";

                if (buttompaging)
                    cache[guid].buttom = { data: data, loadCallback: loadCallback, options: options };
                else
                    cache[guid] = { data: data, loadCallback: loadCallback, options: options };

                var page = parseInt(data.PageIndex) + 1;
                $("." + guid + options.pageindex).html("" + page);
                $("." + guid + options.pagecount).html("" + data.TotalPages);

                BindEvents(guid, options);

                if (data.TotalPages > 1)
                {
                    $("#" + guid + options.down).removeClass("inactive").bind("click", { guid: guid, data: data }, BSC.UI.Paging.OnPageIndex);
                    // Render PageIndex

                    $("#" + guid + options.indexview + " ul li").remove();
                    var ul = $("#" + guid + options.indexview + " ul");
                    html = "";
                    for (i = 1; i <= data.TotalPages; i++)
                    {
                        var selected = (i == page) ? " class=\"selected\"" : "";
                        html = html + "<li id=\"" + guid + "_" + i + "\"" + selected + "><a>" + i + "</a></li>";
                    }
                    ul.append(html).bind("click", { guid: guid }, BSC.UI.Paging.OnPageIndexClick);
                }
                else
                    $("#" + guid + options.down).addClass("inactive").unbind("click");

            },

            Remove: function(guid)
            {
                $("#" + guid + cache[guid].options.prev).unbind();
                $("#" + guid + cache[guid].options.down).unbind();
                $("#" + guid + cache[guid].options.next).unbind();
                $("#" + guid + cache[guid].options.indexview + " ul").unbind();

                // Buttom paging
                if (cache[guid].buttom)
                {
                    $("#" + guid + cache[guid].buttom.options.prev).unbind();
                    $("#" + guid + cache[guid].buttom.options.down).unbind();
                    $("#" + guid + cache[guid].buttom.options.next).unbind();
                    $("#" + guid + cache[guid].buttom.options.indexview + " ul").unbind();
                }
            },

            OnNextPage: function(e)
            {
                var guid = e.data.guid;
                var index = cache[guid].data.PageIndex;

                // Buttom paging
                if (cache[guid].buttom)
                {
                    cache[guid].buttom.data.PageIndex = Math.max(Math.min(parseInt(index) + 1, parseInt(cache[guid].buttom.data.TotalPages)), 0);
                    if (isNaN(cache[guid].buttom.data.PageIndex)) cache[guid].buttom.data.PageIndex = 0;
                }
                cache[guid].data.PageIndex = Math.max(Math.min(parseInt(index) + 1, parseInt(cache[guid].data.TotalPages)), 0);
                if (isNaN(cache[guid].data.PageIndex)) cache[guid].data.PageIndex = 0;
                GoIndex(guid);
            },

            OnPreviousPage: function(e)
            {
                var guid = e.data.guid;
                var index = cache[guid].data.PageIndex;

                // Buttom paging
                if (cache[guid].buttom)
                {
                    cache[guid].buttom.data.PageIndex = Math.max(Math.min(parseInt(index) - 1, parseInt(cache[guid].buttom.data.TotalPages)), 0);
                    if (isNaN(cache[guid].buttom.data.PageIndex)) cache[guid].buttom.data.PageIndex = 0;
                }
                cache[guid].data.PageIndex = Math.max(Math.min(parseInt(index) - 1, parseInt(cache[guid].data.TotalPages)), 0);
                if (isNaN(cache[guid].data.PageIndex)) cache[guid].data.PageIndex = 0;
                GoIndex(guid);
            },

            OnPageIndex: function(e)
            {
                var el = BSC.E.GetElement(e, "div", "a");
                var guid = e.data.guid;

                // Buttom paging
                var buttomindex = el.id.match('buttom');
                var indexlist;
                if (buttomindex)
                    indexlist = $("#" + guid + cache[guid].buttom.options.indexview);
                else
                    indexlist = $("#" + guid + cache[guid].options.indexview);

                if (indexlist.css("display") == "block")
                    indexlist.css({ display: 'none' });
                else
                    indexlist.css({ position: 'absolute', display: 'block' });
            },

            OnPageIndexClick: function(e)
            {
                var guid = e.data.guid;
                var w = BSC.UI.Ws.Get(guid);
                var el = BSC.E.GetElement(e, "ul", "li");
                var index = ((el.id).split("_"))[1];

                // Buttom paging
                if (cache[guid.buttom])
                {
                    cache[guid].buttom.data.PageIndex = Math.max(Math.min(parseInt(index - 1), parseInt(cache[guid].buttom.data.TotalPages)), 0);
                    if (isNaN(cache[guid].buttom.data.PageIndex)) cache[guid].buttom.data.PageIndex = 0;
                }
                cache[guid].data.PageIndex = Math.max(Math.min(parseInt(index - 1), parseInt(cache[guid].data.TotalPages)), 0);
                if (isNaN(cache[guid].data.PageIndex)) cache[guid].data.PageIndex = 0;
                GoIndex(guid);
                $("#" + guid + cache[guid].options.indexview).hide();
                // Buttom paging
                $("#" + guid + cache[guid].buttom.options.indexview).hide();
            },

            RenderIndex: function(guid)
            {
                var data = cache[guid].data;

            }
        };
    } ();
})();
