﻿Type.registerNamespace('CorvusCMS.UI');

CorvusCMS.UI.GalleryModal = function(element) {

    CorvusCMS.UI.GalleryModal.initializeBase(this, [element]);

    this._gallery = null;
    this._galleryId = null;
    this._itemWidth = 327;
    this._itemHeight = 224;
    this._itemRepeater = null;
    this._itemTemplate = null;
    this._footerTemplate = null;
    this._headerTemplate = null;
    this._pagerTemplate = null;
    this._renderTopPager = false;
    this._renderBottomPager = true;
    this._extenders = null;
    this._id = null;
    this._modalBackgroundCSSClass = null;
    this._closeImagePath = null;
    this._closeImageCssClass = null;
    this._currentItemControl = null;
    this._containerId = null;

}

CorvusCMS.UI.GalleryModal.prototype = {

    initialize: function() {
        CorvusCMS.UI.GalleryModal.callBaseMethod(this, 'initialize');
    },




    dispose: function() {

        CorvusCMS.UI.GalleryModal.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;
    },


    get_galleryId: function() {
        return this._galleryId;
    },


    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_closeImagePath: function() {
        return this._closeImagePath;
    },

    set_closeImagePath: function(value) {
        if (this._closeImagePath !== value) {
            this._closeImagePath = value;
        }
    },
    
    get_closeImageCssClass: function() {
        return this._closeImageCssClass;
    },

    set_closeImageCssClass: function(value) {
        if (this._closeImageCssClass !== value) {
            this._closeImageCssClass = value;
        }
    },


    get_containerId: function() {
        return this._containerId;
    },

    set_containerId: function(value) {
        if (this._containerId !== value) {
            this._containerId = 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_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_modalBackgroundCSSClass: function() {
        return this._modalBackgroundCSSClass;
    },

    set_modalBackgroundCSSClass: function(value) {
        if (this._modalBackgroundCSSClass !== value) {
            this._modalBackgroundCSSClass = 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;
        }
    }

 


}



CorvusCMS.UI.GalleryModal.Instance = null;
CorvusCMS.UI.GalleryModal.Control = null;


CorvusCMS.UI.GalleryModal.showModal = function(galleryId, index) {
    
    if (arguments.length == 1)
        index = 0;
    
    if (CorvusCMS.UI.GalleryModal.Instance == null) {

        var ctl = null;
        for (var component in Sys.Application.getComponents()) {

            if (CorvusCMS.UI.GalleryModal.isInstanceOfType(Sys.Application.getComponents()[component])) {
                if (ctl != null)
                    throw new Error("More than one instance of gallery modal control has been found.");

                ctl = Sys.Application.getComponents()[component];
            }
        }

        CorvusCMS.UI.GalleryModal.Instance = new CorvusCMS.UI._galleryModalSingleton(ctl);
    }

    CorvusCMS.UI.GalleryModal.Instance._showModal(galleryId, index);
}

CorvusCMS.UI.GalleryModal.registerClass("CorvusCMS.UI.GalleryModal", Sys.UI.Control, Sys.IDisposable);


CorvusCMS.UI._galleryModalSingleton = function(control) {

    this._control = control;

    this._element = control.get_element();
    if ($get("CorvusCMS.UI._galleryModalSingleton") != null)
        throw new Error("Only one gallery modal can be set on page");

    this._control.id = "CorvusCMS.UI._galleryModalSingleton";
    this._modalElement = null;
    this._elementsReady = false;
    this._modal = null;
    this._btnClose = null;
    this._galleryId = null;
    this._itemIndex = 0;
    this._currentItemControl = null;
}


CorvusCMS.UI._galleryModalSingleton.prototype = {

    initialize: function() {
        CorvusCMS.UI._galleryModalSingleton.callBaseMethod(this, 'initialize');
    },

    dispose: function() {
        this._currentItemControl.dispose();
        this._currentItemControl = null;
        this._repeater.dispose();
        this._modal.dispose();
    },


    _showModal: function(galleryId, index) {


        this._galleryId = parseInt(galleryId);
        this._itemIndex = index;

        if (!this._elementsReady) {


            this._modalElement = document.createElement("div");
            this._modalElement.id = this._element.id + "_modal";
            this._modalElement.className = "modalElement";


            document.body.appendChild(this._modalElement);

            this._btnClose = document.createElement("img");
            this._btnClose.id = this._element.id + "_close;"
            this._btnClose.src = this._control.get_closeImagePath();
            this._btnClose.style.cursor = "pointer";
            this._btnClose.className = this._control.get_closeImageCssClass();

            this._modalElement.appendChild(this._btnClose);

            this._modal = $create(AjaxControlToolkit.ModalPopupBehavior,
                       { "BackgroundCssClass": this._control.get_modalBackgroundCSSClass(),
                           "PopupControlID": this._modalElement.id,
                           "CancelControlID": this._btnClose.id,
                           "id": this._modalElement.id
                       }, null, null, this._element);


            this._modal.add_shown(Function.createDelegate(this, this._modalShown));
            this._modal.add_hiding(Function.createDelegate(this, this._hiddingModal));
            this._modal.set_repositionMode(AjaxControlToolkit.ModalPopupRepositionMode.RepositionOnWindowResize);

            var rptContainer = document.createElement("div");
            this._modalElement.appendChild(rptContainer);


            this._repeater = $create(CorvusCMS.UI.Repeater,
            {
                "headerTemplate": this._control.get_headerTemplate(),
                "footerTemplate": this._control.get_footerTemplate(),
                "itemTemplate": this._control.get_itemTemplate(),
                "pagerTemplate": this._control.get_pagerTemplate(),
                "id": this._element.id + "_rptItems",
                "dataSourceHandler": Function.createDelegate(this, this._dataSource),
                "itemDataBoundHandler": Function.createDelegate(this, this._rptDataBoundHandler),
                "pageChangedHandler": Function.createDelegate(this, this._rptPageChangedHandler),
                "ItemsPerPage": 1,
                "renderBottomPager": this._control.get_renderBottomPager(),
                "customCommandHandler": Function.createDelegate(this, this._rptCommandHandler),
                "renderTopPager": this._control.get_renderTopPager(),
                "pageChangingHandler": Function.createDelegate(this, this._pageChanging)
            }, null, null, rptContainer);

        }



        $addHandler($get(this._element.id + "_modal_backgroundElement"), "click", Function.createDelegate(this, this._modalBackgroundClicked));
        $addHandler(document, "keydown", Function.createDelegate(this, this._bodyKeypress));

        this._repeater.goToPage(index + 1, true);

        //setTimeout(Function.createDelegate(this._modal, this._modal.show), 200);

        this._elementsReady = true;
    },

    _hiddingModal: function() {
        if (this._currentItemControl && this._currentItemControl.dispose) {
            this._currentItemControl.dispose();
            this._currentItemControl = null;
        }
    },

    _pageChanging: function(sender, args) {

        if (this._currentItemControl)
            this._currentItemControl.dispose();


    },


    _dataSource: function(sender, args) {
        args.set_webRequestManager(new $Gallery(this._galleryId).GetItems([args.get_itemsPerPage(), args.get_newPage()], args.succededFunction, args.failFunction, null, this._control.get_extenders()));

    },

    _rptDataBoundHandler: function(sender, args) {
        if (args.get_itemType() == CorvusCMS.UI.TemplateType.ItemTemplate) {

            this._currentItemControl = CorvusCMS.UI.GalleryItem.renderControl(args.get_item(), args.findControl(this._control.get_containerId()), this._control.get_itemWidth(), this._control.get_itemHeight());
        }
    },

    _rptPageChangedHandler: function(sender, args) {
        this._modal.show();
    },
    _rptCommandHandler: function(sender, args) {

    },

    _modalBackgroundClicked: function(evt) {
        $clearHandlers($get(this._element.id + "_modal_backgroundElement"));
        this._modal.hide();
    },
    _bodyKeypress: function(ev) {
        if ((ev.keyCode ? ev.keyCode : ev.rawEvent.keyCode) == Sys.UI.Key.esc) {
            this._modal.hide();
        }
    },
    _modalShown: function() {

        var windowWidth = 0, windowHeight = 0, scrollX = 0, scrollY = 0;
        if (typeof (window.innerWidth) == 'number') {
            //Non-IE
            windowWidth = window.innerWidth;
            windowHeight = window.innerHeight;
            scrollX = window.scrollX;
            scrollY = window.scrollY
        } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
            scrollX = document.body.parentNode.scrollLeft;
            scrollY = document.body.parentNode.scrollTop;

        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
            scrollX = document.body.scrollLeft;
            scrollY = document.body.scrollTop;
        }

        var itemWidth = 0;
        var itemHeight = 0;

        if (this._currentItemControl.get_item().Width != undefined)
            var itemWidth = this._currentItemControl.get_item().Width;
        else
            var itemWidth = this._currentItemControl.get_width();

        if (this._currentItemControl.get_item().Height != undefined)
            var itemHeight = this._currentItemControl.get_item().Height;
        else
            var itemHeight = this._currentItemControl.get_height();

        var pointX = (windowWidth / 2) - (itemWidth / 2);
        if (pointX < 0)
            pointX = 0;

        var pointY = (windowHeight / 2) - (itemHeight / 2);
        if (pointY < 0)
            pointY = 0;

        $common.setLocation(this._modalElement, new Sys.UI.Point(pointX + scrollX, pointY + scrollY));

    }

}


CorvusCMS.UI._galleryModalSingleton.registerClass("CorvusCMS.UI.GalleryModal", null, Sys.IDisposable);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();