﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.SignIn = function(element) {
    BandSite.SignIn.initializeBase(this, [element]);
}

BandSite.SignIn.prototype = {
    initialize: function() {
        BandSite.SignIn.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;
        this._signInButton.add_buttonClicked( function(sender, e) { that.signInButtonClicked(); } );
        this._signOutButton.add_buttonClicked( function(sender, e) { that.signOutButtonClicked(); } );
        this._signUpButton.add_buttonClicked( function(sender, e) { that.signUpButtonClicked(); } );
        this._manageProfileButton.add_buttonClicked( function(sender, e) { that.manageProfileButtonClicked(); } );
        
        /*
         **************************************************
        TEMPORARY - TO SIMULATE SIGN-IN
         **************************************************
        */
        VisitorService.CheckSignIn(
            this._bandID,
            'usertest@gmail.com',
            'password',
            function(friend) {
            
                if (friend == null) {
                    // Means login was invalid
                    // alert("Invalid");
                    // that._messageLabel.innerHTML = "Invalid";
                    // setTimeout(function() { that._messageLabel.innerHTML = ''; }, 1000);
                    that._messageLabel.flashMessage("Invalid");
                } else {
                    // Maybe better to do this with a PanelSet?
                    that.signIn(friend);
                }
                                
            },
            webServiceError
        );        
        
    },
    dispose: function() {
        //Add custom dispose actions here
        BandSite.SignIn.callBaseMethod(this, 'dispose');
    },
    
    signInButtonClicked : function() {
        
        var that = this;
        
        var valid = true;
        // var message = "";
        
        var email = this._emailInput.get_value();
        var password = this._passwordInput.get_value();
        
        if (email.length < 3) {
            valid = false;
        }
        if (password.length < 3) {
            valid = false;
        }
        
        if (valid) {
        
            // Perhaps we should have a standard 'MessageLabel' control?
            this._messageLabel.innerHTML = "";
            
            VisitorService.CheckSignIn(
                this._bandID,
                email,
                password,
                function(friend) {
                
                    if (friend == null) {
                        // Means login was invalid
                        // alert("Invalid");
                        // that._messageLabel.innerHTML = "Invalid";
                        // setTimeout(function() { that._messageLabel.innerHTML = ''; }, 1000);
                        that._messageLabel.flashMessage("Invalid");
                    } else {
                        // Maybe better to do this with a PanelSet?
                        that.signIn(friend);
                    }
                                    
                },
                webServiceError
            );
            
        } else {
            // this._messageLabel.set_value("Invalid");
            // setTimeout(function() { that._messageLabel.set_value(''); }, 1000);
            this._messageLabel.flashMessage("Invalid");
        }

    },
    
    signIn : function(friend) {    
        // $get("notSignedInDiv").style.display = 'none';
        // $get("signedInDiv").style.display = 'block';
        this._nameLabel.set_value(friend.FirstName);
        this._friend = friend;            
        this._raiseEvent("signedIn", {});        
        this._panelSet.showPanel("SignedInPanel");
        VisitorService.EmailAlert(
            this._bandID,
            "AltPro: Friend signed in",
            friend.FirstName + " " + friend.LastName + " signed in.",
            function(result) {
            },
            webServiceError         
        );
    },
    
    signOutButtonClicked : function() {
                
        this._messageLabel.set_value("");
        // $get("signedInDiv").style.display = 'none';
        // $get("notSignedInDiv").style.display = 'block';
        this._nameLabel.set_value("");
        this._friend = null;
        this._emailInput.set_value("");
        this._passwordInput.set_value("");
        this._raiseEvent("signedOut", {});        
        this._panelSet.showPanel("NotSignedInPanel");
        
        // Make sure we're not on a tab that you can only
        // access if signed in
//        var menuTabSet = $find(this._menuTabSetClientID);
//        if (menuTabSet.get_selectedTabID() == 'ProfileManagerTab') {
//            menuTabSet.showTab("Tab1");
//        }        
        var mainPanel = $find("MainPanel");
        if (mainPanel.get_selectedPagePanelID() == 'ProfileManagerPanel') {
            mainPanel.showFirstPage();
        }
        
    },
    
    signUpButtonClicked: function() {
        // var menuTabSet = $find(this._menuTabSetClientID);
        // menuTabSet.showTab("SignUpTab");
        $find("MenuPanel").unselectMenuButtons();
        $find("SignUpSection").resetSection();
        $find("MainPanel").get_mainPanelSet().showPanel("SignUpPanel");
    },
    
    manageProfileButtonClicked: function() {    
        // var menuTabSet = $find(this._menuTabSetClientID);
        // menuTabSet.showTab("ProfileManagerTab");
        $find("MenuPanel").unselectMenuButtons();
        $find("MainPanel").showSitePagePanel("ProfileManagerPanel");
    }
    
}
BandSite.SignIn.createProperty("bandID");
BandSite.SignIn.createProperty("panelSet");
BandSite.SignIn.createProperty("emailInput");
BandSite.SignIn.createProperty("passwordInput");
BandSite.SignIn.createProperty("signInButton");
BandSite.SignIn.createProperty("nameLabel");
BandSite.SignIn.createProperty("signOutButton");
BandSite.SignIn.createProperty("manageProfileButton");
BandSite.SignIn.createProperty("signUpButton");
BandSite.SignIn.createProperty("messageLabel");
// BandSite.SignIn.createProperty("menuTabSetClientID");

BandSite.SignIn.createEvent("signedIn");
BandSite.SignIn.createEvent("signedOut");

BandSite.SignIn.registerClass('BandSite.SignIn', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
