if(typeof Btv == "undefined") Btv={};
if(typeof Btv.Photos == "undefined") Btv.Photos={};
Btv.Photos.DataQueries_class = function() {};
ajaxProPrototypes.Object.extend(Btv.Photos.DataQueries_class.prototype, ajaxProPrototypes.Object.extend(new AjaxPro.AjaxClass(), {
	GetAutoAlbum: function(albumID) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.GetAutoAlbum.arguments)[1]) || (null);
		return this.invoke(false, v, "POST", "GetAutoAlbum", {"albumID":albumID}, ajaxProPrototypes.Function.argumentsToArray(this.GetAutoAlbum.arguments).slice(2));
	},
	SetAutoAlbum: function(albumID, defaultOrdinal, curationIds, objectIds) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.SetAutoAlbum.arguments)[4]) || (null);
		return this.invoke(false, v, "POST", "SetAutoAlbum", {"albumID":albumID, "defaultOrdinal":defaultOrdinal, "curationIds":curationIds, "objectIds":objectIds}, ajaxProPrototypes.Function.argumentsToArray(this.SetAutoAlbum.arguments).slice(5));
	},
	GetChildrenAlbumInfo: function(affiliateID, parentAlbumID, initial, categoryChildren) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.GetChildrenAlbumInfo.arguments)[4]) || (null);
		return this.invoke(false, v, "POST", "GetChildrenAlbumInfo", {"affiliateID":affiliateID, "parentAlbumID":parentAlbumID, "initial":initial, "categoryChildren":categoryChildren}, ajaxProPrototypes.Function.argumentsToArray(this.GetChildrenAlbumInfo.arguments).slice(5));
	},
	GetBatches: function(affiliateID) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.GetBatches.arguments)[1]) || (null);
		return this.invoke(false, v, "POST", "GetBatches", {"affiliateID":affiliateID}, ajaxProPrototypes.Function.argumentsToArray(this.GetBatches.arguments).slice(2));
	},
	UpdateAlbum: function(albumInfo) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.UpdateAlbum.arguments)[1]) || (null);
		return this.invoke(false, v, "POST", "UpdateAlbum", {"albumInfo":albumInfo}, ajaxProPrototypes.Function.argumentsToArray(this.UpdateAlbum.arguments).slice(2));
	},
	UpdateAlbumImageOrdinal: function(affiliateID, albumID, imageIds, ordinalValues) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.UpdateAlbumImageOrdinal.arguments)[4]) || (null);
		return this.invoke(false, v, "POST", "UpdateAlbumImageOrdinal", {"affiliateID":affiliateID, "albumID":albumID, "imageIds":imageIds, "ordinalValues":ordinalValues}, ajaxProPrototypes.Function.argumentsToArray(this.UpdateAlbumImageOrdinal.arguments).slice(5));
	},
	UpdateCategoryAlbumOrdinal: function(affiliateID, categoryID, albumIds, ordinalValues) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.UpdateCategoryAlbumOrdinal.arguments)[4]) || (null);
		return this.invoke(false, v, "POST", "UpdateCategoryAlbumOrdinal", {"affiliateID":affiliateID, "categoryID":categoryID, "albumIds":albumIds, "ordinalValues":ordinalValues}, ajaxProPrototypes.Function.argumentsToArray(this.UpdateCategoryAlbumOrdinal.arguments).slice(5));
	},
	UpdateMetaData: function(imageID, metaData) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.UpdateMetaData.arguments)[2]) || (null);
		return this.invoke(false, v, "POST", "UpdateMetaData", {"imageID":imageID, "metaData":metaData}, ajaxProPrototypes.Function.argumentsToArray(this.UpdateMetaData.arguments).slice(3));
	},
	RemoveAlbum: function(albumId, deleteFromSystem) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.RemoveAlbum.arguments)[2]) || (null);
		return this.invoke(false, v, "POST", "RemoveAlbum", {"albumId":albumId, "deleteFromSystem":deleteFromSystem}, ajaxProPrototypes.Function.argumentsToArray(this.RemoveAlbum.arguments).slice(3));
	},
	CopyAlbums: function(targetAlbumId, sourceTargetIds, removeFromParentIds) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.CopyAlbums.arguments)[3]) || (null);
		return this.invoke(false, v, "POST", "CopyAlbums", {"targetAlbumId":targetAlbumId, "sourceTargetIds":sourceTargetIds, "removeFromParentIds":removeFromParentIds}, ajaxProPrototypes.Function.argumentsToArray(this.CopyAlbums.arguments).slice(4));
	},
	RemoveChildAlbums: function(childIds, parentAlbumId) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.RemoveChildAlbums.arguments)[2]) || (null);
		return this.invoke(false, v, "POST", "RemoveChildAlbums", {"childIds":childIds, "parentAlbumId":parentAlbumId}, ajaxProPrototypes.Function.argumentsToArray(this.RemoveChildAlbums.arguments).slice(3));
	},
	CreateChildAlbum: function(parentAlbumId, title, description, isCategory) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.CreateChildAlbum.arguments)[4]) || (null);
		return this.invoke(false, v, "POST", "CreateChildAlbum", {"parentAlbumId":parentAlbumId, "title":title, "description":description, "isCategory":isCategory}, ajaxProPrototypes.Function.argumentsToArray(this.CreateChildAlbum.arguments).slice(5));
	},
	RemoveImageFromAlbum: function(albumId, imageIds, deleteFromSystem) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.RemoveImageFromAlbum.arguments)[3]) || (null);
		return this.invoke(false, v, "POST", "RemoveImageFromAlbum", {"albumId":albumId, "imageIds":imageIds, "deleteFromSystem":deleteFromSystem}, ajaxProPrototypes.Function.argumentsToArray(this.RemoveImageFromAlbum.arguments).slice(4));
	},
	AddImagesToAlbum: function(albumId, imageIds) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.AddImagesToAlbum.arguments)[2]) || (null);
		return this.invoke(false, v, "POST", "AddImagesToAlbum", {"albumId":albumId, "imageIds":imageIds}, ajaxProPrototypes.Function.argumentsToArray(this.AddImagesToAlbum.arguments).slice(3));
	},
	SaveImportBatch: function(importBatchName) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.SaveImportBatch.arguments)[1]) || (null);
		return this.invoke(false, v, "POST", "SaveImportBatch", {"importBatchName":importBatchName}, ajaxProPrototypes.Function.argumentsToArray(this.SaveImportBatch.arguments).slice(2));
	},
	RatePhoto: function(photoID, photoRating) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.RatePhoto.arguments)[2]) || (null);
		return this.invoke(false, v, "POST", "RatePhoto", {"photoID":photoID, "photoRating":photoRating}, ajaxProPrototypes.Function.argumentsToArray(this.RatePhoto.arguments).slice(3));
	},
	FlagPhoto: function(contentScopeId, photoID) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.FlagPhoto.arguments)[2]) || (null);
		return this.invoke(false, v, "POST", "FlagPhoto", {"contentScopeId":contentScopeId, "photoID":photoID}, ajaxProPrototypes.Function.argumentsToArray(this.FlagPhoto.arguments).slice(3));
	},
	GetPhotoGalleryContent: function(context, query, range) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.GetPhotoGalleryContent.arguments)[3]) || (null);
		return this.invoke(false, v, "POST", "GetPhotoGalleryContent", {"context":context, "query":query, "range":range}, ajaxProPrototypes.Function.argumentsToArray(this.GetPhotoGalleryContent.arguments).slice(4));
	},
	GetCategoryPage: function(affiliateId, albumId, contentScopeId, contentId, categoryFirstIndex, categoryLastIndex, albumFirstIndex, albumLastIndex, breadcrumbIds, albumName) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.GetCategoryPage.arguments)[10]) || (null);
		return this.invoke(false, v, "POST", "GetCategoryPage", {"affiliateId":affiliateId, "albumId":albumId, "contentScopeId":contentScopeId, "contentId":contentId, "categoryFirstIndex":categoryFirstIndex, "categoryLastIndex":categoryLastIndex, "albumFirstIndex":albumFirstIndex, "albumLastIndex":albumLastIndex, "breadcrumbIds":breadcrumbIds, "albumName":albumName}, ajaxProPrototypes.Function.argumentsToArray(this.GetCategoryPage.arguments).slice(11));
	},
	GetCategories: function(affiliateId, albumId, contentScopeId, contentId, firstIndex, lastIndex, isCategory) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.GetCategories.arguments)[7]) || (null);
		return this.invoke(false, v, "POST", "GetCategories", {"affiliateId":affiliateId, "albumId":albumId, "contentScopeId":contentScopeId, "contentId":contentId, "firstIndex":firstIndex, "lastIndex":lastIndex, "isCategory":isCategory}, ajaxProPrototypes.Function.argumentsToArray(this.GetCategories.arguments).slice(8));
	},
	AdvancedImageQuery: function(context, query, range) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.AdvancedImageQuery.arguments)[3]) || (null);
		return this.invoke(false, v, "POST", "AdvancedImageQuery", {"context":context, "query":query, "range":range}, ajaxProPrototypes.Function.argumentsToArray(this.AdvancedImageQuery.arguments).slice(4));
	},
	GetCategory: function(affiliateId, albumId) {
		var v = (ajaxProPrototypes.Function.argumentsToArray(this.GetCategory.arguments)[2]) || (null);
		return this.invoke(false, v, "POST", "GetCategory", {"affiliateId":affiliateId, "albumId":albumId}, ajaxProPrototypes.Function.argumentsToArray(this.GetCategory.arguments).slice(3));
	},
	url: '/ajaxpro/Btv.Photos.DataQueries,App_Code.ashx?typeVersion=11.01'
}));
Btv.Photos.DataQueries = new Btv.Photos.DataQueries_class();
Btv.Photos.DataQueries._Include = new (function(){
this.RawProcessor = function(oRawResponse)
{
    function ProcessMetaDataTable(ht, oTbl, oTblOverlay)
    {
        for (var i = 0; i < oTbl.Rows.length; i++)
        {
            var oRow = oTbl.Rows[i];
            var iScopeID = oRow.ScopeId;
            var o = {
                ID: oRow.ContentId
                , ScopeID: iScopeID
                , Title: oRow.Label
                , Img: Btv.UI.FixImage(oRow.Img)
                , Url: oRow.Url
            };

            if (!ht[iScopeID]) ht[iScopeID] = {};
            ht[iScopeID][o.ID] = o;
        }

        for (var i = 0; i < oTblOverlay.Rows.length; i++)
        {
            var oRow = oTblOverlay.Rows[i];
            var iScope = oRow.ScopeID;
            var o = {
                ID: oRow.ID
                , Term: oRow.Label
                , Url: oRow.Url
                , PhotoCount: oRow.PhotoCount
                , Rank: oRow.Rank
                , ImageSrc: oRow.Image
                , RelatedID: oRow.RelatedParentID
                , MaxCount: -1
                , Scope: ((iScope == 21) ? 0 : 1)
            };
            if (ht[iScope] && ht[iScope][oRow.ID]) ht[iScope][oRow.ID].OverlayData = o;
        };
    }


    function AssignImageTitle(oImg)
    {
        oImg.Title = "";
        var oMeta = oImg.MetaData;
        for (var i = 0; i < oMeta.length; i++)
        {
            var iScopeID = oMeta[i].ScopeID;
            oImg.Title = oMeta[i].Title;
            if (iScopeID == 21) return;
        }
    }


    function AddToImage(iTblNum, fnc)
    {
        for (var i = 0; i < aTables[iTblNum].Rows.length; i++)
        {
            var oRow = aTables[iTblNum].Rows[i];
            fnc(oRow, htImages[oRow.ImageId + ''])
        }
    }


    var aTables = oRawResponse.Data.Tables;
    var htImages = {};

    var oAlbum = null;
    if (aTables[13].Rows.length)
    {
        var oRow = aTables[13].Rows[0];
        oAlbum = {
            Id: oRow.AlbumId
            , AffiliateId: oRow.AffiliateId
            , Title: oRow.Title
            , Description: oRow.Description
            , DateCreated: oRow.DateCreated
            , DateUpdated: oRow.DateUpdated
            , CoverImageId: oRow.CoverImageId
        };
    }


    oRawResponse.Context.TotalCount = aTables[0].Rows[0].TotalCount;
    oRawResponse.Context.FirstIndex = aTables[0].Rows[0].FirstIndex;
    oRawResponse.Context.Album = oAlbum;
    oRawResponse.Query.AlbumId = aTables[0].Rows[0].AlbumId;





    oRawResponse.Data = {
        FirstIndex: aTables[0].Rows[0].FirstIndex
            , Items: []
    }

    // process the image ids table
    for (var i = 0; i < aTables[1].Rows.length; i++)
    {
        var oImg = {
            ID: aTables[1].Rows[i].ImageId
                , MetaData: []
                , Views: []
                , Ratings: {
                    RatingCount: 0
                    , RatingAvg: -1
                    , RatingUser: -1
                }
                , CommentCount: 0
                , OrdinalIndex: aTables[1].Rows[i].OrdinalIndex
        };
        oRawResponse.Data.Items.push(oImg);
        htImages[oImg.ID + ''] = oImg;
    }


    // process the metadata
    var htMetaData = {};
    ProcessMetaDataTable(htMetaData, aTables[2], aTables[10]);
    ProcessMetaDataTable(htMetaData, aTables[3], aTables[11]);


    AddToImage(4, function(oRow, oImg)
    {
        var oMeta = htMetaData[oRow.ContentScopeId][oRow.ContentId];
        if (oMeta) oImg.MetaData.push(htMetaData[oRow.ContentScopeId][oRow.ContentId]);
    });


    AddToImage(5, function(oRow, oImg)
    {
        oImg.Views.push({
            Src: Btv.UI.FixImage(oRow.Src)
            , Width: oRow.Width
            , Height: oRow.Height
        });
    });


    // process the TotalRatings table
    AddToImage(6, function(oRow, oImg)
    {
        oImg.Ratings.RatingCount = oRow.Total;
        oImg.Ratings.RatingAvg = oRow.Average;
    });


    // process the UserRatings table
    AddToImage(7, function(oRow, oImg)
    {
        oImg.Ratings.RatingUser = oRow.User;
    });


    // process the CommentCount table
    AddToImage(8, function(oRow, oImg)
    {
        oImg.CommentCount = oRow.CommentCount;
    });


    // process the ImageProperties table
    AddToImage(9, function(oRow, oImg)
    {
        oImg.Credit = oRow.Credit;
        oImg.Caption = oRow.Caption;
        oImg.TakenDate = oRow.TakenDate;
        oImg.DateGranularity = oRow.DateGranularity;
    });


    var aItems = oRawResponse.Data.Items;
    for (var i = 0; i < aItems.length; i++)
    {
        AssignImageTitle(aItems[i]);
    }

    return (oRawResponse);
}



this.AdminRawProcessor = function(oRawResponse)
{
    function AddToImage(iTblNum, fnc)
    {
        for (var i = 0; i < aTables[iTblNum].Rows.length; i++)
        {
            var oRow = aTables[iTblNum].Rows[i];
            fnc(oRow, htImages[oRow.ImageId+''])
        }
    }

    var aTables = oRawResponse.Data.Tables;
    var htImages = {};
    oRawResponse.Context.TotalCount = aTables[0].Rows[0].TotalCount;
    oRawResponse.Context.FirstIndex = aTables[0].Rows[0].FirstIndex;



    oRawResponse.Data = {
        FirstIndex: aTables[0].Rows[0].FirstIndex
            , Items: []
    }

    // process the image ids table
    for (var i = 0; i < aTables[1].Rows.length; i++)
    {
        var oImg = {
            ID: aTables[1].Rows[i].ImageId
            , Views: []
            , InAlbum: !!aTables[1].Rows[i].AlbumId
        };
        oRawResponse.Data.Items.push(oImg);
        htImages[oImg.ID] = oImg;
    }


    AddToImage(2, function(oRow, oImg)
    {
        oImg.Views.push({
            Src: Btv.UI.FixImage(oRow.Src)
            , Width: oRow.Width
            , Height: oRow.Height
        });
    });

    return (oRawResponse);
}

})();
