
/**
 * @requires bam.session.js
 * @requires bam.xd.js
 */
var MediaLogin = (function(window, document, $, bam) {

    /*
    bam.loadSync(bam.homePath + "bam.session.js");
    bam.loadSync(bam.homePath + "bam.xd.js");
    */

    bam.loadSync(bam.homePath + "bam.overlay.js");
    bam.loadSync(bam.homePath + "bam.popModule.js");
    //
    // @TODO: fix this bam.popModule init hack. i think i'm doing this so the popModule DOM elements get added once DOM is ready
    $(function () {
        bam.popModule.init({
            css : "/shared/css/bam/bam.popModule.css"
        });
    }); 

    var 

    TPL_DIR = "/media/player/v3.0",

    loginViewPath = TPL_DIR + "/loginFrame.js.tpl",
    loginFormPath = TPL_DIR + "/login.html",
    purchaseViewPath = TPL_DIR + "/purchase.html",

    loginView = $.template(loginViewPath),
    purchaseView,

    // DOM elements
    loginViewContainer;

    function MediaLogin(sslServer) {
        MediaLogin.superclass.constructor.call(this);

        this._loginFrame = bam.xd.iframe({
                //src : GD_SETTINGS.secureUrl + loginFormPath
                src: sslServer + loginFormPath
            })
            .bind("loginSuccess", $.proxy(function() {
                this.isLoggedIn();
                this.hide();
                this._loginFrame.destroy();
            }, this))
            .bind("loginError", $.proxy(function() {
                this.trigger("loginError");   
            }, this));

        purchaseView = $.ajax({
                url : purchaseViewPath,
                dataType : "html"
            });
    }

    

    $.inherit(MediaLogin, bam.session, {
        display : function() {

            purchaseView
                .success($.proxy(function (purchaseViewHTML) {
                    var loginViewHTML = loginView({
                               purchaseHTML : purchaseViewHTML 
                            }, true);

                    bam.popModule.setWidth(585);
                    bam.popModule.show({
                            htmlContent : loginViewHTML
                        });

                    this._loginFrame.create("#login-form-container");

                    this.bind("loginError", $.eventProxy(function() {
                        $("#login-form-container").find("iframe").css({ height : "330px" });
                        $("#gameday-audio-authentication-view").find(".gda-auth-frame").css({ height : "356px" });
                    }));
                }, this));
        },

        displayUpsell : function() {

            purchaseView
                .success($.proxy(function (purchaseViewHTML) {
                    var loginViewHTML = loginView({
                               isUpsell : true,
                               purchaseHTML : purchaseViewHTML 
                            }, true);

                    bam.popModule.setWidth(585);
                    bam.popModule.show({
                            htmlContent : loginViewHTML
                        });

                }, this));

        },

        hide : function() {
            bam.popModule.exit();
        }
    });

    return MediaLogin;

})(this, this.document, this.$, this.bam);





