﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Gabber.Modules.PartyAgenda");

Gabber.Modules.PartyAgenda.FlyerResizeBehavior = function(element) {
    Gabber.Modules.PartyAgenda.FlyerResizeBehavior.initializeBase(this, [element]);

    this._resizeDelegate = null;
    this._horizontal = false;
    this._vertical = false;
    this._horizontalPadding = 0;
    this._verticalPadding = 0;
}

Gabber.Modules.PartyAgenda.FlyerResizeBehavior.prototype = {
    initialize: function() {
        Gabber.Modules.PartyAgenda.FlyerResizeBehavior.callBaseMethod(this, 'initialize');

        if (this._resizeDelegate == null) {
            this._resizeDelegate = Function.createDelegate(this, this._resizeHandler);
        }
        $addHandler(window, 'resize', this._resizeDelegate);
        this.resizeElement();
    },
    dispose: function() {
        if (this._resizeDelegate) {
            $removeHandler(window, 'resize', this._resizeDelegate);
        }

        Gabber.Modules.PartyAgenda.FlyerResizeBehavior.callBaseMethod(this, 'dispose');
    },
    resizeElement: function() {
        var clientBounds = $common.getClientBounds();
        var flyerContentElement = this.get_element();

        if (this._horizontal) {
            var flyerWidth = clientBounds.width - this._horizontalPadding;
            flyerContentElement.style.maxWidth = flyerWidth.toString() + "px";
        }
        if (this._vertical) {
            var flyerHeight = clientBounds.height - this._verticalPadding;
            flyerContentElement.style.maxHeight = flyerHeight.toString() + "px";
        }
    },
    _resizeHandler: function(e) {
        this.resizeElement();
    },
    get_horizontal: function() {
        return this._horizontal;
    },
    set_horizontal: function(value) {
        if (this._horizontal !== value) {
            this._horizontal = value;
            this.raisePropertyChanged('horizontal');
        }
    },
    get_vertical: function() {
        return this._vertical;
    },
    set_vertical: function(value) {
        if (this._vertical !== value) {
            this._vertical = value;
            this.raisePropertyChanged('vertical');
        }
    },
    get_horizontalPadding: function() {
        return this._horizontalPadding;
    },
    set_horizontalPadding: function(value) {
        if (this._horizontalPadding !== value) {
            this._horizontalPadding = value;
            this.raisePropertyChanged('horizontalPadding');
        }
    },
    get_verticalPadding: function() {
        return this._verticalPadding;
    },
    set_verticalPadding: function(value) {
        if (this._verticalPadding !== value) {
            this._verticalPadding = value;
            this.raisePropertyChanged('verticalPadding');
        }
    }
}

Gabber.Modules.PartyAgenda.FlyerResizeBehavior.registerClass('Gabber.Modules.PartyAgenda.FlyerResizeBehavior', Sys.UI.Behavior);

Gabber.Modules.PartyAgenda.ShowFlyer = function(flyerUrl, flyerImageElementId, loadingAnimationUrl) {
    var popupShowFullFlyerBehavior = $find("popupShowFullFlyerBehavior");
    var flyerImageElement = $get(flyerImageElementId);

    if (typeof (flyerImageElement.onFlyerLoadedHandler) == "undefined") {
        flyerImageElement.onFlyerLoadedHandler = null;
    }

    flyerImageElement.src = loadingAnimationUrl;
    
    var flyerImage = new Image();
    flyerImageElement.fullFlyerImage = flyerImage;
    flyerImageElement.onFlyerLoadedHandler = new Gabber.Modules.PartyAgenda.FlyerLoadedHandler(flyerImageElement, popupShowFullFlyerBehavior);
    flyerImage.src = flyerUrl;

    popupShowFullFlyerBehavior.set_X(10);
    popupShowFullFlyerBehavior.set_Y(1);

    popupShowFullFlyerBehavior.show();

    popupShowFullFlyerBehavior.set_X(-1);
    popupShowFullFlyerBehavior.set_Y(-1);
    popupShowFullFlyerBehavior._layout();
    popupShowFullFlyerBehavior._layout();

    return false;
}

Gabber.Modules.PartyAgenda.FlyerLoadedHandler = function(flyerImageElement, flyerPopupBehavior) {
    this.flyerImageElement = flyerImageElement;
    this.flyerPopupBehavior = flyerPopupBehavior;
    this.flyerImage = flyerImageElement.fullFlyerImage;
    this.flyerLoadedDelegate = Function.createDelegate(this, this.flyerLoadedHandler);
    this.flyerImage.onload = this.flyerLoadedDelegate;
}

Gabber.Modules.PartyAgenda.FlyerLoadedHandler.prototype.flyerLoadedHandler = function() {
    this.flyerImage.onload = null;
    
    if (this.flyerImageElement.fullFlyerImage == this.flyerImage) {
        this.flyerImageElement.onFlyerLoadedHandler = null;
        this.flyerImageElement.src = this.flyerImage.src;

        this.flyerPopupBehavior._layout();
        this.flyerPopupBehavior._layout();
    }
}

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();