﻿
var albumSelf = new Album();

function Album() {

    this.pageSizeMain = 20;
    this.pageSizeMember = 30;
    this.pageSizeView = 20;
    this.pageSizeEdit = 50;
    this.data = null;
    this.albumType = 1;
    this.pageIndex = 1;
    this.albumID = -1;
}

Album.prototype.GetDatas = function(url) {
    var data;
    $.ajax({ type: "POST", url: url, async: false, dataType: "json", timeout: 5000, success: function(json2) { data = json2; } });
    return data;
}

Album.prototype.PostDatas = function(url, datas) {
    var returnValue;
    $.ajax({
        type: "POST",
        url: url,
        data: datas,
        async: false,
        dataType: "json",
        timeout: 5000,
        success: function(json2) {
            returnValue = json2;
        }
    });
    return returnValue;
}

//Album/Main.aspx
Album.prototype.initPageAlbumList = function(totalCount) {

    var pageSize = albumSelf.pageSizeMain;
    var currentPage = albumSelf.pageIndex;
    var fuctionName = "albumSelf.ChangeAlbumMainListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;
    if (totalCount < 1) {
        $("#NoRecord").show();
        $(".pagination").hide();
        $("#albumMainList").hide();
    } else {
        $("#NoRecord").hide();
        $(".pagination").show();
        $("#albumMainList").show();
        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        $("div.pagealbum_pages").html("");
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

Album.prototype.ChangeAlbumMainListPage = function(currentPage) {

    var pageSize = albumSelf.pageSizeMain;
    albumSelf.BindAlbumMainData(currentPage, pageSize, 'albumMainList', 'templateAlbumMainList', 'loading_cir');
}

Album.prototype.BindAlbumMainData = function(currentPage, pageSize, ContentId, tempName, loadingCss) {

    albumSelf.pageIndex = currentPage;
    if (loadingCss != "") {
        $("span." + loadingCss).show();
    }
    var dataUrl = JJYRemoteUrl.AlbumMain + "?act=GetAlbumMainList&albumType=" + albumSelf.albumType;
    template = $("#" + tempName).html();

    albumSelf.data = getData(dataUrl, currentPage, pageSize);
    if (albumSelf.data == null)
        return;

    var rowi = 0;
    $.fn.bindTo.ItemDataBound = function(template, data) {
        if (rowi % 2 == 0)
            template = template.replace('[alt]', 'altbg');
        else
            template = template.replace('[alt]', '');
        rowi++;
        return template;
    }

    $("#" + ContentId).html("");
    if (loadingCss != "") { $("span." + loadingCss).hide(); }
    
    $("#" + ContentId).append($(template).bindTo(albumSelf.data));
    
    var totalCount = albumSelf.data.Total;
    var fuctionName = "albumSelf.ChangeAlbumMainListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;

    if (totalCount < 1) {
        $("#NoRecord").show();
        $(".pagination").hide();
        $("#albumMainList").hide();
    } else {
        $("#NoRecord").hide();
        $(".pagination").show();
        $("#albumMainList").show();
        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        $("div.pagealbum_pages").html("");
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

////Album/MyAlbums.aspx
////Album/MemberAlbum.aspx
Album.prototype.initPageMemberAlbumList = function(totalCount) {

    var pageSize = albumSelf.pageSizeMember;
    var currentPage = albumSelf.pageIndex;
    var fuctionName = "albumSelf.ChangeAlbumMemberListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;

    if (totalCount < 1) {
        $("#p_NoData").show();
        $("#albumMemberList").hide();
        $(".pagination").hide();
    }
    else {
        $("#p_NoData").hide();
        $("#albumMemberList").show();
        $(".pagination").show();

        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

Album.prototype.ChangeAlbumMemberListPage = function(currentPage) {

    var pageSize = albumSelf.pageSizeMember;
    albumSelf.BindAlbumMemberData(currentPage, pageSize, 'albumMemberList', 'templateAlbumMemberList', 'loading_cir');
}

Album.prototype.BindAlbumMemberData = function(currentPage, pageSize, ContentId, tempName, loadingCss) {

    albumSelf.pageIndex = currentPage;
    if (loadingCss != "") {
        $("span." + loadingCss).show();
    }

    var memberID = QueryString("mid");
    var strArg = "";
    if (memberID != null && memberID != "") {
        strArg = "&aoid=" + memberID;
    }

    var dataUrl = JJYRemoteUrl.AlbumMember + "?act=GetAlbumMemberList&albumType=" + albumSelf.albumType + strArg;
    template = $("#" + tempName).html();

    albumSelf.data = getData(dataUrl, currentPage, pageSize);
    
    if (albumSelf.data == null) {
        $("#p_NoData").show();
        $("#albumMemberList").hide();
        $(".pagination").hide();
        return;
    }
    var rowi = 0;
    $.fn.bindTo.ItemDataBound = function(template, data) {
        if (rowi % 2 == 0)
            template = template.replace('[alt]', '');
        else
            template = template.replace('[alt]', 'altbg');
        rowi++;
        return template;
    }

    $("#" + ContentId).html("");
    if (loadingCss != "") { $("span." + loadingCss).hide(); }
    $("#" + ContentId).append($(template).bindTo(albumSelf.data));

    var totalCount = albumSelf.data.Total;
    var fuctionName = "albumSelf.ChangeAlbumMemberListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;

    if (totalCount < 1) {
        $("#p_NoData").show();
        $("#albumMemberList").hide();
        $(".pagination").hide();
    }
    else {
        $("#p_NoData").hide();
        $("#albumMemberList").show();
        $(".pagination").show();

        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

Album.prototype.DeleteAlbum = function(albumID) {

    var dataUrl = JJYRemoteUrl.AlbumMember + "?act=DeleteAlbum&aid=" + albumID;
    AjaxPost(null, dataUrl, null, "json", false);
}

////Album/View.aspx
Album.prototype.initPageAlbumViewList = function(totalCount) {

    var pageSize = albumSelf.pageSizeView;
    var currentPage = albumSelf.pageIndex;
    var fuctionName = "albumSelf.ChangeAlbumViewListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;

    if (totalCount < 1) {
        $(".pagination").hide();
        $("#P_NoData").hide();
        $("#albumPhotoList").show();
    }
    else {
        $(".pagination").show();
        $("#P_NoData").show();
        $("#albumPhotoList").hide();

        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

Album.prototype.ChangeAlbumViewListPage = function(currentPage) {

    var pageSize = albumSelf.pageSizeView;
    albumSelf.BindAlbumViewData(currentPage, pageSize, 'albumPhotoList', 'templateAlbumPhotoList', 'loading_cir');
}

Album.prototype.BindAlbumViewData = function(currentPage, pageSize, ContentId, tempName, loadingCss) {

    albumSelf.pageIndex = currentPage;
    if (loadingCss != "") {
        $("span." + loadingCss).show();
    }

    var dataUrl = JJYRemoteUrl.AlbumView + "?act=GetAlbumViewList&aid=" + albumSelf.albumID;
    template = $("#" + tempName).html();

    albumSelf.data = getData(dataUrl, currentPage, pageSize);
    if (albumSelf.data == null) return;

    $("#" + ContentId).html("");
    if (loadingCss != "") { $("span." + loadingCss).hide(); }
    $("#" + ContentId).append($(template).bindTo(albumSelf.data));

    var totalCount = albumSelf.data.Total;
    var fuctionName = "albumSelf.ChangeAlbumViewListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;

    if (totalCount < 1) {
        $(".pagination").hide();
        $("#P_NoData").show();
        $("#albumPhotoList").hide();
    }
    else {
        $(".pagination").show();
        $("#P_NoData").hide();
        $("#albumPhotoList").show();

        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

////Album/Photo.aspx
Album.prototype.GetPhotoInfo = function(albumID, photoID) {
    var postUrl = JJYRemoteUrl.AlbumPhotoDetail + "?act=GetPhotoInfo&aid=" + albumID + "&pid=" + photoID;
    return albumSelf.GetDatas(postUrl);
}

Album.prototype.DelPhoto = function(photoID, albumID, albumOwner) {
    var postUrl = JJYRemoteUrl.AlbumPhotoDetail + "?act=DelPhoto&pid=" + photoID + "&aid=" + albumID + "&aoid=" + albumOwner;
    return albumSelf.GetDatas(postUrl);
}

Album.prototype.UpdatePhoto = function(objDiv, photoID, tagNoHTML, albumOwner) {
    var postUrl = JJYRemoteUrl.AlbumPhotoDetail + "?act=UpdatePhoto&pid=" + photoID + "&otag=" + escape(tagNoHTML) + "&aoid=" + albumOwner;
    postUrl = encodeURI(postUrl);
    AjaxPost(null, postUrl, objDiv, "json", false);
}

Album.prototype.Rating = function(currentTotalRating, currentVoteCount, photoID, ratingCount) {
    var postUrl = JJYRemoteUrl.AlbumPhotoDetail + "?act=Rating&pid=" + photoID + "&cr=" + currentTotalRating + "&vc=" + currentVoteCount + "&c=" + ratingCount;
    return AjaxPost(null, postUrl, null, "text", false);
}

////Album/Add.aspx
Album.prototype.CreateAlbum = function(objDiv, AlbumOwerID, AlbumAccessID) {
    var postUrl = JJYRemoteUrl.AlbumAdd + "?act=CreateAlbum&aoid=" + AlbumOwerID + "&lvl=" + AlbumAccessID;
    postUrl = encodeURI(postUrl);
    return AjaxPost(null, postUrl, objDiv, "json", false);
}

Album.prototype.UpdateAlbum = function(objDiv, AlbumID, AlbumAccessID) {
    var postUrl = JJYRemoteUrl.AlbumAdd + "?act=UpdateAlbum&aid=" + AlbumID + "&lvl=" + AlbumAccessID;
    postUrl = encodeURI(postUrl);
    return AjaxPost(null, postUrl, objDiv, "json", false);
}

////Album/Edit.aspx
Album.prototype.initPageAlbumPhotoEditList = function(totalCount) {

    var pageSize = albumSelf.pageSizeEdit;
    var currentPage = albumSelf.pageIndex;
    var fuctionName = "albumSelf.ChangeAlbumPhotoEditListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;

    if (totalCount < 1) {
        $("#photolist").hide();
        $(".selectall").hide();
        $(".pagination_sub").hide();
    }
    else {
        $("#photolist").show();
        $(".selectall").show();
        $(".pagination_sub").show();
        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

Album.prototype.ChangeAlbumPhotoEditListPage = function(currentPage) {

    var pageSize = albumSelf.pageSizeEdit;
    albumSelf.BindAlbumPhotoEditData(currentPage, pageSize, 'photolist', 'templateAlbumPhotoEditList', 'loading_cir');
}

Album.prototype.BindAlbumPhotoEditData = function(currentPage, pageSize, ContentId, tempName, loadingCss) {

    albumSelf.pageIndex = currentPage;
    if (loadingCss != "") {
        $("span." + loadingCss).show();
    }
    var albumID = QueryString("aid");
    var dataUrl = JJYRemoteUrl.AlbumPhotoEdit + "?act=GetAlbumPhotoEditList&aid=" + albumID;
    template = $("#" + tempName).html();
    albumSelf.data = getData(dataUrl, currentPage, pageSize);
    if (albumSelf.data == null) return;

    $("#" + ContentId).html("");
    if (loadingCss != "") { $("span." + loadingCss).hide(); }
    $("#" + ContentId).append($(template).bindTo(albumSelf.data));

    var totalCount = albumSelf.data.Total;
    var fuctionName = "albumSelf.ChangeAlbumPhotoEditListPage";

    var endSize = currentPage * pageSize;
    if (endSize > totalCount) { endSize = totalCount; }
    var infoSize = (currentPage - 1) * pageSize + 1 + "-" + endSize;

    if (totalCount < 1) {
        $("#photolist").hide();
        $(".selectall").hide();
        $(".pagination_sub").hide();
    }
    else {
        $("#photolist").show();
        $(".selectall").show();
        $(".pagination_sub").show();
        $("div.pagealbum_info").html(pageInfo.replace("$info1$", infoSize).replace("$info2$", totalCount));
        if (totalCount > pageSize) {
            $("div.pagealbum_pages").html(pageTxt + Paging(currentPage, pageSize, totalCount, skipCount, fuctionName, currentStyleName, currentUseLink, prevText, nextText, firstText, lastText));
        }
    }
}

Album.prototype.DeletePhotos = function(photoIDs) {
    var postUrl = JJYRemoteUrl.AlbumPhotoEdit + "?act=DeletePhotos&pids=" + photoIDs;
    albumSelf.GetDatas(postUrl);
    albumSelf.ChangeAlbumPhotoEditListPage(albumSelf.pageIndex);
}

Album.prototype.SavePhotosEdit = function(photoIDs, oldTags, tags, descs) {
    var datas = "pids=" + photoIDs.join("|") + "&ot=" + oldTags.join("|") + "&t=" + tags.join("|") + "&d=" + descs.join("|");
    var postUrl = JJYRemoteUrl.AlbumPhotoEdit + "?act=SavePhotosEdit&" + datas;
    postUrl = encodeURI(postUrl);
    albumSelf.GetDatas(postUrl);
}