﻿Type.registerNamespace('CorvusCMS.UI');

CorvusCMS.UI.Gallery = function(element) {

    CorvusCMS.UI.Gallery.initializeBase(this, [element]);

    this._gallery = null;
    this._galleryId = null;
    this._itemWidth = 327;
    this._itemHeight = 224;
    this._thumbSizeId = 99;
    this._itemRepeater = null;
    this._itemsPerPage = null;
    this._itemTemplate = null;
    this._separatorTemplate = null;
    this._footerTemplate = null;
    this._headerTemplate = null;
    this._pagerTemplate = null;
    this._renderTopPager = false;
    this._renderBottomPager = true;
    this._extenders = null;
    this._id = null;
    this._containerId = null;
}

CorvusCMS.UI.Gallery.prototype = {

    initialize: function() {
        CorvusCMS.UI.Gallery.callBaseMethod(this, 'initialize');

        if (!this._gallery && !this._galleryId)
            throw new Error("Gallery or galleryId is not set.");

        if (!this._gallery && this._galleryId) {
            this.set_gallery(new $Gallery(this._galleryId));
            this.get_gallery().SetData(Function.createDelegate(this, gallerySet), Function.createDelegate(this, this._error));
        }
        else {
            this.set_gallery(cfw_cast(this._gallery));
        }

        if (this._gallery && !this._galleryId)
            this.set_galleryId(this._gallery().Id);



        this._repeater = $create(CorvusCMS.UI.Repeater,
            {   
                "footerTemplate": this._footerTemplate,
                "headerTemplate": this._headerTemplate,
                "itemTemplate": this._itemTemplate,
                "pagerTemplate": this._pagerTemplate,
                "separatorTemplate": this._separatorTemplate,
                "id": this.get_element().id + "_rptItems",
                "dataSourceHandler": Function.createDelegate(this, this._rptPageChanged),
                "itemDataBoundHandler": Function.createDelegate(this, this._rptDataBoundHandler),
                "ItemsPerPage": this.get_itemsPerPage(),
                "renderBottomPager": this.get_renderBottomPager(),
                "customCommandHandler": Function.createDelegate(this, this._rptCommandHandler),
                "renderTopPager": this.get_renderTopPager()
            }, null, null, this.get_element());

        this._repeater.onFirstPage();
    },

    dispose: function() {

        CorvusCMS.UI.Gallery.callBaseMethod(this, 'dispose');


    },


    get_id: function() {
        return this._id;
    },

    set_id: function(value) {
        if (this._id !== value) {
            this._id = value;
        }
    },

    get_gallery: function() {
        return this._gallery;
    },

    set_gallery: function(value) {
        if (this._gallery !== value) {
            this._gallery = value;
        }
    },

    get_galleryId: function() {
        return this._galleryId;
    },

    set_galleryId: function(value) {
        if (this._galleryId !== value) {
            this._galleryId = value;
        }
    },

    get_containerId: function() {
        return this._containerId;
    },

    set_containerId: function(value) {
        if (this._containerId !== value) {
            this._containerId = value;
        }
    },

    set_item: function(value) {
        if (this._item !== value) {
            this._item = value;
        }
    },

    get_itemType: function() {
        return this._itemType;
    },

    set_itemType: function(value) {
        if (this._itemType !== value) {
            this._itemType = value;
        }
    },

    get_itemWidth: function() {
        return this._itemWidth;
    },

    set_itemWidth: function(value) {
        if (this._itemWidth !== value) {
            this._itemWidth = value;
        }
    },

    get_itemHeight: function() {
        return this._itemHeight;
    },

    set_itemHeight: function(value) {
        if (this._itemHeight !== value) {
            this._itemHeight = value;
        }
    },

    get_itemsPerPage: function() {
        return this._itemsPerPage;
    },

    set_itemsPerPage: function(value) {
        if (this._itemsPerPage !== value) {
            this._itemsPerPage = value;
        }
    },

    get_itemTemplate: function() {
        return this._itemTemplate;
    },

    set_itemTemplate: function(value) {
        if (this._itemTemplate !== value) {
            this._itemTemplate = value;
        }
    },

    get_footerTemplate: function() {
        return this._footerTemplate;
    },

    set_footerTemplate: function(value) {
        if (this._footerTemplate !== value) {
            this._footerTemplate = value;
        }
    },

    get_separatorTemplate: function() {
        return this._separatorTemplate;
    },

    set_separatorTemplate: function(value) {
        if (this._separatorTemplate !== value) {
            this._separatorTemplate = value;
        }
    },

    get_headerTemplate: function() {
        return this._headerTemplate;
    },

    set_headerTemplate: function(value) {
        if (this._headerTemplate !== value) {
            this._headerTemplate = value;
        }
    },

    get_pagerTemplate: function() {
        return this._pagerTemplate;
    },

    set_pagerTemplate: function(value) {
        if (this._pagerTemplate !== value) {
            this._pagerTemplate = value;
        }
    },

    get_renderTopPager: function() {
        return this._renderTopPager;
    },

    set_renderTopPager: function(value) {
        if (this._renderTopPager !== value) {
            this._renderTopPager = value;
        }
    },

    get_renderBottomPager: function() {
        return this._renderBottomPager;
    },

    set_renderBottomPager: function(value) {
        if (this._renderBottomPager !== value) {
            this._renderBottomPager = value;
        }
    },

    get_extenders: function() {
        return this._extenders;
    },

    set_extenders: function(value) {
        if (this._extenders !== value) {
            this._extenders = value;
        }
    },

    _rptDataBoundHandler: function(sender, args) {
     if (args.get_itemType() == CorvusCMS.UI.TemplateType.ItemTemplate) {
        var cont = args.findControl(this.get_containerId());
        cont.setAttribute("cName", "galleryItem");
        if(this.get_itemsPerPage() > 0)
            cont.setAttribute("cArg", (this._repeater.get_currentPage()-1)* this.get_itemsPerPage() + args.get_index());
        else
            cont.setAttribute("cArg", args.get_index());
        cont.style.cursor = "pointer";
        }
    },

    _rptPageChanged: function(sender, args) {
        if(args.get_itemsPerPage() > 0)
            args.set_webRequestManager(this.get_gallery().GetItems([args.get_itemsPerPage(), args.get_newPage()], args.succededFunction, args.failFunction, null, this.get_extenders()));
        else
            args.set_webRequestManager(this.get_gallery().GetItems([], args.succededFunction, args.failFunction, null, this.get_extenders()));
    },

    _rptCommandHandler: function(sender, args) {
        if (args.get_commandName() == "galleryItem")
            CorvusCMS.UI.GalleryModal.showModal(this.get_gallery().Id, parseInt(args.get_commandArgument()));
    },

    _error: function(err) {
        alert(err.Message);
    },

    gallerySet: function(gallery) {
        this.set_gallery(gallery);
    }


}


CorvusCMS.UI.Gallery.registerClass("CorvusCMS.UI.Gallery", Sys.UI.Control, Sys.IDisposable);



if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();