/**
 * Manages the ppf, opens popup windows with the correct
 * url, manages session pass-through
 * 
 * @param lang the language to use
 * @param auth user authenticated
 * @param status status of the user
 * @param forum wether the show a forum link
 * @param sessionid the current session id
 */
function PPF(lang, auth, status, forum, sessionid) {

    this._lang = lang;
    this._auth = auth;
    this._status = status;
    this._showForum = forum;
    this._sessionid = sessionid;
    this._sports = ["soccer", "handball", "athletics", "chess", "ping_pong", "gymnastics"];
    this._translations = {
        "login"                : { "de" : "Login", "en" : "Login" },                                 
        "username"             : { "de" : "Username", "en" : "Username" },                           
        "password"             : { "de" : "Passwort", "en" : "Password" },                           
        "forgot"               : { "de" : "Passwort vergessen?", "en" : "Forgot password?" },        
        "register"             : { "de" : "Neu hier? Zur Anmeldung", "en" : "New to SVW? Log on" },
        "not recognized"       : { "de" : "Logindaten nicht erkannt!", "en" : "Unknown login data!" },
        "attention"            : { "de" : "Achtung", "en" : "Attention" },
        "paste old data"       : { "de" : "Meine Accountdaten &uuml;bertragen", "en" : "Use existing login data" },
        "not recognized_text"  : { "de" : "Aufgrund des Relaunches der Werder Bremen Online Welt, m&uuml;ssen bestehende Accounts f&uuml;r Werder.tv, Forum oder VIP-Portal erneut best&auml;tigt werden", "en" : "blubber" },
        "soccer"               : {"de" : "Fu&szlig;ball", "en" : "Soccer"},
        "handball"             : {"de" : "Handball", "en" : "Handball"},
        "athletics"            : {"de" : "Leichtathletik", "en" : "Athletics"},
        "chess"                : {"de" : "Schach", "en" : "Chess"},
        "ping_pong"            : {"de" : "Tischtennis", "en" : "Ping pong"},
        "gymnastics"           : {"de" : "Gymnastik", "en" : "Gymnastics"},
        "personal settings"    : {"de" : "pers&ouml;nliche Einstellungen", "en" : "personal settings" },
        "personal_text"        : {"de" : "Ich m&ouml;chte auf den Startseiten von Werder.de und Werder.TV auch &uuml;ber folgende Sportarten informiert werden:", "en" : "Show me additional information about the following sports on the front pages of Werder.de and Werder.TV:"},
        "login to forum_text"  : {"de" : "Melden Sie sich zun&auml;chst mit Ihren Werder.de-Zugangsdaten an, Sie werden dann automatisch auf das Forum geleitet.", "en" : "Please log in with your Werder.de-Account. You will then be taken to the forums."},
        "forum login"          : {"de" : "Forum Login", "en" : "Forums login"}
    };

    /* LOGIN WINDOW */

    /**
     * Displays an overlay window styled
     * by css
     * 
     * @param auth boolean user authenticated
     */
    this.showLoginWindow = function(auth) {
        //display black overlay
        var el = $('<div class="overlay">');
        $('body').append(el);
        el.css({
            "height" : $(document).height() + "px",
            "width"  : $(document).width() + "px"         
        });
        //create the login window
        var loginWindow = this._createLoginWindow();
        //position it
        /*var pos = $('.loginbox').offset();
        loginWindow.css({
          "top"  : (pos.top + 60) + "px",
          "left"  : (pos.left - 150) + "px"
        });*/
        $('.loginWindow').find('.username').focus();
        //modify login window, if necessary
        if (!auth) {
            //extend the login window with some more information
            var additional = '';
            additional += '<span class="Black font11b Upper normalText">' + this._translate("not recognized") + '</span>';
            additional += '<div class="divider"></div>';
            additional += '<span class="Black font11b Upper normalText">' + this._translate("attention") + '</span>';
            additional += '<span class="Black font11b normalText">' + this._translate("not recognized_text") + '</span>';
            additional += '<span class="White font11b register oldData">' + this._translate("paste old data") + '</span>';
            $('.loginWindow').find('.forgotPwd').after(additional);
            //events            
            $('.loginWindow').find('.oldData').click(jQuery.proxy(this.showMergeAccounts, this));
        }
    };
    
    /**
     * Displays the login window with some information
     * for forum users
     */
    this.showForumLogin = function() {
        //display black overlay
        var el = $('<div class="overlay">');
        $('body').append(el);
        el.css({
            "height" : $(document).height() + "px",
            "width"  : $(document).width() + "px"         
        });
        //create the login window
        var loginWindow = this._createLoginWindow();
        //position it
        /*var pos = $('.loginbox').offset();
        loginWindow.css({
          "top"  : (pos.top + 60) + "px",
          "left"  : (pos.left - 150) + "px"
        }); */
        //extend the login window with some more information
        var additional = '';
        additional += '<span class="Black font11b Upper normalText">' + this._translate("forum login") + '</span>';
        additional += '<div class="divider"></div>';
        additional += '<span class="Black font11b normalText">' + this._translate("login to forum_text") + '</span>';
        additional += '<span class="White font11b register oldData">' + this._translate("paste old data") + '</span>';
        $('.loginWindow').find('.forgotPwd').after(additional);
        //events            
        $('.loginWindow').find('.oldData').click(jQuery.proxy(this.showMergeAccounts, this));
        //change form action
        $('.loginWindow').find('#loginWindowForm').attr('action', '/forum_login.php');
    };

    /**
     * Displays the orange overlay for personal configuration
     */
    this.showPersonalConfig = function() {
        //display black overlay
        var el = $('<div class="overlay">');
        $('body').append(el);
        el.css({
            "height" : $(document).height() + "px",
            "width"  : $(document).width() + "px"
        });
        //create the login window
        var configWindow = this._createConfigWindow();
        //position it
        /*var pos = $('.loginbox').offset();
        configWindow.css({
          "top"  : (pos.top + 60) + "px",
          "left"  : (pos.left - 50) + "px"
        });*/
    };

    /**
     * Builds the login window, returns it
     *
     * @return login window jquery object
     */
    this._createLoginWindow = function() {         
        var template = '';                                                                                               
        template += '<div class="content">';                                                                         
        template += '    <h3 class="White Upper font14b">' + this._translate("login") + '</h3>';                    
        template += '    <div class="closeBtn"></div>';                                                            
        template += '    <form id="loginWindowForm" action="' + document.location.href + '" method="post">';                            
        template += '        <span class="username White font11b">' + this._translate("username") + '</span>';      
        template += '        <span class="password White font11b">' + this._translate("password") + '</span>';     
        template += '        <input type="text" name="USERNAME" class="username" />';                          
        template += '        <input type="password" name="PASSWORD" class="password" />';                         
        template += '        <input type="submit" name="submit" value="" class="loginBtn" />';                    
        template += '        <input type="hidden" name="authorize" value="true" />';                                 
        template += '        <span class="forgotPwd White font11b">' + this._translate("forgot") + '</span>';       
        template += '        <div class="divider"></div>';                                                          
        template += '        <span class="register White font11b">' + this._translate("register") + '</span>';    
        template += '    </form>';                                                                                   
        template += '</div>';                                                                                       
        template += '<div class="bottom"></div>';   

        var el = $('<div class="loginWindow">').html(template);
        $('body').append(el);
        //events
        el.find('.closeBtn').click(jQuery.proxy(this._closeLogin, this));
        el.find('.loginBtn').click(jQuery.proxy(this._doLogin, this));
        el.find('.forgotPwd').click(jQuery.proxy(this._doForgotPwd, this));
        el.find('.register').click(jQuery.proxy(this.showRegister, this));
        //return it
        return el;
    };

    /**
     * Creates the configuration window
     */
    this._createConfigWindow = function() {
        var template = '';
        template += '<div class="content">'; 
        template += '   <h3 class="White Upper font14b">' + this._translate("personal settings") + '</h3>';  
        template += '   <div class="closeBtn"></div>';
        template += '   <span class="Black font11b normalText">' + this._translate("personal_text") + '</span>';
        template += '   <ul class="personal">';
        //cycle sports
        for(var key in this._sports) {
            template += '       <li><input type="checkbox" id="' + this._sports[key] + '" name="' + this._sports[key] + '" /><label for="' + this._sports[key] + '" class="font11b White">' + this._translate(this._sports[key]) + '</label></li>';
        }
        //end of window
        template += '   </ul>';
        template += '   <span class="rightLink White font11b">' + this._translate('save now') + '</span>';
        template += '   <span class="rightLink White font11b">' + this._translate('save longer') + '</span>';
        template += '</div>';
        template += '<div class="bottom"></div>';   

        var el = $('<div class="loginWindow">').html(template);
        $('body').append(el);
        //events
        el.find('.closeBtn').click(jQuery.proxy(this._closeLogin, this));
        //return it
        return el;
    };

    /**
     * Closes the login window and the overlay
     *
     * @param ev mouse event
     */
    this._closeLogin = function(ev) {
        $('.loginWindow').remove();
        $('.overlay').remove();
    };

    /**
     * Logging the user in
     */
    this._doLogin = function() {
        $('.loginWindow').find('form').submit();
    };
    
    /**
     * Opens the ppf with the password forgotten
     * page
     */
    this._doForgotPwd = function() {
        var win = window.open('http://www.werder.de/' + this._lang + '/user/?c=c&action=password_forgotten',"Werder","width=1000,height=800,scrollbars=yes,resizable=yes");
    };

    /**
     * Displays the merge accounts screen
     */
    this.showMergeAccounts = function() {
        var win = window.open('http://www.werder.de/' + this._lang + '/user/?action=reactivate&sid=' + this._sessionid,"Werder","width=1000,height=800,scrollbars=yes,resizable=yes");
    };

    /* REGISTER */

    /**
     * Displays a register window as popup
     */         
    this.showRegister = function() {
        var win = window.open('http://www.werder.de/' + this._lang + '/user/?c=c&sid=' + this._sessionid,"Werder","width=1000,height=800,scrollbars=yes,resizable=yes");
    };

    /**
     * Opens the ppf with parameters
     */
    this.showPPF = function(param) {
        var win = window.open('http://www.werder.de/' + this._lang + '/user/?c=c&' + param,"Werder","width=1000,height=800,scrollbars=yes,resizable=yes");
    };

    /* PERSONAL DATA */

    /**
     * Opens a popup with the personal data
     */         
    this.showData = function() {
        this.showRegister();
    }

    /* BASIC STUFF */

    /**
     * Translates the given text
     *
     * @param txt string the text to translate
     * @return string the translation
     */
    this._translate = function(txt) {
        if (this._translations[txt] != null)
            return this._translations[txt][this._lang];
        return txt;
    };
    
    /**
     * Returns the sessionid
     */
    this.getSID = function() {
        return this._sessionid;
    };

    /* PERFORM CONSTRUCTOR */

    //check status, display password forgotten, if false entry
    if (this._status == "noauth" && !this._auth) {
        //user has authenticated with false settings
        this.showLoginWindow(false);
    };

    //check status for a forum, display the login window with
    //some more text
    if (this._showForum) {
        if (this._auth) {
            document.location = "/forum_login.php";
        } else {
            this.showForumLogin();            
        }
    }
}
